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

throw errors #52

Closed
muturgan opened this issue Dec 18, 2018 · 1 comment
Closed

throw errors #52

muturgan opened this issue Dec 18, 2018 · 1 comment

Comments

@muturgan
Copy link
Contributor

imagine
I want to validate any data, and show any message to user on error
so I must to do somethink like:

const result = prettyValidator.test(data);

if (!result) {
   const errors = prettyValidator.getErrors(data);
   procassErrors(errors); // in my case -> res.status(422).send(errors);
   return;
}

go on...

so I must to go through this cycle twice on error. and my data can contains lot of fields.

well, i can do this:

const errors= prettyValidator.getErrors(data);

if (errors.length > 0) {
   res.status(422).send(errors);
   return;
}

go on...

these performance is better, but the syntax is not semantic - looks like I expect an error.
and the result is not semantic - I expect an empty array on success

I recommend you to generate the errorArray in the test method too, but return true on success and throw errorArray on error.

this will allow to use try/catch and write more clean code

@oussamahamdaoui
Copy link
Owner

That’s right but it will break a lot of code, people are expecting a boolean from test, it’s like regex test function

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

No branches or pull requests

2 participants