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

add nullishAsync API reference #961

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
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
232 changes: 231 additions & 1 deletion website/src/routes/api/(async)/nullishAsync/index.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,240 @@
---
title: nullishAsync
description: Creates a nullish schema.
source: /schemas/nullish/nullishAsync.ts
contributors:
- fabian-hiller
- santoshyadavdev
- EltonLobo07
---

import { Link } from '@builder.io/qwik-city';
import { ApiList, Property } from '~/components';
import { properties } from './properties';

# nullishAsync

> 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/nullish/nullishAsync.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 nullish schema.

```ts
const Schema = v.nullishAsync<TWrapped, TDefault>(wrapped, default_);
```

## Generics

- `TWrapped` <Property {...properties.TWrapped} />
- `TDefault` <Property {...properties.TDefault} />

## Parameters

- `wrapped` <Property {...properties.wrapped} />
- `default_` {/* prettier-ignore */}<Property {...properties.default_} />

### Explanation

With `nullishAsync` the validation of your schema will pass `undefined` and `null` inputs, and if you specify a `default_` input value, the schema will use it if the input is `undefined` or `null`. For this reason, the output type may differ from the input type of the schema.

> Note that `nullishAsync` accepts `undefined` or `null` as an input. If you want to accept only `null` inputs, use <Link href="../nullableAsync/">`nullableAsync`</Link>, and if you want to accept only `undefined` inputs, use <Link href="../optionalAsync/">`optionalAsync`</Link> instead. Also, if you want to set a default output value for any invalid input, you should use <Link href="../fallbackAsync/">`fallbackAsync`</Link> instead.

## Returns

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

## Examples

The following examples show how `nullishAsync` can be used.

### Nullish username schema

Schema that accepts a unique username, `undefined` or `null`.

> By using a function as the `default_` parameter, the schema will return a unique username from the function call each time the input is `undefined` or `null`.

```ts
import { getUniqueUsername, isUsernameUnique } from '~/api';

const NullishUsernameSchema = v.nullishAsync(
v.pipeAsync(
v.string(),
v.nonEmpty(),
v.checkAsync(isUsernameUnique, 'The username is not unique.')
),
getUniqueUsername
);
```

### New user schema

Schema to validate new user details.

```ts
import { isEmailUnique, isUsernameUnique } from '~/api';

const NewUserSchema = v.objectAsync({
email: v.pipeAsync(
v.string(),
v.email(),
v.checkAsync(isEmailUnique, 'The email is not unique.')
),
username: v.nullishAsync(
v.pipeAsync(
v.string(),
v.nonEmpty(),
v.checkAsync(isUsernameUnique, 'The username is not unique.')
)
),
password: v.pipe(v.string(), v.minLength(8)),
});

/*
The input and output types of the schema:
{
email: string;
password: string;
username?: string | null | undefined;
}
*/
```

### Username schema

Schema that accepts a unique username.

```ts
import { isUsernameUnique } from '~/api';

const UsernameSchema = v.unwrap(
// Assume this schema is from a different file and is reused here
v.nullishAsync(
v.pipeAsync(
v.string(),
v.nonEmpty(),
v.checkAsync(isUsernameUnique, 'The username is not unique.')
)
)
);
```

## Related

The following APIs can be combined with `nullishAsync`.

### 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', 'getDefault', 'getFallback', 'unwrap']} />

### Actions

<ApiList
items={[
'brand',
'check',
'description',
'metadata',
'partialCheck',
'rawCheck',
'rawTransform',
'readonly',
'title',
'transform',
]}
/>

### Utils

<ApiList items={['entriesFromList', 'isOfKind', 'isOfType']} />

### Async

<ApiList
items={[
'arrayAsync',
'checkAsync',
'customAsync',
'fallbackAsync',
'getDefaultsAsync',
'getFallbacksAsync',
'intersectAsync',
'lazyAsync',
'looseObjectAsync',
'looseTupleAsync',
'mapAsync',
'nonNullableAsync',
'nonNullishAsync',
'nonOptionalAsync',
'nullableAsync',
'objectAsync',
'objectWithRestAsync',
'optionalAsync',
'parseAsync',
'parserAsync',
'partialCheckAsync',
'pipeAsync',
'rawCheckAsync',
'rawTransformAsync',
'recordAsync',
'safeParseAsync',
'safeParserAsync',
'setAsync',
'strictObjectAsync',
'strictTupleAsync',
'transformAsync',
'tupleAsync',
'tupleWithRestAsync',
'undefinedableAsync',
'unionAsync',
'variantAsync',
]}
/>
89 changes: 89 additions & 0 deletions website/src/routes/api/(async)/nullishAsync/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
TWrapped: {
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'],
},
],
},
],
},
},
TDefault: {
modifier: 'extends',
type: {
type: 'custom',
name: 'DefaultAsync',
href: '../DefaultAsync/',
generics: [
{
type: 'custom',
name: 'TWrapped',
},
{
type: 'union',
options: ['null', 'undefined'],
},
],
},
},
wrapped: {
type: {
type: 'custom',
name: 'TWrapped',
},
},
default_: {
type: {
type: 'custom',
name: 'TDefault',
},
},
Schema: {
type: {
type: 'custom',
name: 'NullishSchemaAsync',
href: '../NullishSchemaAsync/',
generics: [
{
type: 'custom',
name: 'TWrapped',
},
{
type: 'custom',
name: 'TDefault',
},
],
},
},
};
Loading