fallbackAsync

Returns a fallback value as output if the input does not match the schema.

const Schema = v.fallbackAsync<TSchema, TFallback>(schema, fallback);

Generics

Parameters

  • schema TSchema
  • fallback TFallback

Explanation

fallbackAsync allows you to define a fallback value for the output that will be used if the validation of the input fails. This means that no issues will be returned when using fallbackAsync and the schema will always return an output.

If you only want to set a default value for null or undefined inputs, you should use optionalAsync, nullableAsync or nullishAsync instead.

The fallback value is not validated. Make sure that the fallback value matches your schema.

Returns

Examples

The following examples show how fallbackAsync can be used.

Unique username schema

Schema that will always return a unique username.

By using a function as the fallbackAsync parameter, the schema will return any unique username each time the input does not match the schema.

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

const UniqueUsernameSchema = v.fallbackAsync(
  v.pipeAsync(v.string(), v.minLength(4), v.checkAsync(isUsernameUnique)),
  getAnyUniqueUsername
);

The following APIs can be combined with fallbackAsync.

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 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 fabulousgk
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of isoden
  • GitHub profile picture of nickytonline
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of luckasnix
  • GitHub profile picture of andrew-3kb
  • GitHub profile picture of dslatkin