-
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
Create default [highly nested] object easily? #413
Comments
You can kind of work around that with |
I think Regarding use cases, there are components that I talk to that expect fully fleshed-out data structures (e.g. all elements are required, not optional). Passing '{}' does not validate. In cases where new data structures need to be created, it's nice to have a sane default. Here is my current workaround which traverses the structure and applies the children defaults to the parent-objects (coffeescript):
It works for me. As far as I'm concerned, this issue can be closed (since it was really a question, and has been answered). Feel free to re-open if you'd like to entertain something like the above making it into the code base. I'm willing to take a stab at it, but don't feel strongly about it. |
OK I get it now, I think it should be part of Joi. |
I have the same need for one of the apps I'm working on. It would be really nice to allow deeply nested defaults as you described. Thanks for taking the time to describe the problem! |
I have a pretty good idea what nested is for an object, what do you all consider it should do for an array ? |
👍 I think support for objects could be implemented first, array later. I'm happy to create a pull request. |
Does that last commit look good according to what you wanted ? |
Looks good to me. Thanks. |
Glad to see this is implemented. How can I make use of it? |
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. |
I've got a pretty massive and nested Joi structure. I'm wondering if there's an easy way to have it spit out a default object. Something like:
But instead of just returning
{ beef: 'hi' }
, have it return{ baz: { foo: 'bar' }, beef: 'hi' }
.Is this currently possible?
I could create
baz
usingJoi.object().default(...)
, but then I'd have to specify the entire substructure as the default. This would be more difficult to maintain.The text was updated successfully, but these errors were encountered: