union

Creates an union schema.

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

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

Generics

Parameters

  • options TOptions
  • message TMessage

Explanation

With union 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 union can contradict each other.

Returns

Examples

The following examples show how union can be used.

URL schema

Schema to validate an URL or empty string.

const UrlSchema = v.union([v.pipe(v.string(), v.url()), v.literal('')]);

Number schema

Schema to validate a number or decimal string.

const NumberSchema = v.union([v.number(), v.pipe(v.string(), v.decimal())]);

Date schema

Schema to validate a Date or ISO timestamp.

const DateSchema = v.union([v.date(), v.pipe(v.string(), v.isoTimestamp())]);

The following APIs can be combined with union.

Schemas

Methods

Actions

Utils

Contributors

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

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of morinokami
  • GitHub profile picture of thundermiracle

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