nullableAsync

Creates a nullable schema.

const Schema = v.nullableAsync<TWrapped, TDefault>(wrapped, default_);

Generics

Parameters

  • wrapped TWrapped
  • default_ TDefault

Explanation

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

Note that nullableAsync does not accept undefined as an input. If you want to accept undefined inputs, use optionalAsync, and if you want to accept null and undefined inputs, use nullishAsync instead. Also, if you want to set a default output value for any invalid input, you should use fallbackAsync instead.

Returns

Examples

The following examples show how nullableAsync can be used.

Nullable username schema

Schema that accepts a unique username 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 null.

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

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

Username schema

Schema that accepts a unique username.

import { isUsernameUnique } from '~/api';

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

The following APIs can be combined with nullableAsync.

Schemas

Methods

Actions

Utils

Async

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of santoshyadavdev
  • GitHub profile picture of EltonLobo07

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of Unique-Pixels
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of nickytonline
  • GitHub profile picture of KubaJastrz
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of akhmadqasim
  • GitHub profile picture of dslatkin