diff --git a/src/logic/base.js b/src/logic/base.js index acdbed92..f8508215 100644 --- a/src/logic/base.js +++ b/src/logic/base.js @@ -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; diff --git a/src/util/validator.js b/src/util/validator.js index 636eae54..91121c6c 100644 --- a/src/util/validator.js +++ b/src/util/validator.js @@ -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. diff --git a/test/logic/base.js b/test/logic/base.js index 5b0f8c85..7bb10da5 100644 --- a/test/logic/base.js +++ b/test/logic/base.js @@ -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(),