-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
521b58f
commit 8273118
Showing
2 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
158 changes: 157 additions & 1 deletion
158
website/src/routes/api/(async)/safeParserAsync/index.mdx
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,11 +1,167 @@ | ||
--- | ||
title: safeParserAsync | ||
description: Returns a function that parses an unknown input based on a schema. | ||
source: /methods/safeParser/safeParserAsync.ts | ||
contributors: | ||
- EltonLobo07 | ||
- fabian-hiller | ||
--- | ||
|
||
import { ApiList, Property } from '~/components'; | ||
import { properties } from './properties'; | ||
|
||
# safeParserAsync | ||
|
||
> 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/methods/safeParser/safeParserAsync.ts) or take a look at [issue #287](https://github.com/fabian-hiller/valibot/issues/287) to help us extend the API reference. | ||
Returns a function that parses an unknown input based on a schema. | ||
|
||
```ts | ||
const safeParser = v.safeParserAsync<TSchema, TConfig>(schema, config); | ||
``` | ||
|
||
## Generics | ||
|
||
- `TSchema` <Property {...properties.TSchema} /> | ||
- `TConfig` <Property {...properties.TConfig} /> | ||
|
||
## Parameters | ||
|
||
- `schema` <Property {...properties.schema} /> | ||
- `config` <Property {...properties.config} /> | ||
|
||
## Returns | ||
|
||
- `safeParser` <Property {...properties.safeParser} /> | ||
|
||
## Example | ||
|
||
The following example shows how `safeParserAsync` can be used. | ||
|
||
```ts | ||
import { isEmailPresent } from '~/api'; | ||
|
||
const StoredEmailSchema = v.pipeAsync( | ||
v.string(), | ||
v.email(), | ||
v.checkAsync(isEmailPresent, 'The email is not in the database.') | ||
); | ||
const safeStoredEmailParser = v.safeParserAsync(StoredEmailSchema); | ||
const result = await safeStoredEmailParser('jane@example.com'); | ||
|
||
if (result.success) { | ||
const storedEmail = result.output; | ||
} else { | ||
console.error(result.issues); | ||
} | ||
``` | ||
|
||
## Related | ||
|
||
The following APIs can be combined with `safeParserAsync`. | ||
|
||
### Schemas | ||
|
||
<ApiList | ||
items={[ | ||
'any', | ||
'array', | ||
'bigint', | ||
'blob', | ||
'boolean', | ||
'custom', | ||
'date', | ||
'enum', | ||
'file', | ||
'function', | ||
'instance', | ||
'intersect', | ||
'lazy', | ||
'literal', | ||
'looseObject', | ||
'looseTuple', | ||
'map', | ||
'nan', | ||
'never', | ||
'nonNullable', | ||
'nonNullish', | ||
'nonOptional', | ||
'null', | ||
'nullable', | ||
'nullish', | ||
'number', | ||
'object', | ||
'objectWithRest', | ||
'optional', | ||
'picklist', | ||
'promise', | ||
'record', | ||
'set', | ||
'strictObject', | ||
'strictTuple', | ||
'string', | ||
'symbol', | ||
'tuple', | ||
'tupleWithRest', | ||
'undefined', | ||
'undefinedable', | ||
'union', | ||
'unknown', | ||
'variant', | ||
'void', | ||
]} | ||
/> | ||
|
||
### Methods | ||
|
||
<ApiList | ||
items={[ | ||
'config', | ||
'fallback', | ||
'flatten', | ||
'keyof', | ||
'omit', | ||
'partial', | ||
'pick', | ||
'pipe', | ||
'required', | ||
'unwrap', | ||
]} | ||
/> | ||
|
||
### Utils | ||
|
||
<ApiList items={['getDotPath']} /> | ||
|
||
### Async | ||
|
||
<ApiList | ||
items={[ | ||
'arrayAsync', | ||
'customAsync', | ||
'fallbackAsync', | ||
'intersectAsync', | ||
'lazyAsync', | ||
'looseObjectAsync', | ||
'looseTupleAsync', | ||
'mapAsync', | ||
'nonNullableAsync', | ||
'nonNullishAsync', | ||
'nonOptionalAsync', | ||
'nullableAsync', | ||
'nullishAsync', | ||
'objectAsync', | ||
'objectWithRestAsync', | ||
'optionalAsync', | ||
'partialAsync', | ||
'pipeAsync', | ||
'recordAsync', | ||
'requiredAsync', | ||
'setAsync', | ||
'strictObjectAsync', | ||
'strictTupleAsync', | ||
'tupleAsync', | ||
'tupleWithRestAsync', | ||
'undefinedableAsync', | ||
'unionAsync', | ||
'variantAsync', | ||
]} | ||
/> |
98 changes: 98 additions & 0 deletions
98
website/src/routes/api/(async)/safeParserAsync/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,98 @@ | ||
import type { PropertyProps } from '~/components'; | ||
|
||
export const properties: Record<string, PropertyProps> = { | ||
TSchema: { | ||
modifier: 'extends', | ||
type: { | ||
type: 'union', | ||
options: [ | ||
{ | ||
type: 'custom', | ||
name: 'BaseSchema', | ||
href: '../BaseSchema/', | ||
generics: [ | ||
'unknown', | ||
'unknown', | ||
{ | ||
type: 'custom', | ||
name: 'BaseIssue', | ||
href: '../BaseIssue/', | ||
generics: ['unknown'], | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'custom', | ||
name: 'BaseSchemaAsync', | ||
href: '../BaseSchemaAsync/', | ||
generics: [ | ||
'unknown', | ||
'unknown', | ||
{ | ||
type: 'custom', | ||
name: 'BaseIssue', | ||
href: '../BaseIssue/', | ||
generics: ['unknown'], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
TConfig: { | ||
modifier: 'extends', | ||
type: { | ||
type: 'union', | ||
options: [ | ||
{ | ||
type: 'custom', | ||
name: 'Config', | ||
href: '../Config/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'InferIssue', | ||
href: '../InferIssue/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'TSchema', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
'undefined', | ||
], | ||
}, | ||
}, | ||
schema: { | ||
type: { | ||
type: 'custom', | ||
name: 'TSchema', | ||
}, | ||
}, | ||
config: { | ||
type: { | ||
type: 'custom', | ||
name: 'TConfig', | ||
}, | ||
}, | ||
safeParser: { | ||
type: { | ||
type: 'custom', | ||
name: 'SafeParserAsync', | ||
href: '../SafeParserAsync/', | ||
generics: [ | ||
{ | ||
type: 'custom', | ||
name: 'TSchema', | ||
}, | ||
{ | ||
type: 'custom', | ||
name: 'TConfig', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; |