-
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
support factory-function in .default() ? #363
Comments
Nah. If you need unique instances of a default object you should move that logic to code after validation. |
This would be useful for things like: var schema = Joi.object().keys({
created: Joi.date().default(Date.now)
}); |
👍 I had the same use case @Starefossen shared. |
+1 same use case as @Starefossen |
+1 |
+1 |
Is the only use case to accept 'now' as is already the case in min and max ? |
No, I certainly have other use cases, such as special cases of Date.now (such as 00:00 of today), uuids, expire times (30 minutes from now), etc.. Obviously it's easy to do this in code, but it would be nice to be able to just pass default a function. |
@Marsup I got an use case for {
artists: [{ name: 'Madonna', role: 'main' }]
display_artist: 'Madonna'
} The {
artists: Joi.array()....,
display_artist: Joi.string().trim().default(function (obj) {
return obj.artists.reduce(function (previousVal, currentVal) {
return previousVal += currentVal.name + ' ';
}, '').trim();
})
} At the moment I'm doing the complex "default" stuff like this after Joi validation. |
+1 |
+1 I could benefit from two of the use cases mentioned already: Date.now and uuid. |
+1 Same here on date and uuid generation. |
+1 |
1 similar comment
+1 |
allow passing a method to .default(), for #363
Nice :) |
so is this a breaking change for existing joi.func().default(fn) schema expressions? |
Thanks @bleupen, this one slipped through the reviews, we'll fix that before release. |
Fixed by #555. |
add some additional guards and tests for #363
I'm very hapi you guys are working on this one, although something feels wrong about the modal nature of default() based on the presence of a description attribute. It violates the principal of least surprise imo. Have you considered adding a factory() instead and preserve the existing behavior of default()? Sent from my iPhone
|
I don't understand, this is not a breaking change, default will continue to work as it is today, it's just an improvement. |
It’s really only an issue when the item type is joi.func() (i use this feature a lot to sanitize drivers). In this case, if I understand correctly, adding a second description parameter to default() is what throws joi into "higher order function mode”. While non-breaking, it feels surprising to me. Perhaps I’m misreading the code.
|
That or having a description property on your function. Why is that surprising ? |
Because whether a function is a factory or not is orthogonal to its description. Not a huge deal, I can definitely work with whats here.
|
I think it's clearer this way, I don't want a debate about whether default or factory wins if both exists :) Joke aside, the description is more of a side-effect that's needed if we want to keep the ability to describe validations. I see it the other way around, the description is needed because you provided a function, it doesn't make the function special, except in the Joi.func() case you highlighted. |
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. |
As the readme notes using an reference-type as the value for default makes the same instance shared by all invocations, which is not that useful.
Would it be an idea to allow a function as value, to be called when a new default value is needed so it can produces a new instance?
The text was updated successfully, but these errors were encountered: