From ee59c64b1aeb7c4cafa00c7ea58b1facd42b5fe3 Mon Sep 17 00:00:00 2001 From: NewOldMax Date: Wed, 3 Feb 2021 06:18:20 +0000 Subject: [PATCH] Fix duplicated validations --- package.json | 2 +- src/ValidatorComponent.jsx | 3 ++- src/ValidatorForm.jsx | 27 ++++++--------------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index cd8a7aa..91db869 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-form-validator-core", - "version": "1.0.1", + "version": "1.1.0", "description": "Core validator component for react forms.", "main": "./lib/index.js", "scripts": { diff --git a/src/ValidatorComponent.jsx b/src/ValidatorComponent.jsx index 2dde888..8e74352 100644 --- a/src/ValidatorComponent.jsx +++ b/src/ValidatorComponent.jsx @@ -84,7 +84,7 @@ class ValidatorComponent extends React.Component { this.state.validators.map(validator => ValidatorForm.getValidator(validator, value, includeRequired)), ); - validations.then((results) => { + return validations.then((results) => { this.invalid = []; let valid = true; results.forEach((result, key) => { @@ -98,6 +98,7 @@ class ValidatorComponent extends React.Component { this.props.validatorListener(this.state.isValid); }); } + return valid; }); } diff --git a/src/ValidatorForm.jsx b/src/ValidatorForm.jsx index 93fd22c..82572b0 100644 --- a/src/ValidatorForm.jsx +++ b/src/ValidatorForm.jsx @@ -108,29 +108,14 @@ class ValidatorForm extends React.Component { ) validate = (input, includeRequired, dryRun) => ( - new Promise((resolve) => { + new Promise(async (resolve) => { const { value, validators } = input.props; const result = []; - let valid = true; - const validations = Promise.all( - validators.map(validator => ( - Promise.all([ - this.constructor.getValidator(validator, value, includeRequired), - ]).then((data) => { - result.push({ input, result: data && data[0] }); - input.validate(input.props.value, true, dryRun); - }) - )), - ); - validations.then(() => { - result.forEach((item) => { - if (!item.result) { - valid = false; - this.errors.push(item.input); - } - }); - resolve(valid); - }); + const valid = await input.validate(input.props.value, includeRequired, dryRun); + if (!valid) { + this.errors.push(input); + } + resolve(valid); }) )