Skip to content

Conversation

robations
Copy link

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?

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?
Copy link

vercel bot commented Sep 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
valibot Ready Ready Preview Comment Sep 25, 2025 9:43am

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. documentation Improvements or additions to documentation labels Sep 25, 2025
Copy link
Owner

@fabian-hiller fabian-hiller left a 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.
Copy link
Owner

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 };
```
Copy link
Owner

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),
});

@fabian-hiller fabian-hiller self-assigned this Sep 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants