@sinclair/typebox 0.31.28 → 0.32.0-dev-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{compiler → cjs/compiler}/compiler.js +118 -107
- package/{compiler → cjs/compiler}/index.js +3 -3
- package/{errors → cjs/errors}/errors.js +48 -44
- package/cjs/index.js +205 -0
- package/cjs/system/errors.js +192 -0
- package/{value → cjs/system}/index.js +13 -14
- package/cjs/system/policy.js +74 -0
- package/cjs/system/system.js +71 -0
- package/cjs/type/any/any.js +36 -0
- package/{system → cjs/type/any}/index.js +2 -5
- package/cjs/type/array/array.js +42 -0
- package/cjs/type/array/index.js +44 -0
- package/cjs/type/async-iterator/async-iterator.js +42 -0
- package/cjs/type/async-iterator/index.js +44 -0
- package/cjs/type/awaited/awaited.js +66 -0
- package/cjs/type/awaited/index.js +44 -0
- package/cjs/type/bigint/bigint.js +40 -0
- package/cjs/type/bigint/index.js +44 -0
- package/cjs/type/boolean/boolean.js +40 -0
- package/cjs/type/boolean/index.js +44 -0
- package/cjs/type/clone/index.js +32 -0
- package/cjs/type/clone/type.js +41 -0
- package/cjs/type/clone/value.js +58 -0
- package/cjs/type/composite/composite.js +47 -0
- package/cjs/type/composite/index.js +44 -0
- package/cjs/type/const/const.js +84 -0
- package/cjs/type/const/index.js +44 -0
- package/cjs/type/constructor/constructor.js +43 -0
- package/cjs/type/constructor/index.js +44 -0
- package/cjs/type/constructor-parameters/constructor-parameters.js +37 -0
- package/cjs/type/constructor-parameters/index.js +44 -0
- package/cjs/type/date/date.js +40 -0
- package/cjs/type/date/index.js +44 -0
- package/cjs/type/deref/deref.js +136 -0
- package/cjs/type/deref/index.js +44 -0
- package/cjs/type/discard/discard.js +38 -0
- package/cjs/type/discard/index.js +44 -0
- package/cjs/type/enum/enum.js +46 -0
- package/cjs/type/enum/index.js +44 -0
- package/cjs/type/exclude/exclude.js +54 -0
- package/cjs/type/exclude/index.js +44 -0
- package/cjs/type/extends/extends-check.js +654 -0
- package/cjs/type/extends/extends-undefined.js +51 -0
- package/cjs/type/extends/extends.js +47 -0
- package/cjs/type/extends/index.js +46 -0
- package/cjs/type/extract/extract.js +54 -0
- package/cjs/type/extract/index.js +44 -0
- package/cjs/type/function/function.js +43 -0
- package/cjs/type/function/index.js +44 -0
- package/cjs/type/guard/index.js +32 -0
- package/cjs/type/guard/type.js +515 -0
- package/cjs/type/guard/value.js +100 -0
- package/cjs/type/helpers/helpers.js +35 -0
- package/cjs/type/helpers/index.js +44 -0
- package/cjs/type/index.js +113 -0
- package/cjs/type/indexed/index.js +46 -0
- package/cjs/type/indexed/indexed-key.js +65 -0
- package/cjs/type/indexed/indexed-type.js +106 -0
- package/cjs/type/indexed/indexed.js +41 -0
- package/cjs/type/instance-type/index.js +44 -0
- package/cjs/type/instance-type/instance-type.js +36 -0
- package/cjs/type/integer/index.js +44 -0
- package/cjs/type/integer/integer.js +40 -0
- package/cjs/type/intersect/index.js +44 -0
- package/cjs/type/intersect/intersect.js +59 -0
- package/cjs/type/intrinsic/index.js +44 -0
- package/cjs/type/intrinsic/intrinsic.js +106 -0
- package/cjs/type/iterator/index.js +44 -0
- package/cjs/type/iterator/iterator.js +42 -0
- package/cjs/type/keyof/index.js +46 -0
- package/cjs/type/keyof/keyof-string.js +103 -0
- package/cjs/type/keyof/keyof-type.js +48 -0
- package/cjs/type/keyof/keyof.js +38 -0
- package/cjs/type/literal/index.js +44 -0
- package/cjs/type/literal/literal.js +41 -0
- package/cjs/type/mapped/index.js +44 -0
- package/cjs/type/mapped/mapped.js +47 -0
- package/cjs/type/modifiers/index.js +44 -0
- package/cjs/type/modifiers/modifiers.js +78 -0
- package/cjs/type/never/index.js +44 -0
- package/cjs/type/never/never.js +40 -0
- package/cjs/type/not/index.js +44 -0
- package/cjs/type/not/not.js +41 -0
- package/cjs/type/null/index.js +44 -0
- package/cjs/type/null/null.js +40 -0
- package/cjs/type/number/index.js +44 -0
- package/cjs/type/number/number.js +40 -0
- package/cjs/type/object/index.js +44 -0
- package/cjs/type/object/object.js +47 -0
- package/cjs/type/omit/index.js +44 -0
- package/cjs/type/omit/omit.js +75 -0
- package/cjs/type/operators/index.js +44 -0
- package/cjs/type/operators/operators.js +119 -0
- package/cjs/type/optional/index.js +44 -0
- package/cjs/type/optional/optional.js +37 -0
- package/cjs/type/parameters/index.js +44 -0
- package/cjs/type/parameters/parameters.js +37 -0
- package/cjs/type/partial/index.js +44 -0
- package/cjs/type/partial/partial.js +73 -0
- package/cjs/type/patterns/index.js +44 -0
- package/cjs/type/patterns/patterns.js +36 -0
- package/cjs/type/pick/index.js +44 -0
- package/cjs/type/pick/pick.js +66 -0
- package/cjs/type/promise/index.js +44 -0
- package/cjs/type/promise/promise.js +42 -0
- package/cjs/type/readonly/index.js +44 -0
- package/cjs/type/readonly/readonly.js +37 -0
- package/cjs/type/readonly-optional/index.js +44 -0
- package/cjs/type/readonly-optional/readonly-optional.js +37 -0
- package/cjs/type/record/index.js +44 -0
- package/cjs/type/record/record.js +97 -0
- package/cjs/type/recursive/index.js +44 -0
- package/cjs/type/recursive/recursive.js +48 -0
- package/cjs/type/ref/index.js +44 -0
- package/cjs/type/ref/ref.js +45 -0
- package/cjs/type/regexp/index.js +44 -0
- package/cjs/type/regexp/regexp.js +38 -0
- package/cjs/type/registry/format.js +62 -0
- package/cjs/type/registry/index.js +32 -0
- package/cjs/type/registry/type.js +62 -0
- package/cjs/type/required/index.js +44 -0
- package/cjs/type/required/required.js +72 -0
- package/cjs/type/rest/index.js +44 -0
- package/cjs/type/rest/rest.js +41 -0
- package/cjs/type/return-type/index.js +44 -0
- package/cjs/type/return-type/return-type.js +36 -0
- package/cjs/type/schema/anyschema.js +29 -0
- package/cjs/type/schema/index.js +45 -0
- package/cjs/type/schema/schema.js +30 -0
- package/cjs/type/static/index.js +44 -0
- package/cjs/type/static/static.js +29 -0
- package/cjs/type/strict/index.js +44 -0
- package/cjs/type/strict/strict.js +35 -0
- package/cjs/type/string/index.js +44 -0
- package/cjs/type/string/string.js +36 -0
- package/cjs/type/symbol/index.js +44 -0
- package/cjs/type/symbol/symbol.js +36 -0
- package/cjs/type/symbols/index.js +44 -0
- package/cjs/type/symbols/symbols.js +40 -0
- package/cjs/type/template-literal/finite.js +71 -0
- package/cjs/type/template-literal/generate.js +74 -0
- package/cjs/type/template-literal/index.js +50 -0
- package/cjs/type/template-literal/parser.js +175 -0
- package/cjs/type/template-literal/pattern.js +61 -0
- package/cjs/type/template-literal/syntax.js +87 -0
- package/cjs/type/template-literal/template-literal.js +43 -0
- package/cjs/type/template-literal/union.js +48 -0
- package/cjs/type/transform/index.js +44 -0
- package/cjs/type/transform/transform.js +72 -0
- package/cjs/type/tuple/index.js +44 -0
- package/cjs/type/tuple/tuple.js +42 -0
- package/cjs/type/type/index.js +44 -0
- package/cjs/type/type/javascript.js +120 -0
- package/cjs/type/type/json.js +255 -0
- package/cjs/type/type/type.js +153 -0
- package/cjs/type/uint8array/index.js +44 -0
- package/cjs/type/uint8array/uint8array.js +36 -0
- package/cjs/type/undefined/index.js +44 -0
- package/cjs/type/undefined/undefined.js +36 -0
- package/cjs/type/union/index.js +44 -0
- package/cjs/type/union/union.js +50 -0
- package/cjs/type/unknown/index.js +44 -0
- package/cjs/type/unknown/unknown.js +39 -0
- package/cjs/type/unsafe/index.js +44 -0
- package/cjs/type/unsafe/unsafe.js +39 -0
- package/cjs/type/void/index.js +44 -0
- package/cjs/type/void/void.js +40 -0
- package/{value → cjs/value/cast}/cast.js +62 -83
- package/cjs/value/cast/index.js +44 -0
- package/cjs/value/check/check.js +468 -0
- package/cjs/value/check/index.js +44 -0
- package/cjs/value/clean/clean.js +167 -0
- package/cjs/value/clean/index.js +44 -0
- package/{value → cjs/value/clone}/clone.js +6 -6
- package/cjs/value/clone/index.js +44 -0
- package/{value → cjs/value/convert}/convert.js +93 -116
- package/cjs/value/convert/index.js +44 -0
- package/{value → cjs/value/create}/create.js +69 -62
- package/cjs/value/create/index.js +44 -0
- package/cjs/value/default/default.js +175 -0
- package/cjs/value/default/index.js +44 -0
- package/{value → cjs/value/delta}/delta.js +57 -53
- package/cjs/value/delta/index.js +44 -0
- package/{value → cjs/value/deref}/deref.js +1 -2
- package/cjs/value/deref/index.js +44 -0
- package/{value → cjs/value/equal}/equal.js +14 -14
- package/cjs/value/equal/index.js +44 -0
- package/cjs/value/guard/index.js +44 -0
- package/{value → cjs/value/hash}/hash.js +26 -26
- package/cjs/value/hash/index.js +44 -0
- package/cjs/value/index.js +123 -0
- package/cjs/value/mutate/index.js +44 -0
- package/{value → cjs/value/mutate}/mutate.js +25 -25
- package/cjs/value/pointer/index.js +31 -0
- package/cjs/value/pointer/pointer.js +151 -0
- package/cjs/value/transform/decode.js +211 -0
- package/cjs/value/transform/encode.js +219 -0
- package/cjs/value/transform/has.js +146 -0
- package/cjs/value/transform/index.js +46 -0
- package/cjs/value/value/index.js +31 -0
- package/cjs/value/value/value.js +125 -0
- package/{compiler/compiler.d.ts → esm/compiler/compiler.d.mts} +25 -18
- package/esm/compiler/compiler.mjs +658 -0
- package/{compiler/index.d.ts → esm/compiler/index.d.mts} +2 -2
- package/esm/compiler/index.mjs +29 -0
- package/{errors/errors.d.ts → esm/errors/errors.d.mts} +7 -7
- package/esm/errors/errors.mjs +583 -0
- package/esm/errors/index.d.mts +1 -0
- package/esm/errors/index.mjs +28 -0
- package/esm/index.d.mts +71 -0
- package/esm/index.mjs +105 -0
- package/esm/system/errors.d.mts +14 -0
- package/esm/system/errors.mjs +188 -0
- package/esm/system/index.d.mts +4 -0
- package/esm/system/index.mjs +31 -0
- package/esm/system/policy.d.mts +21 -0
- package/esm/system/policy.mjs +71 -0
- package/esm/system/system.d.mts +13 -0
- package/esm/system/system.mjs +66 -0
- package/esm/type/any/any.d.mts +8 -0
- package/esm/type/any/any.mjs +32 -0
- package/esm/type/any/index.d.mts +1 -0
- package/esm/type/any/index.mjs +28 -0
- package/esm/type/array/array.d.mts +25 -0
- package/esm/type/array/array.mjs +38 -0
- package/esm/type/array/index.d.mts +1 -0
- package/esm/type/array/index.mjs +28 -0
- package/esm/type/async-iterator/async-iterator.d.mts +11 -0
- package/esm/type/async-iterator/async-iterator.mjs +38 -0
- package/esm/type/async-iterator/index.d.mts +1 -0
- package/esm/type/async-iterator/index.mjs +28 -0
- package/esm/type/awaited/awaited.d.mts +12 -0
- package/esm/type/awaited/awaited.mjs +61 -0
- package/esm/type/awaited/index.d.mts +1 -0
- package/esm/type/awaited/index.mjs +28 -0
- package/esm/type/bigint/bigint.d.mts +16 -0
- package/esm/type/bigint/bigint.mjs +36 -0
- package/esm/type/bigint/index.d.mts +1 -0
- package/esm/type/bigint/index.mjs +28 -0
- package/esm/type/boolean/boolean.d.mts +9 -0
- package/esm/type/boolean/boolean.mjs +36 -0
- package/esm/type/boolean/index.d.mts +1 -0
- package/esm/type/boolean/index.mjs +28 -0
- package/esm/type/clone/index.d.mts +2 -0
- package/esm/type/clone/index.mjs +29 -0
- package/esm/type/clone/type.d.mts +5 -0
- package/esm/type/clone/type.mjs +36 -0
- package/esm/type/clone/value.d.mts +2 -0
- package/esm/type/clone/value.mjs +54 -0
- package/esm/type/composite/composite.d.mts +15 -0
- package/esm/type/composite/composite.mjs +42 -0
- package/esm/type/composite/index.d.mts +1 -0
- package/esm/type/composite/index.mjs +28 -0
- package/esm/type/const/const.d.mts +30 -0
- package/esm/type/const/const.mjs +80 -0
- package/esm/type/const/index.d.mts +1 -0
- package/esm/type/const/index.mjs +28 -0
- package/esm/type/constructor/constructor.d.mts +17 -0
- package/esm/type/constructor/constructor.mjs +39 -0
- package/esm/type/constructor/index.d.mts +1 -0
- package/esm/type/constructor/index.mjs +28 -0
- package/esm/type/constructor-parameters/constructor-parameters.d.mts +7 -0
- package/esm/type/constructor-parameters/constructor-parameters.mjs +33 -0
- package/esm/type/constructor-parameters/index.d.mts +1 -0
- package/esm/type/constructor-parameters/index.mjs +28 -0
- package/esm/type/date/date.d.mts +21 -0
- package/esm/type/date/date.mjs +36 -0
- package/esm/type/date/index.d.mts +1 -0
- package/esm/type/date/index.mjs +28 -0
- package/esm/type/deref/deref.d.mts +24 -0
- package/esm/type/deref/deref.mjs +131 -0
- package/esm/type/deref/index.d.mts +1 -0
- package/esm/type/deref/index.mjs +28 -0
- package/esm/type/discard/discard.d.mts +1 -0
- package/esm/type/discard/discard.mjs +34 -0
- package/esm/type/discard/index.d.mts +1 -0
- package/esm/type/discard/index.mjs +28 -0
- package/esm/type/enum/enum.d.mts +14 -0
- package/esm/type/enum/enum.mjs +42 -0
- package/esm/type/enum/index.d.mts +1 -0
- package/esm/type/enum/index.mjs +28 -0
- package/esm/type/exclude/exclude.d.mts +21 -0
- package/esm/type/exclude/exclude.mjs +49 -0
- package/esm/type/exclude/index.d.mts +1 -0
- package/esm/type/exclude/index.mjs +28 -0
- package/esm/type/extends/extends-check.d.mts +9 -0
- package/esm/type/extends/extends-check.mjs +649 -0
- package/esm/type/extends/extends-undefined.d.mts +3 -0
- package/esm/type/extends/extends-undefined.mjs +47 -0
- package/esm/type/extends/extends.d.mts +9 -0
- package/esm/type/extends/extends.mjs +42 -0
- package/esm/type/extends/index.d.mts +3 -0
- package/esm/type/extends/index.mjs +30 -0
- package/esm/type/extract/extract.d.mts +20 -0
- package/esm/type/extract/extract.mjs +49 -0
- package/esm/type/extract/index.d.mts +1 -0
- package/esm/type/extract/index.mjs +28 -0
- package/esm/type/function/function.d.mts +17 -0
- package/esm/type/function/function.mjs +39 -0
- package/esm/type/function/index.d.mts +1 -0
- package/esm/type/function/index.mjs +28 -0
- package/esm/type/guard/index.d.mts +2 -0
- package/esm/type/guard/index.mjs +29 -0
- package/esm/type/guard/type.d.mts +130 -0
- package/esm/type/guard/type.mjs +469 -0
- package/esm/type/guard/value.d.mts +28 -0
- package/esm/type/guard/value.mjs +83 -0
- package/esm/type/helpers/helpers.d.mts +42 -0
- package/esm/type/helpers/helpers.mjs +31 -0
- package/esm/type/helpers/index.d.mts +1 -0
- package/esm/type/helpers/index.mjs +28 -0
- package/esm/type/index.d.mts +70 -0
- package/esm/type/index.mjs +97 -0
- package/esm/type/indexed/index.d.mts +3 -0
- package/esm/type/indexed/index.mjs +30 -0
- package/esm/type/indexed/indexed-key.d.mts +17 -0
- package/esm/type/indexed/indexed-key.mjs +61 -0
- package/esm/type/indexed/indexed-type.d.mts +33 -0
- package/esm/type/indexed/indexed-type.mjs +102 -0
- package/esm/type/indexed/indexed.d.mts +8 -0
- package/esm/type/indexed/indexed.mjs +37 -0
- package/esm/type/instance-type/index.d.mts +1 -0
- package/esm/type/instance-type/index.mjs +28 -0
- package/esm/type/instance-type/instance-type.d.mts +5 -0
- package/esm/type/instance-type/instance-type.mjs +32 -0
- package/esm/type/integer/index.d.mts +1 -0
- package/esm/type/integer/index.mjs +28 -0
- package/esm/type/integer/integer.d.mts +16 -0
- package/esm/type/integer/integer.mjs +36 -0
- package/esm/type/intersect/index.d.mts +1 -0
- package/esm/type/intersect/index.mjs +28 -0
- package/esm/type/intersect/intersect.d.mts +22 -0
- package/esm/type/intersect/intersect.mjs +54 -0
- package/esm/type/intrinsic/index.d.mts +1 -0
- package/esm/type/intrinsic/index.mjs +28 -0
- package/esm/type/intrinsic/intrinsic.d.mts +27 -0
- package/esm/type/intrinsic/intrinsic.mjs +98 -0
- package/esm/type/iterator/index.d.mts +1 -0
- package/esm/type/iterator/index.mjs +28 -0
- package/esm/type/iterator/iterator.d.mts +11 -0
- package/esm/type/iterator/iterator.mjs +38 -0
- package/esm/type/keyof/index.d.mts +3 -0
- package/esm/type/keyof/index.mjs +30 -0
- package/esm/type/keyof/keyof-string.d.mts +30 -0
- package/esm/type/keyof/keyof-string.mjs +98 -0
- package/esm/type/keyof/keyof-type.d.mts +11 -0
- package/esm/type/keyof/keyof-type.mjs +44 -0
- package/esm/type/keyof/keyof.d.mts +5 -0
- package/esm/type/keyof/keyof.mjs +34 -0
- package/esm/type/literal/index.d.mts +1 -0
- package/esm/type/literal/index.mjs +28 -0
- package/esm/type/literal/literal.d.mts +10 -0
- package/esm/type/literal/literal.mjs +37 -0
- package/esm/type/mapped/index.d.mts +1 -0
- package/esm/type/mapped/index.mjs +28 -0
- package/esm/type/mapped/mapped.d.mts +26 -0
- package/esm/type/mapped/mapped.mjs +43 -0
- package/esm/type/modifiers/index.d.mts +1 -0
- package/esm/type/modifiers/index.mjs +28 -0
- package/esm/type/modifiers/modifiers.d.mts +18 -0
- package/esm/type/modifiers/modifiers.mjs +73 -0
- package/esm/type/never/index.d.mts +1 -0
- package/esm/type/never/index.mjs +28 -0
- package/esm/type/never/never.d.mts +9 -0
- package/esm/type/never/never.mjs +36 -0
- package/esm/type/not/index.d.mts +1 -0
- package/esm/type/not/index.mjs +28 -0
- package/esm/type/not/not.d.mts +10 -0
- package/esm/type/not/not.mjs +37 -0
- package/esm/type/null/index.d.mts +1 -0
- package/esm/type/null/index.mjs +28 -0
- package/esm/type/null/null.d.mts +9 -0
- package/esm/type/null/null.mjs +36 -0
- package/esm/type/number/index.d.mts +1 -0
- package/esm/type/number/index.mjs +28 -0
- package/esm/type/number/number.d.mts +16 -0
- package/esm/type/number/number.mjs +36 -0
- package/esm/type/object/index.d.mts +1 -0
- package/esm/type/object/index.mjs +28 -0
- package/esm/type/object/object.d.mts +44 -0
- package/esm/type/object/object.mjs +43 -0
- package/esm/type/omit/index.d.mts +1 -0
- package/esm/type/omit/index.mjs +28 -0
- package/esm/type/omit/omit.d.mts +21 -0
- package/esm/type/omit/omit.mjs +70 -0
- package/esm/type/operators/index.d.mts +1 -0
- package/esm/type/operators/index.mjs +28 -0
- package/esm/type/operators/operators.d.mts +24 -0
- package/esm/type/operators/operators.mjs +108 -0
- package/esm/type/optional/index.d.mts +1 -0
- package/esm/type/optional/index.mjs +28 -0
- package/esm/type/optional/optional.d.mts +7 -0
- package/esm/type/optional/optional.mjs +33 -0
- package/esm/type/parameters/index.d.mts +1 -0
- package/esm/type/parameters/index.mjs +28 -0
- package/esm/type/parameters/parameters.d.mts +7 -0
- package/esm/type/parameters/parameters.mjs +33 -0
- package/esm/type/partial/index.d.mts +1 -0
- package/esm/type/partial/index.mjs +28 -0
- package/esm/type/partial/partial.d.mts +23 -0
- package/esm/type/partial/partial.mjs +68 -0
- package/esm/type/patterns/index.d.mts +1 -0
- package/esm/type/patterns/index.mjs +28 -0
- package/esm/type/patterns/patterns.d.mts +6 -0
- package/esm/type/patterns/patterns.mjs +33 -0
- package/esm/type/pick/index.d.mts +1 -0
- package/esm/type/pick/index.mjs +28 -0
- package/esm/type/pick/pick.d.mts +21 -0
- package/esm/type/pick/pick.mjs +61 -0
- package/esm/type/promise/index.d.mts +1 -0
- package/esm/type/promise/index.mjs +28 -0
- package/esm/type/promise/promise.d.mts +11 -0
- package/esm/type/promise/promise.mjs +38 -0
- package/esm/type/readonly/index.d.mts +1 -0
- package/esm/type/readonly/index.mjs +28 -0
- package/esm/type/readonly/readonly.d.mts +7 -0
- package/esm/type/readonly/readonly.mjs +33 -0
- package/esm/type/readonly-optional/index.d.mts +1 -0
- package/esm/type/readonly-optional/index.mjs +28 -0
- package/esm/type/readonly-optional/readonly-optional.d.mts +6 -0
- package/esm/type/readonly-optional/readonly-optional.mjs +33 -0
- package/esm/type/record/index.d.mts +1 -0
- package/esm/type/record/index.mjs +28 -0
- package/esm/type/record/record.d.mts +55 -0
- package/esm/type/record/record.mjs +92 -0
- package/esm/type/recursive/index.d.mts +1 -0
- package/esm/type/recursive/index.mjs +28 -0
- package/esm/type/recursive/recursive.d.mts +15 -0
- package/esm/type/recursive/recursive.mjs +44 -0
- package/esm/type/ref/index.d.mts +1 -0
- package/esm/type/ref/index.mjs +28 -0
- package/esm/type/ref/ref.d.mts +12 -0
- package/esm/type/ref/ref.mjs +41 -0
- package/esm/type/regexp/index.d.mts +1 -0
- package/esm/type/regexp/index.mjs +28 -0
- package/esm/type/regexp/regexp.d.mts +7 -0
- package/esm/type/regexp/regexp.mjs +34 -0
- package/esm/type/registry/format.d.mts +13 -0
- package/esm/type/registry/format.mjs +53 -0
- package/esm/type/registry/index.d.mts +2 -0
- package/esm/type/registry/index.mjs +29 -0
- package/esm/type/registry/type.d.mts +13 -0
- package/esm/type/registry/type.mjs +53 -0
- package/esm/type/required/index.d.mts +1 -0
- package/esm/type/required/index.mjs +28 -0
- package/esm/type/required/required.d.mts +23 -0
- package/esm/type/required/required.mjs +67 -0
- package/esm/type/rest/index.d.mts +1 -0
- package/esm/type/rest/index.mjs +28 -0
- package/esm/type/rest/rest.d.mts +10 -0
- package/esm/type/rest/rest.mjs +36 -0
- package/esm/type/return-type/index.d.mts +1 -0
- package/esm/type/return-type/index.mjs +28 -0
- package/esm/type/return-type/return-type.d.mts +5 -0
- package/esm/type/return-type/return-type.mjs +32 -0
- package/esm/type/schema/anyschema.d.mts +32 -0
- package/esm/type/schema/anyschema.mjs +28 -0
- package/esm/type/schema/index.d.mts +2 -0
- package/esm/type/schema/index.mjs +29 -0
- package/esm/type/schema/schema.d.mts +29 -0
- package/esm/type/schema/schema.mjs +28 -0
- package/esm/type/static/index.d.mts +1 -0
- package/esm/type/static/index.mjs +28 -0
- package/esm/type/static/static.d.mts +34 -0
- package/esm/type/static/static.mjs +28 -0
- package/esm/type/strict/index.d.mts +1 -0
- package/esm/type/strict/index.mjs +28 -0
- package/esm/type/strict/strict.d.mts +3 -0
- package/esm/type/strict/strict.mjs +31 -0
- package/esm/type/string/index.d.mts +1 -0
- package/esm/type/string/index.mjs +28 -0
- package/esm/type/string/string.d.mts +25 -0
- package/esm/type/string/string.mjs +32 -0
- package/esm/type/symbol/index.d.mts +1 -0
- package/esm/type/symbol/index.mjs +28 -0
- package/esm/type/symbol/symbol.d.mts +10 -0
- package/esm/type/symbol/symbol.mjs +32 -0
- package/esm/type/symbols/index.d.mts +1 -0
- package/esm/type/symbols/index.mjs +28 -0
- package/esm/type/symbols/symbols.d.mts +10 -0
- package/esm/type/symbols/symbols.mjs +37 -0
- package/esm/type/template-literal/finite.d.mts +16 -0
- package/esm/type/template-literal/finite.mjs +66 -0
- package/esm/type/template-literal/generate.d.mts +15 -0
- package/esm/type/template-literal/generate.mjs +69 -0
- package/esm/type/template-literal/index.d.mts +7 -0
- package/esm/type/template-literal/index.mjs +34 -0
- package/esm/type/template-literal/parser.d.mts +19 -0
- package/esm/type/template-literal/parser.mjs +169 -0
- package/esm/type/template-literal/pattern.d.mts +4 -0
- package/esm/type/template-literal/pattern.mjs +56 -0
- package/esm/type/template-literal/syntax.d.mts +20 -0
- package/esm/type/template-literal/syntax.mjs +83 -0
- package/esm/type/template-literal/template-literal.d.mts +30 -0
- package/esm/type/template-literal/template-literal.mjs +39 -0
- package/esm/type/template-literal/union.d.mts +7 -0
- package/esm/type/template-literal/union.mjs +44 -0
- package/esm/type/transform/index.d.mts +1 -0
- package/esm/type/transform/index.mjs +28 -0
- package/esm/type/transform/transform.d.mts +29 -0
- package/esm/type/transform/transform.mjs +69 -0
- package/esm/type/tuple/index.d.mts +1 -0
- package/esm/type/tuple/index.mjs +28 -0
- package/esm/type/tuple/tuple.d.mts +15 -0
- package/esm/type/tuple/tuple.mjs +38 -0
- package/esm/type/type/index.d.mts +6 -0
- package/esm/type/type/index.mjs +40 -0
- package/esm/type/type/javascript.d.mts +58 -0
- package/esm/type/type/javascript.mjs +116 -0
- package/esm/type/type/json.d.mts +139 -0
- package/esm/type/type/json.mjs +251 -0
- package/esm/type/type/type.d.mts +58 -0
- package/esm/type/type/type.mjs +88 -0
- package/esm/type/uint8array/index.d.mts +1 -0
- package/esm/type/uint8array/index.mjs +28 -0
- package/esm/type/uint8array/uint8array.d.mts +13 -0
- package/esm/type/uint8array/uint8array.mjs +32 -0
- package/esm/type/undefined/index.d.mts +1 -0
- package/esm/type/undefined/index.mjs +28 -0
- package/esm/type/undefined/undefined.d.mts +9 -0
- package/esm/type/undefined/undefined.mjs +32 -0
- package/esm/type/union/index.d.mts +1 -0
- package/esm/type/union/index.mjs +28 -0
- package/esm/type/union/union.d.mts +16 -0
- package/esm/type/union/union.mjs +45 -0
- package/esm/type/unknown/index.d.mts +1 -0
- package/esm/type/unknown/index.mjs +28 -0
- package/esm/type/unknown/unknown.d.mts +8 -0
- package/esm/type/unknown/unknown.mjs +35 -0
- package/esm/type/unsafe/index.d.mts +1 -0
- package/esm/type/unsafe/index.mjs +28 -0
- package/esm/type/unsafe/unsafe.d.mts +11 -0
- package/esm/type/unsafe/unsafe.mjs +35 -0
- package/esm/type/void/index.d.mts +1 -0
- package/esm/type/void/index.mjs +28 -0
- package/esm/type/void/void.d.mts +9 -0
- package/esm/type/void/void.mjs +36 -0
- package/esm/value/cast/cast.d.mts +23 -0
- package/esm/value/cast/cast.mjs +260 -0
- package/esm/value/cast/index.d.mts +1 -0
- package/esm/value/cast/index.mjs +28 -0
- package/esm/value/check/check.d.mts +10 -0
- package/{value/check.js → esm/value/check/check.mjs} +52 -51
- package/esm/value/check/index.d.mts +1 -0
- package/esm/value/check/index.mjs +28 -0
- package/esm/value/clean/clean.d.mts +5 -0
- package/esm/value/clean/clean.mjs +163 -0
- package/esm/value/clean/index.d.mts +1 -0
- package/esm/value/clean/index.mjs +28 -0
- package/esm/value/clone/clone.mjs +64 -0
- package/esm/value/clone/index.d.mts +1 -0
- package/esm/value/clone/index.mjs +28 -0
- package/esm/value/convert/convert.d.mts +9 -0
- package/esm/value/convert/convert.mjs +287 -0
- package/esm/value/convert/index.d.mts +1 -0
- package/esm/value/convert/index.mjs +28 -0
- package/esm/value/create/create.d.mts +31 -0
- package/esm/value/create/create.mjs +504 -0
- package/esm/value/create/index.d.mts +1 -0
- package/esm/value/create/index.mjs +28 -0
- package/esm/value/default/default.d.mts +5 -0
- package/esm/value/default/default.mjs +171 -0
- package/esm/value/default/index.d.mts +1 -0
- package/esm/value/default/index.mjs +28 -0
- package/esm/value/delta/delta.d.mts +41 -0
- package/esm/value/delta/delta.mjs +194 -0
- package/esm/value/delta/index.d.mts +1 -0
- package/esm/value/delta/index.mjs +28 -0
- package/{value/deref.d.ts → esm/value/deref/deref.d.mts} +4 -2
- package/esm/value/deref/deref.mjs +41 -0
- package/esm/value/deref/index.d.mts +1 -0
- package/esm/value/deref/index.mjs +28 -0
- package/esm/value/equal/equal.mjs +73 -0
- package/esm/value/equal/index.d.mts +1 -0
- package/esm/value/equal/index.mjs +28 -0
- package/esm/value/guard/guard.mjs +123 -0
- package/esm/value/guard/index.d.mts +1 -0
- package/esm/value/guard/index.mjs +28 -0
- package/{value/hash.d.ts → esm/value/hash/hash.d.mts} +0 -13
- package/esm/value/hash/hash.mjs +173 -0
- package/esm/value/hash/index.d.mts +1 -0
- package/esm/value/hash/index.mjs +28 -0
- package/esm/value/index.d.mts +18 -0
- package/esm/value/index.mjs +57 -0
- package/esm/value/mutate/index.d.mts +1 -0
- package/esm/value/mutate/index.mjs +28 -0
- package/{value/mutate.d.ts → esm/value/mutate/mutate.d.mts} +1 -1
- package/esm/value/mutate/mutate.mjs +123 -0
- package/esm/value/pointer/index.d.mts +1 -0
- package/esm/value/pointer/index.mjs +28 -0
- package/esm/value/pointer/pointer.d.mts +21 -0
- package/esm/value/pointer/pointer.mjs +146 -0
- package/esm/value/transform/decode.d.mts +19 -0
- package/esm/value/transform/decode.mjs +210 -0
- package/esm/value/transform/encode.d.mts +20 -0
- package/esm/value/transform/encode.mjs +218 -0
- package/esm/value/transform/has.d.mts +3 -0
- package/esm/value/transform/has.mjs +142 -0
- package/esm/value/transform/index.d.mts +3 -0
- package/esm/value/transform/index.mjs +30 -0
- package/esm/value/value/index.d.mts +1 -0
- package/esm/value/value/index.mjs +28 -0
- package/esm/value/value/value.d.mts +53 -0
- package/esm/value/value/value.mjs +107 -0
- package/package.json +37 -27
- package/readme.md +183 -155
- package/errors/index.d.ts +0 -1
- package/system/index.d.ts +0 -2
- package/system/system.d.ts +0 -48
- package/system/system.js +0 -274
- package/typebox.d.ts +0 -962
- package/typebox.js +0 -2364
- package/value/cast.d.ts +0 -24
- package/value/check.d.ts +0 -9
- package/value/convert.d.ts +0 -10
- package/value/create.d.ts +0 -30
- package/value/delta.d.ts +0 -41
- package/value/index.d.ts +0 -5
- package/value/pointer.d.ts +0 -24
- package/value/pointer.js +0 -148
- package/value/transform.d.ts +0 -37
- package/value/transform.js +0 -473
- package/value/value.d.ts +0 -47
- package/value/value.js +0 -117
- /package/{errors → cjs/errors}/index.js +0 -0
- /package/{value → cjs/value/guard}/guard.js +0 -0
- /package/{value/clone.d.ts → esm/value/clone/clone.d.mts} +0 -0
- /package/{value/equal.d.ts → esm/value/equal/equal.d.mts} +0 -0
- /package/{value/guard.d.ts → esm/value/guard/guard.d.mts} +0 -0
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
/*--------------------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
@sinclair/typebox/compiler
|
|
4
|
+
|
|
5
|
+
The MIT License (MIT)
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in
|
|
17
|
+
all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
|
+
THE SOFTWARE.
|
|
26
|
+
|
|
27
|
+
---------------------------------------------------------------------------*/
|
|
28
|
+
import { Encode as TransformEncode, Decode as TransformDecode, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/index.mjs';
|
|
29
|
+
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard/index.mjs';
|
|
30
|
+
import { Errors } from '../errors/index.mjs';
|
|
31
|
+
import { TypeSystemPolicy } from '../system/index.mjs';
|
|
32
|
+
import { Deref } from '../value/deref/index.mjs';
|
|
33
|
+
import { Hash } from '../value/hash/index.mjs';
|
|
34
|
+
import { Kind } from '../type/symbols/index.mjs';
|
|
35
|
+
import { TSchema as IsSchemaType } from '../type/guard/type.mjs';
|
|
36
|
+
import { TypeRegistry, FormatRegistry } from '../type/registry/index.mjs';
|
|
37
|
+
import { KeyOfStringResolvePattern } from '../type/keyof/index.mjs';
|
|
38
|
+
import { ExtendsUndefinedCheck } from '../type/extends/extends-undefined.mjs';
|
|
39
|
+
import { Never } from '../type/never/index.mjs';
|
|
40
|
+
// ------------------------------------------------------------------
|
|
41
|
+
// TypeCheck
|
|
42
|
+
// ------------------------------------------------------------------
|
|
43
|
+
export class TypeCheck {
|
|
44
|
+
schema;
|
|
45
|
+
references;
|
|
46
|
+
checkFunc;
|
|
47
|
+
code;
|
|
48
|
+
hasTransform;
|
|
49
|
+
constructor(schema, references, checkFunc, code) {
|
|
50
|
+
this.schema = schema;
|
|
51
|
+
this.references = references;
|
|
52
|
+
this.checkFunc = checkFunc;
|
|
53
|
+
this.code = code;
|
|
54
|
+
this.hasTransform = HasTransform(schema, references);
|
|
55
|
+
}
|
|
56
|
+
/** Returns the generated assertion code used to validate this type. */
|
|
57
|
+
Code() {
|
|
58
|
+
return this.code;
|
|
59
|
+
}
|
|
60
|
+
/** Returns an iterator for each error in this value. */
|
|
61
|
+
Errors(value) {
|
|
62
|
+
return Errors(this.schema, this.references, value);
|
|
63
|
+
}
|
|
64
|
+
/** Returns true if the value matches the compiled type. */
|
|
65
|
+
Check(value) {
|
|
66
|
+
return this.checkFunc(value);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Decodes a value or throws if error
|
|
70
|
+
* @deprecated Use raw `Decode` from `Value` module
|
|
71
|
+
*/
|
|
72
|
+
Decode(value) {
|
|
73
|
+
if (!this.checkFunc(value))
|
|
74
|
+
throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
75
|
+
return this.hasTransform ? TransformDecode(this.schema, this.references, value) : value;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Encodes a value or throws if error
|
|
79
|
+
* @deprecated Use raw `Encode` from `Value` module
|
|
80
|
+
*/
|
|
81
|
+
Encode(value) {
|
|
82
|
+
const encoded = this.hasTransform ? TransformEncode(this.schema, this.references, value) : value;
|
|
83
|
+
if (!this.checkFunc(encoded))
|
|
84
|
+
throw new TransformEncodeCheckError(this.schema, value, this.Errors(value).First());
|
|
85
|
+
return encoded;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// ------------------------------------------------------------------
|
|
89
|
+
// Character
|
|
90
|
+
// ------------------------------------------------------------------
|
|
91
|
+
var Character;
|
|
92
|
+
(function (Character) {
|
|
93
|
+
function DollarSign(code) {
|
|
94
|
+
return code === 36;
|
|
95
|
+
}
|
|
96
|
+
Character.DollarSign = DollarSign;
|
|
97
|
+
function IsUnderscore(code) {
|
|
98
|
+
return code === 95;
|
|
99
|
+
}
|
|
100
|
+
Character.IsUnderscore = IsUnderscore;
|
|
101
|
+
function IsAlpha(code) {
|
|
102
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
103
|
+
}
|
|
104
|
+
Character.IsAlpha = IsAlpha;
|
|
105
|
+
function IsNumeric(code) {
|
|
106
|
+
return code >= 48 && code <= 57;
|
|
107
|
+
}
|
|
108
|
+
Character.IsNumeric = IsNumeric;
|
|
109
|
+
})(Character || (Character = {}));
|
|
110
|
+
// ------------------------------------------------------------------
|
|
111
|
+
// MemberExpression
|
|
112
|
+
// ------------------------------------------------------------------
|
|
113
|
+
var MemberExpression;
|
|
114
|
+
(function (MemberExpression) {
|
|
115
|
+
function IsFirstCharacterNumeric(value) {
|
|
116
|
+
if (value.length === 0)
|
|
117
|
+
return false;
|
|
118
|
+
return Character.IsNumeric(value.charCodeAt(0));
|
|
119
|
+
}
|
|
120
|
+
function IsAccessor(value) {
|
|
121
|
+
if (IsFirstCharacterNumeric(value))
|
|
122
|
+
return false;
|
|
123
|
+
for (let i = 0; i < value.length; i++) {
|
|
124
|
+
const code = value.charCodeAt(i);
|
|
125
|
+
const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code);
|
|
126
|
+
if (!check)
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
function EscapeHyphen(key) {
|
|
132
|
+
return key.replace(/'/g, "\\'");
|
|
133
|
+
}
|
|
134
|
+
function Encode(object, key) {
|
|
135
|
+
return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
|
|
136
|
+
}
|
|
137
|
+
MemberExpression.Encode = Encode;
|
|
138
|
+
})(MemberExpression || (MemberExpression = {}));
|
|
139
|
+
// ------------------------------------------------------------------
|
|
140
|
+
// Identifier
|
|
141
|
+
// ------------------------------------------------------------------
|
|
142
|
+
var Identifier;
|
|
143
|
+
(function (Identifier) {
|
|
144
|
+
function Encode($id) {
|
|
145
|
+
const buffer = [];
|
|
146
|
+
for (let i = 0; i < $id.length; i++) {
|
|
147
|
+
const code = $id.charCodeAt(i);
|
|
148
|
+
if (Character.IsNumeric(code) || Character.IsAlpha(code)) {
|
|
149
|
+
buffer.push($id.charAt(i));
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
buffer.push(`_${code}_`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return buffer.join('').replace(/__/g, '_');
|
|
156
|
+
}
|
|
157
|
+
Identifier.Encode = Encode;
|
|
158
|
+
})(Identifier || (Identifier = {}));
|
|
159
|
+
// ------------------------------------------------------------------
|
|
160
|
+
// LiteralString
|
|
161
|
+
// ------------------------------------------------------------------
|
|
162
|
+
var LiteralString;
|
|
163
|
+
(function (LiteralString) {
|
|
164
|
+
function Escape(content) {
|
|
165
|
+
return content.replace(/'/g, "\\'");
|
|
166
|
+
}
|
|
167
|
+
LiteralString.Escape = Escape;
|
|
168
|
+
})(LiteralString || (LiteralString = {}));
|
|
169
|
+
// ------------------------------------------------------------------
|
|
170
|
+
// Errors
|
|
171
|
+
// ------------------------------------------------------------------
|
|
172
|
+
export class TypeCompilerUnknownTypeError extends Error {
|
|
173
|
+
schema;
|
|
174
|
+
constructor(schema) {
|
|
175
|
+
super('Unknown type');
|
|
176
|
+
this.schema = schema;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export class TypeCompilerTypeGuardError extends Error {
|
|
180
|
+
schema;
|
|
181
|
+
constructor(schema) {
|
|
182
|
+
super('Preflight validation check failed to guard for the given schema');
|
|
183
|
+
this.schema = schema;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// ------------------------------------------------------------------
|
|
187
|
+
// Policy
|
|
188
|
+
// ------------------------------------------------------------------
|
|
189
|
+
export var Policy;
|
|
190
|
+
(function (Policy) {
|
|
191
|
+
function IsExactOptionalProperty(value, key, expression) {
|
|
192
|
+
return TypeSystemPolicy.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`;
|
|
193
|
+
}
|
|
194
|
+
Policy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
195
|
+
function IsObjectLike(value) {
|
|
196
|
+
return !TypeSystemPolicy.AllowArrayObject ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
197
|
+
}
|
|
198
|
+
Policy.IsObjectLike = IsObjectLike;
|
|
199
|
+
function IsRecordLike(value) {
|
|
200
|
+
return !TypeSystemPolicy.AllowArrayObject
|
|
201
|
+
? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
|
|
202
|
+
: `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`;
|
|
203
|
+
}
|
|
204
|
+
Policy.IsRecordLike = IsRecordLike;
|
|
205
|
+
function IsNumberLike(value) {
|
|
206
|
+
return !TypeSystemPolicy.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
|
|
207
|
+
}
|
|
208
|
+
Policy.IsNumberLike = IsNumberLike;
|
|
209
|
+
function IsVoidLike(value) {
|
|
210
|
+
return TypeSystemPolicy.AllowNullVoid ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`;
|
|
211
|
+
}
|
|
212
|
+
Policy.IsVoidLike = IsVoidLike;
|
|
213
|
+
})(Policy || (Policy = {}));
|
|
214
|
+
/** Compiles Types for Runtime Type Checking */
|
|
215
|
+
export var TypeCompiler;
|
|
216
|
+
(function (TypeCompiler) {
|
|
217
|
+
// ----------------------------------------------------------------
|
|
218
|
+
// Guards
|
|
219
|
+
// ----------------------------------------------------------------
|
|
220
|
+
function IsAnyOrUnknown(schema) {
|
|
221
|
+
return schema[Kind] === 'Any' || schema[Kind] === 'Unknown';
|
|
222
|
+
}
|
|
223
|
+
// ----------------------------------------------------------------
|
|
224
|
+
// Types
|
|
225
|
+
// ----------------------------------------------------------------
|
|
226
|
+
function* TAny(schema, references, value) {
|
|
227
|
+
yield 'true';
|
|
228
|
+
}
|
|
229
|
+
function* TArray(schema, references, value) {
|
|
230
|
+
yield `Array.isArray(${value})`;
|
|
231
|
+
const [parameter, accumulator] = [CreateParameter('value', 'any'), CreateParameter('acc', 'number')];
|
|
232
|
+
if (IsNumber(schema.maxItems))
|
|
233
|
+
yield `${value}.length <= ${schema.maxItems}`;
|
|
234
|
+
if (IsNumber(schema.minItems))
|
|
235
|
+
yield `${value}.length >= ${schema.minItems}`;
|
|
236
|
+
const elementExpression = CreateExpression(schema.items, references, 'value');
|
|
237
|
+
yield `${value}.every((${parameter}) => ${elementExpression})`;
|
|
238
|
+
if (IsSchemaType(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains)) {
|
|
239
|
+
const containsSchema = IsSchemaType(schema.contains) ? schema.contains : Never();
|
|
240
|
+
const checkExpression = CreateExpression(containsSchema, references, 'value');
|
|
241
|
+
const checkMinContains = IsNumber(schema.minContains) ? [`(count >= ${schema.minContains})`] : [];
|
|
242
|
+
const checkMaxContains = IsNumber(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : [];
|
|
243
|
+
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`;
|
|
244
|
+
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ');
|
|
245
|
+
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`;
|
|
246
|
+
}
|
|
247
|
+
if (schema.uniqueItems === true) {
|
|
248
|
+
const check = `const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true`;
|
|
249
|
+
const block = `const set = new Set(); for(const element of value) { ${check} }`;
|
|
250
|
+
yield `((${parameter}) => { ${block} )(${value})`;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function* TAsyncIterator(schema, references, value) {
|
|
254
|
+
yield `(typeof value === 'object' && Symbol.asyncIterator in ${value})`;
|
|
255
|
+
}
|
|
256
|
+
function* TBigInt(schema, references, value) {
|
|
257
|
+
yield `(typeof ${value} === 'bigint')`;
|
|
258
|
+
if (IsBigInt(schema.exclusiveMaximum))
|
|
259
|
+
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
260
|
+
if (IsBigInt(schema.exclusiveMinimum))
|
|
261
|
+
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
262
|
+
if (IsBigInt(schema.maximum))
|
|
263
|
+
yield `${value} <= BigInt(${schema.maximum})`;
|
|
264
|
+
if (IsBigInt(schema.minimum))
|
|
265
|
+
yield `${value} >= BigInt(${schema.minimum})`;
|
|
266
|
+
if (IsBigInt(schema.multipleOf))
|
|
267
|
+
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
268
|
+
}
|
|
269
|
+
function* TBoolean(schema, references, value) {
|
|
270
|
+
yield `(typeof ${value} === 'boolean')`;
|
|
271
|
+
}
|
|
272
|
+
function* TConstructor(schema, references, value) {
|
|
273
|
+
yield* Visit(schema.returns, references, `${value}.prototype`);
|
|
274
|
+
}
|
|
275
|
+
function* TDate(schema, references, value) {
|
|
276
|
+
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
277
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp))
|
|
278
|
+
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
279
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp))
|
|
280
|
+
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
281
|
+
if (IsNumber(schema.maximumTimestamp))
|
|
282
|
+
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
283
|
+
if (IsNumber(schema.minimumTimestamp))
|
|
284
|
+
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
285
|
+
if (IsNumber(schema.multipleOfTimestamp))
|
|
286
|
+
yield `(${value}.getTime() % ${schema.multipleOfTimestamp}) === 0`;
|
|
287
|
+
}
|
|
288
|
+
function* TFunction(schema, references, value) {
|
|
289
|
+
yield `(typeof ${value} === 'function')`;
|
|
290
|
+
}
|
|
291
|
+
function* TInteger(schema, references, value) {
|
|
292
|
+
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
293
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
294
|
+
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
295
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
296
|
+
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
297
|
+
if (IsNumber(schema.maximum))
|
|
298
|
+
yield `${value} <= ${schema.maximum}`;
|
|
299
|
+
if (IsNumber(schema.minimum))
|
|
300
|
+
yield `${value} >= ${schema.minimum}`;
|
|
301
|
+
if (IsNumber(schema.multipleOf))
|
|
302
|
+
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
303
|
+
}
|
|
304
|
+
function* TIntersect(schema, references, value) {
|
|
305
|
+
const check1 = schema.allOf.map((schema) => CreateExpression(schema, references, value)).join(' && ');
|
|
306
|
+
if (schema.unevaluatedProperties === false) {
|
|
307
|
+
const keyCheck = CreateVariable(`${new RegExp(KeyOfStringResolvePattern(schema))};`);
|
|
308
|
+
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`;
|
|
309
|
+
yield `(${check1} && ${check2})`;
|
|
310
|
+
}
|
|
311
|
+
else if (IsSchemaType(schema.unevaluatedProperties)) {
|
|
312
|
+
const keyCheck = CreateVariable(`${new RegExp(KeyOfStringResolvePattern(schema))};`);
|
|
313
|
+
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
314
|
+
yield `(${check1} && ${check2})`;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
yield `(${check1})`;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function* TIterator(schema, references, value) {
|
|
321
|
+
yield `(typeof value === 'object' && Symbol.iterator in ${value})`;
|
|
322
|
+
}
|
|
323
|
+
function* TLiteral(schema, references, value) {
|
|
324
|
+
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
325
|
+
yield `(${value} === ${schema.const})`;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
yield `(${value} === '${LiteralString.Escape(schema.const)}')`;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
function* TNever(schema, references, value) {
|
|
332
|
+
yield `false`;
|
|
333
|
+
}
|
|
334
|
+
function* TNot(schema, references, value) {
|
|
335
|
+
const expression = CreateExpression(schema.not, references, value);
|
|
336
|
+
yield `(!${expression})`;
|
|
337
|
+
}
|
|
338
|
+
function* TNull(schema, references, value) {
|
|
339
|
+
yield `(${value} === null)`;
|
|
340
|
+
}
|
|
341
|
+
function* TNumber(schema, references, value) {
|
|
342
|
+
yield Policy.IsNumberLike(value);
|
|
343
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
344
|
+
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
345
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
346
|
+
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
347
|
+
if (IsNumber(schema.maximum))
|
|
348
|
+
yield `${value} <= ${schema.maximum}`;
|
|
349
|
+
if (IsNumber(schema.minimum))
|
|
350
|
+
yield `${value} >= ${schema.minimum}`;
|
|
351
|
+
if (IsNumber(schema.multipleOf))
|
|
352
|
+
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
353
|
+
}
|
|
354
|
+
function* TObject(schema, references, value) {
|
|
355
|
+
yield Policy.IsObjectLike(value);
|
|
356
|
+
if (IsNumber(schema.minProperties))
|
|
357
|
+
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
358
|
+
if (IsNumber(schema.maxProperties))
|
|
359
|
+
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
360
|
+
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
361
|
+
for (const knownKey of knownKeys) {
|
|
362
|
+
const memberExpression = MemberExpression.Encode(value, knownKey);
|
|
363
|
+
const property = schema.properties[knownKey];
|
|
364
|
+
if (schema.required && schema.required.includes(knownKey)) {
|
|
365
|
+
yield* Visit(property, references, memberExpression);
|
|
366
|
+
if (ExtendsUndefinedCheck(property) || IsAnyOrUnknown(property))
|
|
367
|
+
yield `('${knownKey}' in ${value})`;
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const expression = CreateExpression(property, references, memberExpression);
|
|
371
|
+
yield Policy.IsExactOptionalProperty(value, knownKey, expression);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (schema.additionalProperties === false) {
|
|
375
|
+
if (schema.required && schema.required.length === knownKeys.length) {
|
|
376
|
+
yield `Object.getOwnPropertyNames(${value}).length === ${knownKeys.length}`;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
380
|
+
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
384
|
+
const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`);
|
|
385
|
+
const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
386
|
+
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
function* TPromise(schema, references, value) {
|
|
390
|
+
yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
|
|
391
|
+
}
|
|
392
|
+
function* TRecord(schema, references, value) {
|
|
393
|
+
yield Policy.IsRecordLike(value);
|
|
394
|
+
if (IsNumber(schema.minProperties))
|
|
395
|
+
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
396
|
+
if (IsNumber(schema.maxProperties))
|
|
397
|
+
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
398
|
+
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
399
|
+
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
400
|
+
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
401
|
+
const check2 = IsSchemaType(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
402
|
+
const expression = `(${variable}.test(key) ? ${check1} : ${check2})`;
|
|
403
|
+
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
|
404
|
+
}
|
|
405
|
+
function* TRef(schema, references, value) {
|
|
406
|
+
const target = Deref(schema, references);
|
|
407
|
+
// Reference: If we have seen this reference before we can just yield and return the function call.
|
|
408
|
+
// If this isn't the case we defer to visit to generate and set the function for subsequent passes.
|
|
409
|
+
if (state.functions.has(schema.$ref))
|
|
410
|
+
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
411
|
+
yield* Visit(target, references, value);
|
|
412
|
+
}
|
|
413
|
+
function* TString(schema, references, value) {
|
|
414
|
+
yield `(typeof ${value} === 'string')`;
|
|
415
|
+
if (IsNumber(schema.maxLength))
|
|
416
|
+
yield `${value}.length <= ${schema.maxLength}`;
|
|
417
|
+
if (IsNumber(schema.minLength))
|
|
418
|
+
yield `${value}.length >= ${schema.minLength}`;
|
|
419
|
+
if (schema.pattern !== undefined) {
|
|
420
|
+
const variable = CreateVariable(`${new RegExp(schema.pattern)};`);
|
|
421
|
+
yield `${variable}.test(${value})`;
|
|
422
|
+
}
|
|
423
|
+
if (schema.format !== undefined) {
|
|
424
|
+
yield `format('${schema.format}', ${value})`;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function* TSymbol(schema, references, value) {
|
|
428
|
+
yield `(typeof ${value} === 'symbol')`;
|
|
429
|
+
}
|
|
430
|
+
function* TTemplateLiteral(schema, references, value) {
|
|
431
|
+
yield `(typeof ${value} === 'string')`;
|
|
432
|
+
const variable = CreateVariable(`${new RegExp(schema.pattern)};`);
|
|
433
|
+
yield `${variable}.test(${value})`;
|
|
434
|
+
}
|
|
435
|
+
function* TThis(schema, references, value) {
|
|
436
|
+
// Note: This types are assured to be hoisted prior to this call. Just yield the function.
|
|
437
|
+
yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
438
|
+
}
|
|
439
|
+
function* TTuple(schema, references, value) {
|
|
440
|
+
yield `Array.isArray(${value})`;
|
|
441
|
+
if (schema.items === undefined)
|
|
442
|
+
return yield `${value}.length === 0`;
|
|
443
|
+
yield `(${value}.length === ${schema.maxItems})`;
|
|
444
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
445
|
+
const expression = CreateExpression(schema.items[i], references, `${value}[${i}]`);
|
|
446
|
+
yield `${expression}`;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function* TUndefined(schema, references, value) {
|
|
450
|
+
yield `${value} === undefined`;
|
|
451
|
+
}
|
|
452
|
+
function* TUnion(schema, references, value) {
|
|
453
|
+
const expressions = schema.anyOf.map((schema) => CreateExpression(schema, references, value));
|
|
454
|
+
yield `(${expressions.join(' || ')})`;
|
|
455
|
+
}
|
|
456
|
+
function* TUint8Array(schema, references, value) {
|
|
457
|
+
yield `${value} instanceof Uint8Array`;
|
|
458
|
+
if (IsNumber(schema.maxByteLength))
|
|
459
|
+
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
460
|
+
if (IsNumber(schema.minByteLength))
|
|
461
|
+
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
462
|
+
}
|
|
463
|
+
function* TUnknown(schema, references, value) {
|
|
464
|
+
yield 'true';
|
|
465
|
+
}
|
|
466
|
+
function* TVoid(schema, references, value) {
|
|
467
|
+
yield Policy.IsVoidLike(value);
|
|
468
|
+
}
|
|
469
|
+
function* TKind(schema, references, value) {
|
|
470
|
+
const instance = state.instances.size;
|
|
471
|
+
state.instances.set(instance, schema);
|
|
472
|
+
yield `kind('${schema[Kind]}', ${instance}, ${value})`;
|
|
473
|
+
}
|
|
474
|
+
function* Visit(schema, references, value, useHoisting = true) {
|
|
475
|
+
const references_ = IsString(schema.$id) ? [...references, schema] : references;
|
|
476
|
+
const schema_ = schema;
|
|
477
|
+
// --------------------------------------------------------------
|
|
478
|
+
// Hoisting
|
|
479
|
+
// --------------------------------------------------------------
|
|
480
|
+
if (useHoisting && IsString(schema.$id)) {
|
|
481
|
+
const functionName = CreateFunctionName(schema.$id);
|
|
482
|
+
if (state.functions.has(functionName)) {
|
|
483
|
+
return yield `${functionName}(${value})`;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
const functionCode = CreateFunction(functionName, schema, references, 'value', false);
|
|
487
|
+
state.functions.set(functionName, functionCode);
|
|
488
|
+
return yield `${functionName}(${value})`;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
switch (schema_[Kind]) {
|
|
492
|
+
case 'Any':
|
|
493
|
+
return yield* TAny(schema_, references_, value);
|
|
494
|
+
case 'Array':
|
|
495
|
+
return yield* TArray(schema_, references_, value);
|
|
496
|
+
case 'AsyncIterator':
|
|
497
|
+
return yield* TAsyncIterator(schema_, references_, value);
|
|
498
|
+
case 'BigInt':
|
|
499
|
+
return yield* TBigInt(schema_, references_, value);
|
|
500
|
+
case 'Boolean':
|
|
501
|
+
return yield* TBoolean(schema_, references_, value);
|
|
502
|
+
case 'Constructor':
|
|
503
|
+
return yield* TConstructor(schema_, references_, value);
|
|
504
|
+
case 'Date':
|
|
505
|
+
return yield* TDate(schema_, references_, value);
|
|
506
|
+
case 'Function':
|
|
507
|
+
return yield* TFunction(schema_, references_, value);
|
|
508
|
+
case 'Integer':
|
|
509
|
+
return yield* TInteger(schema_, references_, value);
|
|
510
|
+
case 'Intersect':
|
|
511
|
+
return yield* TIntersect(schema_, references_, value);
|
|
512
|
+
case 'Iterator':
|
|
513
|
+
return yield* TIterator(schema_, references_, value);
|
|
514
|
+
case 'Literal':
|
|
515
|
+
return yield* TLiteral(schema_, references_, value);
|
|
516
|
+
case 'Never':
|
|
517
|
+
return yield* TNever(schema_, references_, value);
|
|
518
|
+
case 'Not':
|
|
519
|
+
return yield* TNot(schema_, references_, value);
|
|
520
|
+
case 'Null':
|
|
521
|
+
return yield* TNull(schema_, references_, value);
|
|
522
|
+
case 'Number':
|
|
523
|
+
return yield* TNumber(schema_, references_, value);
|
|
524
|
+
case 'Object':
|
|
525
|
+
return yield* TObject(schema_, references_, value);
|
|
526
|
+
case 'Promise':
|
|
527
|
+
return yield* TPromise(schema_, references_, value);
|
|
528
|
+
case 'Record':
|
|
529
|
+
return yield* TRecord(schema_, references_, value);
|
|
530
|
+
case 'Ref':
|
|
531
|
+
return yield* TRef(schema_, references_, value);
|
|
532
|
+
case 'String':
|
|
533
|
+
return yield* TString(schema_, references_, value);
|
|
534
|
+
case 'Symbol':
|
|
535
|
+
return yield* TSymbol(schema_, references_, value);
|
|
536
|
+
case 'TemplateLiteral':
|
|
537
|
+
return yield* TTemplateLiteral(schema_, references_, value);
|
|
538
|
+
case 'This':
|
|
539
|
+
return yield* TThis(schema_, references_, value);
|
|
540
|
+
case 'Tuple':
|
|
541
|
+
return yield* TTuple(schema_, references_, value);
|
|
542
|
+
case 'Undefined':
|
|
543
|
+
return yield* TUndefined(schema_, references_, value);
|
|
544
|
+
case 'Union':
|
|
545
|
+
return yield* TUnion(schema_, references_, value);
|
|
546
|
+
case 'Uint8Array':
|
|
547
|
+
return yield* TUint8Array(schema_, references_, value);
|
|
548
|
+
case 'Unknown':
|
|
549
|
+
return yield* TUnknown(schema_, references_, value);
|
|
550
|
+
case 'Void':
|
|
551
|
+
return yield* TVoid(schema_, references_, value);
|
|
552
|
+
default:
|
|
553
|
+
if (!TypeRegistry.Has(schema_[Kind]))
|
|
554
|
+
throw new TypeCompilerUnknownTypeError(schema);
|
|
555
|
+
return yield* TKind(schema_, references_, value);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
// ----------------------------------------------------------------
|
|
559
|
+
// Compiler State
|
|
560
|
+
// ----------------------------------------------------------------
|
|
561
|
+
// prettier-ignore
|
|
562
|
+
const state = {
|
|
563
|
+
language: 'javascript', // target language
|
|
564
|
+
functions: new Map(), // local functions
|
|
565
|
+
variables: new Map(), // local variables
|
|
566
|
+
instances: new Map() // exterior kind instances
|
|
567
|
+
};
|
|
568
|
+
// ----------------------------------------------------------------
|
|
569
|
+
// Compiler Factory
|
|
570
|
+
// ----------------------------------------------------------------
|
|
571
|
+
function CreateExpression(schema, references, value, useHoisting = true) {
|
|
572
|
+
return `(${[...Visit(schema, references, value, useHoisting)].join(' && ')})`;
|
|
573
|
+
}
|
|
574
|
+
function CreateFunctionName($id) {
|
|
575
|
+
return `check_${Identifier.Encode($id)}`;
|
|
576
|
+
}
|
|
577
|
+
function CreateVariable(expression) {
|
|
578
|
+
const variableName = `local_${state.variables.size}`;
|
|
579
|
+
state.variables.set(variableName, `const ${variableName} = ${expression}`);
|
|
580
|
+
return variableName;
|
|
581
|
+
}
|
|
582
|
+
function CreateFunction(name, schema, references, value, useHoisting = true) {
|
|
583
|
+
const [newline, pad] = ['\n', (length) => ''.padStart(length, ' ')];
|
|
584
|
+
const parameter = CreateParameter('value', 'any');
|
|
585
|
+
const returns = CreateReturns('boolean');
|
|
586
|
+
const expression = [...Visit(schema, references, value, useHoisting)].map((expression) => `${pad(4)}${expression}`).join(` &&${newline}`);
|
|
587
|
+
return `function ${name}(${parameter})${returns} {${newline}${pad(2)}return (${newline}${expression}${newline}${pad(2)})\n}`;
|
|
588
|
+
}
|
|
589
|
+
function CreateParameter(name, type) {
|
|
590
|
+
const annotation = state.language === 'typescript' ? `: ${type}` : '';
|
|
591
|
+
return `${name}${annotation}`;
|
|
592
|
+
}
|
|
593
|
+
function CreateReturns(type) {
|
|
594
|
+
return state.language === 'typescript' ? `: ${type}` : '';
|
|
595
|
+
}
|
|
596
|
+
// ----------------------------------------------------------------
|
|
597
|
+
// Compile
|
|
598
|
+
// ----------------------------------------------------------------
|
|
599
|
+
function Build(schema, references, options) {
|
|
600
|
+
const functionCode = CreateFunction('check', schema, references, 'value'); // will populate functions and variables
|
|
601
|
+
const parameter = CreateParameter('value', 'any');
|
|
602
|
+
const returns = CreateReturns('boolean');
|
|
603
|
+
const functions = [...state.functions.values()];
|
|
604
|
+
const variables = [...state.variables.values()];
|
|
605
|
+
// prettier-ignore
|
|
606
|
+
const checkFunction = IsString(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
607
|
+
? `return function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
608
|
+
: `return ${functionCode}`;
|
|
609
|
+
return [...variables, ...functions, checkFunction].join('\n');
|
|
610
|
+
}
|
|
611
|
+
/** Generates the code used to assert this type and returns it as a string */
|
|
612
|
+
function Code(...args) {
|
|
613
|
+
const defaults = { language: 'javascript' };
|
|
614
|
+
// prettier-ignore
|
|
615
|
+
const [schema, references, options] = (args.length === 2 && IsArray(args[1]) ? [args[0], args[1], defaults] :
|
|
616
|
+
args.length === 2 && !IsArray(args[1]) ? [args[0], [], args[1]] :
|
|
617
|
+
args.length === 3 ? [args[0], args[1], args[2]] :
|
|
618
|
+
args.length === 1 ? [args[0], [], defaults] :
|
|
619
|
+
[null, [], defaults]);
|
|
620
|
+
// compiler-reset
|
|
621
|
+
state.language = options.language;
|
|
622
|
+
state.variables.clear();
|
|
623
|
+
state.functions.clear();
|
|
624
|
+
state.instances.clear();
|
|
625
|
+
if (!IsSchemaType(schema))
|
|
626
|
+
throw new TypeCompilerTypeGuardError(schema);
|
|
627
|
+
for (const schema of references)
|
|
628
|
+
if (!IsSchemaType(schema))
|
|
629
|
+
throw new TypeCompilerTypeGuardError(schema);
|
|
630
|
+
return Build(schema, references, options);
|
|
631
|
+
}
|
|
632
|
+
TypeCompiler.Code = Code;
|
|
633
|
+
/** Compiles a TypeBox type for optimal runtime type checking. Types must be valid TypeBox types of TSchema */
|
|
634
|
+
function Compile(schema, references = []) {
|
|
635
|
+
const generatedCode = Code(schema, references, { language: 'javascript' });
|
|
636
|
+
const compiledFunction = globalThis.Function('kind', 'format', 'hash', generatedCode);
|
|
637
|
+
const instances = new Map(state.instances);
|
|
638
|
+
function typeRegistryFunction(kind, instance, value) {
|
|
639
|
+
if (!TypeRegistry.Has(kind) || !instances.has(instance))
|
|
640
|
+
return false;
|
|
641
|
+
const checkFunc = TypeRegistry.Get(kind);
|
|
642
|
+
const schema = instances.get(instance);
|
|
643
|
+
return checkFunc(schema, value);
|
|
644
|
+
}
|
|
645
|
+
function formatRegistryFunction(format, value) {
|
|
646
|
+
if (!FormatRegistry.Has(format))
|
|
647
|
+
return false;
|
|
648
|
+
const checkFunc = FormatRegistry.Get(format);
|
|
649
|
+
return checkFunc(value);
|
|
650
|
+
}
|
|
651
|
+
function hashFunction(value) {
|
|
652
|
+
return Hash(value);
|
|
653
|
+
}
|
|
654
|
+
const checkFunction = compiledFunction(typeRegistryFunction, formatRegistryFunction, hashFunction);
|
|
655
|
+
return new TypeCheck(schema, references, checkFunction, generatedCode);
|
|
656
|
+
}
|
|
657
|
+
TypeCompiler.Compile = Compile;
|
|
658
|
+
})(TypeCompiler || (TypeCompiler = {}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index';
|
|
2
|
-
export * from './compiler';
|
|
1
|
+
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
2
|
+
export * from './compiler.mjs';
|