Skip to content

Commit

Permalink
Change JSON example and add it to lazy reference
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Ewald <[email protected]>
  • Loading branch information
fabian-hiller and jhnns committed Dec 22, 2024
1 parent 9ba11cf commit 4758c9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
31 changes: 29 additions & 2 deletions website/src/routes/api/(schemas)/lazy/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const Schema = v.lazy<TWrapped>(getter);

The `getter` function is called lazily to retrieve the schema. This is necessary to be able to access the input through the first argument of the `getter` function and to avoid a circular dependency for recursive schemas.

> Due to a TypeScript limitation, the input and output types of recursive schemas cannot be inferred automatically. Therefore, you must explicitly specify these types using <Link href="/api/GenericSchema/">`GenericSchema`</Link>. Please see the examples below.
## Returns

- `Schema` <Property {...properties.Schema} />
Expand All @@ -44,8 +46,6 @@ The following examples show how `lazy` can be used.

Recursive schema to validate a binary tree.

> Due to a TypeScript limitation, the input and output types of recursive schemas cannot be inferred automatically. Therefore, you must explicitly specify these types using <Link href="/api/GenericSchema/">`GenericSchema`</Link>.
```ts
type BinaryTree = {
element: string;
Expand All @@ -60,6 +60,33 @@ const BinaryTreeSchema: v.GenericSchema<BinaryTree> = v.object({
});
```

### JSON data schema

Schema to validate all possible `JSON` values.

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

type JsonData =
| string
| number
| boolean
| null
| { [key: string]: JsonData }
| JsonData[];

const JsonSchema: v.GenericSchema<JsonData> = v.lazy(() =>
v.union([
v.string(),
v.number(),
v.boolean(),
v.null(),
v.record(v.string(), JsonSchema),
v.array(JsonSchema),
])
);
```

### Lazy union schema

Schema to validate a discriminated union of objects.
Expand Down
27 changes: 27 additions & 0 deletions website/src/routes/guides/(schemas)/other/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,30 @@ const BinaryTreeSchema: v.GenericSchema<BinaryTree> = v.object({
right: v.nullable(v.lazy(() => BinaryTreeSchema)),
});
```

### JSON schema

Another practical use case for `lazy` is a schema for all possible `JSON` values. These are all values that can be serialized and deserialized using `JSON.stringify()` and `JSON.parse()`.

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

type JsonData =
| string
| number
| boolean
| null
| { [key: string]: JsonData }
| JsonData[];

const JsonSchema: v.GenericSchema<JsonData> = v.lazy(() =>
v.union([
v.string(),
v.number(),
v.boolean(),
v.null(),
v.record(v.string(), JsonSchema),
v.array(JsonSchema),
])
);
```

0 comments on commit 4758c9d

Please sign in to comment.