Skip to content
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 do we attach the error list to the validation function? #236

Closed
tshelburne opened this issue Jul 16, 2016 · 2 comments
Closed

Why do we attach the error list to the validation function? #236

tshelburne opened this issue Jul 16, 2016 · 2 comments
Labels

Comments

@tshelburne
Copy link

tshelburne commented Jul 16, 2016

This is less of an issue and more of a question, but I've never understood why schema validators prefer to store error lists on the function object instead of just returning them - I can understand wanting to simply return a boolean for conditional checks, but the statefulness greatly increases (at least for me) the mental strain to figure out how and when errors should be added. Recursion is so much more difficult to use, it's feels easy to end up in weird states, and it feels like we're passing data by attaching to function instances rather than using return as it's intended.

It seems like to me either returning a list of errors or a tuple of errors and validity would be the most transparent and simple interface - is there a deeper justification I'm just not aware of?

To be clear, I'm not against the "stateful" storage of errors in principle, but I think it should be left to the downstream developer - if I were to feel that it was necessary for my particular project, I would do something like the following to accomplish the same interface:

validation.js

import AJV from 'ajv';

export var errors = []

... // all the AJV initialization business

export default function(obj) {
  errors = validate(obj)
  return errors.length > 0
}

Right now it's always required. Just seems extreme.

@epoberezkin
Copy link
Member

The reason is performance - it is faster to return boolean. I was implementing the same api as other fast validators. Slow validators usually return { valid: false, errors: [] } object.

You can either define your own wrapper function or use json-schema-consolidate to return such object.

@tshelburne
Copy link
Author

Makes sense - thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants
@tshelburne @epoberezkin and others