@sinclair/typebox
Advanced tools
| export * from './system'; |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/system | ||
| The MIT License (MIT) | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./system"), exports); |
| import * as Types from '../typebox'; | ||
| export declare class TypeSystemDuplicateTypeKind extends Error { | ||
| constructor(kind: string); | ||
| } | ||
| export declare class TypeSystemDuplicateFormat extends Error { | ||
| constructor(kind: string); | ||
| } | ||
| /** Creates user defined types and formats and provides overrides for value checking behaviours */ | ||
| export declare namespace TypeSystem { | ||
| /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */ | ||
| let ExactOptionalPropertyTypes: boolean; | ||
| /** Sets whether arrays should be treated as a kind of objects. The default is `false` */ | ||
| let AllowArrayObjects: boolean; | ||
| /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */ | ||
| let AllowNaN: boolean; | ||
| /** Sets whether `null` should validate for void types. The default is `false` */ | ||
| let AllowVoidNull: boolean; | ||
| /** Creates a new type */ | ||
| function Type<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>; | ||
| /** Creates a new string format */ | ||
| function Format<F extends string>(format: F, check: (value: string) => boolean): F; | ||
| } |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/system | ||
| The MIT License (MIT) | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDuplicateTypeKind = void 0; | ||
| const Types = require("../typebox"); | ||
| class TypeSystemDuplicateTypeKind extends Error { | ||
| constructor(kind) { | ||
| super(`Duplicate type kind '${kind}' detected`); | ||
| } | ||
| } | ||
| exports.TypeSystemDuplicateTypeKind = TypeSystemDuplicateTypeKind; | ||
| class TypeSystemDuplicateFormat extends Error { | ||
| constructor(kind) { | ||
| super(`Duplicate string format '${kind}' detected`); | ||
| } | ||
| } | ||
| exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat; | ||
| /** Creates user defined types and formats and provides overrides for value checking behaviours */ | ||
| var TypeSystem; | ||
| (function (TypeSystem) { | ||
| // ------------------------------------------------------------------------ | ||
| // Assertion Policies | ||
| // ------------------------------------------------------------------------ | ||
| /** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */ | ||
| TypeSystem.ExactOptionalPropertyTypes = false; | ||
| /** Sets whether arrays should be treated as a kind of objects. The default is `false` */ | ||
| TypeSystem.AllowArrayObjects = false; | ||
| /** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */ | ||
| TypeSystem.AllowNaN = false; | ||
| /** Sets whether `null` should validate for void types. The default is `false` */ | ||
| TypeSystem.AllowVoidNull = false; | ||
| // ------------------------------------------------------------------------ | ||
| // String Formats and Types | ||
| // ------------------------------------------------------------------------ | ||
| /** Creates a new type */ | ||
| function Type(kind, check) { | ||
| if (Types.TypeRegistry.Has(kind)) | ||
| throw new TypeSystemDuplicateTypeKind(kind); | ||
| Types.TypeRegistry.Set(kind, check); | ||
| return (options = {}) => Types.Type.Unsafe({ ...options, [Types.Kind]: kind }); | ||
| } | ||
| TypeSystem.Type = Type; | ||
| /** Creates a new string format */ | ||
| function Format(format, check) { | ||
| if (Types.FormatRegistry.Has(format)) | ||
| throw new TypeSystemDuplicateFormat(format); | ||
| Types.FormatRegistry.Set(format, check); | ||
| return format; | ||
| } | ||
| TypeSystem.Format = Format; | ||
| })(TypeSystem || (exports.TypeSystem = TypeSystem = {})); |
| import * as Types from '../typebox'; | ||
| export declare class ValueConvertUnknownTypeError extends Error { | ||
| readonly schema: Types.TSchema; | ||
| constructor(schema: Types.TSchema); | ||
| } | ||
| export declare class ValueConvertDereferenceError extends Error { | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
| export declare namespace ValueConvert { | ||
| function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): unknown; | ||
| function Convert<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): unknown; | ||
| } |
+372
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/value | ||
| The MIT License (MIT) | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueConvert = exports.ValueConvertDereferenceError = exports.ValueConvertUnknownTypeError = void 0; | ||
| const Types = require("../typebox"); | ||
| const clone_1 = require("./clone"); | ||
| const check_1 = require("./check"); | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Errors | ||
| // ---------------------------------------------------------------------------------------------- | ||
| class ValueConvertUnknownTypeError extends Error { | ||
| constructor(schema) { | ||
| super('ValueConvert: Unknown type'); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueConvertUnknownTypeError = ValueConvertUnknownTypeError; | ||
| class ValueConvertDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`ValueConvert: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueConvertDereferenceError = ValueConvertDereferenceError; | ||
| var ValueConvert; | ||
| (function (ValueConvert) { | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Guards | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function IsObject(value) { | ||
| return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value); | ||
| } | ||
| function IsArray(value) { | ||
| return typeof value === 'object' && globalThis.Array.isArray(value); | ||
| } | ||
| function IsDate(value) { | ||
| return typeof value === 'object' && value instanceof globalThis.Date; | ||
| } | ||
| function IsSymbol(value) { | ||
| return typeof value === 'symbol'; | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsBoolean(value) { | ||
| return typeof value === 'boolean'; | ||
| } | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number' && !isNaN(value); | ||
| } | ||
| function IsStringNumeric(value) { | ||
| return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value)); | ||
| } | ||
| function IsValueToString(value) { | ||
| return IsBigInt(value) || IsBoolean(value) || IsNumber(value); | ||
| } | ||
| function IsValueTrue(value) { | ||
| return value === true || (IsNumber(value) && value === 1) || (IsBigInt(value) && value === globalThis.BigInt('1')) || (IsString(value) && (value.toLowerCase() === 'true' || value === '1')); | ||
| } | ||
| function IsValueFalse(value) { | ||
| return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === globalThis.BigInt('0')) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0')); | ||
| } | ||
| function IsTimeStringWithTimeZone(value) { | ||
| return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value); | ||
| } | ||
| function IsTimeStringWithoutTimeZone(value) { | ||
| return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value); | ||
| } | ||
| function IsDateTimeStringWithTimeZone(value) { | ||
| return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value); | ||
| } | ||
| function IsDateTimeStringWithoutTimeZone(value) { | ||
| return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value); | ||
| } | ||
| function IsDateString(value) { | ||
| return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\d$/i.test(value); | ||
| } | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Convert | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function TryConvertLiteralString(value, target) { | ||
| const conversion = TryConvertString(value); | ||
| return conversion === target ? conversion : value; | ||
| } | ||
| function TryConvertLiteralNumber(value, target) { | ||
| const conversion = TryConvertNumber(value); | ||
| return conversion === target ? conversion : value; | ||
| } | ||
| function TryConvertLiteralBoolean(value, target) { | ||
| const conversion = TryConvertBoolean(value); | ||
| return conversion === target ? conversion : value; | ||
| } | ||
| function TryConvertLiteral(schema, value) { | ||
| if (typeof schema.const === 'string') { | ||
| return TryConvertLiteralString(value, schema.const); | ||
| } | ||
| else if (typeof schema.const === 'number') { | ||
| return TryConvertLiteralNumber(value, schema.const); | ||
| } | ||
| else if (typeof schema.const === 'boolean') { | ||
| return TryConvertLiteralBoolean(value, schema.const); | ||
| } | ||
| else { | ||
| return clone_1.ValueClone.Clone(value); | ||
| } | ||
| } | ||
| function TryConvertBoolean(value) { | ||
| return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value; | ||
| } | ||
| function TryConvertBigInt(value) { | ||
| return IsStringNumeric(value) ? globalThis.BigInt(parseInt(value)) : IsNumber(value) ? globalThis.BigInt(value | 0) : IsValueFalse(value) ? 0 : IsValueTrue(value) ? 1 : value; | ||
| } | ||
| function TryConvertString(value) { | ||
| return IsValueToString(value) ? value.toString() : IsSymbol(value) && value.description !== undefined ? value.description.toString() : value; | ||
| } | ||
| function TryConvertNumber(value) { | ||
| return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value; | ||
| } | ||
| function TryConvertInteger(value) { | ||
| return IsStringNumeric(value) ? parseInt(value) : IsNumber(value) ? value | 0 : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value; | ||
| } | ||
| function TryConvertNull(value) { | ||
| return IsString(value) && value.toLowerCase() === 'null' ? null : value; | ||
| } | ||
| function TryConvertUndefined(value) { | ||
| return IsString(value) && value === 'undefined' ? undefined : value; | ||
| } | ||
| function TryConvertDate(value) { | ||
| // note: this function may return an invalid dates for the regex tests | ||
| // above. Invalid dates will however be checked during the casting | ||
| // function and will return a epoch date if invalid. Consider better | ||
| // string parsing for the iso dates in future revisions. | ||
| return IsDate(value) | ||
| ? value | ||
| : IsNumber(value) | ||
| ? new globalThis.Date(value) | ||
| : IsValueTrue(value) | ||
| ? new globalThis.Date(1) | ||
| : IsValueFalse(value) | ||
| ? new globalThis.Date(0) | ||
| : IsStringNumeric(value) | ||
| ? new globalThis.Date(parseInt(value)) | ||
| : IsTimeStringWithoutTimeZone(value) | ||
| ? new globalThis.Date(`1970-01-01T${value}.000Z`) | ||
| : IsTimeStringWithTimeZone(value) | ||
| ? new globalThis.Date(`1970-01-01T${value}`) | ||
| : IsDateTimeStringWithoutTimeZone(value) | ||
| ? new globalThis.Date(`${value}.000Z`) | ||
| : IsDateTimeStringWithTimeZone(value) | ||
| ? new globalThis.Date(value) | ||
| : IsDateString(value) | ||
| ? new globalThis.Date(`${value}T00:00:00.000Z`) | ||
| : value; | ||
| } | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Cast | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function Any(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Array(schema, references, value) { | ||
| if (IsArray(value)) { | ||
| return value.map((value) => Visit(schema.items, references, value)); | ||
| } | ||
| return value; | ||
| } | ||
| function BigInt(schema, references, value) { | ||
| return TryConvertBigInt(value); | ||
| } | ||
| function Boolean(schema, references, value) { | ||
| return TryConvertBoolean(value); | ||
| } | ||
| function Constructor(schema, references, value) { | ||
| return clone_1.ValueClone.Clone(value); | ||
| } | ||
| function Date(schema, references, value) { | ||
| return TryConvertDate(value); | ||
| } | ||
| function Function(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Integer(schema, references, value) { | ||
| return TryConvertInteger(value); | ||
| } | ||
| function Intersect(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Literal(schema, references, value) { | ||
| return TryConvertLiteral(schema, value); | ||
| } | ||
| function Never(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Null(schema, references, value) { | ||
| return TryConvertNull(value); | ||
| } | ||
| function Number(schema, references, value) { | ||
| return TryConvertNumber(value); | ||
| } | ||
| function Object(schema, references, value) { | ||
| if (IsObject(value)) | ||
| return globalThis.Object.keys(schema.properties).reduce((acc, key) => { | ||
| return value[key] !== undefined ? { ...acc, [key]: Visit(schema.properties[key], references, value[key]) } : { ...acc }; | ||
| }, value); | ||
| return value; | ||
| } | ||
| function Promise(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Record(schema, references, value) { | ||
| const propertyKey = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0]; | ||
| const property = schema.patternProperties[propertyKey]; | ||
| const result = {}; | ||
| for (const [propKey, propValue] of globalThis.Object.entries(value)) { | ||
| result[propKey] = Visit(property, references, propValue); | ||
| } | ||
| return result; | ||
| } | ||
| function Ref(schema, references, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueConvertDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function String(schema, references, value) { | ||
| return TryConvertString(value); | ||
| } | ||
| function Symbol(schema, references, value) { | ||
| return value; | ||
| } | ||
| function TemplateLiteral(schema, references, value) { | ||
| return value; | ||
| } | ||
| function This(schema, references, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueConvertDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function Tuple(schema, references, value) { | ||
| if (IsArray(value) && schema.items !== undefined) { | ||
| return value.map((value, index) => { | ||
| return index < schema.items.length ? Visit(schema.items[index], references, value) : value; | ||
| }); | ||
| } | ||
| return value; | ||
| } | ||
| function Undefined(schema, references, value) { | ||
| return TryConvertUndefined(value); | ||
| } | ||
| function Union(schema, references, value) { | ||
| for (const subschema of schema.anyOf) { | ||
| const converted = Visit(subschema, references, value); | ||
| if (check_1.ValueCheck.Check(subschema, references, converted)) { | ||
| return converted; | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
| function Uint8Array(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Unknown(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Void(schema, references, value) { | ||
| return value; | ||
| } | ||
| function UserDefined(schema, references, value) { | ||
| return value; | ||
| } | ||
| function Visit(schema, references, value) { | ||
| const references_ = IsString(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| switch (schema[Types.Kind]) { | ||
| case 'Any': | ||
| return Any(schema_, references_, value); | ||
| case 'Array': | ||
| return Array(schema_, references_, value); | ||
| case 'BigInt': | ||
| return BigInt(schema_, references_, value); | ||
| case 'Boolean': | ||
| return Boolean(schema_, references_, value); | ||
| case 'Constructor': | ||
| return Constructor(schema_, references_, value); | ||
| case 'Date': | ||
| return Date(schema_, references_, value); | ||
| case 'Function': | ||
| return Function(schema_, references_, value); | ||
| case 'Integer': | ||
| return Integer(schema_, references_, value); | ||
| case 'Intersect': | ||
| return Intersect(schema_, references_, value); | ||
| case 'Literal': | ||
| return Literal(schema_, references_, value); | ||
| case 'Never': | ||
| return Never(schema_, references_, value); | ||
| case 'Null': | ||
| return Null(schema_, references_, value); | ||
| case 'Number': | ||
| return Number(schema_, references_, value); | ||
| case 'Object': | ||
| return Object(schema_, references_, value); | ||
| case 'Promise': | ||
| return Promise(schema_, references_, value); | ||
| case 'Record': | ||
| return Record(schema_, references_, value); | ||
| case 'Ref': | ||
| return Ref(schema_, references_, value); | ||
| case 'String': | ||
| return String(schema_, references_, value); | ||
| case 'Symbol': | ||
| return Symbol(schema_, references_, value); | ||
| case 'TemplateLiteral': | ||
| return TemplateLiteral(schema_, references_, value); | ||
| case 'This': | ||
| return This(schema_, references_, value); | ||
| case 'Tuple': | ||
| return Tuple(schema_, references_, value); | ||
| case 'Undefined': | ||
| return Undefined(schema_, references_, value); | ||
| case 'Union': | ||
| return Union(schema_, references_, value); | ||
| case 'Uint8Array': | ||
| return Uint8Array(schema_, references_, value); | ||
| case 'Unknown': | ||
| return Unknown(schema_, references_, value); | ||
| case 'Void': | ||
| return Void(schema_, references_, value); | ||
| default: | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new ValueConvertUnknownTypeError(schema_); | ||
| return UserDefined(schema_, references_, value); | ||
| } | ||
| } | ||
| ValueConvert.Visit = Visit; | ||
| function Convert(schema, references, value) { | ||
| return Visit(schema, references, clone_1.ValueClone.Clone(value)); | ||
| } | ||
| ValueConvert.Convert = Convert; | ||
| })(ValueConvert || (exports.ValueConvert = ValueConvert = {})); |
| export declare class ValueHashError extends Error { | ||
| readonly value: unknown; | ||
| constructor(value: unknown); | ||
| } | ||
| export declare namespace ValueHash { | ||
| /** Creates a FNV1A-64 non cryptographic hash of the given value */ | ||
| function Create(value: unknown): bigint; | ||
| } |
+208
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/hash | ||
| The MIT License (MIT) | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueHash = exports.ValueHashError = void 0; | ||
| class ValueHashError extends Error { | ||
| constructor(value) { | ||
| super(`Hash: Unable to hash value`); | ||
| this.value = value; | ||
| } | ||
| } | ||
| exports.ValueHashError = ValueHashError; | ||
| var ValueHash; | ||
| (function (ValueHash) { | ||
| let ByteMarker; | ||
| (function (ByteMarker) { | ||
| ByteMarker[ByteMarker["Undefined"] = 0] = "Undefined"; | ||
| ByteMarker[ByteMarker["Null"] = 1] = "Null"; | ||
| ByteMarker[ByteMarker["Boolean"] = 2] = "Boolean"; | ||
| ByteMarker[ByteMarker["Number"] = 3] = "Number"; | ||
| ByteMarker[ByteMarker["String"] = 4] = "String"; | ||
| ByteMarker[ByteMarker["Object"] = 5] = "Object"; | ||
| ByteMarker[ByteMarker["Array"] = 6] = "Array"; | ||
| ByteMarker[ByteMarker["Date"] = 7] = "Date"; | ||
| ByteMarker[ByteMarker["Uint8Array"] = 8] = "Uint8Array"; | ||
| ByteMarker[ByteMarker["Symbol"] = 9] = "Symbol"; | ||
| ByteMarker[ByteMarker["BigInt"] = 10] = "BigInt"; | ||
| })(ByteMarker || (ByteMarker = {})); | ||
| // ---------------------------------------------------- | ||
| // State | ||
| // ---------------------------------------------------- | ||
| let Hash = globalThis.BigInt('14695981039346656037'); | ||
| const [Prime, Size] = [globalThis.BigInt('1099511628211'), globalThis.BigInt('2') ** globalThis.BigInt('64')]; | ||
| const Bytes = globalThis.Array.from({ length: 256 }).map((_, i) => globalThis.BigInt(i)); | ||
| const F64 = new globalThis.Float64Array(1); | ||
| const F64In = new globalThis.DataView(F64.buffer); | ||
| const F64Out = new globalThis.Uint8Array(F64.buffer); | ||
| // ---------------------------------------------------- | ||
| // Guards | ||
| // ---------------------------------------------------- | ||
| function IsDate(value) { | ||
| return value instanceof globalThis.Date; | ||
| } | ||
| function IsUint8Array(value) { | ||
| return value instanceof globalThis.Uint8Array; | ||
| } | ||
| function IsArray(value) { | ||
| return globalThis.Array.isArray(value); | ||
| } | ||
| function IsBoolean(value) { | ||
| return typeof value === 'boolean'; | ||
| } | ||
| function IsNull(value) { | ||
| return value === null; | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number'; | ||
| } | ||
| function IsSymbol(value) { | ||
| return typeof value === 'symbol'; | ||
| } | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsObject(value) { | ||
| return typeof value === 'object' && value !== null && !IsArray(value) && !IsDate(value) && !IsUint8Array(value); | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsUndefined(value) { | ||
| return value === undefined; | ||
| } | ||
| // ---------------------------------------------------- | ||
| // Encoding | ||
| // ---------------------------------------------------- | ||
| function Array(value) { | ||
| FNV1A64(ByteMarker.Array); | ||
| for (const item of value) { | ||
| Visit(item); | ||
| } | ||
| } | ||
| function Boolean(value) { | ||
| FNV1A64(ByteMarker.Boolean); | ||
| FNV1A64(value ? 1 : 0); | ||
| } | ||
| function BigInt(value) { | ||
| FNV1A64(ByteMarker.BigInt); | ||
| F64In.setBigInt64(0, value); | ||
| for (const byte of F64Out) { | ||
| FNV1A64(byte); | ||
| } | ||
| } | ||
| function Date(value) { | ||
| FNV1A64(ByteMarker.Date); | ||
| Visit(value.getTime()); | ||
| } | ||
| function Null(value) { | ||
| FNV1A64(ByteMarker.Null); | ||
| } | ||
| function Number(value) { | ||
| FNV1A64(ByteMarker.Number); | ||
| F64In.setFloat64(0, value); | ||
| for (const byte of F64Out) { | ||
| FNV1A64(byte); | ||
| } | ||
| } | ||
| function Object(value) { | ||
| FNV1A64(ByteMarker.Object); | ||
| for (const key of globalThis.Object.keys(value).sort()) { | ||
| Visit(key); | ||
| Visit(value[key]); | ||
| } | ||
| } | ||
| function String(value) { | ||
| FNV1A64(ByteMarker.String); | ||
| for (let i = 0; i < value.length; i++) { | ||
| FNV1A64(value.charCodeAt(i)); | ||
| } | ||
| } | ||
| function Symbol(value) { | ||
| FNV1A64(ByteMarker.Symbol); | ||
| Visit(value.description); | ||
| } | ||
| function Uint8Array(value) { | ||
| FNV1A64(ByteMarker.Uint8Array); | ||
| for (let i = 0; i < value.length; i++) { | ||
| FNV1A64(value[i]); | ||
| } | ||
| } | ||
| function Undefined(value) { | ||
| return FNV1A64(ByteMarker.Undefined); | ||
| } | ||
| function Visit(value) { | ||
| if (IsArray(value)) { | ||
| Array(value); | ||
| } | ||
| else if (IsBoolean(value)) { | ||
| Boolean(value); | ||
| } | ||
| else if (IsBigInt(value)) { | ||
| BigInt(value); | ||
| } | ||
| else if (IsDate(value)) { | ||
| Date(value); | ||
| } | ||
| else if (IsNull(value)) { | ||
| Null(value); | ||
| } | ||
| else if (IsNumber(value)) { | ||
| Number(value); | ||
| } | ||
| else if (IsObject(value)) { | ||
| Object(value); | ||
| } | ||
| else if (IsString(value)) { | ||
| String(value); | ||
| } | ||
| else if (IsSymbol(value)) { | ||
| Symbol(value); | ||
| } | ||
| else if (IsUint8Array(value)) { | ||
| Uint8Array(value); | ||
| } | ||
| else if (IsUndefined(value)) { | ||
| Undefined(value); | ||
| } | ||
| else { | ||
| throw new ValueHashError(value); | ||
| } | ||
| } | ||
| function FNV1A64(byte) { | ||
| Hash = Hash ^ Bytes[byte]; | ||
| Hash = (Hash * Prime) % Size; | ||
| } | ||
| /** Creates a FNV1A-64 non cryptographic hash of the given value */ | ||
| function Create(value) { | ||
| Hash = globalThis.BigInt('14695981039346656037'); | ||
| Visit(value); | ||
| return Hash; | ||
| } | ||
| ValueHash.Create = Create; | ||
| })(ValueHash || (exports.ValueHash = ValueHash = {})); |
| export declare class ValueMutateTypeMismatchError extends Error { | ||
| constructor(); | ||
| } | ||
| export declare class ValueMutateInvalidRootMutationError extends Error { | ||
| constructor(); | ||
| } | ||
| export type Mutable = { | ||
| [key: string]: unknown; | ||
| } | unknown[]; | ||
| export declare namespace ValueMutate { | ||
| /** Performs a deep mutable value assignment while retaining internal references. */ | ||
| function Mutate(current: Mutable, next: Mutable): void; | ||
| } |
+121
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/value | ||
| The MIT License (MIT) | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueMutate = exports.ValueMutateInvalidRootMutationError = exports.ValueMutateTypeMismatchError = void 0; | ||
| const is_1 = require("./is"); | ||
| const pointer_1 = require("./pointer"); | ||
| const clone_1 = require("./clone"); | ||
| class ValueMutateTypeMismatchError extends Error { | ||
| constructor() { | ||
| super('ValueMutate: Cannot assign due type mismatch of assignable values'); | ||
| } | ||
| } | ||
| exports.ValueMutateTypeMismatchError = ValueMutateTypeMismatchError; | ||
| class ValueMutateInvalidRootMutationError extends Error { | ||
| constructor() { | ||
| super('ValueMutate: Only object and array types can be mutated at the root level'); | ||
| } | ||
| } | ||
| exports.ValueMutateInvalidRootMutationError = ValueMutateInvalidRootMutationError; | ||
| var ValueMutate; | ||
| (function (ValueMutate) { | ||
| function Object(root, path, current, next) { | ||
| if (!is_1.Is.Object(current)) { | ||
| pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next)); | ||
| } | ||
| else { | ||
| const currentKeys = globalThis.Object.keys(current); | ||
| const nextKeys = globalThis.Object.keys(next); | ||
| for (const currentKey of currentKeys) { | ||
| if (!nextKeys.includes(currentKey)) { | ||
| delete current[currentKey]; | ||
| } | ||
| } | ||
| for (const nextKey of nextKeys) { | ||
| if (!currentKeys.includes(nextKey)) { | ||
| current[nextKey] = null; | ||
| } | ||
| } | ||
| for (const nextKey of nextKeys) { | ||
| Visit(root, `${path}/${nextKey}`, current[nextKey], next[nextKey]); | ||
| } | ||
| } | ||
| } | ||
| function Array(root, path, current, next) { | ||
| if (!is_1.Is.Array(current)) { | ||
| pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next)); | ||
| } | ||
| else { | ||
| for (let index = 0; index < next.length; index++) { | ||
| Visit(root, `${path}/${index}`, current[index], next[index]); | ||
| } | ||
| current.splice(next.length); | ||
| } | ||
| } | ||
| function TypedArray(root, path, current, next) { | ||
| if (is_1.Is.TypedArray(current) && current.length === next.length) { | ||
| for (let i = 0; i < current.length; i++) { | ||
| current[i] = next[i]; | ||
| } | ||
| } | ||
| else { | ||
| pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next)); | ||
| } | ||
| } | ||
| function Value(root, path, current, next) { | ||
| if (current === next) | ||
| return; | ||
| pointer_1.ValuePointer.Set(root, path, next); | ||
| } | ||
| function Visit(root, path, current, next) { | ||
| if (is_1.Is.Array(next)) { | ||
| return Array(root, path, current, next); | ||
| } | ||
| else if (is_1.Is.TypedArray(next)) { | ||
| return TypedArray(root, path, current, next); | ||
| } | ||
| else if (is_1.Is.Object(next)) { | ||
| return Object(root, path, current, next); | ||
| } | ||
| else if (is_1.Is.Value(next)) { | ||
| return Value(root, path, current, next); | ||
| } | ||
| } | ||
| /** Performs a deep mutable value assignment while retaining internal references. */ | ||
| function Mutate(current, next) { | ||
| if (is_1.Is.TypedArray(current) || is_1.Is.Value(current) || is_1.Is.TypedArray(next) || is_1.Is.Value(next)) { | ||
| throw new ValueMutateInvalidRootMutationError(); | ||
| } | ||
| if ((is_1.Is.Object(current) && is_1.Is.Array(next)) || (is_1.Is.Array(current) && is_1.Is.Object(next))) { | ||
| throw new ValueMutateTypeMismatchError(); | ||
| } | ||
| Visit(current, '', current, next); | ||
| } | ||
| ValueMutate.Mutate = Mutate; | ||
| })(ValueMutate || (exports.ValueMutate = ValueMutate = {})); |
@@ -1,4 +0,4 @@ | ||
| import { ValueError } from '../errors/index'; | ||
| import * as Types from '../typebox'; | ||
| export declare type CheckFunction = (value: unknown) => boolean; | ||
| import { ValueErrorIterator } from '../errors/index'; | ||
| export type CheckFunction = (value: unknown) => boolean; | ||
| export declare class TypeCheck<T extends Types.TSchema> { | ||
@@ -10,12 +10,9 @@ private readonly schema; | ||
| constructor(schema: T, references: Types.TSchema[], checkFunc: CheckFunction, code: string); | ||
| /** Returns the generated validation code used to validate this type. */ | ||
| /** Returns the generated assertion code used to validate this type. */ | ||
| Code(): string; | ||
| /** Returns an iterator for each error in this value. */ | ||
| Errors(value: unknown): IterableIterator<ValueError>; | ||
| /** Returns true if the value matches the given type. */ | ||
| Errors(value: unknown): ValueErrorIterator; | ||
| /** Returns true if the value matches the compiled type. */ | ||
| Check(value: unknown): value is Types.Static<T>; | ||
| } | ||
| export declare namespace Property { | ||
| function Check(propertyName: string): boolean; | ||
| } | ||
| export declare class TypeCompilerUnknownTypeError extends Error { | ||
@@ -25,6 +22,16 @@ readonly schema: Types.TSchema; | ||
| } | ||
| export declare class TypeCompilerDereferenceError extends Error { | ||
| readonly schema: Types.TRef; | ||
| constructor(schema: Types.TRef); | ||
| } | ||
| export declare class TypeCompilerTypeGuardError extends Error { | ||
| readonly schema: Types.TSchema; | ||
| constructor(schema: Types.TSchema); | ||
| } | ||
| /** Compiles Types for Runtime Type Checking */ | ||
| export declare namespace TypeCompiler { | ||
| /** Returns the generated assertion code used to validate this type. */ | ||
| function Code<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): string; | ||
| /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */ | ||
| function Compile<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): TypeCheck<T>; | ||
| } |
+404
-224
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -31,7 +31,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.Property = exports.TypeCheck = void 0; | ||
| exports.TypeCompiler = exports.TypeCompilerTypeGuardError = exports.TypeCompilerDereferenceError = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0; | ||
| const Types = require("../typebox"); | ||
| const index_1 = require("../errors/index"); | ||
| const index_2 = require("../guard/index"); | ||
| const index_3 = require("../format/index"); | ||
| const Types = require("../typebox"); | ||
| const index_2 = require("../system/index"); | ||
| const hash_1 = require("../value/hash"); | ||
| // ------------------------------------------------------------------- | ||
@@ -47,3 +47,3 @@ // TypeCheck | ||
| } | ||
| /** Returns the generated validation code used to validate this type. */ | ||
| /** Returns the generated assertion code used to validate this type. */ | ||
| Code() { | ||
@@ -56,3 +56,3 @@ return this.code; | ||
| } | ||
| /** Returns true if the value matches the given type. */ | ||
| /** Returns true if the value matches the compiled type. */ | ||
| Check(value) { | ||
@@ -64,37 +64,72 @@ return this.checkFunc(value); | ||
| // ------------------------------------------------------------------- | ||
| // Property | ||
| // Character | ||
| // ------------------------------------------------------------------- | ||
| var Property; | ||
| (function (Property) { | ||
| var Character; | ||
| (function (Character) { | ||
| function DollarSign(code) { | ||
| return code === 36; | ||
| } | ||
| function Underscore(code) { | ||
| Character.DollarSign = DollarSign; | ||
| function IsUnderscore(code) { | ||
| return code === 95; | ||
| } | ||
| function Numeric(code) { | ||
| Character.IsUnderscore = IsUnderscore; | ||
| function IsAlpha(code) { | ||
| return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); | ||
| } | ||
| Character.IsAlpha = IsAlpha; | ||
| function IsNumeric(code) { | ||
| return code >= 48 && code <= 57; | ||
| } | ||
| function Alpha(code) { | ||
| return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); | ||
| Character.IsNumeric = IsNumeric; | ||
| })(Character || (Character = {})); | ||
| // ------------------------------------------------------------------- | ||
| // MemberExpression | ||
| // ------------------------------------------------------------------- | ||
| var MemberExpression; | ||
| (function (MemberExpression) { | ||
| function IsFirstCharacterNumeric(value) { | ||
| if (value.length === 0) | ||
| return false; | ||
| return Character.IsNumeric(value.charCodeAt(0)); | ||
| } | ||
| function Check(propertyName) { | ||
| if (propertyName.length === 0) | ||
| function IsAccessor(value) { | ||
| if (IsFirstCharacterNumeric(value)) | ||
| return false; | ||
| { | ||
| const code = propertyName.charCodeAt(0); | ||
| if (!(DollarSign(code) || Underscore(code) || Alpha(code))) { | ||
| for (let i = 0; i < value.length; i++) { | ||
| const code = value.charCodeAt(i); | ||
| const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code); | ||
| if (!check) | ||
| return false; | ||
| } | ||
| } | ||
| for (let i = 1; i < propertyName.length; i++) { | ||
| const code = propertyName.charCodeAt(i); | ||
| if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) { | ||
| return false; | ||
| return true; | ||
| } | ||
| function EscapeHyphen(key) { | ||
| return key.replace(/'/g, "\\'"); | ||
| } | ||
| function Encode(object, key) { | ||
| return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`; | ||
| } | ||
| MemberExpression.Encode = Encode; | ||
| })(MemberExpression || (MemberExpression = {})); | ||
| // ------------------------------------------------------------------- | ||
| // Identifier | ||
| // ------------------------------------------------------------------- | ||
| var Identifier; | ||
| (function (Identifier) { | ||
| function Encode($id) { | ||
| const buffer = []; | ||
| for (let i = 0; i < $id.length; i++) { | ||
| const code = $id.charCodeAt(i); | ||
| if (Character.IsNumeric(code) || Character.IsAlpha(code)) { | ||
| buffer.push($id.charAt(i)); | ||
| } | ||
| else { | ||
| buffer.push(`_${code}_`); | ||
| } | ||
| } | ||
| return true; | ||
| return buffer.join('').replace(/__/g, '_'); | ||
| } | ||
| Property.Check = Check; | ||
| })(Property = exports.Property || (exports.Property = {})); | ||
| Identifier.Encode = Encode; | ||
| })(Identifier || (Identifier = {})); | ||
| // ------------------------------------------------------------------- | ||
@@ -110,2 +145,16 @@ // TypeCompiler | ||
| exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError; | ||
| class TypeCompilerDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`TypeCompiler: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.TypeCompilerDereferenceError = TypeCompilerDereferenceError; | ||
| class TypeCompilerTypeGuardError extends Error { | ||
| constructor(schema) { | ||
| super('TypeCompiler: Preflight validation check failed to guard for the given schema'); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.TypeCompilerTypeGuardError = TypeCompilerTypeGuardError; | ||
| /** Compiles Types for Runtime Type Checking */ | ||
@@ -115,277 +164,388 @@ var TypeCompiler; | ||
| // ------------------------------------------------------------------- | ||
| // Guards | ||
| // ------------------------------------------------------------------- | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number' && globalThis.Number.isFinite(value); | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // SchemaGuards | ||
| // ---------------------------------------------------------------------- | ||
| function IsAnyOrUnknown(schema) { | ||
| return schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'; | ||
| } | ||
| // ------------------------------------------------------------------- | ||
| // Polices | ||
| // ------------------------------------------------------------------- | ||
| function IsExactOptionalProperty(value, key, expression) { | ||
| return index_2.TypeSystem.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`; | ||
| } | ||
| function IsObjectCheck(value) { | ||
| return !index_2.TypeSystem.AllowArrayObjects ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`; | ||
| } | ||
| function IsRecordCheck(value) { | ||
| return !index_2.TypeSystem.AllowArrayObjects | ||
| ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))` | ||
| : `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`; | ||
| } | ||
| function IsNumberCheck(value) { | ||
| return !index_2.TypeSystem.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`; | ||
| } | ||
| function IsVoidCheck(value) { | ||
| return index_2.TypeSystem.AllowVoidNull ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`; | ||
| } | ||
| // ------------------------------------------------------------------- | ||
| // Types | ||
| // ------------------------------------------------------------------- | ||
| function* Any(schema, value) { | ||
| yield '(true)'; | ||
| function* Any(schema, references, value) { | ||
| yield 'true'; | ||
| } | ||
| function* Array(schema, value) { | ||
| const expression = CreateExpression(schema.items, 'value'); | ||
| if (schema.minItems !== undefined) | ||
| yield `(${value}.length >= ${schema.minItems})`; | ||
| if (schema.maxItems !== undefined) | ||
| yield `(${value}.length <= ${schema.maxItems})`; | ||
| if (schema.uniqueItems !== undefined) | ||
| yield `(new Set(${value}).size === ${value}.length)`; | ||
| yield `(Array.isArray(${value}) && ${value}.every(value => ${expression}))`; | ||
| function* Array(schema, references, value) { | ||
| yield `Array.isArray(${value})`; | ||
| if (IsNumber(schema.minItems)) | ||
| yield `${value}.length >= ${schema.minItems}`; | ||
| if (IsNumber(schema.maxItems)) | ||
| yield `${value}.length <= ${schema.maxItems}`; | ||
| if (schema.uniqueItems === true) | ||
| yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`; | ||
| const expression = CreateExpression(schema.items, references, 'value'); | ||
| yield `${value}.every(value => ${expression})`; | ||
| } | ||
| function* Boolean(schema, value) { | ||
| yield `(typeof ${value} === 'boolean')`; | ||
| function* BigInt(schema, references, value) { | ||
| yield `(typeof ${value} === 'bigint')`; | ||
| if (IsBigInt(schema.multipleOf)) | ||
| yield `(${value} % BigInt(${schema.multipleOf})) === 0`; | ||
| if (IsBigInt(schema.exclusiveMinimum)) | ||
| yield `${value} > BigInt(${schema.exclusiveMinimum})`; | ||
| if (IsBigInt(schema.exclusiveMaximum)) | ||
| yield `${value} < BigInt(${schema.exclusiveMaximum})`; | ||
| if (IsBigInt(schema.minimum)) | ||
| yield `${value} >= BigInt(${schema.minimum})`; | ||
| if (IsBigInt(schema.maximum)) | ||
| yield `${value} <= BigInt(${schema.maximum})`; | ||
| } | ||
| function* Constructor(schema, value) { | ||
| yield* Visit(schema.returns, `${value}.prototype`); | ||
| function* Boolean(schema, references, value) { | ||
| yield `typeof ${value} === 'boolean'`; | ||
| } | ||
| function* Function(schema, value) { | ||
| yield `(typeof ${value} === 'function')`; | ||
| function* Constructor(schema, references, value) { | ||
| yield* Visit(schema.returns, references, `${value}.prototype`); | ||
| } | ||
| function* Integer(schema, value) { | ||
| function* Date(schema, references, value) { | ||
| yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`; | ||
| if (IsNumber(schema.exclusiveMinimumTimestamp)) | ||
| yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`; | ||
| if (IsNumber(schema.exclusiveMaximumTimestamp)) | ||
| yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`; | ||
| if (IsNumber(schema.minimumTimestamp)) | ||
| yield `${value}.getTime() >= ${schema.minimumTimestamp}`; | ||
| if (IsNumber(schema.maximumTimestamp)) | ||
| yield `${value}.getTime() <= ${schema.maximumTimestamp}`; | ||
| } | ||
| function* Function(schema, references, value) { | ||
| yield `typeof ${value} === 'function'`; | ||
| } | ||
| function* Integer(schema, references, value) { | ||
| yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`; | ||
| if (schema.multipleOf !== undefined) | ||
| yield `(${value} % ${schema.multipleOf} === 0)`; | ||
| if (schema.exclusiveMinimum !== undefined) | ||
| yield `(${value} > ${schema.exclusiveMinimum})`; | ||
| if (schema.exclusiveMaximum !== undefined) | ||
| yield `(${value} < ${schema.exclusiveMaximum})`; | ||
| if (schema.minimum !== undefined) | ||
| yield `(${value} >= ${schema.minimum})`; | ||
| if (schema.maximum !== undefined) | ||
| yield `(${value} <= ${schema.maximum})`; | ||
| if (IsNumber(schema.multipleOf)) | ||
| yield `(${value} % ${schema.multipleOf}) === 0`; | ||
| if (IsNumber(schema.exclusiveMinimum)) | ||
| yield `${value} > ${schema.exclusiveMinimum}`; | ||
| if (IsNumber(schema.exclusiveMaximum)) | ||
| yield `${value} < ${schema.exclusiveMaximum}`; | ||
| if (IsNumber(schema.minimum)) | ||
| yield `${value} >= ${schema.minimum}`; | ||
| if (IsNumber(schema.maximum)) | ||
| yield `${value} <= ${schema.maximum}`; | ||
| } | ||
| function* Literal(schema, value) { | ||
| function* Intersect(schema, references, value) { | ||
| const check1 = schema.allOf.map((schema) => CreateExpression(schema, references, value)).join(' && '); | ||
| if (schema.unevaluatedProperties === false) { | ||
| const keyCheck = PushLocal(`${new RegExp(Types.KeyResolver.ResolvePattern(schema))};`); | ||
| const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`; | ||
| yield `${check1} && ${check2}`; | ||
| } | ||
| else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) { | ||
| const keyCheck = PushLocal(`${new RegExp(Types.KeyResolver.ResolvePattern(schema))};`); | ||
| const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`; | ||
| yield `${check1} && ${check2}`; | ||
| } | ||
| else { | ||
| yield `${check1}`; | ||
| } | ||
| } | ||
| function* Literal(schema, references, value) { | ||
| if (typeof schema.const === 'number' || typeof schema.const === 'boolean') { | ||
| yield `(${value} === ${schema.const})`; | ||
| yield `${value} === ${schema.const}`; | ||
| } | ||
| else { | ||
| yield `(${value} === '${schema.const}')`; | ||
| yield `${value} === '${schema.const}'`; | ||
| } | ||
| } | ||
| function* Never(schema, value) { | ||
| yield `(false)`; | ||
| function* Never(schema, references, value) { | ||
| yield `false`; | ||
| } | ||
| function* Null(schema, value) { | ||
| yield `(${value} === null)`; | ||
| function* Not(schema, references, value) { | ||
| const left = CreateExpression(schema.allOf[0].not, references, value); | ||
| const right = CreateExpression(schema.allOf[1], references, value); | ||
| yield `!${left} && ${right}`; | ||
| } | ||
| function* Number(schema, value) { | ||
| yield `(typeof ${value} === 'number')`; | ||
| if (schema.multipleOf !== undefined) | ||
| yield `(${value} % ${schema.multipleOf} === 0)`; | ||
| if (schema.exclusiveMinimum !== undefined) | ||
| yield `(${value} > ${schema.exclusiveMinimum})`; | ||
| if (schema.exclusiveMaximum !== undefined) | ||
| yield `(${value} < ${schema.exclusiveMaximum})`; | ||
| if (schema.minimum !== undefined) | ||
| yield `(${value} >= ${schema.minimum})`; | ||
| if (schema.maximum !== undefined) | ||
| yield `(${value} <= ${schema.maximum})`; | ||
| function* Null(schema, references, value) { | ||
| yield `${value} === null`; | ||
| } | ||
| function* Object(schema, value) { | ||
| yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`; | ||
| if (schema.minProperties !== undefined) | ||
| yield `(Object.keys(${value}).length >= ${schema.minProperties})`; | ||
| if (schema.maxProperties !== undefined) | ||
| yield `(Object.keys(${value}).length <= ${schema.maxProperties})`; | ||
| const propertyKeys = globalThis.Object.keys(schema.properties); | ||
| if (schema.additionalProperties === false) { | ||
| // Optimization: If the property key length matches the required keys length | ||
| // then we only need check that the values property key length matches that | ||
| // of the property key length. This is because exhaustive testing for values | ||
| // will occur in subsequent property tests. | ||
| if (schema.required && schema.required.length === propertyKeys.length) { | ||
| yield `(Object.keys(${value}).length === ${propertyKeys.length})`; | ||
| function* Number(schema, references, value) { | ||
| yield IsNumberCheck(value); | ||
| if (IsNumber(schema.multipleOf)) | ||
| yield `(${value} % ${schema.multipleOf}) === 0`; | ||
| if (IsNumber(schema.exclusiveMinimum)) | ||
| yield `${value} > ${schema.exclusiveMinimum}`; | ||
| if (IsNumber(schema.exclusiveMaximum)) | ||
| yield `${value} < ${schema.exclusiveMaximum}`; | ||
| if (IsNumber(schema.minimum)) | ||
| yield `${value} >= ${schema.minimum}`; | ||
| if (IsNumber(schema.maximum)) | ||
| yield `${value} <= ${schema.maximum}`; | ||
| } | ||
| function* Object(schema, references, value) { | ||
| yield IsObjectCheck(value); | ||
| if (IsNumber(schema.minProperties)) | ||
| yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`; | ||
| if (IsNumber(schema.maxProperties)) | ||
| yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`; | ||
| const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties); | ||
| for (const knownKey of knownKeys) { | ||
| const memberExpression = MemberExpression.Encode(value, knownKey); | ||
| const property = schema.properties[knownKey]; | ||
| if (schema.required && schema.required.includes(knownKey)) { | ||
| yield* Visit(property, references, memberExpression); | ||
| if (Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property)) | ||
| yield `('${knownKey}' in ${value})`; | ||
| } | ||
| else { | ||
| const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| yield `(Object.keys(${value}).every(key => ${keys}.includes(key)))`; | ||
| const expression = CreateExpression(property, references, memberExpression); | ||
| yield IsExactOptionalProperty(value, knownKey, expression); | ||
| } | ||
| } | ||
| if (index_2.TypeGuard.TSchema(schema.additionalProperties)) { | ||
| const expression = CreateExpression(schema.additionalProperties, 'value[key]'); | ||
| const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| yield `(Object.keys(${value}).every(key => ${keys}.includes(key) || ${expression}))`; | ||
| } | ||
| for (const propertyKey of propertyKeys) { | ||
| const memberExpression = Property.Check(propertyKey) ? `${value}.${propertyKey}` : `${value}['${propertyKey}']`; | ||
| const propertySchema = schema.properties[propertyKey]; | ||
| if (schema.required && schema.required.includes(propertyKey)) { | ||
| yield* Visit(propertySchema, memberExpression); | ||
| if (schema.additionalProperties === false) { | ||
| if (schema.required && schema.required.length === knownKeys.length) { | ||
| yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`; | ||
| } | ||
| else { | ||
| const expression = CreateExpression(propertySchema, memberExpression); | ||
| yield `(${memberExpression} === undefined ? true : (${expression}))`; | ||
| const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`; | ||
| } | ||
| } | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`); | ||
| const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`; | ||
| yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`; | ||
| } | ||
| } | ||
| function* Promise(schema, value) { | ||
| function* Promise(schema, references, value) { | ||
| yield `(typeof value === 'object' && typeof ${value}.then === 'function')`; | ||
| } | ||
| function* Record(schema, value) { | ||
| yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`; | ||
| const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const local = PushLocal(`new RegExp(/${keyPattern}/)`); | ||
| yield `(Object.keys(${value}).every(key => ${local}.test(key)))`; | ||
| const expression = CreateExpression(valueSchema, 'value'); | ||
| yield `(Object.values(${value}).every(value => ${expression}))`; | ||
| function* Record(schema, references, value) { | ||
| yield IsRecordCheck(value); | ||
| if (IsNumber(schema.minProperties)) | ||
| yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`; | ||
| if (IsNumber(schema.maxProperties)) | ||
| yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`; | ||
| const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const local = PushLocal(`new RegExp(/${patternKey}/)`); | ||
| const check1 = CreateExpression(patternSchema, references, 'value'); | ||
| const check2 = Types.TypeGuard.TSchema(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true'; | ||
| const expression = `(${local}.test(key) ? ${check1} : ${check2})`; | ||
| yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`; | ||
| } | ||
| function* Ref(schema, value) { | ||
| // Reference: If we have seen this reference before we can just yield and return | ||
| // the function call. If this isn't the case we defer to visit to generate and | ||
| // set the function for subsequent passes. Consider for refactor. | ||
| if (names.has(schema.$ref)) | ||
| return yield `(${CreateFunctionName(schema.$ref)}(${value}))`; | ||
| if (!referenceMap.has(schema.$ref)) | ||
| throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`); | ||
| const reference = referenceMap.get(schema.$ref); | ||
| yield* Visit(reference, value); | ||
| function* Ref(schema, references, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new TypeCompilerDereferenceError(schema); | ||
| const target = references[index]; | ||
| // Reference: If we have seen this reference before we can just yield and | ||
| // return the function call. If this isn't the case we defer to visit to | ||
| // generate and set the function for subsequent passes. | ||
| if (state_local_function_names.has(schema.$ref)) | ||
| return yield `${CreateFunctionName(schema.$ref)}(${value})`; | ||
| yield* Visit(target, references, value); | ||
| } | ||
| function* Self(schema, value) { | ||
| const func = CreateFunctionName(schema.$ref); | ||
| yield `(${func}(${value}))`; | ||
| } | ||
| function* String(schema, value) { | ||
| function* String(schema, references, value) { | ||
| yield `(typeof ${value} === 'string')`; | ||
| if (schema.minLength !== undefined) { | ||
| yield `(${value}.length >= ${schema.minLength})`; | ||
| } | ||
| if (schema.maxLength !== undefined) { | ||
| yield `(${value}.length <= ${schema.maxLength})`; | ||
| } | ||
| if (IsNumber(schema.minLength)) | ||
| yield `${value}.length >= ${schema.minLength}`; | ||
| if (IsNumber(schema.maxLength)) | ||
| yield `${value}.length <= ${schema.maxLength}`; | ||
| if (schema.pattern !== undefined) { | ||
| const local = PushLocal(`new RegExp(/${schema.pattern}/);`); | ||
| yield `(${local}.test(${value}))`; | ||
| const local = PushLocal(`${new RegExp(schema.pattern)};`); | ||
| yield `${local}.test(${value})`; | ||
| } | ||
| if (schema.format !== undefined) { | ||
| yield `(format('${schema.format}', ${value}))`; | ||
| yield `format('${schema.format}', ${value})`; | ||
| } | ||
| } | ||
| function* Tuple(schema, value) { | ||
| yield `(Array.isArray(${value}))`; | ||
| function* Symbol(schema, references, value) { | ||
| yield `(typeof ${value} === 'symbol')`; | ||
| } | ||
| function* TemplateLiteral(schema, references, value) { | ||
| yield `(typeof ${value} === 'string')`; | ||
| const local = PushLocal(`${new RegExp(schema.pattern)};`); | ||
| yield `${local}.test(${value})`; | ||
| } | ||
| function* This(schema, references, value) { | ||
| const func = CreateFunctionName(schema.$ref); | ||
| yield `${func}(${value})`; | ||
| } | ||
| function* Tuple(schema, references, value) { | ||
| yield `Array.isArray(${value})`; | ||
| if (schema.items === undefined) | ||
| return yield `(${value}.length === 0)`; | ||
| return yield `${value}.length === 0`; | ||
| yield `(${value}.length === ${schema.maxItems})`; | ||
| for (let i = 0; i < schema.items.length; i++) { | ||
| const expression = CreateExpression(schema.items[i], `${value}[${i}]`); | ||
| yield `(${expression})`; | ||
| const expression = CreateExpression(schema.items[i], references, `${value}[${i}]`); | ||
| yield `${expression}`; | ||
| } | ||
| } | ||
| function* Undefined(schema, value) { | ||
| yield `(${value} === undefined)`; | ||
| function* Undefined(schema, references, value) { | ||
| yield `${value} === undefined`; | ||
| } | ||
| function* Union(schema, value) { | ||
| const expressions = schema.anyOf.map((schema) => CreateExpression(schema, value)); | ||
| function* Union(schema, references, value) { | ||
| const expressions = schema.anyOf.map((schema) => CreateExpression(schema, references, value)); | ||
| yield `(${expressions.join(' || ')})`; | ||
| } | ||
| function* Uint8Array(schema, value) { | ||
| yield `(${value} instanceof Uint8Array)`; | ||
| if (schema.maxByteLength) | ||
| function* Uint8Array(schema, references, value) { | ||
| yield `${value} instanceof Uint8Array`; | ||
| if (IsNumber(schema.maxByteLength)) | ||
| yield `(${value}.length <= ${schema.maxByteLength})`; | ||
| if (schema.minByteLength) | ||
| if (IsNumber(schema.minByteLength)) | ||
| yield `(${value}.length >= ${schema.minByteLength})`; | ||
| } | ||
| function* Unknown(schema, value) { | ||
| yield '(true)'; | ||
| function* Unknown(schema, references, value) { | ||
| yield 'true'; | ||
| } | ||
| function* Void(schema, value) { | ||
| yield `(${value} === null)`; | ||
| function* Void(schema, references, value) { | ||
| yield IsVoidCheck(value); | ||
| } | ||
| function* Visit(schema, value) { | ||
| // Reference: Referenced schemas can originate from either additional schemas | ||
| // or inline in the schema itself. Ideally the recursive path should align to | ||
| // reference path. Consider for refactor. | ||
| if (schema.$id && !names.has(schema.$id)) { | ||
| names.add(schema.$id); | ||
| function* UserDefined(schema, references, value) { | ||
| const schema_key = `schema_key_${state_remote_custom_types.size}`; | ||
| state_remote_custom_types.set(schema_key, schema); | ||
| yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`; | ||
| } | ||
| function* Visit(schema, references, value, root = false) { | ||
| const references_ = IsString(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| // Rule: Types with identifiers are hoisted into their own functions. | ||
| // The following will generate a function for the schema and yield the | ||
| // call to that function. This call is only made if NOT the root type | ||
| // which allows the generated function to yield its expression. The | ||
| // root argument is only true when making calls via CreateFunction(). | ||
| // Note there is potential to omit the root argument and conditional | ||
| // by refactoring the logic below. Consider for review. | ||
| if (IsString(schema.$id)) { | ||
| const name = CreateFunctionName(schema.$id); | ||
| const body = CreateFunction(name, schema, 'value'); | ||
| PushFunction(body); | ||
| yield `(${name}(${value}))`; | ||
| return; | ||
| if (!state_local_function_names.has(schema.$id)) { | ||
| state_local_function_names.add(schema.$id); | ||
| const body = CreateFunction(name, schema, references, 'value'); | ||
| PushFunction(body); | ||
| } | ||
| if (!root) | ||
| return yield `${name}(${value})`; | ||
| } | ||
| const anySchema = schema; | ||
| switch (anySchema[Types.Kind]) { | ||
| switch (schema_[Types.Kind]) { | ||
| case 'Any': | ||
| return yield* Any(anySchema, value); | ||
| return yield* Any(schema_, references_, value); | ||
| case 'Array': | ||
| return yield* Array(anySchema, value); | ||
| return yield* Array(schema_, references_, value); | ||
| case 'BigInt': | ||
| return yield* BigInt(schema_, references_, value); | ||
| case 'Boolean': | ||
| return yield* Boolean(anySchema, value); | ||
| return yield* Boolean(schema_, references_, value); | ||
| case 'Constructor': | ||
| return yield* Constructor(anySchema, value); | ||
| return yield* Constructor(schema_, references_, value); | ||
| case 'Date': | ||
| return yield* Date(schema_, references_, value); | ||
| case 'Function': | ||
| return yield* Function(anySchema, value); | ||
| return yield* Function(schema_, references_, value); | ||
| case 'Integer': | ||
| return yield* Integer(anySchema, value); | ||
| return yield* Integer(schema_, references_, value); | ||
| case 'Intersect': | ||
| return yield* Intersect(schema_, references_, value); | ||
| case 'Literal': | ||
| return yield* Literal(anySchema, value); | ||
| return yield* Literal(schema_, references_, value); | ||
| case 'Never': | ||
| return yield* Never(anySchema, value); | ||
| return yield* Never(schema_, references_, value); | ||
| case 'Not': | ||
| return yield* Not(schema_, references_, value); | ||
| case 'Null': | ||
| return yield* Null(anySchema, value); | ||
| return yield* Null(schema_, references_, value); | ||
| case 'Number': | ||
| return yield* Number(anySchema, value); | ||
| return yield* Number(schema_, references_, value); | ||
| case 'Object': | ||
| return yield* Object(anySchema, value); | ||
| return yield* Object(schema_, references_, value); | ||
| case 'Promise': | ||
| return yield* Promise(anySchema, value); | ||
| return yield* Promise(schema_, references_, value); | ||
| case 'Record': | ||
| return yield* Record(anySchema, value); | ||
| return yield* Record(schema_, references_, value); | ||
| case 'Ref': | ||
| return yield* Ref(anySchema, value); | ||
| case 'Self': | ||
| return yield* Self(anySchema, value); | ||
| return yield* Ref(schema_, references_, value); | ||
| case 'String': | ||
| return yield* String(anySchema, value); | ||
| return yield* String(schema_, references_, value); | ||
| case 'Symbol': | ||
| return yield* Symbol(schema_, references_, value); | ||
| case 'TemplateLiteral': | ||
| return yield* TemplateLiteral(schema_, references_, value); | ||
| case 'This': | ||
| return yield* This(schema_, references_, value); | ||
| case 'Tuple': | ||
| return yield* Tuple(anySchema, value); | ||
| return yield* Tuple(schema_, references_, value); | ||
| case 'Undefined': | ||
| return yield* Undefined(anySchema, value); | ||
| return yield* Undefined(schema_, references_, value); | ||
| case 'Union': | ||
| return yield* Union(anySchema, value); | ||
| return yield* Union(schema_, references_, value); | ||
| case 'Uint8Array': | ||
| return yield* Uint8Array(anySchema, value); | ||
| return yield* Uint8Array(schema_, references_, value); | ||
| case 'Unknown': | ||
| return yield* Unknown(anySchema, value); | ||
| return yield* Unknown(schema_, references_, value); | ||
| case 'Void': | ||
| return yield* Void(anySchema, value); | ||
| return yield* Void(schema_, references_, value); | ||
| default: | ||
| throw new TypeCompilerUnknownTypeError(schema); | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new TypeCompilerUnknownTypeError(schema); | ||
| return yield* UserDefined(schema_, references_, value); | ||
| } | ||
| } | ||
| // ------------------------------------------------------------------- | ||
| // Compile State | ||
| // Compiler State | ||
| // ------------------------------------------------------------------- | ||
| const referenceMap = new Map(); | ||
| const locals = new Set(); // local variables and functions | ||
| const names = new Set(); // cache of local functions | ||
| const state_local_variables = new Set(); // local variables and functions | ||
| const state_local_function_names = new Set(); // local function names used call ref validators | ||
| const state_remote_custom_types = new Map(); // remote custom types used during compilation | ||
| function ResetCompiler() { | ||
| referenceMap.clear(); | ||
| locals.clear(); | ||
| names.clear(); | ||
| state_local_variables.clear(); | ||
| state_local_function_names.clear(); | ||
| state_remote_custom_types.clear(); | ||
| } | ||
| function AddReferences(schemas = []) { | ||
| for (const schema of schemas) { | ||
| if (!schema.$id) | ||
| throw new Error(`TypeCompiler: Referenced schemas must specify an $id.`); | ||
| if (referenceMap.has(schema.$id)) | ||
| throw new Error(`TypeCompiler: Duplicate schema $id found for '${schema.$id}'`); | ||
| referenceMap.set(schema.$id, schema); | ||
| } | ||
| function CreateExpression(schema, references, value) { | ||
| return `(${[...Visit(schema, references, value)].join(' && ')})`; | ||
| } | ||
| function CreateExpression(schema, value) { | ||
| return [...Visit(schema, value)].join(' && '); | ||
| } | ||
| function CreateFunctionName($id) { | ||
| return `check_${$id.replace(/-/g, '_')}`; | ||
| return `check_${Identifier.Encode($id)}`; | ||
| } | ||
| function CreateFunction(name, schema, value) { | ||
| const expression = [...Visit(schema, value)].map((condition) => ` ${condition}`).join(' &&\n'); | ||
| function CreateFunction(name, schema, references, value) { | ||
| const expression = [...Visit(schema, references, value, true)].map((condition) => ` ${condition}`).join(' &&\n'); | ||
| return `function ${name}(value) {\n return (\n${expression}\n )\n}`; | ||
| } | ||
| function PushFunction(functionBody) { | ||
| locals.add(functionBody); | ||
| state_local_variables.add(functionBody); | ||
| } | ||
| function PushLocal(expression) { | ||
| const local = `local_${locals.size}`; | ||
| locals.add(`const ${local} = ${expression}`); | ||
| const local = `local_${state_local_variables.size}`; | ||
| state_local_variables.add(`const ${local} = ${expression}`); | ||
| return local; | ||
| } | ||
| function GetLocals() { | ||
| return [...locals.values()]; | ||
| return [...state_local_variables.values()]; | ||
| } | ||
@@ -395,23 +555,43 @@ // ------------------------------------------------------------------- | ||
| // ------------------------------------------------------------------- | ||
| function Build(schema, references = []) { | ||
| function Build(schema, references) { | ||
| ResetCompiler(); | ||
| AddReferences(references); | ||
| const check = CreateFunction('check', schema, 'value'); | ||
| const check = CreateFunction('check', schema, references, 'value'); // interior visit | ||
| const locals = GetLocals(); | ||
| return `${locals.join('\n')}\nreturn ${check}`; | ||
| // prettier-ignore | ||
| return IsString(schema.$id) // ensure top level schemas with $id's are hoisted | ||
| ? `${locals.join('\n')}\nreturn function check(value) {\n return ${CreateFunctionName(schema.$id)}(value)\n}` | ||
| : `${locals.join('\n')}\nreturn ${check}`; | ||
| } | ||
| /** Returns the generated assertion code used to validate this type. */ | ||
| function Code(schema, references = []) { | ||
| if (!Types.TypeGuard.TSchema(schema)) | ||
| throw new TypeCompilerTypeGuardError(schema); | ||
| for (const schema of references) | ||
| if (!Types.TypeGuard.TSchema(schema)) | ||
| throw new TypeCompilerTypeGuardError(schema); | ||
| return Build(schema, references); | ||
| } | ||
| TypeCompiler.Code = Code; | ||
| /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */ | ||
| function Compile(schema, references = []) { | ||
| index_2.TypeGuard.Assert(schema, references); | ||
| const code = Build(schema, references); | ||
| const func1 = globalThis.Function('format', code); | ||
| const func2 = func1((format, value) => { | ||
| if (!index_3.Format.Has(format)) | ||
| const code = Code(schema, references); | ||
| const custom_schemas = new Map(state_remote_custom_types); | ||
| const compiledFunction = globalThis.Function('custom', 'format', 'hash', code); | ||
| const checkFunction = compiledFunction((kind, schema_key, value) => { | ||
| if (!Types.TypeRegistry.Has(kind) || !custom_schemas.has(schema_key)) | ||
| return false; | ||
| const func = index_3.Format.Get(format); | ||
| const schema = custom_schemas.get(schema_key); | ||
| const func = Types.TypeRegistry.Get(kind); | ||
| return func(schema, value); | ||
| }, (format, value) => { | ||
| if (!Types.FormatRegistry.Has(format)) | ||
| return false; | ||
| const func = Types.FormatRegistry.Get(format); | ||
| return func(value); | ||
| }, (value) => { | ||
| return hash_1.ValueHash.Create(value); | ||
| }); | ||
| return new TypeCheck(schema, references, func2, code); | ||
| return new TypeCheck(schema, references, checkFunction, code); | ||
| } | ||
| TypeCompiler.Compile = Compile; | ||
| })(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {})); | ||
| })(TypeCompiler || (exports.TypeCompiler = TypeCompiler = {})); |
| export { ValueError, ValueErrorType } from '../errors/index'; | ||
| export * from './compiler'; |
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -11,0 +11,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
+68
-40
@@ -7,41 +7,57 @@ import * as Types from '../typebox'; | ||
| ArrayUniqueItems = 3, | ||
| Boolean = 4, | ||
| Function = 5, | ||
| Integer = 6, | ||
| IntegerMultipleOf = 7, | ||
| IntegerExclusiveMinimum = 8, | ||
| IntegerExclusiveMaximum = 9, | ||
| IntegerMinimum = 10, | ||
| IntegerMaximum = 11, | ||
| Literal = 12, | ||
| Never = 13, | ||
| Null = 14, | ||
| Number = 15, | ||
| NumberMultipleOf = 16, | ||
| NumberExclusiveMinimum = 17, | ||
| NumberExclusiveMaximum = 18, | ||
| NumberMinumum = 19, | ||
| NumberMaximum = 20, | ||
| Object = 21, | ||
| ObjectMinProperties = 22, | ||
| ObjectMaxProperties = 23, | ||
| ObjectAdditionalProperties = 24, | ||
| ObjectRequiredProperties = 25, | ||
| Promise = 26, | ||
| RecordKeyNumeric = 27, | ||
| RecordKeyString = 28, | ||
| String = 29, | ||
| StringMinLength = 30, | ||
| StringMaxLength = 31, | ||
| StringPattern = 32, | ||
| StringFormatUnknown = 33, | ||
| StringFormat = 34, | ||
| TupleZeroLength = 35, | ||
| TupleLength = 36, | ||
| Undefined = 37, | ||
| Union = 38, | ||
| Uint8Array = 39, | ||
| Uint8ArrayMinByteLength = 40, | ||
| Uint8ArrayMaxByteLength = 41, | ||
| Void = 42 | ||
| BigInt = 4, | ||
| BigIntMultipleOf = 5, | ||
| BigIntExclusiveMinimum = 6, | ||
| BigIntExclusiveMaximum = 7, | ||
| BigIntMinimum = 8, | ||
| BigIntMaximum = 9, | ||
| Boolean = 10, | ||
| Date = 11, | ||
| DateExclusiveMinimumTimestamp = 12, | ||
| DateExclusiveMaximumTimestamp = 13, | ||
| DateMinimumTimestamp = 14, | ||
| DateMaximumTimestamp = 15, | ||
| Function = 16, | ||
| Integer = 17, | ||
| IntegerMultipleOf = 18, | ||
| IntegerExclusiveMinimum = 19, | ||
| IntegerExclusiveMaximum = 20, | ||
| IntegerMinimum = 21, | ||
| IntegerMaximum = 22, | ||
| Intersect = 23, | ||
| IntersectUnevaluatedProperties = 24, | ||
| Literal = 25, | ||
| Never = 26, | ||
| Not = 27, | ||
| Null = 28, | ||
| Number = 29, | ||
| NumberMultipleOf = 30, | ||
| NumberExclusiveMinimum = 31, | ||
| NumberExclusiveMaximum = 32, | ||
| NumberMinumum = 33, | ||
| NumberMaximum = 34, | ||
| Object = 35, | ||
| ObjectMinProperties = 36, | ||
| ObjectMaxProperties = 37, | ||
| ObjectAdditionalProperties = 38, | ||
| ObjectRequiredProperties = 39, | ||
| Promise = 40, | ||
| RecordKeyNumeric = 41, | ||
| RecordKeyString = 42, | ||
| String = 43, | ||
| StringMinLength = 44, | ||
| StringMaxLength = 45, | ||
| StringPattern = 46, | ||
| StringFormatUnknown = 47, | ||
| StringFormat = 48, | ||
| Symbol = 49, | ||
| TupleZeroLength = 50, | ||
| TupleLength = 51, | ||
| Undefined = 52, | ||
| Union = 53, | ||
| Uint8Array = 54, | ||
| Uint8ArrayMinByteLength = 55, | ||
| Uint8ArrayMaxByteLength = 56, | ||
| Void = 57, | ||
| Custom = 58 | ||
| } | ||
@@ -55,2 +71,9 @@ export interface ValueError { | ||
| } | ||
| export declare class ValueErrorIterator { | ||
| private readonly iterator; | ||
| constructor(iterator: IterableIterator<ValueError>); | ||
| [Symbol.iterator](): IterableIterator<ValueError>; | ||
| /** Returns the first value error or undefined if no errors */ | ||
| First(): ValueError | undefined; | ||
| } | ||
| export declare class ValueErrorsUnknownTypeError extends Error { | ||
@@ -60,4 +83,9 @@ readonly schema: Types.TSchema; | ||
| } | ||
| export declare class ValueErrorsDereferenceError extends Error { | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
| /** Provides functionality to generate a sequence of errors against a TypeBox type. */ | ||
| export declare namespace ValueErrors { | ||
| function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>; | ||
| function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): ValueErrorIterator; | ||
| } |
+368
-154
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueErrors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0; | ||
| /*-------------------------------------------------------------------------- | ||
@@ -8,3 +10,3 @@ | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -30,6 +32,5 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0; | ||
| const Types = require("../typebox"); | ||
| const index_1 = require("../format/index"); | ||
| const index_1 = require("../system/index"); | ||
| const hash_1 = require("../value/hash"); | ||
| // ------------------------------------------------------------------- | ||
@@ -44,43 +45,76 @@ // ValueErrorType | ||
| ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems"; | ||
| ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean"; | ||
| ValueErrorType[ValueErrorType["Function"] = 5] = "Function"; | ||
| ValueErrorType[ValueErrorType["Integer"] = 6] = "Integer"; | ||
| ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 7] = "IntegerMultipleOf"; | ||
| ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 8] = "IntegerExclusiveMinimum"; | ||
| ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 9] = "IntegerExclusiveMaximum"; | ||
| ValueErrorType[ValueErrorType["IntegerMinimum"] = 10] = "IntegerMinimum"; | ||
| ValueErrorType[ValueErrorType["IntegerMaximum"] = 11] = "IntegerMaximum"; | ||
| ValueErrorType[ValueErrorType["Literal"] = 12] = "Literal"; | ||
| ValueErrorType[ValueErrorType["Never"] = 13] = "Never"; | ||
| ValueErrorType[ValueErrorType["Null"] = 14] = "Null"; | ||
| ValueErrorType[ValueErrorType["Number"] = 15] = "Number"; | ||
| ValueErrorType[ValueErrorType["NumberMultipleOf"] = 16] = "NumberMultipleOf"; | ||
| ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 17] = "NumberExclusiveMinimum"; | ||
| ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 18] = "NumberExclusiveMaximum"; | ||
| ValueErrorType[ValueErrorType["NumberMinumum"] = 19] = "NumberMinumum"; | ||
| ValueErrorType[ValueErrorType["NumberMaximum"] = 20] = "NumberMaximum"; | ||
| ValueErrorType[ValueErrorType["Object"] = 21] = "Object"; | ||
| ValueErrorType[ValueErrorType["ObjectMinProperties"] = 22] = "ObjectMinProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 23] = "ObjectMaxProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 24] = "ObjectAdditionalProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 25] = "ObjectRequiredProperties"; | ||
| ValueErrorType[ValueErrorType["Promise"] = 26] = "Promise"; | ||
| ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 27] = "RecordKeyNumeric"; | ||
| ValueErrorType[ValueErrorType["RecordKeyString"] = 28] = "RecordKeyString"; | ||
| ValueErrorType[ValueErrorType["String"] = 29] = "String"; | ||
| ValueErrorType[ValueErrorType["StringMinLength"] = 30] = "StringMinLength"; | ||
| ValueErrorType[ValueErrorType["StringMaxLength"] = 31] = "StringMaxLength"; | ||
| ValueErrorType[ValueErrorType["StringPattern"] = 32] = "StringPattern"; | ||
| ValueErrorType[ValueErrorType["StringFormatUnknown"] = 33] = "StringFormatUnknown"; | ||
| ValueErrorType[ValueErrorType["StringFormat"] = 34] = "StringFormat"; | ||
| ValueErrorType[ValueErrorType["TupleZeroLength"] = 35] = "TupleZeroLength"; | ||
| ValueErrorType[ValueErrorType["TupleLength"] = 36] = "TupleLength"; | ||
| ValueErrorType[ValueErrorType["Undefined"] = 37] = "Undefined"; | ||
| ValueErrorType[ValueErrorType["Union"] = 38] = "Union"; | ||
| ValueErrorType[ValueErrorType["Uint8Array"] = 39] = "Uint8Array"; | ||
| ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 40] = "Uint8ArrayMinByteLength"; | ||
| ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 41] = "Uint8ArrayMaxByteLength"; | ||
| ValueErrorType[ValueErrorType["Void"] = 42] = "Void"; | ||
| })(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {})); | ||
| ValueErrorType[ValueErrorType["BigInt"] = 4] = "BigInt"; | ||
| ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 5] = "BigIntMultipleOf"; | ||
| ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 6] = "BigIntExclusiveMinimum"; | ||
| ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 7] = "BigIntExclusiveMaximum"; | ||
| ValueErrorType[ValueErrorType["BigIntMinimum"] = 8] = "BigIntMinimum"; | ||
| ValueErrorType[ValueErrorType["BigIntMaximum"] = 9] = "BigIntMaximum"; | ||
| ValueErrorType[ValueErrorType["Boolean"] = 10] = "Boolean"; | ||
| ValueErrorType[ValueErrorType["Date"] = 11] = "Date"; | ||
| ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 12] = "DateExclusiveMinimumTimestamp"; | ||
| ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 13] = "DateExclusiveMaximumTimestamp"; | ||
| ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 14] = "DateMinimumTimestamp"; | ||
| ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 15] = "DateMaximumTimestamp"; | ||
| ValueErrorType[ValueErrorType["Function"] = 16] = "Function"; | ||
| ValueErrorType[ValueErrorType["Integer"] = 17] = "Integer"; | ||
| ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 18] = "IntegerMultipleOf"; | ||
| ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 19] = "IntegerExclusiveMinimum"; | ||
| ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 20] = "IntegerExclusiveMaximum"; | ||
| ValueErrorType[ValueErrorType["IntegerMinimum"] = 21] = "IntegerMinimum"; | ||
| ValueErrorType[ValueErrorType["IntegerMaximum"] = 22] = "IntegerMaximum"; | ||
| ValueErrorType[ValueErrorType["Intersect"] = 23] = "Intersect"; | ||
| ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 24] = "IntersectUnevaluatedProperties"; | ||
| ValueErrorType[ValueErrorType["Literal"] = 25] = "Literal"; | ||
| ValueErrorType[ValueErrorType["Never"] = 26] = "Never"; | ||
| ValueErrorType[ValueErrorType["Not"] = 27] = "Not"; | ||
| ValueErrorType[ValueErrorType["Null"] = 28] = "Null"; | ||
| ValueErrorType[ValueErrorType["Number"] = 29] = "Number"; | ||
| ValueErrorType[ValueErrorType["NumberMultipleOf"] = 30] = "NumberMultipleOf"; | ||
| ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 31] = "NumberExclusiveMinimum"; | ||
| ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 32] = "NumberExclusiveMaximum"; | ||
| ValueErrorType[ValueErrorType["NumberMinumum"] = 33] = "NumberMinumum"; | ||
| ValueErrorType[ValueErrorType["NumberMaximum"] = 34] = "NumberMaximum"; | ||
| ValueErrorType[ValueErrorType["Object"] = 35] = "Object"; | ||
| ValueErrorType[ValueErrorType["ObjectMinProperties"] = 36] = "ObjectMinProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 37] = "ObjectMaxProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 38] = "ObjectAdditionalProperties"; | ||
| ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 39] = "ObjectRequiredProperties"; | ||
| ValueErrorType[ValueErrorType["Promise"] = 40] = "Promise"; | ||
| ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 41] = "RecordKeyNumeric"; | ||
| ValueErrorType[ValueErrorType["RecordKeyString"] = 42] = "RecordKeyString"; | ||
| ValueErrorType[ValueErrorType["String"] = 43] = "String"; | ||
| ValueErrorType[ValueErrorType["StringMinLength"] = 44] = "StringMinLength"; | ||
| ValueErrorType[ValueErrorType["StringMaxLength"] = 45] = "StringMaxLength"; | ||
| ValueErrorType[ValueErrorType["StringPattern"] = 46] = "StringPattern"; | ||
| ValueErrorType[ValueErrorType["StringFormatUnknown"] = 47] = "StringFormatUnknown"; | ||
| ValueErrorType[ValueErrorType["StringFormat"] = 48] = "StringFormat"; | ||
| ValueErrorType[ValueErrorType["Symbol"] = 49] = "Symbol"; | ||
| ValueErrorType[ValueErrorType["TupleZeroLength"] = 50] = "TupleZeroLength"; | ||
| ValueErrorType[ValueErrorType["TupleLength"] = 51] = "TupleLength"; | ||
| ValueErrorType[ValueErrorType["Undefined"] = 52] = "Undefined"; | ||
| ValueErrorType[ValueErrorType["Union"] = 53] = "Union"; | ||
| ValueErrorType[ValueErrorType["Uint8Array"] = 54] = "Uint8Array"; | ||
| ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 55] = "Uint8ArrayMinByteLength"; | ||
| ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 56] = "Uint8ArrayMaxByteLength"; | ||
| ValueErrorType[ValueErrorType["Void"] = 57] = "Void"; | ||
| ValueErrorType[ValueErrorType["Custom"] = 58] = "Custom"; | ||
| })(ValueErrorType || (exports.ValueErrorType = ValueErrorType = {})); | ||
| // ------------------------------------------------------------------- | ||
| // ValueErrorIterator | ||
| // ------------------------------------------------------------------- | ||
| class ValueErrorIterator { | ||
| constructor(iterator) { | ||
| this.iterator = iterator; | ||
| } | ||
| [Symbol.iterator]() { | ||
| return this.iterator; | ||
| } | ||
| /** Returns the first value error or undefined if no errors */ | ||
| First() { | ||
| const next = this.iterator.next(); | ||
| return next.done ? undefined : next.value; | ||
| } | ||
| } | ||
| exports.ValueErrorIterator = ValueErrorIterator; | ||
| // ------------------------------------------------------------------- | ||
| // ValueErrors | ||
@@ -95,4 +129,51 @@ // ------------------------------------------------------------------- | ||
| exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError; | ||
| class ValueErrorsDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`ValueErrors: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueErrorsDereferenceError = ValueErrorsDereferenceError; | ||
| /** Provides functionality to generate a sequence of errors against a TypeBox type. */ | ||
| var ValueErrors; | ||
| (function (ValueErrors) { | ||
| // ---------------------------------------------------------------------- | ||
| // Guards | ||
| // ---------------------------------------------------------------------- | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsInteger(value) { | ||
| return globalThis.Number.isInteger(value); | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsDefined(value) { | ||
| return value !== undefined; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // Policies | ||
| // ---------------------------------------------------------------------- | ||
| function IsExactOptionalProperty(value, key) { | ||
| return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined; | ||
| } | ||
| function IsObject(value) { | ||
| const result = typeof value === 'object' && value !== null; | ||
| return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value); | ||
| } | ||
| function IsRecordObject(value) { | ||
| return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array); | ||
| } | ||
| function IsNumber(value) { | ||
| const result = typeof value === 'number'; | ||
| return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value); | ||
| } | ||
| function IsVoid(value) { | ||
| const result = value === undefined; | ||
| return index_1.TypeSystem.AllowVoidNull ? result || value === null : result; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // Types | ||
| // ---------------------------------------------------------------------- | ||
| function* Any(schema, references, path, value) { } | ||
@@ -103,9 +184,18 @@ function* Array(schema, references, path, value) { | ||
| } | ||
| if (schema.minItems !== undefined && !(value.length >= schema.minItems)) { | ||
| if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) { | ||
| yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` }; | ||
| } | ||
| if (schema.maxItems !== undefined && !(value.length <= schema.maxItems)) { | ||
| if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) { | ||
| yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` }; | ||
| } | ||
| if (schema.uniqueItems === true && !(new Set(value).size === value.length)) { | ||
| // prettier-ignore | ||
| if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) { | ||
| const hashed = hash_1.ValueHash.Create(element); | ||
| if (set.has(hashed)) { | ||
| return false; | ||
| } | ||
| else { | ||
| set.add(hashed); | ||
| } | ||
| } return true; })())) { | ||
| yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` }; | ||
@@ -117,2 +207,22 @@ } | ||
| } | ||
| function* BigInt(schema, references, path, value) { | ||
| if (!IsBigInt(value)) { | ||
| return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` }; | ||
| } | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) { | ||
| yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` }; | ||
| } | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` }; | ||
| } | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` }; | ||
| } | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` }; | ||
| } | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` }; | ||
| } | ||
| } | ||
| function* Boolean(schema, references, path, value) { | ||
@@ -126,2 +236,22 @@ if (!(typeof value === 'boolean')) { | ||
| } | ||
| function* Date(schema, references, path, value) { | ||
| if (!(value instanceof globalThis.Date)) { | ||
| return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` }; | ||
| } | ||
| if (!globalThis.isFinite(value.getTime())) { | ||
| return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` }; | ||
| } | ||
| if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) { | ||
| yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` }; | ||
| } | ||
| if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) { | ||
| yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` }; | ||
| } | ||
| if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) { | ||
| yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` }; | ||
| } | ||
| if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) { | ||
| yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` }; | ||
| } | ||
| } | ||
| function* Function(schema, references, path, value) { | ||
@@ -133,24 +263,52 @@ if (!(typeof value === 'function')) { | ||
| function* Integer(schema, references, path, value) { | ||
| if (!(typeof value === 'number')) { | ||
| return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` }; | ||
| if (!IsInteger(value)) { | ||
| return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` }; | ||
| } | ||
| if (!globalThis.Number.isInteger(value)) { | ||
| yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` }; | ||
| } | ||
| if (schema.multipleOf && !(value % schema.multipleOf === 0)) { | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) { | ||
| yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` }; | ||
| } | ||
| if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) { | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` }; | ||
| } | ||
| if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) { | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` }; | ||
| } | ||
| if (schema.minimum && !(value >= schema.minimum)) { | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` }; | ||
| } | ||
| if (schema.maximum && !(value <= schema.maximum)) { | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` }; | ||
| } | ||
| } | ||
| function* Intersect(schema, references, path, value) { | ||
| for (const inner of schema.allOf) { | ||
| const next = Visit(inner, references, path, value).next(); | ||
| if (!next.done) { | ||
| yield next.value; | ||
| yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` }; | ||
| return; | ||
| } | ||
| } | ||
| if (schema.unevaluatedProperties === false) { | ||
| const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema)); | ||
| for (const valueKey of globalThis.Object.getOwnPropertyNames(value)) { | ||
| if (!keyCheck.test(valueKey)) { | ||
| yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` }; | ||
| } | ||
| } | ||
| } | ||
| if (typeof schema.unevaluatedProperties === 'object') { | ||
| const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema)); | ||
| for (const valueKey of globalThis.Object.getOwnPropertyNames(value)) { | ||
| if (!keyCheck.test(valueKey)) { | ||
| const next = Visit(schema.unevaluatedProperties, references, `${path}/${valueKey}`, value[valueKey]).next(); | ||
| if (!next.done) { | ||
| yield next.value; | ||
| yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` }; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| function* Literal(schema, references, path, value) { | ||
@@ -165,2 +323,8 @@ if (!(value === schema.const)) { | ||
| } | ||
| function* Not(schema, references, path, value) { | ||
| if (Visit(schema.allOf[0].not, references, path, value).next().done === true) { | ||
| yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` }; | ||
| } | ||
| yield* Visit(schema.allOf[1], references, path, value); | ||
| } | ||
| function* Null(schema, references, path, value) { | ||
@@ -172,18 +336,18 @@ if (!(value === null)) { | ||
| function* Number(schema, references, path, value) { | ||
| if (!(typeof value === 'number')) { | ||
| if (!IsNumber(value)) { | ||
| return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` }; | ||
| } | ||
| if (schema.multipleOf && !(value % schema.multipleOf === 0)) { | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) { | ||
| yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` }; | ||
| } | ||
| if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) { | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` }; | ||
| } | ||
| if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) { | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` }; | ||
| } | ||
| if (schema.minimum && !(value >= schema.minimum)) { | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` }; | ||
| } | ||
| if (schema.maximum && !(value <= schema.maximum)) { | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` }; | ||
@@ -193,45 +357,47 @@ } | ||
| function* Object(schema, references, path, value) { | ||
| if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) { | ||
| if (!IsObject(value)) { | ||
| return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` }; | ||
| } | ||
| if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) { | ||
| if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) { | ||
| yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` }; | ||
| } | ||
| if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) { | ||
| if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) { | ||
| yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` }; | ||
| } | ||
| const propertyKeys = globalThis.Object.keys(schema.properties); | ||
| if (schema.additionalProperties === false) { | ||
| for (const objectKey of globalThis.Object.keys(value)) { | ||
| if (!propertyKeys.includes(objectKey)) { | ||
| yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${objectKey}`, value: value[objectKey], message: `Unexpected property` }; | ||
| const requiredKeys = globalThis.Array.isArray(schema.required) ? schema.required : []; | ||
| const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties); | ||
| const unknownKeys = globalThis.Object.getOwnPropertyNames(value); | ||
| for (const knownKey of knownKeys) { | ||
| const property = schema.properties[knownKey]; | ||
| if (schema.required && schema.required.includes(knownKey)) { | ||
| yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]); | ||
| if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) { | ||
| yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${knownKey}`, value: undefined, message: `Expected required property` }; | ||
| } | ||
| } | ||
| else { | ||
| if (IsExactOptionalProperty(value, knownKey)) { | ||
| yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]); | ||
| } | ||
| } | ||
| } | ||
| if (schema.required && schema.required.length > 0) { | ||
| const objectKeys = globalThis.Object.keys(value); | ||
| for (const requiredKey of schema.required) { | ||
| if (objectKeys.includes(requiredKey)) | ||
| continue; | ||
| yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` }; | ||
| for (const requiredKey of requiredKeys) { | ||
| if (unknownKeys.includes(requiredKey)) | ||
| continue; | ||
| yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` }; | ||
| } | ||
| if (schema.additionalProperties === false) { | ||
| for (const valueKey of unknownKeys) { | ||
| if (!knownKeys.includes(valueKey)) { | ||
| yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` }; | ||
| } | ||
| } | ||
| } | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| for (const objectKey of globalThis.Object.keys(value)) { | ||
| if (propertyKeys.includes(objectKey)) | ||
| for (const valueKey of unknownKeys) { | ||
| if (knownKeys.includes(valueKey)) | ||
| continue; | ||
| yield* Visit(schema.additionalProperties, references, `${path}/${objectKey}`, value[objectKey]); | ||
| yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]); | ||
| } | ||
| } | ||
| for (const propertyKey of propertyKeys) { | ||
| const propertySchema = schema.properties[propertyKey]; | ||
| if (schema.required && schema.required.includes(propertyKey)) { | ||
| yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]); | ||
| } | ||
| else { | ||
| if (value[propertyKey] !== undefined) { | ||
| yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
@@ -244,37 +410,43 @@ function* Promise(schema, references, path, value) { | ||
| function* Record(schema, references, path, value) { | ||
| if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) { | ||
| return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` }; | ||
| if (!IsRecordObject(value)) { | ||
| return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` }; | ||
| } | ||
| const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const regex = new RegExp(keyPattern); | ||
| if (!globalThis.Object.keys(value).every((key) => regex.test(key))) { | ||
| const numeric = keyPattern === '^(0|[1-9][0-9]*)$'; | ||
| const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString; | ||
| const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings'; | ||
| return yield { type, schema, path, value, message }; | ||
| if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) { | ||
| yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` }; | ||
| } | ||
| for (const [propKey, propValue] of globalThis.Object.entries(value)) { | ||
| yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue); | ||
| if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) { | ||
| yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` }; | ||
| } | ||
| const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const regex = new RegExp(patternKey); | ||
| for (const [propertyKey, propertyValue] of globalThis.Object.entries(value)) { | ||
| if (regex.test(propertyKey)) { | ||
| yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue); | ||
| continue; | ||
| } | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue); | ||
| } | ||
| if (schema.additionalProperties === false) { | ||
| const propertyPath = `${path}/${propertyKey}`; | ||
| const message = `Unexpected property '${propertyPath}'`; | ||
| return yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: propertyPath, value: propertyValue, message }; | ||
| } | ||
| } | ||
| } | ||
| function* Ref(schema, references, path, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`); | ||
| yield* Visit(reference, references, path, value); | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueErrorsDereferenceError(schema); | ||
| const target = references[index]; | ||
| yield* Visit(target, references, path, value); | ||
| } | ||
| function* Self(schema, references, path, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`); | ||
| yield* Visit(reference, references, path, value); | ||
| } | ||
| function* String(schema, references, path, value) { | ||
| if (!(typeof value === 'string')) { | ||
| if (!IsString(value)) { | ||
| return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' }; | ||
| } | ||
| if (schema.minLength !== undefined && !(value.length >= schema.minLength)) { | ||
| if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) { | ||
| yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` }; | ||
| } | ||
| if (schema.maxLength !== undefined && !(value.length <= schema.maxLength)) { | ||
| if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) { | ||
| yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` }; | ||
@@ -289,7 +461,7 @@ } | ||
| if (schema.format !== undefined) { | ||
| if (!index_1.Format.Has(schema.format)) { | ||
| if (!Types.FormatRegistry.Has(schema.format)) { | ||
| yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` }; | ||
| } | ||
| else { | ||
| const format = index_1.Format.Get(schema.format); | ||
| const format = Types.FormatRegistry.Get(schema.format); | ||
| if (!format(value)) { | ||
@@ -301,2 +473,23 @@ yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` }; | ||
| } | ||
| function* Symbol(schema, references, path, value) { | ||
| if (!(typeof value === 'symbol')) { | ||
| return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' }; | ||
| } | ||
| } | ||
| function* TemplateLiteral(schema, references, path, value) { | ||
| if (!IsString(value)) { | ||
| return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' }; | ||
| } | ||
| const regex = new RegExp(schema.pattern); | ||
| if (!regex.test(value)) { | ||
| yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` }; | ||
| } | ||
| } | ||
| function* This(schema, references, path, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueErrorsDereferenceError(schema); | ||
| const target = references[index]; | ||
| yield* Visit(target, references, path, value); | ||
| } | ||
| function* Tuple(schema, references, path, value) { | ||
@@ -332,8 +525,8 @@ if (!globalThis.Array.isArray(value)) { | ||
| } | ||
| if (errors.length > 0) { | ||
| yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' }; | ||
| } | ||
| for (const error of errors) { | ||
| yield error; | ||
| } | ||
| if (errors.length > 0) { | ||
| yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' }; | ||
| } | ||
| } | ||
@@ -344,6 +537,6 @@ function* Uint8Array(schema, references, path, value) { | ||
| } | ||
| if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) { | ||
| if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) { | ||
| yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` }; | ||
| } | ||
| if (schema.minByteLength && !(value.length >= schema.minByteLength)) { | ||
| if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) { | ||
| yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` }; | ||
@@ -354,62 +547,83 @@ } | ||
| function* Void(schema, references, path, value) { | ||
| if (!(value === null)) { | ||
| return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` }; | ||
| if (!IsVoid(value)) { | ||
| return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` }; | ||
| } | ||
| } | ||
| function* UserDefined(schema, references, path, value) { | ||
| const check = Types.TypeRegistry.Get(schema[Types.Kind]); | ||
| if (!check(schema, value)) { | ||
| return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` }; | ||
| } | ||
| } | ||
| function* Visit(schema, references, path, value) { | ||
| const anyReferences = schema.$id === undefined ? references : [schema, ...references]; | ||
| const anySchema = schema; | ||
| switch (anySchema[Types.Kind]) { | ||
| const references_ = IsDefined(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| switch (schema_[Types.Kind]) { | ||
| case 'Any': | ||
| return yield* Any(anySchema, anyReferences, path, value); | ||
| return yield* Any(schema_, references_, path, value); | ||
| case 'Array': | ||
| return yield* Array(anySchema, anyReferences, path, value); | ||
| return yield* Array(schema_, references_, path, value); | ||
| case 'BigInt': | ||
| return yield* BigInt(schema_, references_, path, value); | ||
| case 'Boolean': | ||
| return yield* Boolean(anySchema, anyReferences, path, value); | ||
| return yield* Boolean(schema_, references_, path, value); | ||
| case 'Constructor': | ||
| return yield* Constructor(anySchema, anyReferences, path, value); | ||
| return yield* Constructor(schema_, references_, path, value); | ||
| case 'Date': | ||
| return yield* Date(schema_, references_, path, value); | ||
| case 'Function': | ||
| return yield* Function(anySchema, anyReferences, path, value); | ||
| return yield* Function(schema_, references_, path, value); | ||
| case 'Integer': | ||
| return yield* Integer(anySchema, anyReferences, path, value); | ||
| return yield* Integer(schema_, references_, path, value); | ||
| case 'Intersect': | ||
| return yield* Intersect(schema_, references_, path, value); | ||
| case 'Literal': | ||
| return yield* Literal(anySchema, anyReferences, path, value); | ||
| return yield* Literal(schema_, references_, path, value); | ||
| case 'Never': | ||
| return yield* Never(anySchema, anyReferences, path, value); | ||
| return yield* Never(schema_, references_, path, value); | ||
| case 'Not': | ||
| return yield* Not(schema_, references_, path, value); | ||
| case 'Null': | ||
| return yield* Null(anySchema, anyReferences, path, value); | ||
| return yield* Null(schema_, references_, path, value); | ||
| case 'Number': | ||
| return yield* Number(anySchema, anyReferences, path, value); | ||
| return yield* Number(schema_, references_, path, value); | ||
| case 'Object': | ||
| return yield* Object(anySchema, anyReferences, path, value); | ||
| return yield* Object(schema_, references_, path, value); | ||
| case 'Promise': | ||
| return yield* Promise(anySchema, anyReferences, path, value); | ||
| return yield* Promise(schema_, references_, path, value); | ||
| case 'Record': | ||
| return yield* Record(anySchema, anyReferences, path, value); | ||
| return yield* Record(schema_, references_, path, value); | ||
| case 'Ref': | ||
| return yield* Ref(anySchema, anyReferences, path, value); | ||
| case 'Self': | ||
| return yield* Self(anySchema, anyReferences, path, value); | ||
| return yield* Ref(schema_, references_, path, value); | ||
| case 'String': | ||
| return yield* String(anySchema, anyReferences, path, value); | ||
| return yield* String(schema_, references_, path, value); | ||
| case 'Symbol': | ||
| return yield* Symbol(schema_, references_, path, value); | ||
| case 'TemplateLiteral': | ||
| return yield* TemplateLiteral(schema_, references_, path, value); | ||
| case 'This': | ||
| return yield* This(schema_, references_, path, value); | ||
| case 'Tuple': | ||
| return yield* Tuple(anySchema, anyReferences, path, value); | ||
| return yield* Tuple(schema_, references_, path, value); | ||
| case 'Undefined': | ||
| return yield* Undefined(anySchema, anyReferences, path, value); | ||
| return yield* Undefined(schema_, references_, path, value); | ||
| case 'Union': | ||
| return yield* Union(anySchema, anyReferences, path, value); | ||
| return yield* Union(schema_, references_, path, value); | ||
| case 'Uint8Array': | ||
| return yield* Uint8Array(anySchema, anyReferences, path, value); | ||
| return yield* Uint8Array(schema_, references_, path, value); | ||
| case 'Unknown': | ||
| return yield* Unknown(anySchema, anyReferences, path, value); | ||
| return yield* Unknown(schema_, references_, path, value); | ||
| case 'Void': | ||
| return yield* Void(anySchema, anyReferences, path, value); | ||
| return yield* Void(schema_, references_, path, value); | ||
| default: | ||
| throw new ValueErrorsUnknownTypeError(schema); | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new ValueErrorsUnknownTypeError(schema); | ||
| return yield* UserDefined(schema_, references_, path, value); | ||
| } | ||
| } | ||
| function* Errors(schema, references, value) { | ||
| yield* Visit(schema, references, '', value); | ||
| function Errors(schema, references, value) { | ||
| const iterator = Visit(schema, references, '', value); | ||
| return new ValueErrorIterator(iterator); | ||
| } | ||
| ValueErrors.Errors = Errors; | ||
| })(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {})); | ||
| })(ValueErrors || (exports.ValueErrors = ValueErrors = {})); |
| export * from './errors'; |
+1
-1
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -11,0 +11,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
+11
-4
| { | ||
| "name": "@sinclair/typebox", | ||
| "version": "0.24.51", | ||
| "version": "0.28.16", | ||
| "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -15,2 +15,9 @@ "keywords": [ | ||
| "types": "./typebox.d.ts", | ||
| "exports": { | ||
| "./compiler": "./compiler/index.js", | ||
| "./errors": "./errors/index.js", | ||
| "./system": "./system/index.js", | ||
| "./value": "./value/index.js", | ||
| ".": "./typebox.js" | ||
| }, | ||
| "repository": { | ||
@@ -33,4 +40,4 @@ "type": "git", | ||
| "@types/mocha": "^9.1.1", | ||
| "@types/node": "^18.7.13", | ||
| "ajv": "^8.11.0", | ||
| "@types/node": "^18.11.9", | ||
| "ajv": "^8.12.0", | ||
| "ajv-formats": "^2.1.1", | ||
@@ -40,4 +47,4 @@ "chai": "^4.3.6", | ||
| "prettier": "^2.7.1", | ||
| "typescript": "^4.8.2" | ||
| "typescript": "^5.1.3" | ||
| } | ||
| } |
+577
-202
@@ -0,14 +1,41 @@ | ||
| export declare const Modifier: unique symbol; | ||
| export declare const Hint: unique symbol; | ||
| export declare const Kind: unique symbol; | ||
| export declare const Hint: unique symbol; | ||
| export declare const Modifier: unique symbol; | ||
| export declare type TModifier = TReadonlyOptional<TSchema> | TOptional<TSchema> | TReadonly<TSchema>; | ||
| export declare type TReadonly<T extends TSchema> = T & { | ||
| export declare const PatternBoolean = "(true|false)"; | ||
| export declare const PatternNumber = "(0|[1-9][0-9]*)"; | ||
| export declare const PatternString = "(.*)"; | ||
| export declare const PatternBooleanExact: string; | ||
| export declare const PatternNumberExact: string; | ||
| export declare const PatternStringExact: string; | ||
| export type TupleToIntersect<T extends any[]> = T extends [infer I] ? I : T extends [infer I, ...infer R] ? I & TupleToIntersect<R> : never; | ||
| export type TupleToUnion<T extends any[]> = { | ||
| [K in keyof T]: T[K]; | ||
| }[number]; | ||
| export type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never; | ||
| export type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never; | ||
| export type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L]; | ||
| export type Discard<T extends unknown[], D extends unknown> = T extends [infer L, ...infer R] ? (L extends D ? Discard<R, D> : [L, ...Discard<R, D>]) : []; | ||
| export type Flat<T> = T extends [] ? [] : T extends [infer L] ? [...Flat<L>] : T extends [infer L, ...infer R] ? [...Flat<L>, ...Flat<R>] : [T]; | ||
| export type Trim<T> = T extends `${' '}${infer U}` ? Trim<U> : T extends `${infer U}${' '}` ? Trim<U> : T; | ||
| export type Assert<T, E> = T extends E ? T : never; | ||
| export type Evaluate<T> = T extends infer O ? { | ||
| [K in keyof O]: O[K]; | ||
| } : never; | ||
| export type Ensure<T> = T extends infer U ? U : never; | ||
| export type AssertProperties<T> = T extends TProperties ? T : TProperties; | ||
| export type AssertRest<T, E extends TSchema[] = TSchema[]> = T extends E ? T : []; | ||
| export type AssertType<T, E extends TSchema = TSchema> = T extends E ? T : TNever; | ||
| export type IntersectType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertType<T[0]> : TIntersect<T>; | ||
| export type UnionType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertType<T[0]> : TUnion<T>; | ||
| export type TModifier = TReadonlyOptional<TSchema> | TOptional<TSchema> | TReadonly<TSchema>; | ||
| export type TReadonly<T extends TSchema> = T & { | ||
| [Modifier]: 'Readonly'; | ||
| }; | ||
| export declare type TOptional<T extends TSchema> = T & { | ||
| export type TOptional<T extends TSchema> = T & { | ||
| [Modifier]: 'Optional'; | ||
| }; | ||
| export declare type TReadonlyOptional<T extends TSchema> = T & { | ||
| export type TReadonlyOptional<T extends TSchema> = T & { | ||
| [Modifier]: 'ReadonlyOptional'; | ||
| }; | ||
| export type Key = string | number; | ||
| export interface SchemaOptions { | ||
@@ -24,22 +51,23 @@ $schema?: string; | ||
| default?: any; | ||
| /** Example values matching this schema. */ | ||
| /** Example values matching this schema */ | ||
| examples?: any; | ||
| [prop: string]: any; | ||
| } | ||
| export interface TSchema extends SchemaOptions { | ||
| export interface TKind { | ||
| [Kind]: string; | ||
| } | ||
| export interface TSchema extends SchemaOptions, TKind { | ||
| [Modifier]?: string; | ||
| [Hint]?: string; | ||
| [Modifier]?: string; | ||
| params: unknown[]; | ||
| static: unknown; | ||
| } | ||
| export declare type TAnySchema = TSchema | TAny | TArray | TBoolean | TConstructor | TEnum | TFunction | TInteger | TLiteral | TNull | TNumber | TObject | TPromise | TRecord | TSelf | TRef | TString | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid; | ||
| export interface NumericOptions extends SchemaOptions { | ||
| exclusiveMaximum?: number; | ||
| exclusiveMinimum?: number; | ||
| maximum?: number; | ||
| minimum?: number; | ||
| multipleOf?: number; | ||
| export type TAnySchema = TSchema | TAny | TArray | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid; | ||
| export interface NumericOptions<N extends number | bigint> extends SchemaOptions { | ||
| exclusiveMaximum?: N; | ||
| exclusiveMinimum?: N; | ||
| maximum?: N; | ||
| minimum?: N; | ||
| multipleOf?: N; | ||
| } | ||
| export declare type TNumeric = TInteger | TNumber; | ||
| export interface TAny extends TSchema { | ||
@@ -56,6 +84,12 @@ [Kind]: 'Any'; | ||
| [Kind]: 'Array'; | ||
| static: Array<Static<T, this['params']>>; | ||
| static: Static<T, this['params']>[]; | ||
| type: 'array'; | ||
| items: T; | ||
| } | ||
| export interface TBigInt extends TSchema, NumericOptions<bigint> { | ||
| [Kind]: 'BigInt'; | ||
| static: bigint; | ||
| type: 'null'; | ||
| typeOf: 'BigInt'; | ||
| } | ||
| export interface TBoolean extends TSchema { | ||
@@ -66,14 +100,32 @@ [Kind]: 'Boolean'; | ||
| } | ||
| export declare type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>; | ||
| export declare type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns']; | ||
| export declare type StaticContructorParameters<T extends readonly TSchema[], P extends unknown[]> = [...{ | ||
| [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never; | ||
| export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>; | ||
| export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns']; | ||
| export type TCompositeReduce<T extends TIntersect<TObject[]>, K extends string[]> = K extends [infer L, ...infer R] ? { | ||
| [_ in Assert<L, string>]: TIndexType<T, Assert<L, string>>; | ||
| } & TCompositeReduce<T, Assert<R, string[]>> : {}; | ||
| export type TCompositeSelect<T extends TIntersect<TObject[]>> = UnionToTuple<keyof Static<T>> extends infer K ? Evaluate<TCompositeReduce<T, Assert<K, string[]>>> : {}; | ||
| export type TComposite<T extends TObject[]> = TIntersect<T> extends infer R ? TObject<TCompositeSelect<Assert<R, TIntersect<TObject[]>>>> : TObject<{}>; | ||
| export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{ | ||
| [K in keyof T]: Static<AssertType<T[K]>, P>; | ||
| }]; | ||
| export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema { | ||
| [Kind]: 'Constructor'; | ||
| static: new (...param: StaticContructorParameters<T, this['params']>) => Static<U, this['params']>; | ||
| type: 'constructor'; | ||
| static: new (...param: TConstructorParameterArray<T, this['params']>) => Static<U, this['params']>; | ||
| type: 'object'; | ||
| instanceOf: 'Constructor'; | ||
| parameters: T; | ||
| returns: U; | ||
| } | ||
| export interface DateOptions extends SchemaOptions { | ||
| exclusiveMaximumTimestamp?: number; | ||
| exclusiveMinimumTimestamp?: number; | ||
| maximumTimestamp?: number; | ||
| minimumTimestamp?: number; | ||
| } | ||
| export interface TDate extends TSchema, DateOptions { | ||
| [Kind]: 'Date'; | ||
| static: Date; | ||
| type: 'object'; | ||
| instanceOf: 'Date'; | ||
| } | ||
| export interface TEnumOption<T> { | ||
@@ -86,17 +138,48 @@ type: 'number' | 'string'; | ||
| static: T[keyof T]; | ||
| anyOf: TLiteral<string | number>[]; | ||
| anyOf: TLiteral<T[keyof T]>[]; | ||
| } | ||
| export declare type TParameters<T extends TFunction> = TTuple<T['parameters']>; | ||
| export declare type TReturnType<T extends TFunction> = T['returns']; | ||
| export declare type StaticFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{ | ||
| [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never; | ||
| export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[AssertType<X>, AssertType<Y>]> : AssertType<O> : never; | ||
| export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{ | ||
| [K in T]: TLiteral<K>; | ||
| }[T]>>>; | ||
| export type TExcludeTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Exclude<Static<T>, Static<U>> extends infer S ? TExcludeTemplateLiteralResult<Assert<S, string>> : never; | ||
| export type TExcludeArray<T extends TSchema[], U extends TSchema> = AssertRest<UnionToTuple<{ | ||
| [K in keyof T]: Static<AssertType<T[K]>> extends Static<U> ? never : T[K]; | ||
| }[number]>> extends infer R ? UnionType<AssertRest<R>> : never; | ||
| export type TExclude<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExcludeTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExcludeArray<S, U> : T extends U ? TNever : T; | ||
| export type TExtractTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{ | ||
| [K in T]: TLiteral<K>; | ||
| }[T]>>>; | ||
| export type TExtractTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Extract<Static<T>, Static<U>> extends infer S ? TExtractTemplateLiteralResult<Assert<S, string>> : never; | ||
| export type TExtractArray<T extends TSchema[], U extends TSchema> = AssertRest<UnionToTuple<{ | ||
| [K in keyof T]: Static<AssertType<T[K]>> extends Static<U> ? T[K] : never; | ||
| }[number]>> extends infer R ? UnionType<AssertRest<R>> : never; | ||
| export type TExtract<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExtractTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExtractArray<S, U> : T extends U ? T : T; | ||
| export type TFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{ | ||
| [K in keyof T]: Static<AssertType<T[K]>, P>; | ||
| }]; | ||
| export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema { | ||
| [Kind]: 'Function'; | ||
| static: (...param: StaticFunctionParameters<T, this['params']>) => Static<U, this['params']>; | ||
| type: 'function'; | ||
| static: (...param: TFunctionParameters<T, this['params']>) => Static<U, this['params']>; | ||
| type: 'object'; | ||
| instanceOf: 'Function'; | ||
| parameters: T; | ||
| returns: U; | ||
| } | ||
| export interface TInteger extends TSchema, NumericOptions { | ||
| export type TIndexRest<T extends TSchema[], K extends Key> = T extends [infer L, ...infer R] ? [TIndexType<AssertType<L>, K>, ...TIndexRest<AssertRest<R>, K>] : []; | ||
| export type TIndexProperty<T extends TProperties, K extends Key> = K extends keyof T ? [T[K]] : []; | ||
| export type TIndexTuple<T extends TSchema[], K extends Key> = K extends keyof T ? [T[K]] : []; | ||
| export type TIndexType<T extends TSchema, K extends Key> = T extends TRecursive<infer S> ? TIndexType<S, K> : T extends TIntersect<infer S> ? IntersectType<AssertRest<Discard<Flat<TIndexRest<S, K>>, TNever>>> : T extends TUnion<infer S> ? UnionType<AssertRest<Flat<TIndexRest<S, K>>>> : T extends TObject<infer S> ? UnionType<AssertRest<Flat<TIndexProperty<S, K>>>> : T extends TTuple<infer S> ? UnionType<AssertRest<Flat<TIndexTuple<S, K>>>> : [ | ||
| ]; | ||
| export type TIndexRestMany<T extends TSchema, K extends Key[]> = K extends [infer L, ...infer R] ? [TIndexType<T, Assert<L, Key>>, ...TIndexRestMany<T, Assert<R, Key[]>>] : [ | ||
| ]; | ||
| export type TIndexReduce<T extends TSchema, K extends Key[]> = T extends TRecursive<infer S> ? TIndexReduce<S, K> : T extends TIntersect ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TUnion ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TObject ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TTuple ? UnionType<Flat<TIndexRestMany<T, K>>> : TNever; | ||
| export type TIndex<T extends TSchema, K extends TSchema> = [ | ||
| T, | ||
| K | ||
| ] extends [TTuple, TNumber] ? UnionType<Assert<T['items'], TSchema[]>> : [ | ||
| T, | ||
| K | ||
| ] extends [TArray, TNumber] ? AssertType<T['items']> : K extends TTemplateLiteral ? TIndexReduce<T, TTemplateLiteralKeyRest<K>> : K extends TUnion<TLiteral<Key>[]> ? TIndexReduce<T, TUnionLiteralKeyRest<K>> : K extends TLiteral<Key> ? TIndexReduce<T, [K['const']]> : TNever; | ||
| export interface TInteger extends TSchema, NumericOptions<number> { | ||
| [Kind]: 'Integer'; | ||
@@ -106,25 +189,24 @@ static: number; | ||
| } | ||
| export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {}; | ||
| export declare type IntersectEvaluate<T extends readonly TSchema[], P extends unknown[]> = { | ||
| [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never; | ||
| }; | ||
| export declare type IntersectProperties<T extends readonly TObject[]> = { | ||
| [K in keyof T]: T[K] extends TObject<infer P> ? P : {}; | ||
| }; | ||
| export interface TIntersect<T extends TObject[] = TObject[]> extends TObject { | ||
| static: IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>; | ||
| properties: IntersectReduce<unknown, IntersectProperties<T>>; | ||
| export type TUnevaluatedProperties = undefined | TSchema | boolean; | ||
| export interface IntersectOptions extends SchemaOptions { | ||
| unevaluatedProperties?: TUnevaluatedProperties; | ||
| } | ||
| export declare type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never; | ||
| export declare type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never; | ||
| export declare type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L]; | ||
| export declare type UnionStringLiteralToTuple<T> = T extends TUnion<infer L> ? { | ||
| [I in keyof L]: L[I] extends TLiteral<infer C> ? C : never; | ||
| } : never; | ||
| export declare type UnionLiteralsFromObject<T extends TObject> = { | ||
| [K in ObjectPropertyKeys<T>]: TLiteral<K>; | ||
| } extends infer R ? UnionToTuple<R[keyof R]> : never; | ||
| export interface TKeyOf<T extends TObject> extends TUnion<UnionLiteralsFromObject<T>> { | ||
| export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, IntersectOptions { | ||
| [Kind]: 'Intersect'; | ||
| static: TupleToIntersect<{ | ||
| [K in keyof T]: Static<AssertType<T[K]>, this['params']>; | ||
| }>; | ||
| type?: 'object'; | ||
| allOf: [...T]; | ||
| } | ||
| export declare type TLiteralValue = string | number | boolean; | ||
| export type TKeyOfProperties<T extends TSchema> = Static<T> extends infer S ? UnionToTuple<{ | ||
| [K in keyof S]: TLiteral<Assert<K, TLiteralValue>>; | ||
| }[keyof S]> : []; | ||
| export type TKeyOfIndicesArray<T extends TSchema[]> = UnionToTuple<keyof T & `${number}`>; | ||
| export type TKeyOfIndices<T extends TSchema[]> = AssertRest<TKeyOfIndicesArray<T> extends infer R ? { | ||
| [K in keyof R]: TLiteral<Assert<R[K], TLiteralValue>>; | ||
| } : []>; | ||
| export type TKeyOf<T extends TSchema = TSchema> = (T extends TRecursive<infer S> ? TKeyOfProperties<S> : T extends TIntersect ? TKeyOfProperties<T> : T extends TUnion ? TKeyOfProperties<T> : T extends TObject ? TKeyOfProperties<T> : T extends TTuple<infer K> ? TKeyOfIndices<K> : T extends TArray ? [TNumber] : T extends TRecord<infer K> ? [K] : [ | ||
| ]) extends infer R ? UnionType<AssertRest<R>> : never; | ||
| export type TLiteralValue = string | number | boolean; | ||
| export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema { | ||
@@ -138,9 +220,10 @@ [Kind]: 'Literal'; | ||
| static: never; | ||
| not: {}; | ||
| } | ||
| export interface TNot<Not extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema { | ||
| [Kind]: 'Not'; | ||
| static: Static<T>; | ||
| allOf: [{ | ||
| type: 'boolean'; | ||
| const: false; | ||
| }, { | ||
| type: 'boolean'; | ||
| const: true; | ||
| }]; | ||
| not: Not; | ||
| }, T]; | ||
| } | ||
@@ -152,3 +235,3 @@ export interface TNull extends TSchema { | ||
| } | ||
| export interface TNumber extends TSchema, NumericOptions { | ||
| export interface TNumber extends TSchema, NumericOptions<number> { | ||
| [Kind]: 'Number'; | ||
@@ -158,32 +241,20 @@ static: number; | ||
| } | ||
| export declare type ReadonlyOptionalPropertyKeys<T extends TProperties> = { | ||
| export type ReadonlyOptionalPropertyKeys<T extends TProperties> = { | ||
| [K in keyof T]: T[K] extends TReadonlyOptional<TSchema> ? K : never; | ||
| }[keyof T]; | ||
| export declare type ReadonlyPropertyKeys<T extends TProperties> = { | ||
| export type ReadonlyPropertyKeys<T extends TProperties> = { | ||
| [K in keyof T]: T[K] extends TReadonly<TSchema> ? K : never; | ||
| }[keyof T]; | ||
| export declare type OptionalPropertyKeys<T extends TProperties> = { | ||
| export type OptionalPropertyKeys<T extends TProperties> = { | ||
| [K in keyof T]: T[K] extends TOptional<TSchema> ? K : never; | ||
| }[keyof T]; | ||
| export declare type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>; | ||
| export declare type PropertiesReduce<T extends TProperties, P extends unknown[]> = { | ||
| readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K], P>; | ||
| } & { | ||
| readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K], P>; | ||
| } & { | ||
| [K in OptionalPropertyKeys<T>]?: Static<T[K], P>; | ||
| } & { | ||
| [K in RequiredPropertyKeys<T>]: Static<T[K], P>; | ||
| } extends infer R ? { | ||
| [K in keyof R]: R[K]; | ||
| } : never; | ||
| export declare type TRecordProperties<K extends TUnion<TLiteral[]>, T extends TSchema> = Static<K> extends string ? { | ||
| [X in Static<K>]: T; | ||
| } : never; | ||
| export interface TProperties { | ||
| [key: string]: TSchema; | ||
| } | ||
| export declare type ObjectProperties<T> = T extends TObject<infer U> ? U : never; | ||
| export declare type ObjectPropertyKeys<T> = T extends TObject<infer U> ? keyof U : never; | ||
| export declare type TAdditionalProperties = undefined | TSchema | boolean; | ||
| export type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>; | ||
| export type PropertiesReducer<T extends TProperties, R extends Record<keyof any, unknown>> = Evaluate<(Readonly<Partial<Pick<R, ReadonlyOptionalPropertyKeys<T>>>> & Readonly<Pick<R, ReadonlyPropertyKeys<T>>> & Partial<Pick<R, OptionalPropertyKeys<T>>> & Required<Pick<R, RequiredPropertyKeys<T>>>)>; | ||
| export type PropertiesReduce<T extends TProperties, P extends unknown[]> = PropertiesReducer<T, { | ||
| [K in keyof T]: Static<T[K], P>; | ||
| }>; | ||
| export type TProperties = Record<keyof any, TSchema>; | ||
| export type ObjectProperties<T> = T extends TObject<infer U> ? U : never; | ||
| export type ObjectPropertyKeys<T> = T extends TObject<infer U> ? keyof U : never; | ||
| export type TAdditionalProperties = undefined | TSchema | boolean; | ||
| export interface ObjectOptions extends SchemaOptions { | ||
@@ -202,23 +273,46 @@ additionalProperties?: TAdditionalProperties; | ||
| } | ||
| export interface TOmit<T extends TObject, Properties extends ObjectPropertyKeys<T>[]> extends TObject, ObjectOptions { | ||
| static: Omit<Static<T, this['params']>, Properties[number]>; | ||
| properties: T extends TObject ? Omit<T['properties'], Properties[number]> : never; | ||
| } | ||
| export interface TPartial<T extends TObject> extends TObject { | ||
| static: Partial<Static<T, this['params']>>; | ||
| properties: { | ||
| [K in keyof T['properties']]: T['properties'][K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T['properties'][K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T['properties'][K] extends TOptional<infer U> ? TOptional<U> : TOptional<T['properties'][K]>; | ||
| }; | ||
| } | ||
| export declare type TPick<T extends TObject, Properties extends ObjectPropertyKeys<T>[]> = TObject<{ | ||
| [K in Properties[number]]: T['properties'][K]; | ||
| export type TOmitArray<T extends TSchema[], K extends keyof any> = AssertRest<{ | ||
| [K2 in keyof T]: TOmit<AssertType<T[K2]>, K>; | ||
| }>; | ||
| export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<AssertProperties<Omit<T, K>>>; | ||
| export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T; | ||
| export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>; | ||
| export type TPartialObjectArray<T extends TObject[]> = AssertRest<{ | ||
| [K in keyof T]: TPartial<AssertType<T[K], TObject>>; | ||
| }, TObject[]>; | ||
| export type TPartialArray<T extends TSchema[]> = AssertRest<{ | ||
| [K in keyof T]: TPartial<AssertType<T[K]>>; | ||
| }>; | ||
| export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{ | ||
| [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>; | ||
| }>>; | ||
| export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T; | ||
| export type TPickArray<T extends TSchema[], K extends keyof any> = { | ||
| [K2 in keyof T]: TPick<AssertType<T[K2]>, K>; | ||
| }; | ||
| export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({ | ||
| [K in keyof R]: AssertType<R[K]> extends TSchema ? R[K] : never; | ||
| }) : never; | ||
| export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T; | ||
| export interface TPromise<T extends TSchema = TSchema> extends TSchema { | ||
| [Kind]: 'Promise'; | ||
| static: Promise<Static<T, this['params']>>; | ||
| type: 'promise'; | ||
| type: 'object'; | ||
| instanceOf: 'Promise'; | ||
| item: TSchema; | ||
| } | ||
| export declare type TRecordKey = TString | TNumeric | TUnion<TLiteral<any>[]>; | ||
| export interface TRecord<K extends TRecordKey = TRecordKey, T extends TSchema = TSchema> extends TSchema { | ||
| export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extends TSchema> = Ensure<TObject<Evaluate<{ | ||
| [_ in Static<K>]: T; | ||
| }>>>; | ||
| export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<K, T>; | ||
| export type RecordUnionLiteralType<K extends TUnion, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{ | ||
| [X in Static<K>]: T; | ||
| }>> : never; | ||
| export type RecordLiteralType<K extends TLiteral<string | number>, T extends TSchema> = Ensure<TObject<{ | ||
| [K2 in K['const']]: T; | ||
| }>>; | ||
| export type RecordNumberType<K extends TInteger | TNumber, T extends TSchema> = Ensure<TRecord<K, T>>; | ||
| export type RecordStringType<K extends TString, T extends TSchema> = Ensure<TRecord<K, T>>; | ||
| export type RecordKey = TUnion<TLiteral<string | number>[]> | TLiteral<string | number> | TTemplateLiteral | TInteger | TNumber | TString; | ||
| export interface TRecord<K extends RecordKey = RecordKey, T extends TSchema = TSchema> extends TSchema { | ||
| [Kind]: 'Record'; | ||
@@ -230,11 +324,12 @@ static: Record<Static<K>, Static<T, this['params']>>; | ||
| }; | ||
| additionalProperties: false; | ||
| additionalProperties: TAdditionalProperties; | ||
| } | ||
| export interface TSelf extends TSchema { | ||
| [Kind]: 'Self'; | ||
| export interface TThis extends TSchema { | ||
| [Kind]: 'This'; | ||
| static: this['params'][0]; | ||
| $ref: string; | ||
| } | ||
| export declare type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>]>; | ||
| export type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>]>; | ||
| export interface TRecursive<T extends TSchema> extends TSchema { | ||
| [Hint]: 'Recursive'; | ||
| static: TRecursiveReduce<T>; | ||
@@ -247,18 +342,21 @@ } | ||
| } | ||
| export interface TRequired<T extends TObject | TRef<TObject>> extends TObject { | ||
| static: Required<Static<T, this['params']>>; | ||
| properties: { | ||
| [K in keyof T['properties']]: T['properties'][K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T['properties'][K] extends TReadonly<infer U> ? TReadonly<U> : T['properties'][K] extends TOptional<infer U> ? U : T['properties'][K]; | ||
| }; | ||
| } | ||
| export declare type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex'; | ||
| export interface StringOptions<Format extends string> extends SchemaOptions { | ||
| export type TRest<T extends TSchema> = T extends TTuple<infer R> ? Assert<R, TSchema[]> : Assert<[T], TSchema[]>; | ||
| export type TReturnType<T extends TFunction> = T['returns']; | ||
| export type TRequiredArray<T extends TSchema[]> = AssertRest<{ | ||
| [K in keyof T]: TRequired<AssertType<T[K]>>; | ||
| }>; | ||
| export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{ | ||
| [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K]; | ||
| }>>; | ||
| export type TRequired<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : T extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T; | ||
| export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex'; | ||
| export interface StringOptions extends SchemaOptions { | ||
| minLength?: number; | ||
| maxLength?: number; | ||
| pattern?: string; | ||
| format?: Format; | ||
| format?: string; | ||
| contentEncoding?: '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64'; | ||
| contentMediaType?: string; | ||
| } | ||
| export interface TString<Format extends string = string> extends TSchema, StringOptions<Format> { | ||
| export interface TString extends TSchema, StringOptions { | ||
| [Kind]: 'String'; | ||
@@ -268,8 +366,36 @@ static: string; | ||
| } | ||
| export declare type TupleToArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? R : never; | ||
| export type SymbolValue = string | number | undefined; | ||
| export interface TSymbol extends TSchema, SchemaOptions { | ||
| [Kind]: 'Symbol'; | ||
| static: symbol; | ||
| type: 'null'; | ||
| typeOf: 'Symbol'; | ||
| } | ||
| export type TTemplateLiteralDslParserUnionLiteral<T extends string> = T extends `${infer L}|${infer R}` ? [TLiteral<Trim<L>>, ...TTemplateLiteralDslParserUnionLiteral<R>] : T extends `${infer L}` ? [TLiteral<Trim<L>>] : [ | ||
| ]; | ||
| export type TTemplateLiteralDslParserUnion<T extends string> = UnionType<TTemplateLiteralDslParserUnionLiteral<T>>; | ||
| export type TTemplateLiteralDslParserTerminal<T extends string> = T extends 'boolean' ? TBoolean : T extends 'bigint' ? TBigInt : T extends 'number' ? TNumber : T extends 'string' ? TString : TTemplateLiteralDslParserUnion<T>; | ||
| export type TTemplateLiteralDslParserTemplate<T extends string> = T extends `{${infer L}}${infer R}` ? [TTemplateLiteralDslParserTerminal<L>, ...TTemplateLiteralDslParserTemplate<R>] : T extends `${infer L}$${infer R}` ? [TLiteral<L>, ...TTemplateLiteralDslParserTemplate<R>] : T extends `${infer L}` ? [TLiteral<L>] : [ | ||
| ]; | ||
| export type TTemplateLiteralDslParser<T extends string> = Ensure<TTemplateLiteral<Assert<TTemplateLiteralDslParserTemplate<T>, TTemplateLiteralKind[]>>>; | ||
| export type IsTemplateLiteralFiniteCheck<T> = T extends TTemplateLiteral<infer U> ? IsTemplateLiteralFiniteArray<Assert<U, TTemplateLiteralKind[]>> : T extends TUnion<infer U> ? IsTemplateLiteralFiniteArray<Assert<U, TTemplateLiteralKind[]>> : T extends TString ? false : T extends TBoolean ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TLiteral ? true : false; | ||
| export type IsTemplateLiteralFiniteArray<T extends TTemplateLiteralKind[]> = T extends [infer L, ...infer R] ? IsTemplateLiteralFiniteCheck<L> extends false ? false : IsTemplateLiteralFiniteArray<Assert<R, TTemplateLiteralKind[]>> : true; | ||
| export type IsTemplateLiteralFinite<T> = T extends TTemplateLiteral<infer U> ? IsTemplateLiteralFiniteArray<U> : false; | ||
| export type TTemplateLiteralKind = TUnion | TLiteral | TInteger | TTemplateLiteral | TNumber | TBigInt | TString | TBoolean | TNever; | ||
| export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infer U> ? { | ||
| [K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>; | ||
| }[number] : T extends TTemplateLiteral ? `${Static<T>}` : T extends TLiteral<infer U> ? `${U}` : T extends TString ? `${string}` : T extends TNumber ? `${number}` : T extends TBigInt ? `${bigint}` : T extends TBoolean ? `${boolean}` : never; | ||
| export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc; | ||
| export type TTemplateLiteralKeyRest<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>; | ||
| export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema { | ||
| [Kind]: 'TemplateLiteral'; | ||
| static: TTemplateLiteralUnion<T>; | ||
| type: 'string'; | ||
| pattern: string; | ||
| } | ||
| export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never; | ||
| export type TTupleInfer<T extends TSchema[], P extends unknown[]> = T extends [infer L, ...infer R] ? [Static<AssertType<L>, P>, ...TTupleInfer<AssertRest<R>, P>] : []; | ||
| export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema { | ||
| [Kind]: 'Tuple'; | ||
| static: { | ||
| [K in keyof T]: T[K] extends TSchema ? Static<T[K], this['params']> : T[K]; | ||
| }; | ||
| static: TTupleInfer<T, this['params']>; | ||
| type: 'array'; | ||
@@ -283,6 +409,13 @@ items?: T; | ||
| [Kind]: 'Undefined'; | ||
| specialized: 'Undefined'; | ||
| static: undefined; | ||
| type: 'object'; | ||
| type: 'null'; | ||
| typeOf: 'Undefined'; | ||
| } | ||
| export type TLiteralUnionReduce<T extends TLiteral<string | number>[]> = T extends [infer L, ...infer R] ? [Assert<L, TLiteral<string | number>>['const'], ...TLiteralUnionReduce<Assert<R, TLiteral<string | number>[]>>] : [ | ||
| ]; | ||
| export type TUnionLiteralKeyRest<T extends TUnion<TLiteral<string | number>[]>> = T extends TUnion<infer S> ? TLiteralUnionReduce<Assert<S, TLiteral<string | number>[]>> : [ | ||
| ]; | ||
| export type TUnionTemplateLiteral<T extends TTemplateLiteral, S extends string = Static<T>> = Ensure<UnionType<Assert<UnionToTuple<{ | ||
| [K in S]: TLiteral<K>; | ||
| }[S]>, TLiteral[]>>>; | ||
| export interface TUnion<T extends TSchema[] = TSchema[]> extends TSchema { | ||
@@ -302,3 +435,3 @@ [Kind]: 'Union'; | ||
| static: Uint8Array; | ||
| specialized: 'Uint8Array'; | ||
| instanceOf: 'Uint8Array'; | ||
| type: 'object'; | ||
@@ -321,103 +454,345 @@ } | ||
| type: 'null'; | ||
| typeOf: 'Void'; | ||
| } | ||
| /** Creates a static type from a TypeBox type */ | ||
| export declare type Static<T extends TSchema, P extends unknown[] = []> = (T & { | ||
| /** Infers a static type from a TypeBox type */ | ||
| export type Static<T extends TSchema, P extends unknown[] = []> = (T & { | ||
| params: P; | ||
| })['static']; | ||
| export type TypeRegistryValidationFunction<TSchema> = (schema: TSchema, value: unknown) => boolean; | ||
| /** A registry for user defined types */ | ||
| export declare namespace TypeRegistry { | ||
| /** Returns the entries in this registry */ | ||
| function Entries(): Map<string, TypeRegistryValidationFunction<any>>; | ||
| /** Clears all user defined types */ | ||
| function Clear(): void; | ||
| /** Returns true if this registry contains this kind */ | ||
| function Has(kind: string): boolean; | ||
| /** Sets a validation function for a user defined type */ | ||
| function Set<TSchema = unknown>(kind: string, func: TypeRegistryValidationFunction<TSchema>): void; | ||
| /** Gets a custom validation function for a user defined type */ | ||
| function Get(kind: string): TypeRegistryValidationFunction<any> | undefined; | ||
| } | ||
| export type FormatRegistryValidationFunction = (value: string) => boolean; | ||
| /** A registry for user defined string formats */ | ||
| export declare namespace FormatRegistry { | ||
| /** Returns the entries in this registry */ | ||
| function Entries(): Map<string, FormatRegistryValidationFunction>; | ||
| /** Clears all user defined string formats */ | ||
| function Clear(): void; | ||
| /** Returns true if the user defined string format exists */ | ||
| function Has(format: string): boolean; | ||
| /** Sets a validation function for a user defined string format */ | ||
| function Set(format: string, func: FormatRegistryValidationFunction): void; | ||
| /** Gets a validation function for a user defined string format */ | ||
| function Get(format: string): FormatRegistryValidationFunction | undefined; | ||
| } | ||
| export declare class TypeGuardUnknownTypeError extends Error { | ||
| readonly schema: unknown; | ||
| constructor(schema: unknown); | ||
| } | ||
| /** Provides functions to test if JavaScript values are TypeBox types */ | ||
| export declare namespace TypeGuard { | ||
| /** Returns true if the given schema is TAny */ | ||
| function TAny(schema: unknown): schema is TAny; | ||
| /** Returns true if the given schema is TArray */ | ||
| function TArray(schema: unknown): schema is TArray; | ||
| /** Returns true if the given schema is TBigInt */ | ||
| function TBigInt(schema: unknown): schema is TBigInt; | ||
| /** Returns true if the given schema is TBoolean */ | ||
| function TBoolean(schema: unknown): schema is TBoolean; | ||
| /** Returns true if the given schema is TConstructor */ | ||
| function TConstructor(schema: unknown): schema is TConstructor; | ||
| /** Returns true if the given schema is TDate */ | ||
| function TDate(schema: unknown): schema is TDate; | ||
| /** Returns true if the given schema is TFunction */ | ||
| function TFunction(schema: unknown): schema is TFunction; | ||
| /** Returns true if the given schema is TInteger */ | ||
| function TInteger(schema: unknown): schema is TInteger; | ||
| /** Returns true if the given schema is TIntersect */ | ||
| function TIntersect(schema: unknown): schema is TIntersect; | ||
| /** Returns true if the given schema is TKind */ | ||
| function TKind(schema: unknown): schema is Record<typeof Kind | string, unknown>; | ||
| /** Returns true if the given schema is TLiteral<string> */ | ||
| function TLiteralString(schema: unknown): schema is TLiteral<string>; | ||
| /** Returns true if the given schema is TLiteral<number> */ | ||
| function TLiteralNumber(schema: unknown): schema is TLiteral<number>; | ||
| /** Returns true if the given schema is TLiteral<boolean> */ | ||
| function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>; | ||
| /** Returns true if the given schema is TLiteral */ | ||
| function TLiteral(schema: unknown): schema is TLiteral; | ||
| /** Returns true if the given schema is TNever */ | ||
| function TNever(schema: unknown): schema is TNever; | ||
| /** Returns true if the given schema is TNot */ | ||
| function TNot(schema: unknown): schema is TNot; | ||
| /** Returns true if the given schema is TNull */ | ||
| function TNull(schema: unknown): schema is TNull; | ||
| /** Returns true if the given schema is TNumber */ | ||
| function TNumber(schema: unknown): schema is TNumber; | ||
| /** Returns true if the given schema is TObject */ | ||
| function TObject(schema: unknown): schema is TObject; | ||
| /** Returns true if the given schema is TPromise */ | ||
| function TPromise(schema: unknown): schema is TPromise; | ||
| /** Returns true if the given schema is TRecord */ | ||
| function TRecord(schema: unknown): schema is TRecord; | ||
| /** Returns true if the given schema is TRef */ | ||
| function TRef(schema: unknown): schema is TRef; | ||
| /** Returns true if the given schema is TString */ | ||
| function TString(schema: unknown): schema is TString; | ||
| /** Returns true if the given schema is TSymbol */ | ||
| function TSymbol(schema: unknown): schema is TSymbol; | ||
| /** Returns true if the given schema is TTemplateLiteral */ | ||
| function TTemplateLiteral(schema: unknown): schema is TTemplateLiteral; | ||
| /** Returns true if the given schema is TThis */ | ||
| function TThis(schema: unknown): schema is TThis; | ||
| /** Returns true if the given schema is TTuple */ | ||
| function TTuple(schema: unknown): schema is TTuple; | ||
| /** Returns true if the given schema is TUndefined */ | ||
| function TUndefined(schema: unknown): schema is TUndefined; | ||
| /** Returns true if the given schema is TUnion<Literal<string | number>[]> */ | ||
| function TUnionLiteral(schema: unknown): schema is TUnion<TLiteral[]>; | ||
| /** Returns true if the given schema is TUnion */ | ||
| function TUnion(schema: unknown): schema is TUnion; | ||
| /** Returns true if the given schema is TUint8Array */ | ||
| function TUint8Array(schema: unknown): schema is TUint8Array; | ||
| /** Returns true if the given schema is TUnknown */ | ||
| function TUnknown(schema: unknown): schema is TUnknown; | ||
| /** Returns true if the given schema is a raw TUnsafe */ | ||
| function TUnsafe(schema: unknown): schema is TUnsafe<unknown>; | ||
| /** Returns true if the given schema is TVoid */ | ||
| function TVoid(schema: unknown): schema is TVoid; | ||
| /** Returns true if this schema has the ReadonlyOptional modifier */ | ||
| function TReadonlyOptional<T extends TSchema>(schema: T): schema is TReadonlyOptional<T>; | ||
| /** Returns true if this schema has the Readonly modifier */ | ||
| function TReadonly<T extends TSchema>(schema: T): schema is TReadonly<T>; | ||
| /** Returns true if this schema has the Optional modifier */ | ||
| function TOptional<T extends TSchema>(schema: T): schema is TOptional<T>; | ||
| /** Returns true if the given schema is TSchema */ | ||
| function TSchema(schema: unknown): schema is TSchema; | ||
| } | ||
| /** Fast undefined check used for properties of type undefined */ | ||
| export declare namespace ExtendsUndefined { | ||
| function Check(schema: TSchema): boolean; | ||
| } | ||
| export declare enum TypeExtendsResult { | ||
| Union = 0, | ||
| True = 1, | ||
| False = 2 | ||
| } | ||
| export declare namespace TypeExtends { | ||
| function Extends(left: TSchema, right: TSchema): TypeExtendsResult; | ||
| } | ||
| /** Specialized Clone for Types */ | ||
| export declare namespace TypeClone { | ||
| /** Clones a type. */ | ||
| function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T; | ||
| } | ||
| export declare namespace IndexedAccessor { | ||
| function Resolve(schema: TSchema, keys: Key[], options?: SchemaOptions): TSchema; | ||
| } | ||
| export declare namespace ObjectMap { | ||
| function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T; | ||
| } | ||
| export interface KeyResolverOptions { | ||
| includePatterns: boolean; | ||
| } | ||
| export declare namespace KeyResolver { | ||
| /** Resolves an array of keys in this schema */ | ||
| function ResolveKeys(schema: TSchema, options: KeyResolverOptions): string[]; | ||
| /** Resolves a regular expression pattern matching all keys in this schema */ | ||
| function ResolvePattern(schema: TSchema): string; | ||
| } | ||
| export declare namespace KeyArrayResolver { | ||
| /** Resolves an array of string[] keys from the given schema or array type. */ | ||
| function Resolve(schema: TSchema | string[]): string[]; | ||
| } | ||
| export declare namespace UnionResolver { | ||
| /** Returns a resolved union with interior unions flattened */ | ||
| function Resolve(union: TUnion): TUnion; | ||
| } | ||
| export declare namespace TemplateLiteralPattern { | ||
| function Create(kinds: TTemplateLiteralKind[]): string; | ||
| } | ||
| export declare namespace TemplateLiteralResolver { | ||
| /** Resolves a template literal as a TUnion */ | ||
| function Resolve(template: TTemplateLiteral): TString | TUnion | TLiteral; | ||
| } | ||
| export declare class TemplateLiteralParserError extends Error { | ||
| constructor(message: string); | ||
| } | ||
| export declare namespace TemplateLiteralParser { | ||
| type Expression = And | Or | Const; | ||
| type Const = { | ||
| type: 'const'; | ||
| const: string; | ||
| }; | ||
| type And = { | ||
| type: 'and'; | ||
| expr: Expression[]; | ||
| }; | ||
| type Or = { | ||
| type: 'or'; | ||
| expr: Expression[]; | ||
| }; | ||
| /** Parses a pattern and returns an expression tree */ | ||
| function Parse(pattern: string): Expression; | ||
| /** Parses a pattern and strips forward and trailing ^ and $ */ | ||
| function ParseExact(pattern: string): Expression; | ||
| } | ||
| export declare namespace TemplateLiteralFinite { | ||
| function Check(expression: TemplateLiteralParser.Expression): boolean; | ||
| } | ||
| export declare namespace TemplateLiteralGenerator { | ||
| function Generate(expression: TemplateLiteralParser.Expression): IterableIterator<string>; | ||
| } | ||
| export declare namespace TemplateLiteralDslParser { | ||
| function Parse(template_dsl: string): TTemplateLiteralKind[]; | ||
| } | ||
| export declare class TypeBuilder { | ||
| /** Creates a readonly optional property */ | ||
| ReadonlyOptional<T extends TSchema>(item: T): TReadonlyOptional<T>; | ||
| /** Creates a readonly property */ | ||
| Readonly<T extends TSchema>(item: T): TReadonly<T>; | ||
| /** Creates a optional property */ | ||
| Optional<T extends TSchema>(item: T): TOptional<T>; | ||
| /** Creates a any type */ | ||
| /** `[Utility]` Creates a schema without `static` and `params` types */ | ||
| protected Create<T>(schema: Omit<T, 'static' | 'params'>): T; | ||
| /** `[Standard]` Omits compositing symbols from this schema */ | ||
| Strict<T extends TSchema>(schema: T): T; | ||
| } | ||
| export declare class StandardTypeBuilder extends TypeBuilder { | ||
| /** `[Modifier]` Creates a Optional property */ | ||
| Optional<T extends TSchema>(schema: T): TOptional<T>; | ||
| /** `[Modifier]` Creates a ReadonlyOptional property */ | ||
| ReadonlyOptional<T extends TSchema>(schema: T): TReadonlyOptional<T>; | ||
| /** `[Modifier]` Creates a Readonly object or property */ | ||
| Readonly<T extends TSchema>(schema: T): TReadonly<T>; | ||
| /** `[Standard]` Creates an Any type */ | ||
| Any(options?: SchemaOptions): TAny; | ||
| /** Creates a array type */ | ||
| /** `[Standard]` Creates an Array type */ | ||
| Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>; | ||
| /** Creates a boolean type */ | ||
| /** `[Standard]` Creates a Boolean type */ | ||
| Boolean(options?: SchemaOptions): TBoolean; | ||
| /** Creates a tuple type from this constructors parameters */ | ||
| ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>; | ||
| /** Creates a constructor type */ | ||
| Constructor<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TConstructor<TupleToArray<T>, U>; | ||
| /** Creates a constructor type */ | ||
| Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>; | ||
| /** Creates a enum type */ | ||
| /** `[Standard]` Creates a Composite object type. */ | ||
| Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>; | ||
| /** `[Standard]` Creates a Enum type */ | ||
| Enum<T extends Record<string, string | number>>(item: T, options?: SchemaOptions): TEnum<T>; | ||
| /** Creates a function type */ | ||
| Function<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TFunction<TupleToArray<T>, U>; | ||
| /** Creates a function type */ | ||
| Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>; | ||
| /** Creates a type from this constructors instance type */ | ||
| InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>; | ||
| /** Creates a integer type */ | ||
| Integer(options?: NumericOptions): TInteger; | ||
| /** Creates a intersect type. */ | ||
| Intersect<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TIntersect<T>; | ||
| /** Creates a keyof type */ | ||
| KeyOf<T extends TObject>(object: T, options?: SchemaOptions): TKeyOf<T>; | ||
| /** Creates a literal type. */ | ||
| /** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */ | ||
| Extends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema>(left: L, right: R, trueType: T, falseType: U, options?: SchemaOptions): TExtends<L, R, T, U>; | ||
| /** `[Standard]` Excludes from the left type any type that is not assignable to the right */ | ||
| Exclude<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExclude<L, R>; | ||
| /** `[Standard]` Extracts from the left type any type that is assignable to the right */ | ||
| Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>; | ||
| /** `[Standard]` Returns indexed property types for the given keys */ | ||
| Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndexReduce<T, Assert<K, Key[]>>; | ||
| /** `[Standard]` Returns indexed property types for the given keys */ | ||
| Index<T extends TSchema, K extends TSchema>(schema: T, key: K, options?: SchemaOptions): TIndex<T, K>; | ||
| /** `[Standard]` Creates an Integer type */ | ||
| Integer(options?: NumericOptions<number>): TInteger; | ||
| /** `[Standard]` Creates a Intersect type */ | ||
| Intersect(allOf: [], options?: SchemaOptions): TNever; | ||
| /** `[Standard]` Creates a Intersect type */ | ||
| Intersect<T extends [TSchema]>(allOf: [...T], options?: SchemaOptions): T[0]; | ||
| Intersect<T extends TSchema[]>(allOf: [...T], options?: IntersectOptions): TIntersect<T>; | ||
| /** `[Standard]` Creates a KeyOf type */ | ||
| KeyOf<T extends TSchema>(schema: T, options?: SchemaOptions): TKeyOf<T>; | ||
| /** `[Standard]` Creates a Literal type */ | ||
| Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>; | ||
| /** Creates a never type */ | ||
| /** `[Standard]` Creates a Never type */ | ||
| Never(options?: SchemaOptions): TNever; | ||
| /** Creates a null type */ | ||
| /** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */ | ||
| Not<N extends TSchema, T extends TSchema>(not: N, schema: T, options?: SchemaOptions): TNot<N, T>; | ||
| /** `[Standard]` Creates a Null type */ | ||
| Null(options?: SchemaOptions): TNull; | ||
| /** Creates a number type */ | ||
| Number(options?: NumericOptions): TNumber; | ||
| /** Creates an object type with the given properties */ | ||
| /** `[Standard]` Creates a Number type */ | ||
| Number(options?: NumericOptions<number>): TNumber; | ||
| /** `[Standard]` Creates an Object type */ | ||
| Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>; | ||
| /** Creates a new object whose properties are omitted from the given object */ | ||
| Omit<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TOmit<T, UnionStringLiteralToTuple<K>>; | ||
| /** Creates a new object whose properties are omitted from the given object */ | ||
| Omit<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: readonly [...K], options?: ObjectOptions): TOmit<T, K>; | ||
| /** Creates a tuple type from this functions parameters */ | ||
| /** `[Standard]` Creates a mapped type whose keys are omitted from the given type */ | ||
| Omit<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TOmit<T, K[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are omitted from the given type */ | ||
| Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, TUnionLiteralKeyRest<K>[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are omitted from the given type */ | ||
| Omit<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TOmit<T, K['const']>; | ||
| /** `[Standard]` Creates a mapped type whose keys are omitted from the given type */ | ||
| Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T, TTemplateLiteralKeyRest<K>[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are omitted from the given type */ | ||
| Omit<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TOmit<T, never>; | ||
| /** `[Standard]` Creates a mapped type where all properties are Optional */ | ||
| Partial<T extends TSchema>(schema: T, options?: ObjectOptions): TPartial<T>; | ||
| /** `[Standard]` Creates a mapped type whose keys are picked from the given type */ | ||
| Pick<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TPick<T, K[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are picked from the given type */ | ||
| Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, TUnionLiteralKeyRest<K>[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are picked from the given type */ | ||
| Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>; | ||
| /** `[Standard]` Creates a mapped type whose keys are picked from the given type */ | ||
| Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T, TTemplateLiteralKeyRest<K>[number]>; | ||
| /** `[Standard]` Creates a mapped type whose keys are picked from the given type */ | ||
| Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>; | ||
| /** `[Standard]` Creates a Record type */ | ||
| Record<K extends TUnion, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>; | ||
| /** `[Standard]` Creates a Record type */ | ||
| Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordLiteralType<K, T>; | ||
| /** `[Standard]` Creates a Record type */ | ||
| Record<K extends TTemplateLiteral, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordTemplateLiteralType<K, T>; | ||
| /** `[Standard]` Creates a Record type */ | ||
| Record<K extends TInteger | TNumber, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordNumberType<K, T>; | ||
| /** `[Standard]` Creates a Record type */ | ||
| Record<K extends TString, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordStringType<K, T>; | ||
| /** `[Standard]` Creates a Recursive type */ | ||
| Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>; | ||
| /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */ | ||
| Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>; | ||
| /** `[Standard]` Creates a mapped type where all properties are Required */ | ||
| Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>; | ||
| /** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */ | ||
| Rest<T extends TSchema>(schema: T): TRest<T>; | ||
| /** `[Standard]` Creates a String type */ | ||
| String(options?: StringOptions): TString; | ||
| /** `[Experimental]` Creates a template literal type from dsl string */ | ||
| TemplateLiteral<T extends string>(dsl: T, options?: SchemaOptions): TTemplateLiteralDslParser<T>; | ||
| /** `[Standard]` Creates a template literal type */ | ||
| TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>; | ||
| /** `[Standard]` Creates a Tuple type */ | ||
| Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>; | ||
| /** `[Standard]` Creates a Union type */ | ||
| Union(anyOf: [], options?: SchemaOptions): TNever; | ||
| /** `[Standard]` Creates a Union type */ | ||
| Union<T extends [TSchema]>(anyOf: [...T], options?: SchemaOptions): T[0]; | ||
| /** `[Standard]` Creates a Union type */ | ||
| Union<T extends TSchema[]>(anyOf: [...T], options?: SchemaOptions): TUnion<T>; | ||
| /** `[Experimental]` Remaps a TemplateLiteral into a Union representation. This function is known to cause TS compiler crashes for finite templates with large generation counts. Use with caution. */ | ||
| Union<T extends TTemplateLiteral>(template: T): TUnionTemplateLiteral<T>; | ||
| /** `[Standard]` Creates an Unknown type */ | ||
| Unknown(options?: SchemaOptions): TUnknown; | ||
| /** `[Standard]` Creates a Unsafe type that infers for the generic argument */ | ||
| Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>; | ||
| } | ||
| export declare class ExtendedTypeBuilder extends StandardTypeBuilder { | ||
| /** `[Extended]` Creates a BigInt type */ | ||
| BigInt(options?: NumericOptions<bigint>): TBigInt; | ||
| /** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */ | ||
| ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>; | ||
| /** `[Extended]` Creates a Constructor type */ | ||
| Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>; | ||
| /** `[Extended]` Creates a Date type */ | ||
| Date(options?: DateOptions): TDate; | ||
| /** `[Extended]` Creates a Function type */ | ||
| Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>; | ||
| /** `[Extended]` Extracts the InstanceType from the given Constructor */ | ||
| InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>; | ||
| /** `[Extended]` Extracts the Parameters from the given Function type */ | ||
| Parameters<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TParameters<T>; | ||
| /** Creates an object type whose properties are all optional */ | ||
| Partial<T extends TObject>(schema: T, options?: ObjectOptions): TPartial<T>; | ||
| /** Creates a object whose properties are picked from the given object */ | ||
| Pick<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TPick<T, UnionStringLiteralToTuple<K>>; | ||
| /** Creates a object whose properties are picked from the given object */ | ||
| Pick<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: readonly [...K], options?: ObjectOptions): TPick<T, K>; | ||
| /** Creates a promise type. This type cannot be represented in schema. */ | ||
| /** `[Extended]` Creates a Promise type */ | ||
| Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>; | ||
| /** Creates an object whose properties are derived from the given string literal union. */ | ||
| Record<K extends TUnion<TLiteral[]>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TObject<TRecordProperties<K, T>>; | ||
| /** Creates a record type */ | ||
| Record<K extends TString | TNumeric, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TRecord<K, T>; | ||
| /** Creates a recursive object type */ | ||
| Recursive<T extends TSchema>(callback: (self: TSelf) => T, options?: SchemaOptions): TRecursive<T>; | ||
| /** Creates a reference schema */ | ||
| Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>; | ||
| /** Creates a string type from a regular expression */ | ||
| /** `[Extended]` Creates a regular expression type */ | ||
| RegEx(regex: RegExp, options?: SchemaOptions): TString; | ||
| /** Creates an object type whose properties are all required */ | ||
| Required<T extends TObject>(schema: T, options?: SchemaOptions): TRequired<T>; | ||
| /** Creates a type from this functions return type */ | ||
| /** `[Extended]` Extracts the ReturnType from the given Function */ | ||
| ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>; | ||
| /** Removes Kind and Modifier symbol property keys from this schema */ | ||
| Strict<T extends TSchema>(schema: T): T; | ||
| /** Creates a string type */ | ||
| String<Format extends string>(options?: StringOptions<StringFormatOption | Format>): TString<Format>; | ||
| /** Creates a tuple type */ | ||
| Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>; | ||
| /** Creates a undefined type */ | ||
| /** `[Extended]` Creates a Symbol type */ | ||
| Symbol(options?: SchemaOptions): TSymbol; | ||
| /** `[Extended]` Creates a Undefined type */ | ||
| Undefined(options?: SchemaOptions): TUndefined; | ||
| /** Creates a union type */ | ||
| Union(items: [], options?: SchemaOptions): TNever; | ||
| Union<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TUnion<T>; | ||
| /** Creates a Uint8Array type */ | ||
| /** `[Extended]` Creates a Uint8Array type */ | ||
| Uint8Array(options?: Uint8ArrayOptions): TUint8Array; | ||
| /** Creates an unknown type */ | ||
| Unknown(options?: SchemaOptions): TUnknown; | ||
| /** Creates a user defined schema that infers as type T */ | ||
| Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>; | ||
| /** Creates a void type */ | ||
| /** `[Extended]` Creates a Void type */ | ||
| Void(options?: SchemaOptions): TVoid; | ||
| /** Use this function to return TSchema with static and params omitted */ | ||
| protected Create<T>(schema: Omit<T, 'static' | 'params'>): T; | ||
| /** Clones the given value */ | ||
| protected Clone(value: any): any; | ||
| } | ||
| /** JSON Schema Type Builder with Static Type Resolution for TypeScript */ | ||
| export declare const Type: TypeBuilder; | ||
| /** JSON Schema TypeBuilder with Static Resolution for TypeScript */ | ||
| export declare const StandardType: StandardTypeBuilder; | ||
| /** JSON Schema TypeBuilder with Static Resolution for TypeScript */ | ||
| export declare const Type: ExtendedTypeBuilder; |
+7
-3
| import * as Types from '../typebox'; | ||
| export declare class ValueCastReferenceTypeError extends Error { | ||
| readonly schema: Types.TRef | Types.TSelf; | ||
| constructor(schema: Types.TRef | Types.TSelf); | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
@@ -23,5 +23,9 @@ export declare class ValueCastArrayUniqueItemsTypeError extends Error { | ||
| } | ||
| export declare class ValueCastDereferenceError extends Error { | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
| export declare namespace ValueCast { | ||
| function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any; | ||
| function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>; | ||
| function Cast<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): Types.Static<T>; | ||
| } |
+162
-154
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -31,3 +31,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueCast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0; | ||
| exports.ValueCast = exports.ValueCastDereferenceError = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0; | ||
| const Types = require("../typebox"); | ||
@@ -37,46 +37,5 @@ const create_1 = require("./create"); | ||
| const clone_1 = require("./clone"); | ||
| var UnionValueCast; | ||
| (function (UnionValueCast) { | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // The following will score a schema against a value. For objects, the score is the tally of | ||
| // points awarded for each property of the value. Property points are (1.0 / propertyCount) | ||
| // to prevent large property counts biasing results. Properties that match literal values are | ||
| // maximally awarded as literals are typically used as union discriminator fields. | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function Score(schema, references, value) { | ||
| if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) { | ||
| const object = schema; | ||
| const keys = Object.keys(value); | ||
| const entries = globalThis.Object.entries(object.properties); | ||
| const [point, max] = [1 / entries.length, entries.length]; | ||
| return entries.reduce((acc, [key, schema]) => { | ||
| const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0; | ||
| const checks = check_1.ValueCheck.Check(schema, references, value[key]) ? point : 0; | ||
| const exists = keys.includes(key) ? point : 0; | ||
| return acc + (literal + checks + exists); | ||
| }, 0); | ||
| } | ||
| else { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? 1 : 0; | ||
| } | ||
| } | ||
| function Select(union, references, value) { | ||
| let [select, best] = [union.anyOf[0], 0]; | ||
| for (const schema of union.anyOf) { | ||
| const score = Score(schema, references, value); | ||
| if (score > best) { | ||
| select = schema; | ||
| best = score; | ||
| } | ||
| } | ||
| return select; | ||
| } | ||
| function Create(union, references, value) { | ||
| return check_1.ValueCheck.Check(union, references, value) ? clone_1.ValueClone.Clone(value) : ValueCast.Cast(Select(union, references, value), references, value); | ||
| } | ||
| UnionValueCast.Create = Create; | ||
| })(UnionValueCast || (UnionValueCast = {})); | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Errors | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| class ValueCastReferenceTypeError extends Error { | ||
@@ -118,54 +77,78 @@ constructor(schema) { | ||
| exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError; | ||
| class ValueCastDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`ValueCast: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueCastDereferenceError = ValueCastDereferenceError; | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // The following will score a schema against a value. For objects, the score is the tally of | ||
| // points awarded for each property of the value. Property points are (1.0 / propertyCount) | ||
| // to prevent large property counts biasing results. Properties that match literal values are | ||
| // maximally awarded as literals are typically used as union discriminator fields. | ||
| // ---------------------------------------------------------------------------------------------- | ||
| var UnionCastCreate; | ||
| (function (UnionCastCreate) { | ||
| function Score(schema, references, value) { | ||
| if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) { | ||
| const object = schema; | ||
| const keys = Object.keys(value); | ||
| const entries = globalThis.Object.entries(object.properties); | ||
| const [point, max] = [1 / entries.length, entries.length]; | ||
| return entries.reduce((acc, [key, schema]) => { | ||
| const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0; | ||
| const checks = check_1.ValueCheck.Check(schema, references, value[key]) ? point : 0; | ||
| const exists = keys.includes(key) ? point : 0; | ||
| return acc + (literal + checks + exists); | ||
| }, 0); | ||
| } | ||
| else { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? 1 : 0; | ||
| } | ||
| } | ||
| function Select(union, references, value) { | ||
| let [select, best] = [union.anyOf[0], 0]; | ||
| for (const schema of union.anyOf) { | ||
| const score = Score(schema, references, value); | ||
| if (score > best) { | ||
| select = schema; | ||
| best = score; | ||
| } | ||
| } | ||
| return select; | ||
| } | ||
| function Create(union, references, value) { | ||
| if (union.default !== undefined) { | ||
| return union.default; | ||
| } | ||
| else { | ||
| const schema = Select(union, references, value); | ||
| return ValueCast.Cast(schema, references, value); | ||
| } | ||
| } | ||
| UnionCastCreate.Create = Create; | ||
| })(UnionCastCreate || (UnionCastCreate = {})); | ||
| var ValueCast; | ||
| (function (ValueCast) { | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Guards | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function IsObject(value) { | ||
| return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value); | ||
| } | ||
| function IsArray(value) { | ||
| return typeof value === 'object' && globalThis.Array.isArray(value); | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number' && !isNaN(value); | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsBoolean(value) { | ||
| return typeof value === 'boolean'; | ||
| } | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number'; | ||
| } | ||
| function IsStringNumeric(value) { | ||
| return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value)); | ||
| } | ||
| function IsValueToString(value) { | ||
| return IsBigInt(value) || IsBoolean(value) || IsNumber(value); | ||
| } | ||
| function IsValueTrue(value) { | ||
| return value === true || (IsNumber(value) && value === 1) || (IsBigInt(value) && value === 1n) || (IsString(value) && (value.toLowerCase() === 'true' || value === '1')); | ||
| } | ||
| function IsValueFalse(value) { | ||
| return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0')); | ||
| } | ||
| // ----------------------------------------------------------- | ||
| // Convert | ||
| // ----------------------------------------------------------- | ||
| function TryConvertString(value) { | ||
| return IsValueToString(value) ? value.toString() : value; | ||
| } | ||
| function TryConvertNumber(value) { | ||
| return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : value; | ||
| } | ||
| function TryConvertInteger(value) { | ||
| return IsStringNumeric(value) ? parseInt(value) : IsValueTrue(value) ? 1 : value; | ||
| } | ||
| function TryConvertBoolean(value) { | ||
| return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value; | ||
| } | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| // Cast | ||
| // ----------------------------------------------------------- | ||
| // ---------------------------------------------------------------------------------------------- | ||
| function Any(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
@@ -186,5 +169,7 @@ function Array(schema, references, value) { | ||
| } | ||
| function BigInt(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Boolean(schema, references, value) { | ||
| const conversion = TryConvertBoolean(value); | ||
| return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
@@ -203,4 +188,4 @@ function Constructor(schema, references, value) { | ||
| } | ||
| function Enum(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| function Date(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
@@ -211,5 +196,9 @@ function Function(schema, references, value) { | ||
| function Integer(schema, references, value) { | ||
| const conversion = TryConvertInteger(value); | ||
| return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Intersect(schema, references, value) { | ||
| const created = create_1.ValueCreate.Create(schema, references); | ||
| const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value; | ||
| return check_1.ValueCheck.Check(schema, references, mapped) ? mapped : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Literal(schema, references, value) { | ||
@@ -221,2 +210,5 @@ return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Not(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema.allOf[1], references); | ||
| } | ||
| function Null(schema, references, value) { | ||
@@ -226,8 +218,7 @@ return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| function Number(schema, references, value) { | ||
| const conversion = TryConvertNumber(value); | ||
| return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Object(schema, references, value) { | ||
| if (check_1.ValueCheck.Check(schema, references, value)) | ||
| return clone_1.ValueClone.Clone(value); | ||
| return value; | ||
| if (value === null || typeof value !== 'object') | ||
@@ -244,7 +235,7 @@ return create_1.ValueCreate.Create(schema, references); | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| const propertyKeys = globalThis.Object.keys(schema.properties); | ||
| for (const objectKey of globalThis.Object.keys(value)) { | ||
| if (propertyKeys.includes(objectKey)) | ||
| const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties); | ||
| for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) { | ||
| if (propertyNames.includes(propertyName)) | ||
| continue; | ||
| result[objectKey] = Visit(schema.additionalProperties, references, value[objectKey]); | ||
| result[propertyName] = Visit(schema.additionalProperties, references, value[propertyName]); | ||
| } | ||
@@ -260,6 +251,6 @@ } | ||
| return clone_1.ValueClone.Clone(value); | ||
| if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value)) | ||
| if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value) || value instanceof globalThis.Date) | ||
| return create_1.ValueCreate.Create(schema, references); | ||
| const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0]; | ||
| const subschema = schema.patternProperties[subschemaKey]; | ||
| const subschemaPropertyName = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0]; | ||
| const subschema = schema.patternProperties[subschemaPropertyName]; | ||
| const result = {}; | ||
@@ -271,21 +262,25 @@ for (const [propKey, propValue] of globalThis.Object.entries(value)) { | ||
| } | ||
| function Recursive(schema, references, value) { | ||
| throw new ValueCastRecursiveTypeError(schema); | ||
| } | ||
| function Ref(schema, references, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new ValueCastReferenceTypeError(schema); | ||
| return Visit(reference, references, value); | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueCastDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function Self(schema, references, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new ValueCastReferenceTypeError(schema); | ||
| return Visit(reference, references, value); | ||
| } | ||
| function String(schema, references, value) { | ||
| const conversion = TryConvertString(value); | ||
| return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Symbol(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function TemplateLiteral(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function This(schema, references, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueCastDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function Tuple(schema, references, value) { | ||
@@ -301,70 +296,83 @@ if (check_1.ValueCheck.Check(schema, references, value)) | ||
| function Undefined(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Union(schema, references, value) { | ||
| return UnionValueCast.Create(schema, references, value); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : UnionCastCreate.Create(schema, references, value); | ||
| } | ||
| function Uint8Array(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Unknown(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Void(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references); | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function UserDefined(schema, references, value) { | ||
| return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references); | ||
| } | ||
| function Visit(schema, references, value) { | ||
| const anyReferences = schema.$id === undefined ? references : [schema, ...references]; | ||
| const anySchema = schema; | ||
| const references_ = IsString(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| switch (schema[Types.Kind]) { | ||
| case 'Any': | ||
| return Any(anySchema, anyReferences, value); | ||
| return Any(schema_, references_, value); | ||
| case 'Array': | ||
| return Array(anySchema, anyReferences, value); | ||
| return Array(schema_, references_, value); | ||
| case 'BigInt': | ||
| return BigInt(schema_, references_, value); | ||
| case 'Boolean': | ||
| return Boolean(anySchema, anyReferences, value); | ||
| return Boolean(schema_, references_, value); | ||
| case 'Constructor': | ||
| return Constructor(anySchema, anyReferences, value); | ||
| case 'Enum': | ||
| return Enum(anySchema, anyReferences, value); | ||
| return Constructor(schema_, references_, value); | ||
| case 'Date': | ||
| return Date(schema_, references_, value); | ||
| case 'Function': | ||
| return Function(anySchema, anyReferences, value); | ||
| return Function(schema_, references_, value); | ||
| case 'Integer': | ||
| return Integer(anySchema, anyReferences, value); | ||
| return Integer(schema_, references_, value); | ||
| case 'Intersect': | ||
| return Intersect(schema_, references_, value); | ||
| case 'Literal': | ||
| return Literal(anySchema, anyReferences, value); | ||
| return Literal(schema_, references_, value); | ||
| case 'Never': | ||
| return Never(anySchema, anyReferences, value); | ||
| return Never(schema_, references_, value); | ||
| case 'Not': | ||
| return Not(schema_, references_, value); | ||
| case 'Null': | ||
| return Null(anySchema, anyReferences, value); | ||
| return Null(schema_, references_, value); | ||
| case 'Number': | ||
| return Number(anySchema, anyReferences, value); | ||
| return Number(schema_, references_, value); | ||
| case 'Object': | ||
| return Object(anySchema, anyReferences, value); | ||
| return Object(schema_, references_, value); | ||
| case 'Promise': | ||
| return Promise(anySchema, anyReferences, value); | ||
| return Promise(schema_, references_, value); | ||
| case 'Record': | ||
| return Record(anySchema, anyReferences, value); | ||
| case 'Rec': | ||
| return Recursive(anySchema, anyReferences, value); | ||
| return Record(schema_, references_, value); | ||
| case 'Ref': | ||
| return Ref(anySchema, anyReferences, value); | ||
| case 'Self': | ||
| return Self(anySchema, anyReferences, value); | ||
| return Ref(schema_, references_, value); | ||
| case 'String': | ||
| return String(anySchema, anyReferences, value); | ||
| return String(schema_, references_, value); | ||
| case 'Symbol': | ||
| return Symbol(schema_, references_, value); | ||
| case 'TemplateLiteral': | ||
| return TemplateLiteral(schema_, references_, value); | ||
| case 'This': | ||
| return This(schema_, references_, value); | ||
| case 'Tuple': | ||
| return Tuple(anySchema, anyReferences, value); | ||
| return Tuple(schema_, references_, value); | ||
| case 'Undefined': | ||
| return Undefined(anySchema, anyReferences, value); | ||
| return Undefined(schema_, references_, value); | ||
| case 'Union': | ||
| return Union(anySchema, anyReferences, value); | ||
| return Union(schema_, references_, value); | ||
| case 'Uint8Array': | ||
| return Uint8Array(anySchema, anyReferences, value); | ||
| return Uint8Array(schema_, references_, value); | ||
| case 'Unknown': | ||
| return Unknown(anySchema, anyReferences, value); | ||
| return Unknown(schema_, references_, value); | ||
| case 'Void': | ||
| return Void(anySchema, anyReferences, value); | ||
| return Void(schema_, references_, value); | ||
| default: | ||
| throw new ValueCastUnknownTypeError(anySchema); | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new ValueCastUnknownTypeError(schema_); | ||
| return UserDefined(schema_, references_, value); | ||
| } | ||
@@ -374,5 +382,5 @@ } | ||
| function Cast(schema, references, value) { | ||
| return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value); | ||
| return Visit(schema, references, clone_1.ValueClone.Clone(value)); | ||
| } | ||
| ValueCast.Cast = Cast; | ||
| })(ValueCast = exports.ValueCast || (exports.ValueCast = {})); | ||
| })(ValueCast || (exports.ValueCast = ValueCast = {})); |
+5
-1
@@ -6,4 +6,8 @@ import * as Types from '../typebox'; | ||
| } | ||
| export declare class ValueCheckDereferenceError extends Error { | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
| export declare namespace ValueCheck { | ||
| function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): boolean; | ||
| function Check<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): boolean; | ||
| } |
+273
-112
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -31,8 +31,12 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0; | ||
| exports.ValueCheck = exports.ValueCheckDereferenceError = exports.ValueCheckUnknownTypeError = void 0; | ||
| const Types = require("../typebox"); | ||
| const format_1 = require("../format"); | ||
| const index_1 = require("../system/index"); | ||
| const hash_1 = require("./hash"); | ||
| // ------------------------------------------------------------------------- | ||
| // Errors | ||
| // ------------------------------------------------------------------------- | ||
| class ValueCheckUnknownTypeError extends Error { | ||
| constructor(schema) { | ||
| super('ValueCheck: Unknown type'); | ||
| super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`); | ||
| this.schema = schema; | ||
@@ -42,4 +46,56 @@ } | ||
| exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError; | ||
| class ValueCheckDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`ValueCheck: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueCheckDereferenceError = ValueCheckDereferenceError; | ||
| var ValueCheck; | ||
| (function (ValueCheck) { | ||
| // ---------------------------------------------------------------------- | ||
| // Guards | ||
| // ---------------------------------------------------------------------- | ||
| function IsBigInt(value) { | ||
| return typeof value === 'bigint'; | ||
| } | ||
| function IsInteger(value) { | ||
| return globalThis.Number.isInteger(value); | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsDefined(value) { | ||
| return value !== undefined; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // SchemaGuards | ||
| // ---------------------------------------------------------------------- | ||
| function IsAnyOrUnknown(schema) { | ||
| return schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // Policies | ||
| // ---------------------------------------------------------------------- | ||
| function IsExactOptionalProperty(value, key) { | ||
| return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined; | ||
| } | ||
| function IsObject(value) { | ||
| const result = typeof value === 'object' && value !== null; | ||
| return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value); | ||
| } | ||
| function IsRecordObject(value) { | ||
| return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array); | ||
| } | ||
| function IsNumber(value) { | ||
| const result = typeof value === 'number'; | ||
| return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value); | ||
| } | ||
| function IsVoid(value) { | ||
| const result = value === undefined; | ||
| return index_1.TypeSystem.AllowVoidNull ? result || value === null : result; | ||
| } | ||
| // ---------------------------------------------------------------------- | ||
| // Types | ||
| // ---------------------------------------------------------------------- | ||
| function Any(schema, references, value) { | ||
@@ -52,13 +108,43 @@ return true; | ||
| } | ||
| if (schema.minItems !== undefined && !(value.length >= schema.minItems)) { | ||
| if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) { | ||
| return false; | ||
| } | ||
| if (schema.maxItems !== undefined && !(value.length <= schema.maxItems)) { | ||
| if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) { | ||
| return false; | ||
| } | ||
| if (schema.uniqueItems === true && !(new Set(value).size === value.length)) { | ||
| // prettier-ignore | ||
| if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) { | ||
| const hashed = hash_1.ValueHash.Create(element); | ||
| if (set.has(hashed)) { | ||
| return false; | ||
| } | ||
| else { | ||
| set.add(hashed); | ||
| } | ||
| } return true; })())) { | ||
| return false; | ||
| } | ||
| return value.every((val) => Visit(schema.items, references, val)); | ||
| return value.every((value) => Visit(schema.items, references, value)); | ||
| } | ||
| function BigInt(schema, references, value) { | ||
| if (!IsBigInt(value)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| function Boolean(schema, references, value) { | ||
@@ -70,2 +156,23 @@ return typeof value === 'boolean'; | ||
| } | ||
| function Date(schema, references, value) { | ||
| if (!(value instanceof globalThis.Date)) { | ||
| return false; | ||
| } | ||
| if (!IsNumber(value.getTime())) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) { | ||
| return false; | ||
| } | ||
| if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| function Function(schema, references, value) { | ||
@@ -75,25 +182,38 @@ return typeof value === 'function'; | ||
| function Integer(schema, references, value) { | ||
| if (!(typeof value === 'number')) { | ||
| if (!IsInteger(value)) { | ||
| return false; | ||
| } | ||
| if (!globalThis.Number.isInteger(value)) { | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) { | ||
| return false; | ||
| } | ||
| if (schema.multipleOf !== undefined && !(value % schema.multipleOf === 0)) { | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| return false; | ||
| } | ||
| if (schema.exclusiveMinimum !== undefined && !(value > schema.exclusiveMinimum)) { | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| return false; | ||
| } | ||
| if (schema.exclusiveMaximum !== undefined && !(value < schema.exclusiveMaximum)) { | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| return false; | ||
| } | ||
| if (schema.minimum !== undefined && !(value >= schema.minimum)) { | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| return false; | ||
| } | ||
| if (schema.maximum !== undefined && !(value <= schema.maximum)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| function Intersect(schema, references, value) { | ||
| const check1 = schema.allOf.every((schema) => Visit(schema, references, value)); | ||
| if (schema.unevaluatedProperties === false) { | ||
| const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema)); | ||
| const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key)); | ||
| return check1 && check2; | ||
| } | ||
| else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) { | ||
| const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema)); | ||
| const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key) || Visit(schema.unevaluatedProperties, references, value[key])); | ||
| return check1 && check2; | ||
| } | ||
| else { | ||
| return check1; | ||
| } | ||
| } | ||
| function Literal(schema, references, value) { | ||
@@ -105,2 +225,5 @@ return value === schema.const; | ||
| } | ||
| function Not(schema, references, value) { | ||
| return !Visit(schema.allOf[0].not, references, value) && Visit(schema.allOf[1], references, value); | ||
| } | ||
| function Null(schema, references, value) { | ||
@@ -110,18 +233,18 @@ return value === null; | ||
| function Number(schema, references, value) { | ||
| if (!(typeof value === 'number')) { | ||
| if (!IsNumber(value)) { | ||
| return false; | ||
| } | ||
| if (schema.multipleOf && !(value % schema.multipleOf === 0)) { | ||
| if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) { | ||
| return false; | ||
| } | ||
| if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) { | ||
| if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) { | ||
| return false; | ||
| } | ||
| if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) { | ||
| if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) { | ||
| return false; | ||
| } | ||
| if (schema.minimum && !(value >= schema.minimum)) { | ||
| if (IsDefined(schema.minimum) && !(value >= schema.minimum)) { | ||
| return false; | ||
| } | ||
| if (schema.maximum && !(value <= schema.maximum)) { | ||
| if (IsDefined(schema.maximum) && !(value <= schema.maximum)) { | ||
| return false; | ||
@@ -132,51 +255,45 @@ } | ||
| function Object(schema, references, value) { | ||
| if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) { | ||
| if (!IsObject(value)) { | ||
| return false; | ||
| } | ||
| if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) { | ||
| if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) { | ||
| return false; | ||
| } | ||
| if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) { | ||
| if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) { | ||
| return false; | ||
| } | ||
| const propertyKeys = globalThis.Object.keys(schema.properties); | ||
| if (schema.additionalProperties === false) { | ||
| // optimization: If the property key length matches the required keys length | ||
| // then we only need check that the values property key length matches that | ||
| // of the property key length. This is because exhaustive testing for values | ||
| // will occur in subsequent property tests. | ||
| if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.keys(value).length === propertyKeys.length)) { | ||
| return false; | ||
| } | ||
| else { | ||
| if (!globalThis.Object.keys(value).every((key) => propertyKeys.includes(key))) { | ||
| const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties); | ||
| for (const knownKey of knownKeys) { | ||
| const property = schema.properties[knownKey]; | ||
| if (schema.required && schema.required.includes(knownKey)) { | ||
| if (!Visit(property, references, value[knownKey])) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| for (const objectKey of globalThis.Object.keys(value)) { | ||
| if (propertyKeys.includes(objectKey)) | ||
| continue; | ||
| if (!Visit(schema.additionalProperties, references, value[objectKey])) { | ||
| if ((Types.ExtendsUndefined.Check(property) || IsAnyOrUnknown(property)) && !(knownKey in value)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| for (const propertyKey of propertyKeys) { | ||
| const propertySchema = schema.properties[propertyKey]; | ||
| if (schema.required && schema.required.includes(propertyKey)) { | ||
| if (!Visit(propertySchema, references, value[propertyKey])) { | ||
| else { | ||
| if (IsExactOptionalProperty(value, knownKey) && !Visit(property, references, value[knownKey])) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| if (schema.additionalProperties === false) { | ||
| const valueKeys = globalThis.Object.getOwnPropertyNames(value); | ||
| // optimization: value is valid if schemaKey length matches the valueKey length | ||
| if (schema.required && schema.required.length === knownKeys.length && valueKeys.length === knownKeys.length) { | ||
| return true; | ||
| } | ||
| else { | ||
| if (value[propertyKey] !== undefined) { | ||
| if (!Visit(propertySchema, references, value[propertyKey])) { | ||
| return false; | ||
| } | ||
| } | ||
| return valueKeys.every((valueKey) => knownKeys.includes(valueKey)); | ||
| } | ||
| } | ||
| return true; | ||
| else if (typeof schema.additionalProperties === 'object') { | ||
| const valueKeys = globalThis.Object.getOwnPropertyNames(value); | ||
| return valueKeys.every((key) => knownKeys.includes(key) || Visit(schema.additionalProperties, references, value[key])); | ||
| } | ||
| else { | ||
| return true; | ||
| } | ||
| } | ||
@@ -187,41 +304,46 @@ function Promise(schema, references, value) { | ||
| function Record(schema, references, value) { | ||
| if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) { | ||
| if (!IsRecordObject(value)) { | ||
| return false; | ||
| } | ||
| const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const regex = new RegExp(keyPattern); | ||
| if (!globalThis.Object.keys(value).every((key) => regex.test(key))) { | ||
| if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) { | ||
| return false; | ||
| } | ||
| for (const propValue of globalThis.Object.values(value)) { | ||
| if (!Visit(valueSchema, references, propValue)) | ||
| if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) { | ||
| return false; | ||
| } | ||
| const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| const regex = new RegExp(patternKey); | ||
| return globalThis.Object.entries(value).every(([key, value]) => { | ||
| if (regex.test(key)) { | ||
| return Visit(patternSchema, references, value); | ||
| } | ||
| if (typeof schema.additionalProperties === 'object') { | ||
| return Visit(schema.additionalProperties, references, value); | ||
| } | ||
| if (schema.additionalProperties === false) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| return true; | ||
| }); | ||
| } | ||
| function Ref(schema, references, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueCheck.Ref: Cannot find schema with $id '${schema.$ref}'.`); | ||
| return Visit(reference, references, value); | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueCheckDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function Self(schema, references, value) { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueCheck.Self: Cannot find schema with $id '${schema.$ref}'.`); | ||
| return Visit(reference, references, value); | ||
| } | ||
| function String(schema, references, value) { | ||
| if (!(typeof value === 'string')) { | ||
| if (!IsString(value)) { | ||
| return false; | ||
| } | ||
| if (schema.minLength !== undefined) { | ||
| if (IsDefined(schema.minLength)) { | ||
| if (!(value.length >= schema.minLength)) | ||
| return false; | ||
| } | ||
| if (schema.maxLength !== undefined) { | ||
| if (IsDefined(schema.maxLength)) { | ||
| if (!(value.length <= schema.maxLength)) | ||
| return false; | ||
| } | ||
| if (schema.pattern !== undefined) { | ||
| if (IsDefined(schema.pattern)) { | ||
| const regex = new RegExp(schema.pattern); | ||
@@ -231,6 +353,6 @@ if (!regex.test(value)) | ||
| } | ||
| if (schema.format !== undefined) { | ||
| if (!format_1.Format.Has(schema.format)) | ||
| if (IsDefined(schema.format)) { | ||
| if (!Types.FormatRegistry.Has(schema.format)) | ||
| return false; | ||
| const func = format_1.Format.Get(schema.format); | ||
| const func = Types.FormatRegistry.Get(schema.format); | ||
| return func(value); | ||
@@ -240,2 +362,21 @@ } | ||
| } | ||
| function Symbol(schema, references, value) { | ||
| if (!(typeof value === 'symbol')) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| function TemplateLiteral(schema, references, value) { | ||
| if (!IsString(value)) { | ||
| return false; | ||
| } | ||
| return new RegExp(schema.pattern).test(value); | ||
| } | ||
| function This(schema, references, value) { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$ref); | ||
| if (index === -1) | ||
| throw new ValueCheckDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references, value); | ||
| } | ||
| function Tuple(schema, references, value) { | ||
@@ -270,6 +411,6 @@ if (!globalThis.Array.isArray(value)) { | ||
| } | ||
| if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) { | ||
| if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) { | ||
| return false; | ||
| } | ||
| if (schema.minByteLength && !(value.length >= schema.minByteLength)) { | ||
| if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) { | ||
| return false; | ||
@@ -283,54 +424,74 @@ } | ||
| function Void(schema, references, value) { | ||
| return value === null; | ||
| return IsVoid(value); | ||
| } | ||
| function UserDefined(schema, references, value) { | ||
| if (!Types.TypeRegistry.Has(schema[Types.Kind])) | ||
| return false; | ||
| const func = Types.TypeRegistry.Get(schema[Types.Kind]); | ||
| return func(schema, value); | ||
| } | ||
| function Visit(schema, references, value) { | ||
| const anyReferences = schema.$id === undefined ? references : [schema, ...references]; | ||
| const anySchema = schema; | ||
| switch (anySchema[Types.Kind]) { | ||
| const references_ = IsDefined(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| switch (schema_[Types.Kind]) { | ||
| case 'Any': | ||
| return Any(anySchema, anyReferences, value); | ||
| return Any(schema_, references_, value); | ||
| case 'Array': | ||
| return Array(anySchema, anyReferences, value); | ||
| return Array(schema_, references_, value); | ||
| case 'BigInt': | ||
| return BigInt(schema_, references_, value); | ||
| case 'Boolean': | ||
| return Boolean(anySchema, anyReferences, value); | ||
| return Boolean(schema_, references_, value); | ||
| case 'Constructor': | ||
| return Constructor(anySchema, anyReferences, value); | ||
| return Constructor(schema_, references_, value); | ||
| case 'Date': | ||
| return Date(schema_, references_, value); | ||
| case 'Function': | ||
| return Function(anySchema, anyReferences, value); | ||
| return Function(schema_, references_, value); | ||
| case 'Integer': | ||
| return Integer(anySchema, anyReferences, value); | ||
| return Integer(schema_, references_, value); | ||
| case 'Intersect': | ||
| return Intersect(schema_, references_, value); | ||
| case 'Literal': | ||
| return Literal(anySchema, anyReferences, value); | ||
| return Literal(schema_, references_, value); | ||
| case 'Never': | ||
| return Never(anySchema, anyReferences, value); | ||
| return Never(schema_, references_, value); | ||
| case 'Not': | ||
| return Not(schema_, references_, value); | ||
| case 'Null': | ||
| return Null(anySchema, anyReferences, value); | ||
| return Null(schema_, references_, value); | ||
| case 'Number': | ||
| return Number(anySchema, anyReferences, value); | ||
| return Number(schema_, references_, value); | ||
| case 'Object': | ||
| return Object(anySchema, anyReferences, value); | ||
| return Object(schema_, references_, value); | ||
| case 'Promise': | ||
| return Promise(anySchema, anyReferences, value); | ||
| return Promise(schema_, references_, value); | ||
| case 'Record': | ||
| return Record(anySchema, anyReferences, value); | ||
| return Record(schema_, references_, value); | ||
| case 'Ref': | ||
| return Ref(anySchema, anyReferences, value); | ||
| case 'Self': | ||
| return Self(anySchema, anyReferences, value); | ||
| return Ref(schema_, references_, value); | ||
| case 'String': | ||
| return String(anySchema, anyReferences, value); | ||
| return String(schema_, references_, value); | ||
| case 'Symbol': | ||
| return Symbol(schema_, references_, value); | ||
| case 'TemplateLiteral': | ||
| return TemplateLiteral(schema_, references_, value); | ||
| case 'This': | ||
| return This(schema_, references_, value); | ||
| case 'Tuple': | ||
| return Tuple(anySchema, anyReferences, value); | ||
| return Tuple(schema_, references_, value); | ||
| case 'Undefined': | ||
| return Undefined(anySchema, anyReferences, value); | ||
| return Undefined(schema_, references_, value); | ||
| case 'Union': | ||
| return Union(anySchema, anyReferences, value); | ||
| return Union(schema_, references_, value); | ||
| case 'Uint8Array': | ||
| return Uint8Array(anySchema, anyReferences, value); | ||
| return Uint8Array(schema_, references_, value); | ||
| case 'Unknown': | ||
| return Unknown(anySchema, anyReferences, value); | ||
| return Unknown(schema_, references_, value); | ||
| case 'Void': | ||
| return Void(anySchema, anyReferences, value); | ||
| return Void(schema_, references_, value); | ||
| default: | ||
| throw new ValueCheckUnknownTypeError(anySchema); | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new ValueCheckUnknownTypeError(schema_); | ||
| return UserDefined(schema_, references_, value); | ||
| } | ||
@@ -342,5 +503,5 @@ } | ||
| function Check(schema, references, value) { | ||
| return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value); | ||
| return Visit(schema, references, value); | ||
| } | ||
| ValueCheck.Check = Check; | ||
| })(ValueCheck = exports.ValueCheck || (exports.ValueCheck = {})); | ||
| })(ValueCheck || (exports.ValueCheck = ValueCheck = {})); |
+0
-0
| export declare namespace ValueClone { | ||
| function Clone<T extends unknown>(value: T): T; | ||
| } |
+12
-6
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -35,2 +35,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| (function (ValueClone) { | ||
| function Array(value) { | ||
| return value.map((element) => Clone(element)); | ||
| } | ||
| function Date(value) { | ||
| return new globalThis.Date(value.toISOString()); | ||
| } | ||
| function Object(value) { | ||
@@ -40,5 +46,2 @@ const keys = [...globalThis.Object.keys(value), ...globalThis.Object.getOwnPropertySymbols(value)]; | ||
| } | ||
| function Array(value) { | ||
| return value.map((element) => Clone(element)); | ||
| } | ||
| function TypedArray(value) { | ||
@@ -51,3 +54,6 @@ return value.slice(); | ||
| function Clone(value) { | ||
| if (is_1.Is.Object(value)) { | ||
| if (is_1.Is.Date(value)) { | ||
| return Date(value); | ||
| } | ||
| else if (is_1.Is.Object(value)) { | ||
| return Object(value); | ||
@@ -69,2 +75,2 @@ } | ||
| ValueClone.Clone = Clone; | ||
| })(ValueClone = exports.ValueClone || (exports.ValueClone = {})); | ||
| })(ValueClone || (exports.ValueClone = ValueClone = {})); |
+14
-2
@@ -10,6 +10,18 @@ import * as Types from '../typebox'; | ||
| } | ||
| export declare class ValueCreateIntersectTypeError extends Error { | ||
| readonly schema: Types.TSchema; | ||
| constructor(schema: Types.TSchema); | ||
| } | ||
| export declare class ValueCreateTempateLiteralTypeError extends Error { | ||
| readonly schema: Types.TSchema; | ||
| constructor(schema: Types.TSchema); | ||
| } | ||
| export declare class ValueCreateDereferenceError extends Error { | ||
| readonly schema: Types.TRef | Types.TThis; | ||
| constructor(schema: Types.TRef | Types.TThis); | ||
| } | ||
| export declare namespace ValueCreate { | ||
| /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */ | ||
| function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>; | ||
| function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>; | ||
| function Visit(schema: Types.TSchema, references: Types.TSchema[]): unknown; | ||
| function Create<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>; | ||
| } |
+208
-85
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -31,4 +31,8 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueCreate = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0; | ||
| exports.ValueCreate = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0; | ||
| const Types = require("../typebox"); | ||
| const check_1 = require("./check"); | ||
| // -------------------------------------------------------------------------- | ||
| // Errors | ||
| // -------------------------------------------------------------------------- | ||
| class ValueCreateUnknownTypeError extends Error { | ||
@@ -48,6 +52,39 @@ constructor(schema) { | ||
| exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError; | ||
| class ValueCreateIntersectTypeError extends Error { | ||
| constructor(schema) { | ||
| super('ValueCreate: Intersect produced invalid value. Consider using a default value.'); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError; | ||
| class ValueCreateTempateLiteralTypeError extends Error { | ||
| constructor(schema) { | ||
| super('ValueCreate: Can only create template literal values from patterns that produce finite sequences. Consider using a default value.'); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueCreateTempateLiteralTypeError = ValueCreateTempateLiteralTypeError; | ||
| class ValueCreateDereferenceError extends Error { | ||
| constructor(schema) { | ||
| super(`ValueCreate: Unable to dereference schema with $id '${schema.$ref}'`); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.ValueCreateDereferenceError = ValueCreateDereferenceError; | ||
| // -------------------------------------------------------------------------- | ||
| // ValueCreate | ||
| // -------------------------------------------------------------------------- | ||
| var ValueCreate; | ||
| (function (ValueCreate) { | ||
| // -------------------------------------------------------- | ||
| // Guards | ||
| // -------------------------------------------------------- | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| // -------------------------------------------------------- | ||
| // Types | ||
| // -------------------------------------------------------- | ||
| function Any(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -63,3 +100,3 @@ } | ||
| } | ||
| else if (schema.default !== undefined) { | ||
| else if ('default' in schema) { | ||
| return schema.default; | ||
@@ -76,4 +113,12 @@ } | ||
| } | ||
| function BigInt(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return globalThis.BigInt(0); | ||
| } | ||
| } | ||
| function Boolean(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -86,3 +131,3 @@ } | ||
| function Constructor(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -108,15 +153,15 @@ } | ||
| } | ||
| function Enum(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| function Date(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else if (schema.anyOf.length === 0) { | ||
| throw new Error('ValueCreate.Enum: Cannot create default enum value as this enum has no items'); | ||
| else if (schema.minimumTimestamp !== undefined) { | ||
| return new globalThis.Date(schema.minimumTimestamp); | ||
| } | ||
| else { | ||
| return schema.anyOf[0].const; | ||
| return new globalThis.Date(0); | ||
| } | ||
| } | ||
| function Function(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -129,3 +174,3 @@ } | ||
| function Integer(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -140,4 +185,26 @@ } | ||
| } | ||
| function Intersect(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| // Note: The best we can do here is attempt to instance each sub type and apply through object assign. For non-object | ||
| // sub types, we just escape the assignment and just return the value. In the latter case, this is typically going to | ||
| // be a consequence of an illogical intersection. | ||
| const value = schema.allOf.reduce((acc, schema) => { | ||
| const next = Visit(schema, references); | ||
| return typeof next === 'object' ? { ...acc, ...next } : next; | ||
| }, {}); | ||
| if (!check_1.ValueCheck.Check(schema, references, value)) | ||
| throw new ValueCreateIntersectTypeError(schema); | ||
| return value; | ||
| } | ||
| } | ||
| function Literal(schema, references) { | ||
| return schema.const; | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return schema.const; | ||
| } | ||
| } | ||
@@ -147,7 +214,20 @@ function Never(schema, references) { | ||
| } | ||
| function Not(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return Visit(schema.allOf[1], references); | ||
| } | ||
| } | ||
| function Null(schema, references) { | ||
| return null; | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return null; | ||
| } | ||
| } | ||
| function Number(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -163,3 +243,3 @@ } | ||
| function Object(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -176,3 +256,3 @@ } | ||
| function Promise(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -186,6 +266,6 @@ } | ||
| const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0]; | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) { | ||
| else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) { | ||
| const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|'); | ||
@@ -200,35 +280,17 @@ return propertyKeys.reduce((acc, key) => { | ||
| } | ||
| function Recursive(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| throw new Error('ValueCreate.Recursive: Recursive types require a default value'); | ||
| } | ||
| } | ||
| function Ref(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueCreate.Ref: Cannot find schema with $id '${schema.$ref}'.`); | ||
| return Visit(reference, references); | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$id); | ||
| if (index === -1) | ||
| throw new ValueCreateDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references); | ||
| } | ||
| } | ||
| function Self(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| const reference = references.find((reference) => reference.$id === schema.$ref); | ||
| if (reference === undefined) | ||
| throw new Error(`ValueCreate.Self: Cannot locate schema with $id '${schema.$ref}'`); | ||
| return Visit(reference, references); | ||
| } | ||
| } | ||
| function String(schema, references) { | ||
| if (schema.pattern !== undefined) { | ||
| if (schema.default === undefined) { | ||
| if (!('default' in schema)) { | ||
| throw new Error('ValueCreate.String: String types with patterns must specify a default value'); | ||
@@ -241,3 +303,3 @@ } | ||
| else if (schema.format !== undefined) { | ||
| if (schema.default === undefined) { | ||
| if (!('default' in schema)) { | ||
| throw new Error('ValueCreate.String: String types with formats must specify a default value'); | ||
@@ -250,3 +312,3 @@ } | ||
| else { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -264,4 +326,37 @@ } | ||
| } | ||
| function Symbol(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else if ('value' in schema) { | ||
| return globalThis.Symbol.for(schema.value); | ||
| } | ||
| else { | ||
| return globalThis.Symbol(); | ||
| } | ||
| } | ||
| function TemplateLiteral(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| const expression = Types.TemplateLiteralParser.ParseExact(schema.pattern); | ||
| if (!Types.TemplateLiteralFinite.Check(expression)) | ||
| throw new ValueCreateTempateLiteralTypeError(schema); | ||
| const sequence = Types.TemplateLiteralGenerator.Generate(expression); | ||
| return sequence.next().value; | ||
| } | ||
| function This(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| const index = references.findIndex((foreign) => foreign.$id === schema.$id); | ||
| if (index === -1) | ||
| throw new ValueCreateDereferenceError(schema); | ||
| const target = references[index]; | ||
| return Visit(target, references); | ||
| } | ||
| } | ||
| function Tuple(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -277,6 +372,11 @@ } | ||
| function Undefined(schema, references) { | ||
| return undefined; | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return undefined; | ||
| } | ||
| } | ||
| function Union(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -292,3 +392,3 @@ } | ||
| function Uint8Array(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -304,3 +404,3 @@ } | ||
| function Unknown(schema, references) { | ||
| if (schema.default !== undefined) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
@@ -313,59 +413,82 @@ } | ||
| function Void(schema, references) { | ||
| return null; | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| return void 0; | ||
| } | ||
| } | ||
| function UserDefined(schema, references) { | ||
| if ('default' in schema) { | ||
| return schema.default; | ||
| } | ||
| else { | ||
| throw new Error('ValueCreate.UserDefined: User defined types must specify a default value'); | ||
| } | ||
| } | ||
| /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */ | ||
| function Visit(schema, references) { | ||
| const anyReferences = schema.$id === undefined ? references : [schema, ...references]; | ||
| const anySchema = schema; | ||
| switch (anySchema[Types.Kind]) { | ||
| const references_ = IsString(schema.$id) ? [...references, schema] : references; | ||
| const schema_ = schema; | ||
| switch (schema_[Types.Kind]) { | ||
| case 'Any': | ||
| return Any(anySchema, anyReferences); | ||
| return Any(schema_, references_); | ||
| case 'Array': | ||
| return Array(anySchema, anyReferences); | ||
| return Array(schema_, references_); | ||
| case 'BigInt': | ||
| return BigInt(schema_, references_); | ||
| case 'Boolean': | ||
| return Boolean(anySchema, anyReferences); | ||
| return Boolean(schema_, references_); | ||
| case 'Constructor': | ||
| return Constructor(anySchema, anyReferences); | ||
| case 'Enum': | ||
| return Enum(anySchema, anyReferences); | ||
| return Constructor(schema_, references_); | ||
| case 'Date': | ||
| return Date(schema_, references_); | ||
| case 'Function': | ||
| return Function(anySchema, anyReferences); | ||
| return Function(schema_, references_); | ||
| case 'Integer': | ||
| return Integer(anySchema, anyReferences); | ||
| return Integer(schema_, references_); | ||
| case 'Intersect': | ||
| return Intersect(schema_, references_); | ||
| case 'Literal': | ||
| return Literal(anySchema, anyReferences); | ||
| return Literal(schema_, references_); | ||
| case 'Never': | ||
| return Never(anySchema, anyReferences); | ||
| return Never(schema_, references_); | ||
| case 'Not': | ||
| return Not(schema_, references_); | ||
| case 'Null': | ||
| return Null(anySchema, anyReferences); | ||
| return Null(schema_, references_); | ||
| case 'Number': | ||
| return Number(anySchema, anyReferences); | ||
| return Number(schema_, references_); | ||
| case 'Object': | ||
| return Object(anySchema, anyReferences); | ||
| return Object(schema_, references_); | ||
| case 'Promise': | ||
| return Promise(anySchema, anyReferences); | ||
| return Promise(schema_, references_); | ||
| case 'Record': | ||
| return Record(anySchema, anyReferences); | ||
| case 'Rec': | ||
| return Recursive(anySchema, anyReferences); | ||
| return Record(schema_, references_); | ||
| case 'Ref': | ||
| return Ref(anySchema, anyReferences); | ||
| case 'Self': | ||
| return Self(anySchema, anyReferences); | ||
| return Ref(schema_, references_); | ||
| case 'String': | ||
| return String(anySchema, anyReferences); | ||
| return String(schema_, references_); | ||
| case 'Symbol': | ||
| return Symbol(schema_, references_); | ||
| case 'TemplateLiteral': | ||
| return TemplateLiteral(schema_, references_); | ||
| case 'This': | ||
| return This(schema_, references_); | ||
| case 'Tuple': | ||
| return Tuple(anySchema, anyReferences); | ||
| return Tuple(schema_, references_); | ||
| case 'Undefined': | ||
| return Undefined(anySchema, anyReferences); | ||
| return Undefined(schema_, references_); | ||
| case 'Union': | ||
| return Union(anySchema, anyReferences); | ||
| return Union(schema_, references_); | ||
| case 'Uint8Array': | ||
| return Uint8Array(anySchema, anyReferences); | ||
| return Uint8Array(schema_, references_); | ||
| case 'Unknown': | ||
| return Unknown(anySchema, anyReferences); | ||
| return Unknown(schema_, references_); | ||
| case 'Void': | ||
| return Void(anySchema, anyReferences); | ||
| return Void(schema_, references_); | ||
| default: | ||
| throw new ValueCreateUnknownTypeError(anySchema); | ||
| if (!Types.TypeRegistry.Has(schema_[Types.Kind])) | ||
| throw new ValueCreateUnknownTypeError(schema_); | ||
| return UserDefined(schema_, references_); | ||
| } | ||
@@ -378,2 +501,2 @@ } | ||
| ValueCreate.Create = Create; | ||
| })(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {})); | ||
| })(ValueCreate || (exports.ValueCreate = ValueCreate = {})); |
+39
-18
@@ -1,22 +0,43 @@ | ||
| export declare type Edit<T = unknown> = Insert<T> | Update<T> | Delete<T>; | ||
| export interface Insert<T> { | ||
| brand: T; | ||
| type: 'insert'; | ||
| path: string; | ||
| value: any; | ||
| import { Static } from '../typebox'; | ||
| export type Insert = Static<typeof Insert>; | ||
| export declare const Insert: import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"insert">; | ||
| path: import("../typebox").TString; | ||
| value: import("../typebox").TUnknown; | ||
| }>; | ||
| export type Update = Static<typeof Update>; | ||
| export declare const Update: import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"update">; | ||
| path: import("../typebox").TString; | ||
| value: import("../typebox").TUnknown; | ||
| }>; | ||
| export type Delete = Static<typeof Delete>; | ||
| export declare const Delete: import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"delete">; | ||
| path: import("../typebox").TString; | ||
| }>; | ||
| export type Edit = Static<typeof Edit>; | ||
| export declare const Edit: import("../typebox").TUnion<[import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"insert">; | ||
| path: import("../typebox").TString; | ||
| value: import("../typebox").TUnknown; | ||
| }>, import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"update">; | ||
| path: import("../typebox").TString; | ||
| value: import("../typebox").TUnknown; | ||
| }>, import("../typebox").TObject<{ | ||
| type: import("../typebox").TLiteral<"delete">; | ||
| path: import("../typebox").TString; | ||
| }>]>; | ||
| export declare class ValueDeltaObjectWithSymbolKeyError extends Error { | ||
| readonly key: unknown; | ||
| constructor(key: unknown); | ||
| } | ||
| export interface Update<T> { | ||
| brand: T; | ||
| type: 'update'; | ||
| path: string; | ||
| value: any; | ||
| export declare class ValueDeltaUnableToDiffUnknownValue extends Error { | ||
| readonly value: unknown; | ||
| constructor(value: unknown); | ||
| } | ||
| export interface Delete<T> { | ||
| brand: T; | ||
| type: 'delete'; | ||
| path: string; | ||
| } | ||
| export declare namespace ValueDelta { | ||
| function Diff<T>(current: T, next: T): Edit<T>[]; | ||
| function Patch<T>(current: T, edits: Edit<T>[]): T; | ||
| function Diff(current: unknown, next: unknown): Edit[]; | ||
| function Patch<T = any>(current: unknown, edits: Edit[]): T; | ||
| } |
+44
-8
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -31,6 +31,42 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueDelta = void 0; | ||
| exports.ValueDelta = exports.ValueDeltaUnableToDiffUnknownValue = exports.ValueDeltaObjectWithSymbolKeyError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0; | ||
| const typebox_1 = require("../typebox"); | ||
| const is_1 = require("./is"); | ||
| const clone_1 = require("./clone"); | ||
| const pointer_1 = require("./pointer"); | ||
| exports.Insert = typebox_1.Type.Object({ | ||
| type: typebox_1.Type.Literal('insert'), | ||
| path: typebox_1.Type.String(), | ||
| value: typebox_1.Type.Unknown(), | ||
| }); | ||
| exports.Update = typebox_1.Type.Object({ | ||
| type: typebox_1.Type.Literal('update'), | ||
| path: typebox_1.Type.String(), | ||
| value: typebox_1.Type.Unknown(), | ||
| }); | ||
| exports.Delete = typebox_1.Type.Object({ | ||
| type: typebox_1.Type.Literal('delete'), | ||
| path: typebox_1.Type.String(), | ||
| }); | ||
| exports.Edit = typebox_1.Type.Union([exports.Insert, exports.Update, exports.Delete]); | ||
| // --------------------------------------------------------------------- | ||
| // Errors | ||
| // --------------------------------------------------------------------- | ||
| class ValueDeltaObjectWithSymbolKeyError extends Error { | ||
| constructor(key) { | ||
| super('ValueDelta: Cannot diff objects with symbol keys'); | ||
| this.key = key; | ||
| } | ||
| } | ||
| exports.ValueDeltaObjectWithSymbolKeyError = ValueDeltaObjectWithSymbolKeyError; | ||
| class ValueDeltaUnableToDiffUnknownValue extends Error { | ||
| constructor(value) { | ||
| super('ValueDelta: Unable to create diff edits for unknown value'); | ||
| this.value = value; | ||
| } | ||
| } | ||
| exports.ValueDeltaUnableToDiffUnknownValue = ValueDeltaUnableToDiffUnknownValue; | ||
| // --------------------------------------------------------------------- | ||
| // ValueDelta | ||
| // --------------------------------------------------------------------- | ||
| var ValueDelta; | ||
@@ -60,3 +96,3 @@ (function (ValueDelta) { | ||
| if (typeof key === 'symbol') | ||
| throw Error('ValueDelta: Cannot produce diff symbol keys'); | ||
| throw new ValueDeltaObjectWithSymbolKeyError(key); | ||
| if (next[key] === undefined && nextKeys.includes(key)) | ||
@@ -69,3 +105,3 @@ yield Update(`${path}/${String(key)}`, undefined); | ||
| if (typeof key === 'symbol') | ||
| throw Error('ValueDelta: Cannot produce diff symbol keys'); | ||
| throw new ValueDeltaObjectWithSymbolKeyError(key); | ||
| yield* Visit(`${path}/${String(key)}`, current[key], next[key]); | ||
@@ -75,3 +111,3 @@ } | ||
| if (typeof key === 'symbol') | ||
| throw Error('ValueDelta: Cannot produce diff symbol keys'); | ||
| throw new ValueDeltaObjectWithSymbolKeyError(key); | ||
| if (current[key] === undefined) | ||
@@ -82,3 +118,3 @@ yield Insert(`${path}/${String(key)}`, next[key]); | ||
| if (typeof key === 'symbol') | ||
| throw Error('ValueDelta: Cannot produce diff symbol keys'); | ||
| throw new ValueDeltaObjectWithSymbolKeyError(key); | ||
| if (next[key] === undefined && !nextKeys.includes(key)) | ||
@@ -131,3 +167,3 @@ yield Delete(`${path}/${String(key)}`); | ||
| else { | ||
| throw new Error('ValueDelta: Cannot produce edits for value'); | ||
| throw new ValueDeltaUnableToDiffUnknownValue(current); | ||
| } | ||
@@ -175,2 +211,2 @@ } | ||
| ValueDelta.Patch = Patch; | ||
| })(ValueDelta = exports.ValueDelta || (exports.ValueDelta = {})); | ||
| })(ValueDelta || (exports.ValueDelta = ValueDelta = {})); |
+0
-0
| export declare namespace ValueEqual { | ||
| function Equal<T>(left: T, right: unknown): right is T; | ||
| } |
+8
-2
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -44,2 +44,5 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| } | ||
| function Date(left, right) { | ||
| return is_1.Is.Date(right) && left.getTime() === right.getTime(); | ||
| } | ||
| function Array(left, right) { | ||
@@ -62,2 +65,5 @@ if (!is_1.Is.Array(right) || left.length !== right.length) | ||
| } | ||
| else if (is_1.Is.Date(left)) { | ||
| return Date(left, right); | ||
| } | ||
| else if (is_1.Is.TypedArray(left)) { | ||
@@ -77,2 +83,2 @@ return TypedArray(left, right); | ||
| ValueEqual.Equal = Equal; | ||
| })(ValueEqual = exports.ValueEqual || (exports.ValueEqual = {})); | ||
| })(ValueEqual || (exports.ValueEqual = ValueEqual = {})); |
+4
-1
@@ -1,3 +0,6 @@ | ||
| export { ValueError, ValueErrorType } from '../errors/index'; | ||
| export { ValueError, ValueErrorIterator, ValueErrorType } from '../errors/index'; | ||
| export { ValueHash } from './hash'; | ||
| export { Edit, Insert, Update, Delete } from './delta'; | ||
| export { Mutable } from './mutate'; | ||
| export * from './pointer'; | ||
| export * from './value'; |
+10
-2
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -45,6 +45,14 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ValueErrorType = void 0; | ||
| exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueHash = exports.ValueErrorType = exports.ValueErrorIterator = void 0; | ||
| var index_1 = require("../errors/index"); | ||
| Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return index_1.ValueErrorIterator; } }); | ||
| Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } }); | ||
| var hash_1 = require("./hash"); | ||
| Object.defineProperty(exports, "ValueHash", { enumerable: true, get: function () { return hash_1.ValueHash; } }); | ||
| var delta_1 = require("./delta"); | ||
| Object.defineProperty(exports, "Edit", { enumerable: true, get: function () { return delta_1.Edit; } }); | ||
| Object.defineProperty(exports, "Insert", { enumerable: true, get: function () { return delta_1.Insert; } }); | ||
| Object.defineProperty(exports, "Update", { enumerable: true, get: function () { return delta_1.Update; } }); | ||
| Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return delta_1.Delete; } }); | ||
| __exportStar(require("./pointer"), exports); | ||
| __exportStar(require("./value"), exports); |
+5
-4
@@ -1,7 +0,8 @@ | ||
| export declare type ValueType = null | undefined | Function | symbol | bigint | number | boolean | string; | ||
| export declare type ObjectType = Record<string | number | symbol, unknown>; | ||
| export declare type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; | ||
| export declare type ArrayType = unknown[]; | ||
| export type ValueType = null | undefined | Function | symbol | bigint | number | boolean | string; | ||
| export type ObjectType = Record<string | number | symbol, unknown>; | ||
| export type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; | ||
| export type ArrayType = unknown[]; | ||
| export declare namespace Is { | ||
| function Object(value: unknown): value is ObjectType; | ||
| function Date(value: unknown): value is Date; | ||
| function Array(value: unknown): value is ArrayType; | ||
@@ -8,0 +9,0 @@ function Value(value: unknown): value is ValueType; |
+7
-3
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -35,5 +35,9 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| function Object(value) { | ||
| return value !== null && typeof value === 'object' && !globalThis.Array.isArray(value) && !ArrayBuffer.isView(value); | ||
| return value !== null && typeof value === 'object' && !globalThis.Array.isArray(value) && !ArrayBuffer.isView(value) && !(value instanceof globalThis.Date); | ||
| } | ||
| Is.Object = Object; | ||
| function Date(value) { | ||
| return value instanceof globalThis.Date; | ||
| } | ||
| Is.Date = Date; | ||
| function Array(value) { | ||
@@ -51,2 +55,2 @@ return globalThis.Array.isArray(value) && !ArrayBuffer.isView(value); | ||
| Is.TypedArray = TypedArray; | ||
| })(Is = exports.Is || (exports.Is = {})); | ||
| })(Is || (exports.Is = Is = {})); |
@@ -12,3 +12,3 @@ export declare class ValuePointerRootSetError extends Error { | ||
| } | ||
| /** ValuePointer performs mutable operations on values using RFC6901 Json Pointers */ | ||
| /** Provides functionality to update values through RFC6901 string pointers */ | ||
| export declare namespace ValuePointer { | ||
@@ -15,0 +15,0 @@ /** Formats the given pointer into navigable key components */ |
+3
-3
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -49,3 +49,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| exports.ValuePointerRootDeleteError = ValuePointerRootDeleteError; | ||
| /** ValuePointer performs mutable operations on values using RFC6901 Json Pointers */ | ||
| /** Provides functionality to update values through RFC6901 string pointers */ | ||
| var ValuePointer; | ||
@@ -144,2 +144,2 @@ (function (ValuePointer) { | ||
| ValuePointer.Get = Get; | ||
| })(ValuePointer = exports.ValuePointer || (exports.ValuePointer = {})); | ||
| })(ValuePointer || (exports.ValuePointer = ValuePointer = {})); |
+19
-11
| import * as Types from '../typebox'; | ||
| import { ValueError } from '../errors/index'; | ||
| import { ValueErrorIterator } from '../errors/index'; | ||
| import { Mutable } from './mutate'; | ||
| import { Edit } from './delta'; | ||
| export type { Edit } from './delta'; | ||
| /** Value performs immutable operations on values */ | ||
| /** Provides functions to perform structural updates to JavaScript values */ | ||
| export declare namespace Value { | ||
| /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */ | ||
| /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */ | ||
| function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>; | ||
| /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */ | ||
| /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */ | ||
| function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>; | ||
@@ -19,14 +19,22 @@ /** Creates a value from the given type */ | ||
| function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>; | ||
| /** Converts any type mismatched values to their target type if a conversion is possible. */ | ||
| function Convert<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): unknown; | ||
| /** Converts any type mismatched values to their target type if a conversion is possible. */ | ||
| function Convert<T extends Types.TSchema>(schema: T, value: unknown): unknown; | ||
| /** Returns a structural clone of the given value */ | ||
| function Clone<T>(value: T): T; | ||
| /** Returns an iterator for each error in this value. */ | ||
| function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): IterableIterator<ValueError>; | ||
| function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): ValueErrorIterator; | ||
| /** Returns an iterator for each error in this value. */ | ||
| function Errors<T extends Types.TSchema>(schema: T, value: unknown): IterableIterator<ValueError>; | ||
| function Errors<T extends Types.TSchema>(schema: T, value: unknown): ValueErrorIterator; | ||
| /** Returns true if left and right values are structurally equal */ | ||
| function Equal<T>(left: T, right: unknown): right is T; | ||
| /** Returns a structural clone of the given value */ | ||
| function Clone<T>(value: T): T; | ||
| /** Returns edits to transform the current value into the next value */ | ||
| function Diff<T>(current: T, next: T): Edit<T>[]; | ||
| function Diff(current: unknown, next: unknown): Edit[]; | ||
| /** Returns a FNV1A-64 non cryptographic hash of the given value */ | ||
| function Hash(value: unknown): bigint; | ||
| /** Returns a new value with edits applied to the given value */ | ||
| function Patch<T>(current: T, edits: Edit<T>[]): T; | ||
| function Patch<T = any>(current: unknown, edits: Edit[]): T; | ||
| /** Performs a deep mutable value assignment while retaining internal references. */ | ||
| function Mutate(current: Mutable, next: Mutable): void; | ||
| } |
+28
-10
@@ -8,3 +8,3 @@ "use strict"; | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
@@ -33,9 +33,12 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| const index_1 = require("../errors/index"); | ||
| const mutate_1 = require("./mutate"); | ||
| const hash_1 = require("./hash"); | ||
| const equal_1 = require("./equal"); | ||
| const cast_1 = require("./cast"); | ||
| const clone_1 = require("./clone"); | ||
| const convert_1 = require("./convert"); | ||
| const create_1 = require("./create"); | ||
| const check_1 = require("./check"); | ||
| const delta_1 = require("./delta"); | ||
| /** Value performs immutable operations on values */ | ||
| /** Provides functions to perform structural updates to JavaScript values */ | ||
| var Value; | ||
@@ -58,6 +61,16 @@ (function (Value) { | ||
| Value.Check = Check; | ||
| function* Errors(...args) { | ||
| function Convert(...args) { | ||
| const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]]; | ||
| yield* index_1.ValueErrors.Errors(schema, references, value); | ||
| return convert_1.ValueConvert.Convert(schema, references, value); | ||
| } | ||
| Value.Convert = Convert; | ||
| /** Returns a structural clone of the given value */ | ||
| function Clone(value) { | ||
| return clone_1.ValueClone.Clone(value); | ||
| } | ||
| Value.Clone = Clone; | ||
| function Errors(...args) { | ||
| const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]]; | ||
| return index_1.ValueErrors.Errors(schema, references, value); | ||
| } | ||
| Value.Errors = Errors; | ||
@@ -69,7 +82,2 @@ /** Returns true if left and right values are structurally equal */ | ||
| Value.Equal = Equal; | ||
| /** Returns a structural clone of the given value */ | ||
| function Clone(value) { | ||
| return clone_1.ValueClone.Clone(value); | ||
| } | ||
| Value.Clone = Clone; | ||
| /** Returns edits to transform the current value into the next value */ | ||
@@ -80,2 +88,7 @@ function Diff(current, next) { | ||
| Value.Diff = Diff; | ||
| /** Returns a FNV1A-64 non cryptographic hash of the given value */ | ||
| function Hash(value) { | ||
| return hash_1.ValueHash.Create(value); | ||
| } | ||
| Value.Hash = Hash; | ||
| /** Returns a new value with edits applied to the given value */ | ||
@@ -86,2 +99,7 @@ function Patch(current, edits) { | ||
| Value.Patch = Patch; | ||
| })(Value = exports.Value || (exports.Value = {})); | ||
| /** Performs a deep mutable value assignment while retaining internal references. */ | ||
| function Mutate(current, next) { | ||
| mutate_1.ValueMutate.Mutate(current, next); | ||
| } | ||
| Value.Mutate = Mutate; | ||
| })(Value || (exports.Value = Value = {})); |
| import * as Types from '../typebox'; | ||
| export declare type TExtends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema> = Types.Static<L> extends Types.Static<R> ? T : U; | ||
| export interface TExclude<T extends Types.TUnion, U extends Types.TUnion> extends Types.TUnion<any[]> { | ||
| static: Exclude<Types.Static<T, this['params']>, Types.Static<U, this['params']>>; | ||
| } | ||
| export interface TExtract<T extends Types.TSchema, U extends Types.TUnion> extends Types.TUnion<any[]> { | ||
| static: Extract<Types.Static<T, this['params']>, Types.Static<U, this['params']>>; | ||
| } | ||
| /** Conditional Types */ | ||
| export declare namespace Conditional { | ||
| /** (Experimental) Creates a conditional expression type */ | ||
| function Extends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema>(left: L, right: R, ok: T, fail: U): TExtends<L, R, T, U>; | ||
| /** (Experimental) Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers. */ | ||
| function Exclude<T extends Types.TUnion, U extends Types.TUnion>(unionType: T, excludedMembers: U, options?: Types.SchemaOptions): TExclude<T, U>; | ||
| /** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */ | ||
| function Extract<T extends Types.TSchema, U extends Types.TUnion>(type: T, union: U, options?: Types.SchemaOptions): TExtract<T, U>; | ||
| } |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/conditional | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Conditional = void 0; | ||
| const Types = require("../typebox"); | ||
| const structural_1 = require("./structural"); | ||
| const index_1 = require("../guard/index"); | ||
| /** Conditional Types */ | ||
| var Conditional; | ||
| (function (Conditional) { | ||
| /** (Experimental) Creates a conditional expression type */ | ||
| function Extends(left, right, ok, fail) { | ||
| switch (structural_1.Structural.Check(left, right)) { | ||
| case structural_1.StructuralResult.Union: | ||
| return Types.Type.Union([Clone(ok), Clone(fail)]); | ||
| case structural_1.StructuralResult.True: | ||
| return Clone(ok); | ||
| case structural_1.StructuralResult.False: | ||
| return Clone(fail); | ||
| } | ||
| } | ||
| Conditional.Extends = Extends; | ||
| /** (Experimental) Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers. */ | ||
| function Exclude(unionType, excludedMembers, options = {}) { | ||
| const anyOf = unionType.anyOf | ||
| .filter((schema) => { | ||
| const check = structural_1.Structural.Check(schema, excludedMembers); | ||
| return !(check === structural_1.StructuralResult.True || check === structural_1.StructuralResult.Union); | ||
| }) | ||
| .map((schema) => Clone(schema)); | ||
| return { ...options, [Types.Kind]: 'Union', anyOf }; | ||
| } | ||
| Conditional.Exclude = Exclude; | ||
| /** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */ | ||
| function Extract(type, union, options = {}) { | ||
| if (index_1.TypeGuard.TUnion(type)) { | ||
| const anyOf = type.anyOf.filter((schema) => structural_1.Structural.Check(schema, union) === structural_1.StructuralResult.True).map((schema) => Clone(schema)); | ||
| return { ...options, [Types.Kind]: 'Union', anyOf }; | ||
| } | ||
| else { | ||
| const anyOf = union.anyOf.filter((schema) => structural_1.Structural.Check(type, schema) === structural_1.StructuralResult.True).map((schema) => Clone(schema)); | ||
| return { ...options, [Types.Kind]: 'Union', anyOf }; | ||
| } | ||
| } | ||
| Conditional.Extract = Extract; | ||
| function Clone(value) { | ||
| const isObject = (object) => typeof object === 'object' && object !== null && !Array.isArray(object); | ||
| const isArray = (object) => typeof object === 'object' && object !== null && Array.isArray(object); | ||
| if (isObject(value)) { | ||
| return Object.keys(value).reduce((acc, key) => ({ | ||
| ...acc, | ||
| [key]: Clone(value[key]), | ||
| }), Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ | ||
| ...acc, | ||
| [key]: Clone(value[key]), | ||
| }), {})); | ||
| } | ||
| else if (isArray(value)) { | ||
| return value.map((item) => Clone(item)); | ||
| } | ||
| else { | ||
| return value; | ||
| } | ||
| } | ||
| })(Conditional = exports.Conditional || (exports.Conditional = {})); |
| export * from './conditional'; | ||
| export * from './structural'; |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/conditional | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./conditional"), exports); | ||
| __exportStar(require("./structural"), exports); |
| import * as Types from '../typebox'; | ||
| export declare enum StructuralResult { | ||
| Union = 0, | ||
| True = 1, | ||
| False = 2 | ||
| } | ||
| /** Performs structural equivalence checks against TypeBox types. */ | ||
| export declare namespace Structural { | ||
| /** Structurally tests if the left schema extends the right. */ | ||
| function Check(left: Types.TSchema, right: Types.TSchema): StructuralResult; | ||
| } |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/conditional | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Structural = exports.StructuralResult = void 0; | ||
| const Types = require("../typebox"); | ||
| const guard_1 = require("../guard"); | ||
| // -------------------------------------------------------------------------- | ||
| // StructuralResult | ||
| // -------------------------------------------------------------------------- | ||
| var StructuralResult; | ||
| (function (StructuralResult) { | ||
| StructuralResult[StructuralResult["Union"] = 0] = "Union"; | ||
| StructuralResult[StructuralResult["True"] = 1] = "True"; | ||
| StructuralResult[StructuralResult["False"] = 2] = "False"; | ||
| })(StructuralResult = exports.StructuralResult || (exports.StructuralResult = {})); | ||
| // -------------------------------------------------------------------------- | ||
| // Structural | ||
| // -------------------------------------------------------------------------- | ||
| /** Performs structural equivalence checks against TypeBox types. */ | ||
| var Structural; | ||
| (function (Structural) { | ||
| const referenceMap = new Map(); | ||
| // ------------------------------------------------------------------------ | ||
| // Rules | ||
| // ------------------------------------------------------------------------ | ||
| function AnyOrUnknownRule(right) { | ||
| // https://github.com/microsoft/TypeScript/issues/40049 | ||
| if (right[Types.Kind] === 'Union' && right.anyOf.some((schema) => schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown')) | ||
| return true; | ||
| if (right[Types.Kind] === 'Unknown') | ||
| return true; | ||
| if (right[Types.Kind] === 'Any') | ||
| return true; | ||
| return false; | ||
| } | ||
| function ObjectRightRule(left, right) { | ||
| // type A = boolean extends {} ? 1 : 2 // additionalProperties: false | ||
| // type B = boolean extends object ? 1 : 2 // additionalProperties: true | ||
| const additionalProperties = right.additionalProperties; | ||
| const propertyLength = globalThis.Object.keys(right.properties).length; | ||
| return additionalProperties === false && propertyLength === 0; | ||
| } | ||
| function UnionRightRule(left, right) { | ||
| const result = right.anyOf.some((right) => Visit(left, right) !== StructuralResult.False); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| // ------------------------------------------------------------------------ | ||
| // Records | ||
| // ------------------------------------------------------------------------ | ||
| function RecordPattern(schema) { | ||
| return globalThis.Object.keys(schema.patternProperties)[0]; | ||
| } | ||
| function RecordNumberOrStringKey(schema) { | ||
| const pattern = RecordPattern(schema); | ||
| return pattern === '^.*$' || pattern === '^(0|[1-9][0-9]*)$'; | ||
| } | ||
| function RecordValue(schema) { | ||
| const pattern = RecordPattern(schema); | ||
| return schema.patternProperties[pattern]; | ||
| } | ||
| function RecordKey(schema) { | ||
| const pattern = RecordPattern(schema); | ||
| if (pattern === '^.*$') { | ||
| return Types.Type.String(); | ||
| } | ||
| else if (pattern === '^(0|[1-9][0-9]*)$') { | ||
| return Types.Type.Number(); | ||
| } | ||
| else { | ||
| const keys = pattern.slice(1, pattern.length - 1).split('|'); | ||
| const schemas = keys.map((key) => (isNaN(+key) ? Types.Type.Literal(key) : Types.Type.Literal(parseFloat(key)))); | ||
| return Types.Type.Union(schemas); | ||
| } | ||
| } | ||
| function PropertyMap(schema) { | ||
| const comparable = new Map(); | ||
| if (guard_1.TypeGuard.TRecord(schema)) { | ||
| const propertyPattern = RecordPattern(schema); | ||
| if (propertyPattern === '^.*$' || propertyPattern === '^(0|[1-9][0-9]*)$') | ||
| throw Error('Cannot extract record properties without property constraints'); | ||
| const propertySchema = schema.patternProperties[propertyPattern]; | ||
| const propertyKeys = propertyPattern.slice(1, propertyPattern.length - 1).split('|'); | ||
| propertyKeys.forEach((propertyKey) => { | ||
| comparable.set(propertyKey, propertySchema); | ||
| }); | ||
| } | ||
| else { | ||
| globalThis.Object.entries(schema.properties).forEach(([propertyKey, propertySchema]) => { | ||
| comparable.set(propertyKey, propertySchema); | ||
| }); | ||
| } | ||
| return comparable; | ||
| } | ||
| // ------------------------------------------------------------------------ | ||
| // Indexable | ||
| // ------------------------------------------------------------------------ | ||
| function Indexable(left, right) { | ||
| if (guard_1.TypeGuard.TUnion(right)) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| return Visit(left, right); | ||
| } | ||
| } | ||
| // ------------------------------------------------------------------------ | ||
| // Checks | ||
| // ------------------------------------------------------------------------ | ||
| function Any(left, right) { | ||
| return AnyOrUnknownRule(right) ? StructuralResult.True : StructuralResult.Union; | ||
| } | ||
| function Array(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number') | ||
| return StructuralResult.True; | ||
| if (globalThis.Object.keys(right.properties).length === 0) | ||
| return StructuralResult.True; | ||
| return StructuralResult.False; | ||
| } | ||
| else if (!guard_1.TypeGuard.TArray(right)) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (left.items === undefined && right.items !== undefined) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (left.items !== undefined && right.items === undefined) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (left.items === undefined && right.items === undefined) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| const result = Visit(left.items, right.items) !== StructuralResult.False; | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| } | ||
| function Boolean(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TBoolean(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Constructor(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && globalThis.Object.keys(right.properties).length === 0) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (!guard_1.TypeGuard.TConstructor(right)) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (right.parameters.length < left.parameters.length) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| if (Visit(left.returns, right.returns) === StructuralResult.False) { | ||
| return StructuralResult.False; | ||
| } | ||
| for (let i = 0; i < left.parameters.length; i++) { | ||
| const result = Visit(right.parameters[i], left.parameters[i]); | ||
| if (result === StructuralResult.False) | ||
| return StructuralResult.False; | ||
| } | ||
| return StructuralResult.True; | ||
| } | ||
| } | ||
| function Function(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| if (right.properties['length'] !== undefined && right.properties['length'][Types.Kind] === 'Number') | ||
| return StructuralResult.True; | ||
| if (globalThis.Object.keys(right.properties).length === 0) | ||
| return StructuralResult.True; | ||
| return StructuralResult.False; | ||
| } | ||
| else if (!guard_1.TypeGuard.TFunction(right)) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (right.parameters.length < left.parameters.length) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (Visit(left.returns, right.returns) === StructuralResult.False) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| for (let i = 0; i < left.parameters.length; i++) { | ||
| const result = Visit(right.parameters[i], left.parameters[i]); | ||
| if (result === StructuralResult.False) | ||
| return StructuralResult.False; | ||
| } | ||
| return StructuralResult.True; | ||
| } | ||
| } | ||
| function Integer(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TInteger(right) || guard_1.TypeGuard.TNumber(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Literal(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| if (typeof left.const === 'string') { | ||
| return Indexable(left, RecordValue(right)); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| else if (guard_1.TypeGuard.TLiteral(right) && left.const === right.const) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TString(right) && typeof left.const === 'string') { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TNumber(right) && typeof left.const === 'number') { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TInteger(right) && typeof left.const === 'number') { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TBoolean(right) && typeof left.const === 'boolean') { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Number(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TNumber(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TInteger(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Null(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TNull(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Properties(left, right) { | ||
| if (right.size > left.size) | ||
| return StructuralResult.False; | ||
| if (![...right.keys()].every((rightKey) => left.has(rightKey))) | ||
| return StructuralResult.False; | ||
| for (const rightKey of right.keys()) { | ||
| const leftProp = left.get(rightKey); | ||
| const rightProp = right.get(rightKey); | ||
| if (Visit(leftProp, rightProp) === StructuralResult.False) { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| return StructuralResult.True; | ||
| } | ||
| function Object(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| return Properties(PropertyMap(left), PropertyMap(right)); | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| if (!RecordNumberOrStringKey(right)) { | ||
| return Properties(PropertyMap(left), PropertyMap(right)); | ||
| } | ||
| else { | ||
| return StructuralResult.True; | ||
| } | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Promise(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| if (ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0) { | ||
| return StructuralResult.True; | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| else if (!guard_1.TypeGuard.TPromise(right)) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| const result = Visit(left.item, right.item) !== StructuralResult.False; | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| } | ||
| function Record(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| if (RecordPattern(left) === '^.*$' && right[Types.Hint] === 'Record') { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (RecordPattern(left) === '^.*$') { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| return globalThis.Object.keys(right.properties).length === 0 ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| if (!RecordNumberOrStringKey(left) && !RecordNumberOrStringKey(right)) { | ||
| return Properties(PropertyMap(left), PropertyMap(right)); | ||
| } | ||
| else if (RecordNumberOrStringKey(left) && !RecordNumberOrStringKey(right)) { | ||
| const leftKey = RecordKey(left); | ||
| const rightKey = RecordKey(right); | ||
| if (Visit(rightKey, leftKey) === StructuralResult.False) { | ||
| return StructuralResult.False; | ||
| } | ||
| else { | ||
| return StructuralResult.True; | ||
| } | ||
| } | ||
| else { | ||
| return StructuralResult.True; | ||
| } | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Ref(left, right) { | ||
| if (!referenceMap.has(left.$ref)) | ||
| throw Error(`Cannot locate referenced $id '${left.$ref}'`); | ||
| const resolved = referenceMap.get(left.$ref); | ||
| return Visit(resolved, right); | ||
| } | ||
| function Self(left, right) { | ||
| if (!referenceMap.has(left.$ref)) | ||
| throw Error(`Cannot locate referenced self $id '${left.$ref}'`); | ||
| const resolved = referenceMap.get(left.$ref); | ||
| return Visit(resolved, right); | ||
| } | ||
| function String(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| return Indexable(left, RecordValue(right)); | ||
| } | ||
| else if (guard_1.TypeGuard.TString(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Tuple(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right)) { | ||
| const result = ObjectRightRule(left, right) || globalThis.Object.keys(right.properties).length === 0; | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| return Indexable(left, RecordValue(right)); | ||
| } | ||
| else if (guard_1.TypeGuard.TArray(right)) { | ||
| if (right.items === undefined) { | ||
| return StructuralResult.False; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right.items) && left.items) { | ||
| const result = left.items.every((left) => UnionRightRule(left, right.items) !== StructuralResult.False); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| else if (guard_1.TypeGuard.TAny(right.items)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| if (!guard_1.TypeGuard.TTuple(right)) | ||
| return StructuralResult.False; | ||
| if (left.items === undefined && right.items === undefined) | ||
| return StructuralResult.True; | ||
| if (left.items === undefined && right.items !== undefined) | ||
| return StructuralResult.False; | ||
| if (left.items !== undefined && right.items === undefined) | ||
| return StructuralResult.False; | ||
| if (left.items === undefined && right.items === undefined) | ||
| return StructuralResult.True; | ||
| if (left.minItems !== right.minItems || left.maxItems !== right.maxItems) | ||
| return StructuralResult.False; | ||
| for (let i = 0; i < left.items.length; i++) { | ||
| if (Visit(left.items[i], right.items[i]) === StructuralResult.False) | ||
| return StructuralResult.False; | ||
| } | ||
| return StructuralResult.True; | ||
| } | ||
| function Uint8Array(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(right)) { | ||
| return Indexable(left, RecordValue(right)); | ||
| } | ||
| else if (guard_1.TypeGuard.TUint8Array(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Undefined(left, right) { | ||
| if (AnyOrUnknownRule(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUndefined(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TVoid(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| return UnionRightRule(left, right); | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Union(left, right) { | ||
| if (left.anyOf.some((left) => guard_1.TypeGuard.TAny(left))) { | ||
| return StructuralResult.Union; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(right)) { | ||
| const result = left.anyOf.every((left) => right.anyOf.some((right) => Visit(left, right) !== StructuralResult.False)); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| else { | ||
| const result = left.anyOf.every((left) => Visit(left, right) !== StructuralResult.False); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| } | ||
| function Unknown(left, right) { | ||
| if (guard_1.TypeGuard.TUnion(right)) { | ||
| const result = right.anyOf.some((right) => guard_1.TypeGuard.TAny(right) || guard_1.TypeGuard.TUnknown(right)); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| else if (guard_1.TypeGuard.TAny(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnknown(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| function Void(left, right) { | ||
| if (guard_1.TypeGuard.TUnion(right)) { | ||
| const result = right.anyOf.some((right) => guard_1.TypeGuard.TAny(right) || guard_1.TypeGuard.TUnknown(right)); | ||
| return result ? StructuralResult.True : StructuralResult.False; | ||
| } | ||
| else if (guard_1.TypeGuard.TAny(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TUnknown(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else if (guard_1.TypeGuard.TVoid(right)) { | ||
| return StructuralResult.True; | ||
| } | ||
| else { | ||
| return StructuralResult.False; | ||
| } | ||
| } | ||
| let recursionDepth = 0; | ||
| function Visit(left, right) { | ||
| recursionDepth += 1; | ||
| if (recursionDepth >= 1000) | ||
| return StructuralResult.True; | ||
| if (left.$id !== undefined) | ||
| referenceMap.set(left.$id, left); | ||
| if (right.$id !== undefined) | ||
| referenceMap.set(right.$id, right); | ||
| const resolvedRight = right[Types.Kind] === 'Self' ? referenceMap.get(right.$ref) : right; | ||
| if (guard_1.TypeGuard.TAny(left)) { | ||
| return Any(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TArray(left)) { | ||
| return Array(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TBoolean(left)) { | ||
| return Boolean(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TConstructor(left)) { | ||
| return Constructor(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TFunction(left)) { | ||
| return Function(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TInteger(left)) { | ||
| return Integer(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TLiteral(left)) { | ||
| return Literal(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TNull(left)) { | ||
| return Null(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TNumber(left)) { | ||
| return Number(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TObject(left)) { | ||
| return Object(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TPromise(left)) { | ||
| return Promise(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TRecord(left)) { | ||
| return Record(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TRef(left)) { | ||
| return Ref(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TSelf(left)) { | ||
| return Self(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TString(left)) { | ||
| return String(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TTuple(left)) { | ||
| return Tuple(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TUndefined(left)) { | ||
| return Undefined(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TUint8Array(left)) { | ||
| return Uint8Array(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TUnion(left)) { | ||
| return Union(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TUnknown(left)) { | ||
| return Unknown(left, resolvedRight); | ||
| } | ||
| else if (guard_1.TypeGuard.TVoid(left)) { | ||
| return Void(left, resolvedRight); | ||
| } | ||
| else { | ||
| throw Error(`Structural: Unknown left operand '${left[Types.Kind]}'`); | ||
| } | ||
| } | ||
| /** Structurally tests if the left schema extends the right. */ | ||
| function Check(left, right) { | ||
| referenceMap.clear(); | ||
| recursionDepth = 0; | ||
| return Visit(left, right); | ||
| } | ||
| Structural.Check = Check; | ||
| })(Structural = exports.Structural || (exports.Structural = {})); |
| export declare type FormatValidationFunction = (value: string) => boolean; | ||
| /** Shared string formats used by the TypeCompiler and Value modules */ | ||
| export declare namespace Format { | ||
| /** Clears all formats */ | ||
| function Clear(): void; | ||
| /** Returns true if the string format exists */ | ||
| function Has(format: string): boolean; | ||
| /** Sets a string format validation function */ | ||
| function Set(format: string, func: FormatValidationFunction): void; | ||
| /** Gets a string format validation function */ | ||
| function Get(format: string): FormatValidationFunction | undefined; | ||
| } |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/format | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Format = void 0; | ||
| /** Shared string formats used by the TypeCompiler and Value modules */ | ||
| var Format; | ||
| (function (Format) { | ||
| const formats = new Map(); | ||
| /** Clears all formats */ | ||
| function Clear() { | ||
| return formats.clear(); | ||
| } | ||
| Format.Clear = Clear; | ||
| /** Returns true if the string format exists */ | ||
| function Has(format) { | ||
| return formats.has(format); | ||
| } | ||
| Format.Has = Has; | ||
| /** Sets a string format validation function */ | ||
| function Set(format, func) { | ||
| formats.set(format, func); | ||
| } | ||
| Format.Set = Set; | ||
| /** Gets a string format validation function */ | ||
| function Get(format) { | ||
| return formats.get(format); | ||
| } | ||
| Format.Get = Get; | ||
| })(Format = exports.Format || (exports.Format = {})); |
| export * from './format'; |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/format | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./format"), exports); |
| import * as Types from '../typebox'; | ||
| export declare class TypeGuardInvalidTypeError extends Error { | ||
| readonly schema: unknown; | ||
| constructor(schema: unknown); | ||
| } | ||
| /** TypeGuard tests that values conform to a known TypeBox type specification */ | ||
| export declare namespace TypeGuard { | ||
| /** Returns true if the given schema is TAny */ | ||
| function TAny(schema: unknown): schema is Types.TAny; | ||
| /** Returns true if the given schema is TArray */ | ||
| function TArray(schema: unknown): schema is Types.TArray; | ||
| /** Returns true if the given schema is TBoolean */ | ||
| function TBoolean(schema: unknown): schema is Types.TBoolean; | ||
| /** Returns true if the given schema is TConstructor */ | ||
| function TConstructor(schema: unknown): schema is Types.TConstructor; | ||
| /** Returns true if the given schema is TFunction */ | ||
| function TFunction(schema: unknown): schema is Types.TFunction; | ||
| /** Returns true if the given schema is TInteger */ | ||
| function TInteger(schema: unknown): schema is Types.TInteger; | ||
| /** Returns true if the given schema is TLiteral */ | ||
| function TLiteral(schema: unknown): schema is Types.TLiteral; | ||
| /** Returns true if the given schema is TNever */ | ||
| function TNever(schema: unknown): schema is Types.TNever; | ||
| /** Returns true if the given schema is TNull */ | ||
| function TNull(schema: unknown): schema is Types.TNull; | ||
| /** Returns true if the given schema is TNumber */ | ||
| function TNumber(schema: unknown): schema is Types.TNumber; | ||
| /** Returns true if the given schema is TObject */ | ||
| function TObject(schema: unknown): schema is Types.TObject; | ||
| /** Returns true if the given schema is TPromise */ | ||
| function TPromise(schema: unknown): schema is Types.TPromise; | ||
| /** Returns true if the given schema is TRecord */ | ||
| function TRecord(schema: unknown): schema is Types.TRecord; | ||
| /** Returns true if the given schema is TSelf */ | ||
| function TSelf(schema: unknown): schema is Types.TSelf; | ||
| /** Returns true if the given schema is TRef */ | ||
| function TRef(schema: unknown): schema is Types.TRef; | ||
| /** Returns true if the given schema is TString */ | ||
| function TString(schema: unknown): schema is Types.TString; | ||
| /** Returns true if the given schema is TTuple */ | ||
| function TTuple(schema: unknown): schema is Types.TTuple; | ||
| /** Returns true if the given schema is TUndefined */ | ||
| function TUndefined(schema: unknown): schema is Types.TUndefined; | ||
| /** Returns true if the given schema is TUnion */ | ||
| function TUnion(schema: unknown): schema is Types.TUnion; | ||
| /** Returns true if the given schema is TUint8Array */ | ||
| function TUint8Array(schema: unknown): schema is Types.TUint8Array; | ||
| /** Returns true if the given schema is TUnknown */ | ||
| function TUnknown(schema: unknown): schema is Types.TUnknown; | ||
| /** Returns true if the given schema is TVoid */ | ||
| function TVoid(schema: unknown): schema is Types.TVoid; | ||
| /** Returns true if the given schema is TSchema */ | ||
| function TSchema(schema: unknown): schema is Types.TSchema; | ||
| /** Asserts if this schema and associated references are valid. */ | ||
| function Assert<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): void; | ||
| } |
-351
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/guard | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, dTribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.TypeGuard = exports.TypeGuardInvalidTypeError = void 0; | ||
| const Types = require("../typebox"); | ||
| class TypeGuardInvalidTypeError extends Error { | ||
| constructor(schema) { | ||
| super('TypeGuard: Invalid type'); | ||
| this.schema = schema; | ||
| } | ||
| } | ||
| exports.TypeGuardInvalidTypeError = TypeGuardInvalidTypeError; | ||
| /** TypeGuard tests that values conform to a known TypeBox type specification */ | ||
| var TypeGuard; | ||
| (function (TypeGuard) { | ||
| function IsObject(value) { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
| function IsArray(value) { | ||
| return typeof value === 'object' && value !== null && Array.isArray(value); | ||
| } | ||
| function IsPattern(value) { | ||
| try { | ||
| new RegExp(value); | ||
| return true; | ||
| } | ||
| catch { | ||
| return false; | ||
| } | ||
| } | ||
| function IsControlCharacterFree(value) { | ||
| if (typeof value !== 'string') | ||
| return false; | ||
| for (let i = 0; i < value.length; i++) { | ||
| const code = value.charCodeAt(i); | ||
| if ((code >= 7 && code <= 13) || code === 27 || code === 127) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| function IsString(value) { | ||
| return typeof value === 'string'; | ||
| } | ||
| function IsNumber(value) { | ||
| return typeof value === 'number'; | ||
| } | ||
| function IsBoolean(value) { | ||
| return typeof value === 'boolean'; | ||
| } | ||
| function IsOptionalNumber(value) { | ||
| return value === undefined || (value !== undefined && IsNumber(value)); | ||
| } | ||
| function IsOptionalBoolean(value) { | ||
| return value === undefined || (value !== undefined && IsBoolean(value)); | ||
| } | ||
| function IsOptionalString(value) { | ||
| return value === undefined || (value !== undefined && IsString(value)); | ||
| } | ||
| function IsOptionalPattern(value) { | ||
| return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value) && IsPattern(value)); | ||
| } | ||
| function IsOptionalFormat(value) { | ||
| return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value)); | ||
| } | ||
| function IsOptionalSchema(value) { | ||
| return value === undefined || TSchema(value); | ||
| } | ||
| /** Returns true if the given schema is TAny */ | ||
| function TAny(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Any' && IsOptionalString(schema.$id); | ||
| } | ||
| TypeGuard.TAny = TAny; | ||
| /** Returns true if the given schema is TArray */ | ||
| function TArray(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'Array' && | ||
| schema.type === 'array' && | ||
| IsOptionalString(schema.$id) && | ||
| TSchema(schema.items) && | ||
| IsOptionalNumber(schema.minItems) && | ||
| IsOptionalNumber(schema.maxItems) && | ||
| IsOptionalBoolean(schema.uniqueItems)); | ||
| } | ||
| TypeGuard.TArray = TArray; | ||
| /** Returns true if the given schema is TBoolean */ | ||
| function TBoolean(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean' && IsOptionalString(schema.$id); | ||
| } | ||
| TypeGuard.TBoolean = TBoolean; | ||
| /** Returns true if the given schema is TConstructor */ | ||
| function TConstructor(schema) { | ||
| if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) { | ||
| return false; | ||
| } | ||
| for (const parameter of schema.parameters) { | ||
| if (!TSchema(parameter)) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TConstructor = TConstructor; | ||
| /** Returns true if the given schema is TFunction */ | ||
| function TFunction(schema) { | ||
| if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) { | ||
| return false; | ||
| } | ||
| for (const parameter of schema.parameters) { | ||
| if (!TSchema(parameter)) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TFunction = TFunction; | ||
| /** Returns true if the given schema is TInteger */ | ||
| function TInteger(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'Integer' && | ||
| schema.type === 'integer' && | ||
| IsOptionalString(schema.$id) && | ||
| IsOptionalNumber(schema.multipleOf) && | ||
| IsOptionalNumber(schema.minimum) && | ||
| IsOptionalNumber(schema.maximum) && | ||
| IsOptionalNumber(schema.exclusiveMinimum) && | ||
| IsOptionalNumber(schema.exclusiveMaximum)); | ||
| } | ||
| TypeGuard.TInteger = TInteger; | ||
| /** Returns true if the given schema is TLiteral */ | ||
| function TLiteral(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Literal' && IsOptionalString(schema.$id) && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const)); | ||
| } | ||
| TypeGuard.TLiteral = TLiteral; | ||
| /** Returns true if the given schema is TNever */ | ||
| function TNever(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'Never' && | ||
| IsArray(schema.allOf) && | ||
| schema.allOf.length === 2 && | ||
| IsObject(schema.allOf[0]) && | ||
| IsString(schema.allOf[0].type) && | ||
| schema.allOf[0].type === 'boolean' && | ||
| schema.allOf[0].const === false && | ||
| IsObject(schema.allOf[1]) && | ||
| IsString(schema.allOf[1].type) && | ||
| schema.allOf[1].type === 'boolean' && | ||
| schema.allOf[1].const === true); | ||
| } | ||
| TypeGuard.TNever = TNever; | ||
| /** Returns true if the given schema is TNull */ | ||
| function TNull(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null' && IsOptionalString(schema.$id); | ||
| } | ||
| TypeGuard.TNull = TNull; | ||
| /** Returns true if the given schema is TNumber */ | ||
| function TNumber(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'Number' && | ||
| schema.type === 'number' && | ||
| IsOptionalString(schema.$id) && | ||
| IsOptionalNumber(schema.multipleOf) && | ||
| IsOptionalNumber(schema.minimum) && | ||
| IsOptionalNumber(schema.maximum) && | ||
| IsOptionalNumber(schema.exclusiveMinimum) && | ||
| IsOptionalNumber(schema.exclusiveMaximum)); | ||
| } | ||
| TypeGuard.TNumber = TNumber; | ||
| /** Returns true if the given schema is TObject */ | ||
| function TObject(schema) { | ||
| if (!(IsObject(schema) && | ||
| schema[Types.Kind] === 'Object' && | ||
| schema.type === 'object' && | ||
| IsOptionalString(schema.$id) && | ||
| IsObject(schema.properties) && | ||
| (IsOptionalBoolean(schema.additionalProperties) || IsOptionalSchema(schema.additionalProperties)) && | ||
| IsOptionalNumber(schema.minProperties) && | ||
| IsOptionalNumber(schema.maxProperties))) { | ||
| return false; | ||
| } | ||
| for (const [key, value] of Object.entries(schema.properties)) { | ||
| if (!IsControlCharacterFree(key)) | ||
| return false; | ||
| if (!TSchema(value)) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TObject = TObject; | ||
| /** Returns true if the given schema is TPromise */ | ||
| function TPromise(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && IsOptionalString(schema.$id) && TSchema(schema.item); | ||
| } | ||
| TypeGuard.TPromise = TPromise; | ||
| /** Returns true if the given schema is TRecord */ | ||
| function TRecord(schema) { | ||
| if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.additionalProperties === false && IsObject(schema.patternProperties))) { | ||
| return false; | ||
| } | ||
| const keys = Object.keys(schema.patternProperties); | ||
| if (keys.length !== 1) { | ||
| return false; | ||
| } | ||
| if (!IsPattern(keys[0])) { | ||
| return false; | ||
| } | ||
| if (!TSchema(schema.patternProperties[keys[0]])) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TRecord = TRecord; | ||
| /** Returns true if the given schema is TSelf */ | ||
| function TSelf(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Self' && IsOptionalString(schema.$id) && IsString(schema.$ref); | ||
| } | ||
| TypeGuard.TSelf = TSelf; | ||
| /** Returns true if the given schema is TRef */ | ||
| function TRef(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsOptionalString(schema.$id) && IsString(schema.$ref); | ||
| } | ||
| TypeGuard.TRef = TRef; | ||
| /** Returns true if the given schema is TString */ | ||
| function TString(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'String' && | ||
| schema.type === 'string' && | ||
| IsOptionalString(schema.$id) && | ||
| IsOptionalNumber(schema.minLength) && | ||
| IsOptionalNumber(schema.maxLength) && | ||
| IsOptionalPattern(schema.pattern) && | ||
| IsOptionalFormat(schema.format)); | ||
| } | ||
| TypeGuard.TString = TString; | ||
| /** Returns true if the given schema is TTuple */ | ||
| function TTuple(schema) { | ||
| if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsOptionalString(schema.$id) && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) { | ||
| return false; | ||
| } | ||
| if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) { | ||
| return true; | ||
| } | ||
| if (!IsArray(schema.items)) { | ||
| return false; | ||
| } | ||
| for (const inner of schema.items) { | ||
| if (!TSchema(inner)) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TTuple = TTuple; | ||
| /** Returns true if the given schema is TUndefined */ | ||
| function TUndefined(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.specialized === 'Undefined'; | ||
| } | ||
| TypeGuard.TUndefined = TUndefined; | ||
| /** Returns true if the given schema is TUnion */ | ||
| function TUnion(schema) { | ||
| if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf) && IsOptionalString(schema.$id))) { | ||
| return false; | ||
| } | ||
| for (const inner of schema.anyOf) { | ||
| if (!TSchema(inner)) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| TypeGuard.TUnion = TUnion; | ||
| /** Returns true if the given schema is TUint8Array */ | ||
| function TUint8Array(schema) { | ||
| return (IsObject(schema) && | ||
| schema[Types.Kind] === 'Uint8Array' && | ||
| schema.type === 'object' && | ||
| IsOptionalString(schema.$id) && | ||
| schema.specialized === 'Uint8Array' && | ||
| IsOptionalNumber(schema.minByteLength) && | ||
| IsOptionalNumber(schema.maxByteLength)); | ||
| } | ||
| TypeGuard.TUint8Array = TUint8Array; | ||
| /** Returns true if the given schema is TUnknown */ | ||
| function TUnknown(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Unknown' && IsOptionalString(schema.$id); | ||
| } | ||
| TypeGuard.TUnknown = TUnknown; | ||
| /** Returns true if the given schema is TVoid */ | ||
| function TVoid(schema) { | ||
| return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null' && IsOptionalString(schema.$id); | ||
| } | ||
| TypeGuard.TVoid = TVoid; | ||
| /** Returns true if the given schema is TSchema */ | ||
| function TSchema(schema) { | ||
| return (TAny(schema) || | ||
| TArray(schema) || | ||
| TBoolean(schema) || | ||
| TConstructor(schema) || | ||
| TFunction(schema) || | ||
| TInteger(schema) || | ||
| TLiteral(schema) || | ||
| TNever(schema) || | ||
| TNull(schema) || | ||
| TNumber(schema) || | ||
| TObject(schema) || | ||
| TPromise(schema) || | ||
| TRecord(schema) || | ||
| TSelf(schema) || | ||
| TRef(schema) || | ||
| TString(schema) || | ||
| TTuple(schema) || | ||
| TUndefined(schema) || | ||
| TUnion(schema) || | ||
| TUint8Array(schema) || | ||
| TUnknown(schema) || | ||
| TVoid(schema)); | ||
| } | ||
| TypeGuard.TSchema = TSchema; | ||
| /** Asserts if this schema and associated references are valid. */ | ||
| function Assert(schema, references = []) { | ||
| if (!TSchema(schema)) | ||
| throw new TypeGuardInvalidTypeError(schema); | ||
| for (const schema of references) { | ||
| if (!TSchema(schema)) | ||
| throw new TypeGuardInvalidTypeError(schema); | ||
| } | ||
| } | ||
| TypeGuard.Assert = Assert; | ||
| })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {})); |
| export * from './guard'; |
| "use strict"; | ||
| /*-------------------------------------------------------------------------- | ||
| @sinclair/typebox/guards | ||
| The MIT License (MIT) | ||
| Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| ---------------------------------------------------------------------------*/ | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./guard"), exports); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
462956
51.06%7566
56.45%1564
35.65%43
-8.51%5
Infinity%17
21.43%