mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
add scope for logic
This commit is contained in:
parent
3fa88f8665
commit
322dc7ee1d
@ -22,6 +22,9 @@ module.exports = {
|
||||
}
|
||||
const instance = new Validator(this.ctx);
|
||||
msgs = Object.assign({}, think.app.validators.messages, msgs);
|
||||
if (helper.isObject(this.scope)) {
|
||||
rules = helper.extend(this.scope, rules);
|
||||
}
|
||||
const ret = instance.validate(rules, msgs);
|
||||
if (!helper.isEmpty(ret)) {
|
||||
this.validateErrors = ret;
|
||||
|
||||
@ -17,6 +17,30 @@ test.serial('validate with empty rules', async t => {
|
||||
t.is(result,undefined)
|
||||
});
|
||||
|
||||
test.serial('validate pass', async t => {
|
||||
let rules = {
|
||||
mockResult:false,
|
||||
username: {
|
||||
string: true, // 字段类型为 String 类型
|
||||
required: true, // 字段必填
|
||||
default: 'thinkjs', // 字段默认值为 'thinkjs'
|
||||
trim: true, // 字段需要trim处理
|
||||
method: 'GET' // 指定获取数据的方式
|
||||
},
|
||||
age: {
|
||||
int: {min: 20, max: 60} // 20到60之间的整数
|
||||
}
|
||||
}
|
||||
think.app.validators.rules = {
|
||||
name:{string:true},
|
||||
version:{string:true}
|
||||
};
|
||||
let flag = logic.validate(rules);
|
||||
t.is(flag,true);
|
||||
think.app.validators.rules = null;
|
||||
});
|
||||
|
||||
|
||||
test.serial('validate pass', async t => {
|
||||
let rules = {
|
||||
mockResult:false,
|
||||
@ -100,6 +124,11 @@ test.serial('_after', async t => {
|
||||
|
||||
test.serial('_after', async t => {
|
||||
logic = mock.reRequire('../../../lib/extend/logic');
|
||||
logic.scope = {
|
||||
id: {
|
||||
required: true
|
||||
}
|
||||
}
|
||||
logic.rules = {
|
||||
mockResult:true,
|
||||
username: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user