Skip to content

Commit

Permalink
Add "dependent default" example to v.optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fartinmartin authored Oct 17, 2024
1 parent c0ecedf commit 5473104
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions website/src/routes/api/(schemas)/optional/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ const OptionalEntrySchema = v.object({
});
```

### Optional schema with a dependent default value

Object schema with an optional `version` entry, where the default value depends on the `id` entry.

```ts
const OptionalEntrySchema = v.pipe(
v.object({
id: v.string(),
version: v.optional(v.string()),
}),
v.transform((input) => {
if (!input.version) {
input.version = getVersionFromId(input.id);
}
return input;
}),
);
```

### Unwrap optional schema

Use <Link href="../unwrap/">`unwrap`</Link> to undo the effect of `optional`.
Expand Down

0 comments on commit 5473104

Please sign in to comment.