You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: clarify pipe behavior for missing keys in optional schemas
- Add important notes about pipe execution for missing keys
- Explain that default values are required for pipe execution
- Improve clarity across optional, exactOptional, and guides
- Enhance understanding of transformation behavior
With `exactOptional` the validation of your schema will pass missing object entries, and if you specify a `default_` input value, the schema will use it if the object entry is missing. For this reason, the output type may differ from the input type of the schema.
34
34
35
+
> **Important**: When a key is missing and no `default_` value is provided, the schema's pipe (including transformations) will not be executed. To ensure pipes run for missing keys, provide a `default_` value.
36
+
35
37
> The difference to <Linkhref="../optional/">`optional`</Link> is that this schema function follows the implementation of TypeScript's [`exactOptionalPropertyTypes` configuration](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) and only allows missing but not undefined object entries.
When using `exactOptional` in a <Linkhref="../../pipe/">`pipe`</Link>, the pipe actions only execute if a `default_` value is provided or the key is present. This applies to all pipe actions including <Linkhref="../../transform/">`transform`</Link>, <Linkhref="../../check/">`check`</Link>, and others.
73
+
74
+
```ts
75
+
// ❌ Pipe actions will NOT execute for missing keys - result stays undefined
76
+
const SchemaWithoutDefault =v.object({
77
+
isEnabled: v.pipe(
78
+
v.exactOptional(v.string()),
79
+
v.transform((value) =>value==='1') // Won't run for missing keys
80
+
),
81
+
});
82
+
83
+
// ✅ Pipe actions WILL execute for missing keys using the default value
84
+
const SchemaWithDefault =v.object({
85
+
isEnabled: v.pipe(
86
+
v.exactOptional(v.string(), '0'), // Default value provided
87
+
v.transform((value) =>value==='1') // Runs for all cases
With `optional` the validation of your schema will pass `undefined` inputs, and if you specify a `default_` input value, the schema will use it if the input is `undefined`. For this reason, the output type may differ from the input type of the schema.
35
35
36
+
> **Important**: When used in object schemas, if a key is missing and no `default_` value is provided, the schema's pipe (including transformations) will not be executed. To ensure pipes run for missing keys, provide a `default_` value.
37
+
36
38
> Note that `optional` does not accept `null` as an input. If you want to accept `null` inputs, use <Linkhref="../nullable/">`nullable`</Link>, and if you want to accept `null` and `undefined` inputs, use <Linkhref="../nullish/">`nullish`</Link> instead. Also, if you want to set a default output value for any invalid input, you should use <Linkhref="../fallback/">`fallback`</Link> instead.
When using `optional` in a <Linkhref="../../pipe/">`pipe`</Link>, the pipe actions only execute if a `default_` value is provided or the key is present. This applies to all pipe actions including <Linkhref="../../transform/">`transform`</Link>, <Linkhref="../../check/">`check`</Link>, and others.
88
+
89
+
```ts
90
+
// ❌ Pipe actions will NOT execute for missing keys - result stays undefined
91
+
const SchemaWithoutDefault =v.object({
92
+
isActive: v.pipe(
93
+
v.optional(v.string()),
94
+
v.transform((value) =>value==='true') // Won't run for missing keys
95
+
),
96
+
});
97
+
98
+
// ✅ Pipe actions WILL execute for missing keys using the default value
99
+
const SchemaWithDefault =v.object({
100
+
isActive: v.pipe(
101
+
v.optional(v.string(), 'false'), // Default value provided
102
+
v.transform((value) =>value==='true') // Runs for all cases
When using <Linkhref="/api/optional/">`optional`</Link>, <Linkhref="/api/exactOptional/">`exactOptional`</Link>, or other optional schemas with <Linkhref="/api/pipe/">`pipe`</Link> containing actions, it's important to understand when the pipe executes:
95
+
96
+
### Without default values
97
+
98
+
If no `default_` value is provided, missing object keys are completely ignored and their pipes will **not** be executed.
99
+
100
+
```ts
101
+
import*asvfrom'valibot';
102
+
103
+
const Schema =v.object({
104
+
value: v.pipe(
105
+
v.optional(v.string()),
106
+
v.transform((input) =>input.toUpperCase()) // Won't run for missing keys
107
+
),
108
+
});
109
+
110
+
const result =v.parse(Schema, {}); // { } - value is undefined (value?: string | undefined)
111
+
```
112
+
113
+
### With default values
114
+
115
+
When a `default_` value is provided, the pipe will execute for missing keys using the default value.
116
+
117
+
```ts
118
+
import*asvfrom'valibot';
119
+
120
+
const Schema =v.object({
121
+
value: v.pipe(
122
+
v.optional(v.string(), 'hello'), // Default value provided
123
+
v.transform((input) =>input.toUpperCase()) // Runs with 'hello' for missing keys
0 commit comments