UNPKG

@sinclair/typebox

Version:

Json Schema Type Builder with Static Type Resolution for TypeScript

49 lines (47 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeSystemPolicy = void 0; const index_1 = require("../value/guard/index"); var TypeSystemPolicy; (function (TypeSystemPolicy) { // ------------------------------------------------------------------ // TypeSystemPolicy // ------------------------------------------------------------------ /** Shared assertion routines used by the value and errors modules */ /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */ TypeSystemPolicy.ExactOptionalPropertyTypes = false; /** Sets whether arrays should be treated as a kind of objects. The default is `false` */ TypeSystemPolicy.AllowArrayObject = false; /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */ TypeSystemPolicy.AllowNaN = false; /** Sets whether `null` should validate for void types. The default is `false` */ TypeSystemPolicy.AllowNullVoid = false; /** Asserts this value using the ExactOptionalPropertyTypes policy */ function IsExactOptionalProperty(value, key) { return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined; } TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty; /** Asserts this value using the AllowArrayObjects policy */ function IsObjectLike(value) { const isObject = (0, index_1.IsObject)(value); return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !(0, index_1.IsArray)(value); } TypeSystemPolicy.IsObjectLike = IsObjectLike; /** Asserts this value as a record using the AllowArrayObjects policy */ function IsRecordLike(value) { return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array); } TypeSystemPolicy.IsRecordLike = IsRecordLike; /** Asserts this value using the AllowNaN policy */ function IsNumberLike(value) { const isNumber = (0, index_1.IsNumber)(value); return TypeSystemPolicy.AllowNaN ? isNumber : isNumber && Number.isFinite(value); } TypeSystemPolicy.IsNumberLike = IsNumberLike; /** Asserts this value using the AllowVoidNull policy */ function IsVoidLike(value) { const isUndefined = (0, index_1.IsUndefined)(value); return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined; } TypeSystemPolicy.IsVoidLike = IsVoidLike; })(TypeSystemPolicy || (exports.TypeSystemPolicy = TypeSystemPolicy = {}));