Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
fix(FormBot): Remove validation chaining for async validations (#65)
Browse files Browse the repository at this point in the history
* Remove validation chaining for async validations

* Added resolved Promise to handle async validation

* Fixed context issue
  • Loading branch information
schlegz authored Aug 21, 2019
1 parent 926c60a commit fff8c25
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions src/Form/Formbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,44 +143,39 @@ export default class Formbot extends React.Component {
}

const fieldValue = this.state.values[field];
let errorMsg;

if (hasSchema && typeof validation.validate === 'function') {
validation
.validate(fieldValue, validationOpts)
.catch(e => {
errorMsg = e.message;
})
.finally(() => {
this.updateField(field, { validated: true }).then(() => {
this.setErrors({ [field]: errorMsg }, resolve);
});
});

return;
}

try {
if (typeof validation === 'function') {
validation(fieldValue);
} else {
Object.keys(validation).forEach(method => {
const validator = VALIDATIONS[method];

if (!validator) {
throw new Error(`Formbot: "${method}" is not a built-in validator.`);
}

validator(fieldValue, validation[method]);
});
}
} catch (err) {
errorMsg = err.message;
} finally {
const setFieldValidated = message => {
this.updateField(field, { validated: true }).then(() => {
this.setErrors({ [field]: errorMsg }, resolve);
this.setErrors({ [field]: message }, resolve);
});
};

Promise.resolve()
.then(() => {
if (hasSchema && typeof validation.validate === 'function') {
return validation.validate(fieldValue, validationOpts);
} else if (typeof validation === 'function') {
validation(fieldValue);
} else {
Object.keys(validation).forEach(method => {
const validator = VALIDATIONS[method];

if (!validator) {
throw new Error(`Formbot: "${method}" is not a built-in validator.`);
}

validator(fieldValue, validation[method]);
});
}

return true;
})
.then(() => {
setFieldValidated();
})
.catch(err => {
setFieldValidated(err.message);
});
}
});
}

Expand Down

0 comments on commit fff8c25

Please sign in to comment.