object-shape-tester
Version:
Test object properties and value types.
40 lines (39 loc) • 798 B
JavaScript
import { Type } from '@sinclair/typebox';
import { defineShape } from '../shape/shape.js';
/**
* Converts an object shape to a partial shape.
*
* @category Shape
* @example
*
* ```ts
* import {partialShape, checkValidShape} from 'object-shape-tester';
*
* const myShape = partialShape({
* id: '',
* name: '',
* age: -1,
* });
*
* checkValidShape({}, myShape); // `true`
* checkValidShape(
* {
* id: '123',
* name: 'hello',
* },
* myShape,
* ); // `true`
* checkValidShape(
* {
* invalid: 'prop',
* },
* myShape,
* ); // `false`
* ```
*/
export function partialShape(init) {
const shape = defineShape(init);
return defineShape(Type.Partial(shape.$_schema, {
default: shape.default,
}));
}