tuple
固定长度的数组,每个位置都有自己的 Schema。
签名
ts
function tuple<const T extends readonly Schema[]>(items: T, msg?: Message): Schema<[...]>参数
| 参数 | 类型 | 说明 |
|---|---|---|
items | Schema[] | 每个位置的 Schema |
msg | Message | 数组本身所有错误的自定义信息 |
错误代码
| 代码 | 触发条件 | 默认信息 |
|---|---|---|
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 的数组