-
-
Notifications
You must be signed in to change notification settings - Fork 887
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
Why not return errors immediately after validation? (and remove ajv.errors) #65
Comments
Thank you very much for the feedback!
That is correct, the errors are overwritten every time
For performance reasons - it would be slower. In most validation calls the data is valid so you don't need to access errors. Creating a wrapper object every time you need to return the result makes it substantially slower. Also, you may have seen in readme that although there is
You are right here. Maybe on the next major version change... I will make it clearer in readme that Thanks again |
I added note in readme. |
Fair enough. Thanks for the explanation. |
This library and all the work by @epoberezkin is amazing, but this is a strange deviation from what most users expect. I'd love to submit a pull request if you're open to any of these suggestions:
const result = schema.validate(data)
if (result === true) {
console.log('valid schema')
} else if (result instanceof Error) {
console.log(result)
} |
@epoberezkin Apologies for the noise of the notification, my fault for missing the FAQ -- you've definitely had to address the issue plenty of times. Looks like the async validation is exactly what I need. Thank you so much! |
Just be aware that async requires a non standard keyword in the schema at the moment. |
Thanks for the great library! I've recently started migrating from tv4 to ajv, primarily because of the promised speed improvements. Thus far AJV appears to be very feature complete and I like pre-registering all schemas in a single instance beforehand; keeps my schemas managed centrally in the codebase which is nice.
I noticed one thing that might be a cause of tricky and hard to reproduce bugs when using the library improperly.
When accessing
ajv.errors
(orajv.errorsText
I presume as well) in a callback, promise or any tick after the one in which you've calledajv.validate
there is no guarantee thatajv.errors
contains the set of errors you might expect.Example:
This outputs:
Notice in the last line
Test 1
is not valid but contains no errors anymore.The cause is fairly obvious but it is an easy mistake made given how the library currently works. Why not have
var valid = ajv.validate('testSchema', data);
return a result object containing errors, errorText etc. (a sort ofValidationResult
object)?Another option to avoid misuse of the function is to rename
ajv.errors
toajv.lastErrors
. Not that I like that name, but it makes its intended use more clear.I understand these changes would be backwards incompatible, but it might be worth a thought if planning a big new release.
But overall; thanks for the great library! 👍
The text was updated successfully, but these errors were encountered: