Skip to content

tuple ​

A fixed-length array with its own schema for each position.

Signature ​

ts
function tuple<const T extends readonly Schema[]>(items: T, msg?: Message): Schema<[...]>

Parameters ​

ParameterTypeDescription
itemsSchema[]A schema for each position
msgMessageCustom message for all errors of the array itself

Error codes ​

CodeWhenDefault message
requiredThe value is emptyRequired
arrayNot an arrayMust be an array
itemsWrong number of itemsMust have exactly {length} {length|item|items}

Examples ​

Coordinates ​

ts
const point = tuple([pipe(number(), min(-90), max(90)), pipe(number(), min(-180), max(180))]);
// [number, number]

validate(point, [55.75, 37.62]).valid; // true
validate(point, [55.75]).errors;       // { '': 'Must have exactly 2 items' }
validate(point, [100, 0]).errors;      // { 0: 'Must be at most 90' }

Date range ​

ts
const range = tuple([coerce.date(), coerce.date()]); // [Date, Date]

See also ​

  • array — an array of any length with one schema