-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
add attempt function #708
add attempt function #708
Conversation
danielb2
commented
Aug 17, 2015
- When Joi.assert doesn't throw, return the valid value.
This is not the usual semantic of assert, not much of a fan. |
This pattern is very common: var result = Joi.validate(obj, schema);
Hoek.assert(!result.error, result.error && result.error.annotate()); Then result.valid goes on to be used elsewhere. Do you have a better suggestion? |
Also consider that this does not break the usual and expected semantic. It's just an improvement on how we can use it. |
Where is it common ? |
Multiple different projects at Walmart, at other contracting gigs I've done (not even code I've written). https://github.com/hapijs/hapi/blob/master/lib/schema.js#L12 |
I prefer the explicit version over this proposed shortcut. Assertion should never matter and removing them should not change anything other than less safety. The more verbose code above is actually easier to follow as it breaks what is happening into two clear steps. |
Joi.assert() already does the two steps. It just doesn't do the assign. As far as assertion never mattering; removing it in the code below would render the validate statement useless. var result = Joi.validate(obj, schema);
Hoek.assert(!result.error, result.error && result.error.annotate()); If we said validation doesn't matter for anything but safety that would be one thing, but the proposed change is in the validation library itself, and in particular the assertion function. They are already coupled. Also, one could still choose the more explicit form even after this change, if that's the preference. |
This is currently the laziest way of doing it, and the downside is obvious. |
After some discussion with @Marsup he suggested the word
|
Awesome. I was about to suggest the same. I will update my code to use this once released. |
Good to go ? |
👍 |
Really glad this is being added, it will definitely lead to some cleaned up code for me. 👍 |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |