Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve optionals guide #948

Merged
Merged
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
17 changes: 9 additions & 8 deletions website/src/routes/guides/(schemas)/optionals/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description: >-
contributors:
- fabian-hiller
- fartinmartin
- EltonLobo07
---

import { Link } from '@builder.io/qwik-city';
Expand Down Expand Up @@ -47,8 +48,8 @@ import * as v from 'valibot';

const OptionalStringSchema = v.optional(v.string(), "I'm the default!");

type OptionalStringInput = v.Input<typeof OptionalStringSchema>; // string | undefined
type OptionalStringOutput = v.Output<typeof OptionalStringSchema>; // string
type OptionalStringInput = v.InferInput<typeof OptionalStringSchema>; // string | undefined
type OptionalStringOutput = v.InferOutput<typeof OptionalStringSchema>; // string
```

By providing a default value, the input type of the schema now differs from the output type. The schema in the example now accepts `string` and `undefined` as input, but returns a string as output in both cases.
Expand All @@ -70,17 +71,17 @@ The previous example thus creates a new instance of the [`Date`](https://develop
In rare cases, a default value for an optional entry may depend on the values of another entries in the same object. This can be achieved by using <Link href="/api/transform/">`transform`</Link> in the <Link href="/api/pipe/">`pipe`</Link> of the object.

```ts
import * as v from 'valibot';

const CalculationSchema = v.pipe(
v.object({
a: v.number(),
b: v.number(),
sum: v.optional(v.number()),
}),
v.transform((input) => {
if (input.sum === undefined) {
return { ...input, sum: input.a + input.b };
}
return input;
})
v.transform((input) => ({
...input,
sum: input.sum === undefined ? input.a + input.b : input.sum,
}))
);
```
Loading