-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add content to recursive API reference on website
- Loading branch information
1 parent
eee1c56
commit 7193cb3
Showing
8 changed files
with
267 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,123 @@ | ||
--- | ||
title: recursive | ||
description: Creates a recursive schema. | ||
source: /schemas/recursive/recursive.ts | ||
contributors: | ||
- fabian-hiller | ||
--- | ||
|
||
import { Link } from '@builder.io/qwik-city'; | ||
import { ApiList, Property } from '~/components'; | ||
import { properties } from './properties'; | ||
|
||
# recursive | ||
|
||
> The content of this page is not yet ready. Until then, please use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/schemas/recursive/recursive.ts) or take a look at [issue #287](https://github.com/fabian-hiller/valibot/issues/287) to help us extend the API reference. | ||
Creates a recursive schema. | ||
|
||
> 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/BaseSchema/">`BaseSchema`</Link>. See the [examples](#examples) for more information. | ||
```ts | ||
const Schema = recursive<TGetter>(getter); | ||
``` | ||
|
||
## Generics | ||
|
||
- `TGetter` <Property {...properties.TGetter} /> | ||
|
||
## Parameters | ||
|
||
- `getter` <Property {...properties.getter} /> | ||
|
||
### Explanation | ||
|
||
The `getter` function is called lazily to retrieve the schema. This is necessary to avoid a circular dependency. | ||
|
||
## Returns | ||
|
||
- `Schema` <Property {...properties.Schema} /> | ||
|
||
## Examples | ||
|
||
The following examples show how `recursive` can be used. | ||
|
||
### Binary tree schema | ||
|
||
Recursive schema to validate a binary tree. | ||
|
||
```ts | ||
type BinaryTree = { | ||
element: string; | ||
left: BinaryTree | null; | ||
right: BinaryTree | null; | ||
}; | ||
|
||
const BinaryTreeSchema: BaseSchema<BinaryTree> = object({ | ||
element: string(), | ||
left: nullable(recursive(() => BinaryTreeSchema)), | ||
right: nullable(recursive(() => BinaryTreeSchema)), | ||
}); | ||
``` | ||
|
||
## Related | ||
|
||
The following APIs can be combined with `recursive`. | ||
|
||
### Schemas | ||
|
||
<ApiList | ||
items={[ | ||
'any', | ||
'array', | ||
'bigint', | ||
'blob', | ||
'boolean', | ||
'date', | ||
'enum_', | ||
'instance', | ||
'intersect', | ||
'literal', | ||
'map', | ||
'nan', | ||
'never', | ||
'nonNullable', | ||
'nonNullish', | ||
'nonOptional', | ||
'null_', | ||
'nullable', | ||
'nullish', | ||
'number', | ||
'object', | ||
'optional', | ||
'picklist', | ||
'record', | ||
'set', | ||
'special', | ||
'string', | ||
'symbol', | ||
'tuple', | ||
'undefined_', | ||
'union', | ||
'unknown', | ||
'variant', | ||
'void_', | ||
]} | ||
/> | ||
|
||
### Methods | ||
|
||
<ApiList | ||
items={[ | ||
'brand', | ||
'coerce', | ||
'fallback', | ||
'getDefault', | ||
'getDefaults', | ||
'getFallback', | ||
'getFallbacks', | ||
'is', | ||
'parse', | ||
'safeParse', | ||
'transform', | ||
'unwrap', | ||
]} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { PropertyProps } from '~/components'; | ||
|
||
export const properties: Record<string, PropertyProps> = { | ||
TGetter: { | ||
modifier: 'extends', | ||
type: { | ||
type: 'function', | ||
params: [], | ||
return: { | ||
type: 'custom', | ||
name: 'BaseSchema', | ||
href: '../../types/index/', | ||
}, | ||
}, | ||
}, | ||
getter: { | ||
type: { | ||
type: 'custom', | ||
name: 'TGetter', | ||
}, | ||
}, | ||
Schema: { | ||
type: { | ||
type: 'custom', | ||
name: 'RecursiveSchema', | ||
href: '../RecursiveSchema/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'TGetter', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: RecursiveSchema | ||
description: Recursive schema type. | ||
contributors: | ||
- fabian-hiller | ||
--- | ||
|
||
import { Property } from '~/components'; | ||
import { properties } from './properties'; | ||
|
||
# RecursiveSchema | ||
|
||
Recursive schema type. | ||
|
||
## Generics | ||
|
||
- `TGetter` <Property {...properties.TGetter} /> | ||
- `TOutput` <Property {...properties.TOutput} /> | ||
|
||
## Definition | ||
|
||
- `RecursiveSchema` <Property {...properties.BaseSchema} /> | ||
- `type` <Property {...properties.type} /> | ||
- `getter` <Property {...properties.getter} /> |
79 changes: 79 additions & 0 deletions
79
website/src/routes/api/(types)/RecursiveSchema/properties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { PropertyProps } from '~/components'; | ||
|
||
export const properties: Record<string, PropertyProps> = { | ||
TGetter: { | ||
modifier: 'extends', | ||
type: { | ||
type: 'function', | ||
params: [], | ||
return: { | ||
type: 'custom', | ||
name: 'BaseSchema', | ||
href: '../../types/index/', | ||
}, | ||
}, | ||
}, | ||
TOutput: { | ||
modifier: 'extends', | ||
type: 'any', | ||
default: { | ||
type: 'custom', | ||
name: 'Output', | ||
href: '../Output/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'ReturnType', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'TGetter', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
BaseSchema: { | ||
type: { | ||
type: 'custom', | ||
name: 'BaseSchema', | ||
href: '../BaseSchema/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'Input', | ||
href: '../Input/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'ReturnType', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'TGetter', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'custom', | ||
name: 'TOutput', | ||
}, | ||
], | ||
}, | ||
}, | ||
type: { | ||
type: { | ||
type: 'string', | ||
value: 'recursive', | ||
}, | ||
}, | ||
getter: { | ||
type: { | ||
type: 'custom', | ||
name: 'TGetter', | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters