object-shape-tester
Version:
Test object properties and value types.
41 lines (40 loc) • 845 B
JavaScript
import { Type } from '@sinclair/typebox';
import { defineShape } from '../shape/shape.js';
/**
* Creates a shape that requires a tuple.
*
* @category Shape
* @example
*
* ```ts
* import {tupleShape, checkValidShape} from 'object-shape-tester';
*
* const myShape = tupleShape('', -1);
*
* checkValidShape(
* [
* 'a',
* 10,
* ],
* myShape,
* ); // `true`
* checkValidShape(
* [
* 'a',
* ],
* myShape,
* ); // `false`
* ```
*/
export function tupleShape(...tupleParts) {
const defaultValue = [];
const schemas = [];
tupleParts.forEach((part) => {
const shape = defineShape(part);
defaultValue.push(shape.default);
schemas.push(shape.$_schema);
});
return defineShape(Type.Tuple(schemas, {
default: defaultValue,
}));
}