-
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
Destructuring Throws Fatal #1631
Comments
And for anyone curious, here's the const Joi = require('joi');
const joi = new Proxy(Joi, {
get(o, name) {
return o[name].bind(o);
}
});
const { boolean, validate } = joi;
const schema = boolean();
const result = validate(true, schema); I haven't run benchmarks on what this will do to performance, but I do know from past experience that proxying does make a dent in speed. |
The returned Joi object contains state, which is required for it to work, thus requiring a bind(). The Proxy solution you propose should work quite performant, since it is only applied during the initial destructuring. |
Thanks for replying, I suspected as much. Pending tests to confirm performance, would there be any objection to offering that capability by default in the package? |
Yes, for people transpiling it for the browser it's going to be a problem. |
thanks @Marsup |
Context
What are you trying to achieve or the steps to reproduce ?
Which result you had ?
What did you expect ?
Prepending
Joi
onto the beginning of every assertion type is rather messy (completely subjective, personal opinion) and while I absolutely dig the expressive nature of joi, I'm looking for ways to dial down the noise in code and make the schema setup a little cleaner. I was very surprised to learn that none of the assertion methods can be safely destructured from the main instance (it's not mentioned anywhere in the docs nor issues).It's worth noting that this fails as well:
I could get nuts and implement a
Proxy
to make the assignment happen, but I'd rather not slow down the validation process just for the sake of some pretty code. I'm really hoping there's a way you can cater to those who want to use the assertions and destructure the method names. I'm being pushed hard to useyup
for that reason alone, but I prefer the nuances ofjoi
.The text was updated successfully, but these errors were encountered: