UNPKG

object-shape-tester

Version:
32 lines (31 loc) 1.17 kB
import { type PartialWithUndefined } from '@augment-vir/common'; import { type TOptionalWithFlag, type TUndefined, type TUnion } from '@sinclair/typebox'; import { type Shape, type ShapeInitSchema } from '../shape/shape.js'; /** * Creates a shape that allows an object property to be missing. * * @category Shape * @example * * ```ts * import {optionalShape, checkValidShape, defineShape} from 'object-shape-tester'; * * const myShape = defineShape({ * a: '', * b: optionalShape(-1), * }); * * checkValidShape({a: 'hi', b: 0}, myShape); // `true` * checkValidShape({a: 'hi'}, myShape); // `true` * checkValidShape({b: 0}, myShape); // `false` * ``` */ export declare function optionalShape<T, const AlsoUndefined extends boolean = false>(shape: T, options?: PartialWithUndefined<{ /** * - `true`: Allow the optional property to be present and possibly `undefined`. * - `false`: If the property is present, it cannot be `undefined`. * * @default `false` */ alsoUndefined: AlsoUndefined; }>): Shape<TOptionalWithFlag<AlsoUndefined extends true ? TUnion<[TUndefined, ShapeInitSchema<T>]> : ShapeInitSchema<T>, true>>;