mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
Fix logic validate when item dependency rule not set
This commit is contained in:
parent
21a7d3a895
commit
c825f7faa2
@ -76,6 +76,26 @@ export default class extends think.controller.base {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* merge clean rules(only value)
|
||||
* @param {Object} rules []
|
||||
* @return {Object} []
|
||||
*/
|
||||
_mergeCleanRules(rules){
|
||||
let listData = [this.post(), this.get()];
|
||||
let methods = ['post', 'get'];
|
||||
listData.forEach((item, index) => {
|
||||
for(let key in item){
|
||||
if(!rules[key]){
|
||||
rules[key] = {
|
||||
value: item[key],
|
||||
_method: methods[index]
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
return rules;
|
||||
}
|
||||
/**
|
||||
* validate data
|
||||
* this.validate({
|
||||
@ -92,6 +112,8 @@ export default class extends think.controller.base {
|
||||
return true;
|
||||
}
|
||||
rules = this._parseValidateData(rules);
|
||||
rules = this._mergeCleanRules(rules);
|
||||
|
||||
let methods = {};
|
||||
for(let name in rules){
|
||||
methods[name] = rules[name]._method;
|
||||
|
||||
@ -40,7 +40,7 @@ Validator.requiredIf = (value, anotherField, ...values) => {
|
||||
*/
|
||||
Validator._requiredIf = (args, data) => {
|
||||
let arg0 = args[0];
|
||||
args[0] = data[arg0].value;
|
||||
args[0] = data[arg0] ? data[arg0].value : '';
|
||||
return args;
|
||||
};
|
||||
/**
|
||||
@ -88,7 +88,7 @@ Validator.requiredWith = (value, ...anotherFields) => {
|
||||
*/
|
||||
Validator._requiredWith = (args, data) => {
|
||||
return args.map(item => {
|
||||
return data[item].value;
|
||||
return data[item] ? data[item].value : '';
|
||||
});
|
||||
};
|
||||
/**
|
||||
@ -188,7 +188,8 @@ Validator.equals = (value, comparison) => {
|
||||
* @return {Array} []
|
||||
*/
|
||||
Validator._equals = (args, data) => {
|
||||
return [data[args[0]].value];
|
||||
let item = data[args[0]];
|
||||
return [item ? item.value : ''];
|
||||
};
|
||||
/**
|
||||
* check if the string matches the comparison.
|
||||
|
||||
@ -552,6 +552,59 @@ describe('logic/base', function(){
|
||||
done();
|
||||
})
|
||||
})
|
||||
it('parse rules, different, with clean rules 1', function(done){
|
||||
getInstance({}, {
|
||||
_config: think.config(),
|
||||
_post: {
|
||||
welefen: 30,
|
||||
suredy: 30
|
||||
},
|
||||
method: 'POST'
|
||||
}).then(function(instance){
|
||||
var data = instance.validate({
|
||||
suredy: 'required|int|different:welefen'
|
||||
});
|
||||
assert.deepEqual(data, false);
|
||||
var errors = instance.errors();
|
||||
assert.deepEqual(errors.suredy.length > 0, true);
|
||||
done();
|
||||
})
|
||||
})
|
||||
it('parse rules, different, with clean rules 2', function(done){
|
||||
getInstance({}, {
|
||||
_config: think.config(),
|
||||
_post: {
|
||||
welefen: '',
|
||||
suredy: ''
|
||||
},
|
||||
method: 'POST'
|
||||
}).then(function(instance){
|
||||
var data = instance.validate({
|
||||
suredy: 'requiredWithout:welefen'
|
||||
});
|
||||
assert.deepEqual(data, false);
|
||||
var errors = instance.errors();
|
||||
assert.deepEqual(errors.suredy.length > 0, true);
|
||||
done();
|
||||
})
|
||||
})
|
||||
it('parse rules, different, with clean rules 3', function(done){
|
||||
getInstance({}, {
|
||||
_config: think.config(),
|
||||
_post: {
|
||||
welefen: 'wwww',
|
||||
suredy: ''
|
||||
},
|
||||
method: 'POST'
|
||||
}).then(function(instance){
|
||||
var data = instance.validate({
|
||||
suredy: 'requiredWithout:welefen'
|
||||
});
|
||||
assert.deepEqual(data, true);
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
it('parse rules, after', function(done){
|
||||
getInstance({}, {
|
||||
_config: think.config(),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user