Skip to content

Commit

Permalink
Add content to parser and safeParser API reference on website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jul 2, 2024
1 parent 3e14db4 commit 07cf66b
Show file tree
Hide file tree
Showing 58 changed files with 486 additions and 4 deletions.
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/config/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ The following APIs can be combined with `config`.
'keyof',
'omit',
'parse',
'parser',
'partial',
'pick',
'pipe',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/fallback/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ The following APIs can be combined with `fallback`.
'keyof',
'omit',
'parse',
'parser',
'partial',
'pick',
'pipe',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion website/src/routes/api/(methods)/flatten/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ The following APIs can be combined with `flatten`.

### Methods

<ApiList items={['parse', 'safeParse']} />
<ApiList items={['parse', 'parser', 'safeParse']} />
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/keyof/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ The following APIs can be combined with `keyof`.
'getFallbacks',
'is',
'parse',
'parser',
'pipe',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/omit/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ The following APIs can be combined with `omit`.
'is',
'keyof',
'parse',
'parser',
'partial',
'pick',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
112 changes: 111 additions & 1 deletion website/src/routes/api/(methods)/parser/index.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,120 @@
---
title: parser
description: Returns a function that parses an unknown input based on a schema.
source: /methods/parser/parser.ts
contributors:
- fabian-hiller
---

import { ApiList, Property } from '~/components';
import { properties } from './properties';

# parser

> 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/parser/parser.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 parser = v.parser<TSchema, TConfig>(schema, config);
```

## Generics

- `TSchema` <Property {...properties.TSchema} />
- `TConfig` <Property {...properties.TConfig} />

## Parameters

- `schema` <Property {...properties.schema} />
- `config` <Property {...properties.config} />

## Returns

- `parser` <Property {...properties.parser} />

## Example

The following example show how `parser` can be used.

```ts
try {
const EmailSchema = v.pipe(v.string(), v.email());
const emailParser = v.parser(EmailSchema);
const email = emailParser('[email protected]');

// Handle errors if one occurs
} catch (error) {
console.log(error);
}
```

## Related

The following APIs can be combined with `parser`.

### 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',
'union',
'unknown',
'variant',
'void',
]}
/>

### Methods

<ApiList
items={[
'config',
'fallback',
'flatten',
'keyof',
'omit',
'partial',
'pick',
'pipe',
'required',
'unwrap',
]}
/>
78 changes: 78 additions & 0 deletions website/src/routes/api/(methods)/parser/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
TSchema: {
modifier: 'extends',
type: {
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
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',
},
},
parser: {
type: {
type: 'custom',
name: 'Parser',
href: '../Parser/',
generics: [
{
type: 'custom',
name: 'TSchema',
},
{
type: 'custom',
name: 'TConfig',
},
],
},
},
};
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/partial/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ The following APIs can be combined with `partial`.
'omit',
'keyof',
'parse',
'parser',
'pick',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/pick/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ The following APIs can be combined with `pick`.
'keyof',
'omit',
'parse',
'parser',
'partial',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/pipe/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ The following APIs can be combined with `pipe`.
'keyof',
'omit',
'parse',
'parser',
'partial',
'pick',
'required',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
2 changes: 2 additions & 0 deletions website/src/routes/api/(methods)/required/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ The following APIs can be combined with `required`.
'omit',
'keyof',
'parse',
'parser',
'partial',
'pick',
'safeParse',
'safeParser',
'unwrap',
]}
/>
Expand Down
Loading

0 comments on commit 07cf66b

Please sign in to comment.