Skip to content

tuple ​

固定长度的数组,每个位置都有自己的 Schema。

签名 ​

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

参数 ​

参数类型说明
itemsSchema[]每个位置的 Schema
msgMessage数组本身所有错误的自定义信息

错误代码 ​

代码触发条件默认信息
required值为空Required
array不是数组Must be an array
items元素数量不符Must have exactly {length} {length|item|items}

示例 ​

坐标 ​

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' }

日期范围 ​

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

另请参阅 ​

  • array:任意长度、使用同一个 Schema 的数组