Skip to content

Commit

Permalink
Fix duplicated validations
Browse files Browse the repository at this point in the history
  • Loading branch information
NewOldMax committed Feb 3, 2021
1 parent f07be7a commit ee59c64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/ValidatorComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -98,6 +98,7 @@ class ValidatorComponent extends React.Component {
this.props.validatorListener(this.state.isValid);
});
}
return valid;
});
}

Expand Down
27 changes: 6 additions & 21 deletions src/ValidatorForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
)

Expand Down

0 comments on commit ee59c64

Please sign in to comment.