-
-
Notifications
You must be signed in to change notification settings - Fork 275
Adds example for optional #1321
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
base: main
Are you sure you want to change the base?
Conversation
Relates to the discussion in fabian-hiller#1207 Though I'm thinking this issue might relate to multiple (or all) schemas where undefined is the desired default? If so, is there a better place to document this?
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to also add it to nullish
.
Maybe should also add a little info to the default value section of the optionals guide.
|
||
### Default to `undefined` | ||
|
||
If you wish optional entries to default to an `undefined` value, you must pass the `default_` parameter as a function returning `undefined`, otherwise this will be seen as no parameter being passed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change it to "If you wish missing object entries to default to an undefined
value, ..."?
}); | ||
const res = v.parse(OptionalEntrySchema, {}); | ||
// res = { key: undefined }; | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would just write this to be consistent with the other example. But I see why you added it.
const OptionalEntrySchema = v.object({
key: v.optional(v.string(), () => undefined),
});
What it does
Adds an example of setting a default of
undefined
for the optional schema, which came from discussion in #1207.Open qus
This way of setting undefined might potentially apply to all schemas? Should this also or instead be documented at a higher level?