unionAsync

Creates an union schema.

I recommend that you read the unions guide before using this schema function.

const Schema = v.unionAsync<TOptions, TMessage>(options, message);

Generics

Parameters

  • options TOptions
  • message TMessage

Explanation

With unionAsync you can validate if the input matches one of the given options. If the input does not match a schema and cannot be clearly assigned to one of the options, you can use message to customize the error message.

If a bad input can be uniquely assigned to one of the schemas based on the data type, the result of that schema is returned. Otherwise, a general issue is returned that contains the issues of each schema as subissues. This is a special case within the library, as the issues of unionAsync can contradict each other.

Returns

Examples

The following examples show how unionAsync can be used.

User schema

Schema to validate a user's email or username.

import { isEmailPresent, isUsernamePresent } from '~/api';

const UserSchema = v.unionAsync([
  v.pipeAsync(
    v.string(),
    v.email(),
    v.checkAsync(isEmailPresent, 'The email is not in the database.')
  ),
  v.pipeAsync(
    v.string(),
    v.nonEmpty(),
    v.checkAsync(isUsernamePresent, 'The username is not in the database.')
  ),
]);

The following APIs can be combined with unionAsync.

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