union

Creates an union schema.

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

const Schema = 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

Contributors

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

  • GitHub profile picture of fabian-hiller
  • 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 dailydotdev
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of seahindeniz