From b139dd16d454ef933140b8a8845d909ca0f070bf Mon Sep 17 00:00:00 2001 From: kennicefung Date: Sun, 3 Nov 2019 01:13:53 +0800 Subject: [PATCH 1/3] validate nested state --- index.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 86128b7..fa56b0c 100644 --- a/index.js +++ b/index.js @@ -35,15 +35,27 @@ export default class ValidationComponent extends Component { // Reset errors this._resetErrors(); // Iterate over inner state - for (const key of Object.keys(this.state)) { + this.validateObject(fields, this.state) + return this.isFormValid(); + } + + validateObject(fields, object, parentFieldName) { + if(object == null || object==='undefined') return; + + for (const key of Object.keys(object)) { + if(typeof object[key] === 'object' && fields[key]){ + this.validateObject(fields[key], object[key], key) + } // Check if child name is equals to fields array set up in parameters const rules = fields[key]; if (rules) { // Check rule for current field - this._checkRules(key, rules, this.state[key]); + if(parentFieldName == null || parentFieldName==='undefined') + this._checkRules(key, rules, object[key]); + else + this._checkRules(parentFieldName + "." + key, rules, object[key]); } - }; - return this.isFormValid(); + } } // Method to check rules on a spefific field From 591c461b81aeb7b10a80eb67904c20cc0611a5e1 Mon Sep 17 00:00:00 2001 From: kennicefung Date: Sun, 3 Nov 2019 01:34:02 +0800 Subject: [PATCH 2/3] Update index.js --- index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index fa56b0c..aa00b11 100644 --- a/index.js +++ b/index.js @@ -46,14 +46,16 @@ export default class ValidationComponent extends Component { if(typeof object[key] === 'object' && fields[key]){ this.validateObject(fields[key], object[key], key) } - // Check if child name is equals to fields array set up in parameters - const rules = fields[key]; - if (rules) { - // Check rule for current field - if(parentFieldName == null || parentFieldName==='undefined') - this._checkRules(key, rules, object[key]); - else - this._checkRules(parentFieldName + "." + key, rules, object[key]); + else{ + // Check if child name is equals to fields array set up in parameters + const rules = fields[key]; + if (rules) { + // Check rule for current field + if(parentFieldName == null || parentFieldName==='undefined') + this._checkRules(key, rules, object[key]); + else + this._checkRules(parentFieldName + "." + key, rules, object[key]); + } } } } From f1945a0e272aee49da864d074f449ca70f17ae0b Mon Sep 17 00:00:00 2001 From: kennicefung Date: Sun, 3 Nov 2019 11:08:59 +0800 Subject: [PATCH 3/3] take count of array --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index aa00b11..b14a19e 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,7 @@ export default class ValidationComponent extends Component { if(object == null || object==='undefined') return; for (const key of Object.keys(object)) { - if(typeof object[key] === 'object' && fields[key]){ + if(typeof object[key] === 'object' && !Array.isArray(object[key]) && fields[key]){ this.validateObject(fields[key], object[key], key) } else{ @@ -65,6 +65,7 @@ export default class ValidationComponent extends Component { if (!value && !rules.required ) { return; // if value is empty AND its not required by the rules, no need to check any other rules } + for (const key of Object.keys(rules)) { const isRuleFn = (typeof this.rules[key] == "function"); const isRegExp = (this.rules[key] instanceof RegExp);