-
-
Notifications
You must be signed in to change notification settings - Fork 877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Will error message be overwritten by concurrent validations? #242
Comments
JavaScript is single threaded and the validation itself in your case is synchronous for each ID. const validate = ajv.compile(customerSchema)
function checkCustomer(id) {
return promiseFetch(id).then(customer => {
validate(customer);
return { id: id, errors: validate.errors };
})
}
Promise.all([id1, id2, id3, ...].map(id => checkCustomer(id) ))
.then(results => console.log(results)); It will be mapped correctly. If the schema itself is asynchronous then errors are not returned as the property of validation function but as the property of error object that the promise rejects with. |
In relation to the comment in readme, it means that if you tried to use the same errors in a different execution block they could be errors from the different validation. For example this code has this problem: promiseFetch(id)
.then(customer => ({ id: customer.id, valid: validate(customer) }))
.then(result => {
if (!result.valid) console.log(`Customer ${id} has errors: ${validate.errors}`);
}); But the code below will always work as expected, because errors are used in the same execution block before they can be overwritten: promiseFetch(id)
.then(customer => {
var valid = validate(customer);
return { id: customer.id, valid: valid, errors: validate.errors };
})
.then(result => {
if (!result.valid) console.log(`Customer ${id} has errors: ${result.errors}`);
}); |
Very well explained. Thank you! |
Hi, I was reading the README, the following line interests me:
For example,
Am I right that this code has a risk of having a wrong id-error mapping in
Customer ${id} has errors: ${validate.errors}
output?Do I have to create and compile a new ajv insance inside each promise to ensure that my error messages won't be overwritten?
The text was updated successfully, but these errors were encountered: