From 5cd4e7e981eb59f51a2dfb242da6a4a3b1ceb270 Mon Sep 17 00:00:00 2001 From: Robert Churchill Date: Thu, 25 Sep 2025 10:33:21 +0100 Subject: [PATCH 1/2] Add example for optional defaulting to undefined Relates to the discussion in #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? --- website/src/routes/api/(schemas)/optional/index.mdx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/src/routes/api/(schemas)/optional/index.mdx b/website/src/routes/api/(schemas)/optional/index.mdx index 6282dd9f3..71c24c601 100644 --- a/website/src/routes/api/(schemas)/optional/index.mdx +++ b/website/src/routes/api/(schemas)/optional/index.mdx @@ -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 this as a function returning `undefined`, otherwise this will be seen as no argument being passed). + +```ts +const OptionalEntrySchema = v.object({ + key: v.optional(v.string(), () => undefined), +}); +const res = v.parse(OptionalEntrySchema, {}); +// res = { key: undefined }; +``` + ## Related The following APIs can be combined with `optional`. From 81a2ea124f89007d44481da2cdf1fceabcd393b9 Mon Sep 17 00:00:00 2001 From: Robert Churchill Date: Thu, 25 Sep 2025 10:37:50 +0100 Subject: [PATCH 2/2] Fix editing typo --- website/src/routes/api/(schemas)/optional/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/routes/api/(schemas)/optional/index.mdx b/website/src/routes/api/(schemas)/optional/index.mdx index 71c24c601..d5ecb47cd 100644 --- a/website/src/routes/api/(schemas)/optional/index.mdx +++ b/website/src/routes/api/(schemas)/optional/index.mdx @@ -82,7 +82,7 @@ const NumberSchema = v.unwrap(OptionalNumberSchema); ### Default to `undefined` -If you wish optional entries to default to an `undefined` value, you must pass this as a function returning `undefined`, otherwise this will be seen as no argument being passed). +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. ```ts const OptionalEntrySchema = v.object({