UNPKG

object-shape-tester

Version:
32 lines (31 loc) 813 B
import { Type } from '@sinclair/typebox'; import { defineShape } from '../shape/shape.js'; /** * Creates a shape that requires an of the given possible values. * * @category Shape * @example * * ```ts * import {unionShape, checkValidShape} from 'object-shape-tester'; * * const myShape = unionShape('', -1); * * checkValidShape('a', myShape); // `true` * checkValidShape(10, myShape); // `true` * checkValidShape({}, myShape); // `false` * ``` */ export function unionShape(...inits) { let defaultValue; const schemas = inits.map((init, index) => { const shape = defineShape(init); if (!index) { defaultValue = shape.default; } return shape.$_schema; }); return defineShape(Type.Union(schemas, { default: defaultValue, })); }