Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions website/src/routes/api/(schemas)/optional/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ const OptionalNumberSchema = v.optional(v.number());
const NumberSchema = v.unwrap(OptionalNumberSchema);
```

### 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, ..."?


```ts
const OptionalEntrySchema = v.object({
key: v.optional(v.string(), () => undefined),
});
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),
});


## Related

The following APIs can be combined with `optional`.
Expand Down