union

Creates an union schema.

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

// Union schema with an optional pipe
const Schema = union<TOptions>(options, pipe);

// Union schema with an optional message and pipe
const Schema = union<TOptions>(options, message, pipe);

Generics

Parameters

Explanation

With union you can validate if the input matches one of the given options. With pipe you can transform and validate the further details of the union. 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 = union([string([url()]), literal('')]);

Number schema

Schema to validate a number or decimal string.

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

Date schema

Schema to validate a Date or ISO timestamp.

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

The following APIs can be combined with union.

Schemas

Methods

Transformations

Validations

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 ivan-mihalic
  • 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