@sinclair/typebox 0.31.28 → 0.32.0-dev-1
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/compiler.d.ts +25 -18
- package/compiler/compiler.js +118 -107
- package/errors/errors.d.ts +7 -7
- package/errors/errors.js +48 -44
- package/index.d.ts +71 -0
- package/index.js +205 -0
- package/package.json +8 -19
- package/readme.md +183 -155
- package/system/errors.d.ts +14 -0
- package/system/errors.js +192 -0
- package/system/index.d.ts +3 -1
- package/system/index.js +10 -16
- package/system/policy.d.ts +21 -0
- package/system/policy.js +74 -0
- package/system/system.d.ts +3 -38
- package/system/system.js +15 -218
- package/type/any/any.d.ts +8 -0
- package/type/any/any.js +36 -0
- package/type/any/index.d.ts +1 -0
- package/type/any/index.js +44 -0
- package/type/array/array.d.ts +25 -0
- package/type/array/array.js +42 -0
- package/type/array/index.d.ts +1 -0
- package/type/array/index.js +44 -0
- package/type/async-iterator/async-iterator.d.ts +11 -0
- package/type/async-iterator/async-iterator.js +42 -0
- package/type/async-iterator/index.d.ts +1 -0
- package/type/async-iterator/index.js +44 -0
- package/type/awaited/awaited.d.ts +12 -0
- package/type/awaited/awaited.js +66 -0
- package/type/awaited/index.d.ts +1 -0
- package/type/awaited/index.js +44 -0
- package/type/bigint/bigint.d.ts +16 -0
- package/type/bigint/bigint.js +40 -0
- package/type/bigint/index.d.ts +1 -0
- package/type/bigint/index.js +44 -0
- package/type/boolean/boolean.d.ts +9 -0
- package/type/boolean/boolean.js +40 -0
- package/type/boolean/index.d.ts +1 -0
- package/type/boolean/index.js +44 -0
- package/type/clone/index.d.ts +2 -0
- package/type/clone/index.js +32 -0
- package/type/clone/type.d.ts +5 -0
- package/type/clone/type.js +41 -0
- package/type/clone/value.d.ts +2 -0
- package/type/clone/value.js +58 -0
- package/type/composite/composite.d.ts +15 -0
- package/type/composite/composite.js +47 -0
- package/type/composite/index.d.ts +1 -0
- package/type/composite/index.js +44 -0
- package/type/const/const.d.ts +30 -0
- package/type/const/const.js +84 -0
- package/type/const/index.d.ts +1 -0
- package/type/const/index.js +44 -0
- package/type/constructor/constructor.d.ts +17 -0
- package/type/constructor/constructor.js +43 -0
- package/type/constructor/index.d.ts +1 -0
- package/type/constructor/index.js +44 -0
- package/type/constructor-parameters/constructor-parameters.d.ts +7 -0
- package/type/constructor-parameters/constructor-parameters.js +37 -0
- package/type/constructor-parameters/index.d.ts +1 -0
- package/type/constructor-parameters/index.js +44 -0
- package/type/date/date.d.ts +21 -0
- package/type/date/date.js +40 -0
- package/type/date/index.d.ts +1 -0
- package/type/date/index.js +44 -0
- package/type/deref/deref.d.ts +24 -0
- package/type/deref/deref.js +136 -0
- package/type/deref/index.d.ts +1 -0
- package/type/deref/index.js +44 -0
- package/type/discard/discard.d.ts +1 -0
- package/type/discard/discard.js +38 -0
- package/type/discard/index.d.ts +1 -0
- package/type/discard/index.js +44 -0
- package/type/enum/enum.d.ts +14 -0
- package/type/enum/enum.js +46 -0
- package/type/enum/index.d.ts +1 -0
- package/type/enum/index.js +44 -0
- package/type/exclude/exclude.d.ts +21 -0
- package/type/exclude/exclude.js +54 -0
- package/type/exclude/index.d.ts +1 -0
- package/type/exclude/index.js +44 -0
- package/type/extends/extends-check.d.ts +9 -0
- package/type/extends/extends-check.js +654 -0
- package/type/extends/extends-undefined.d.ts +3 -0
- package/type/extends/extends-undefined.js +51 -0
- package/type/extends/extends.d.ts +9 -0
- package/type/extends/extends.js +47 -0
- package/type/extends/index.d.ts +3 -0
- package/type/extends/index.js +46 -0
- package/type/extract/extract.d.ts +20 -0
- package/type/extract/extract.js +54 -0
- package/type/extract/index.d.ts +1 -0
- package/type/extract/index.js +44 -0
- package/type/function/function.d.ts +17 -0
- package/type/function/function.js +43 -0
- package/type/function/index.d.ts +1 -0
- package/type/function/index.js +44 -0
- package/type/guard/index.d.ts +2 -0
- package/type/guard/index.js +32 -0
- package/type/guard/type.d.ts +130 -0
- package/type/guard/type.js +515 -0
- package/type/guard/value.d.ts +28 -0
- package/type/guard/value.js +100 -0
- package/type/helpers/helpers.d.ts +42 -0
- package/type/helpers/helpers.js +35 -0
- package/type/helpers/index.d.ts +1 -0
- package/type/helpers/index.js +44 -0
- package/type/index.d.ts +70 -0
- package/type/index.js +113 -0
- package/type/indexed/index.d.ts +3 -0
- package/type/indexed/index.js +46 -0
- package/type/indexed/indexed-key.d.ts +17 -0
- package/type/indexed/indexed-key.js +65 -0
- package/type/indexed/indexed-type.d.ts +33 -0
- package/type/indexed/indexed-type.js +106 -0
- package/type/indexed/indexed.d.ts +8 -0
- package/type/indexed/indexed.js +41 -0
- package/type/instance-type/index.d.ts +1 -0
- package/type/instance-type/index.js +44 -0
- package/type/instance-type/instance-type.d.ts +5 -0
- package/type/instance-type/instance-type.js +36 -0
- package/type/integer/index.d.ts +1 -0
- package/type/integer/index.js +44 -0
- package/type/integer/integer.d.ts +16 -0
- package/type/integer/integer.js +40 -0
- package/type/intersect/index.d.ts +1 -0
- package/type/intersect/index.js +44 -0
- package/type/intersect/intersect.d.ts +22 -0
- package/type/intersect/intersect.js +59 -0
- package/type/intrinsic/index.d.ts +1 -0
- package/type/intrinsic/index.js +44 -0
- package/type/intrinsic/intrinsic.d.ts +27 -0
- package/type/intrinsic/intrinsic.js +106 -0
- package/type/iterator/index.d.ts +1 -0
- package/type/iterator/index.js +44 -0
- package/type/iterator/iterator.d.ts +11 -0
- package/type/iterator/iterator.js +42 -0
- package/type/keyof/index.d.ts +3 -0
- package/type/keyof/index.js +46 -0
- package/type/keyof/keyof-string.d.ts +30 -0
- package/type/keyof/keyof-string.js +103 -0
- package/type/keyof/keyof-type.d.ts +11 -0
- package/type/keyof/keyof-type.js +48 -0
- package/type/keyof/keyof.d.ts +5 -0
- package/type/keyof/keyof.js +38 -0
- package/type/literal/index.d.ts +1 -0
- package/type/literal/index.js +44 -0
- package/type/literal/literal.d.ts +10 -0
- package/type/literal/literal.js +41 -0
- package/type/mapped/index.d.ts +1 -0
- package/type/mapped/index.js +44 -0
- package/type/mapped/mapped.d.ts +26 -0
- package/type/mapped/mapped.js +47 -0
- package/type/modifiers/index.d.ts +1 -0
- package/type/modifiers/index.js +44 -0
- package/type/modifiers/modifiers.d.ts +18 -0
- package/type/modifiers/modifiers.js +78 -0
- package/type/never/index.d.ts +1 -0
- package/type/never/index.js +44 -0
- package/type/never/never.d.ts +9 -0
- package/type/never/never.js +40 -0
- package/type/not/index.d.ts +1 -0
- package/type/not/index.js +44 -0
- package/type/not/not.d.ts +10 -0
- package/type/not/not.js +41 -0
- package/type/null/index.d.ts +1 -0
- package/type/null/index.js +44 -0
- package/type/null/null.d.ts +9 -0
- package/type/null/null.js +40 -0
- package/type/number/index.d.ts +1 -0
- package/type/number/index.js +44 -0
- package/type/number/number.d.ts +16 -0
- package/type/number/number.js +40 -0
- package/type/object/index.d.ts +1 -0
- package/type/object/index.js +44 -0
- package/type/object/object.d.ts +44 -0
- package/type/object/object.js +47 -0
- package/type/omit/index.d.ts +1 -0
- package/type/omit/index.js +44 -0
- package/type/omit/omit.d.ts +21 -0
- package/type/omit/omit.js +75 -0
- package/type/operators/index.d.ts +1 -0
- package/type/operators/index.js +44 -0
- package/type/operators/operators.d.ts +24 -0
- package/type/operators/operators.js +119 -0
- package/type/optional/index.d.ts +1 -0
- package/type/optional/index.js +44 -0
- package/type/optional/optional.d.ts +7 -0
- package/type/optional/optional.js +37 -0
- package/type/parameters/index.d.ts +1 -0
- package/type/parameters/index.js +44 -0
- package/type/parameters/parameters.d.ts +7 -0
- package/type/parameters/parameters.js +37 -0
- package/type/partial/index.d.ts +1 -0
- package/type/partial/index.js +44 -0
- package/type/partial/partial.d.ts +23 -0
- package/type/partial/partial.js +73 -0
- package/type/patterns/index.d.ts +1 -0
- package/type/patterns/index.js +44 -0
- package/type/patterns/patterns.d.ts +6 -0
- package/type/patterns/patterns.js +36 -0
- package/type/pick/index.d.ts +1 -0
- package/type/pick/index.js +44 -0
- package/type/pick/pick.d.ts +21 -0
- package/type/pick/pick.js +66 -0
- package/type/promise/index.d.ts +1 -0
- package/type/promise/index.js +44 -0
- package/type/promise/promise.d.ts +11 -0
- package/type/promise/promise.js +42 -0
- package/type/readonly/index.d.ts +1 -0
- package/type/readonly/index.js +44 -0
- package/type/readonly/readonly.d.ts +7 -0
- package/type/readonly/readonly.js +37 -0
- package/type/readonly-optional/index.d.ts +1 -0
- package/type/readonly-optional/index.js +44 -0
- package/type/readonly-optional/readonly-optional.d.ts +6 -0
- package/type/readonly-optional/readonly-optional.js +37 -0
- package/type/record/index.d.ts +1 -0
- package/type/record/index.js +44 -0
- package/type/record/record.d.ts +55 -0
- package/type/record/record.js +97 -0
- package/type/recursive/index.d.ts +1 -0
- package/type/recursive/index.js +44 -0
- package/type/recursive/recursive.d.ts +15 -0
- package/type/recursive/recursive.js +48 -0
- package/type/ref/index.d.ts +1 -0
- package/type/ref/index.js +44 -0
- package/type/ref/ref.d.ts +12 -0
- package/type/ref/ref.js +45 -0
- package/type/regexp/index.d.ts +1 -0
- package/type/regexp/index.js +44 -0
- package/type/regexp/regexp.d.ts +7 -0
- package/type/regexp/regexp.js +38 -0
- package/type/registry/format.d.ts +13 -0
- package/type/registry/format.js +62 -0
- package/type/registry/index.d.ts +2 -0
- package/type/registry/index.js +32 -0
- package/type/registry/type.d.ts +13 -0
- package/type/registry/type.js +62 -0
- package/type/required/index.d.ts +1 -0
- package/type/required/index.js +44 -0
- package/type/required/required.d.ts +23 -0
- package/type/required/required.js +72 -0
- package/type/rest/index.d.ts +1 -0
- package/type/rest/index.js +44 -0
- package/type/rest/rest.d.ts +10 -0
- package/type/rest/rest.js +41 -0
- package/type/return-type/index.d.ts +1 -0
- package/type/return-type/index.js +44 -0
- package/type/return-type/return-type.d.ts +5 -0
- package/type/return-type/return-type.js +36 -0
- package/type/schema/anyschema.d.ts +32 -0
- package/type/schema/anyschema.js +29 -0
- package/type/schema/index.d.ts +2 -0
- package/type/schema/index.js +45 -0
- package/type/schema/schema.d.ts +29 -0
- package/type/schema/schema.js +30 -0
- package/type/static/index.d.ts +1 -0
- package/type/static/index.js +44 -0
- package/type/static/static.d.ts +34 -0
- package/type/static/static.js +29 -0
- package/type/strict/index.d.ts +1 -0
- package/type/strict/index.js +44 -0
- package/type/strict/strict.d.ts +3 -0
- package/type/strict/strict.js +35 -0
- package/type/string/index.d.ts +1 -0
- package/type/string/index.js +44 -0
- package/type/string/string.d.ts +25 -0
- package/type/string/string.js +36 -0
- package/type/symbol/index.d.ts +1 -0
- package/type/symbol/index.js +44 -0
- package/type/symbol/symbol.d.ts +10 -0
- package/type/symbol/symbol.js +36 -0
- package/type/symbols/index.d.ts +1 -0
- package/type/symbols/index.js +44 -0
- package/type/symbols/symbols.d.ts +10 -0
- package/type/symbols/symbols.js +40 -0
- package/type/template-literal/finite.d.ts +16 -0
- package/type/template-literal/finite.js +71 -0
- package/type/template-literal/generate.d.ts +15 -0
- package/type/template-literal/generate.js +74 -0
- package/type/template-literal/index.d.ts +7 -0
- package/type/template-literal/index.js +50 -0
- package/type/template-literal/parser.d.ts +19 -0
- package/type/template-literal/parser.js +175 -0
- package/type/template-literal/pattern.d.ts +4 -0
- package/type/template-literal/pattern.js +61 -0
- package/type/template-literal/syntax.d.ts +20 -0
- package/type/template-literal/syntax.js +87 -0
- package/type/template-literal/template-literal.d.ts +30 -0
- package/type/template-literal/template-literal.js +43 -0
- package/type/template-literal/union.d.ts +7 -0
- package/type/template-literal/union.js +48 -0
- package/type/transform/index.d.ts +1 -0
- package/type/transform/index.js +44 -0
- package/type/transform/transform.d.ts +29 -0
- package/type/transform/transform.js +72 -0
- package/type/tuple/index.d.ts +1 -0
- package/type/tuple/index.js +44 -0
- package/type/tuple/tuple.d.ts +15 -0
- package/type/tuple/tuple.js +42 -0
- package/type/type/index.d.ts +6 -0
- package/type/type/index.js +44 -0
- package/type/type/javascript.d.ts +58 -0
- package/type/type/javascript.js +120 -0
- package/type/type/json.d.ts +139 -0
- package/type/type/json.js +255 -0
- package/type/type/type.d.ts +58 -0
- package/type/type/type.js +153 -0
- package/type/uint8array/index.d.ts +1 -0
- package/type/uint8array/index.js +44 -0
- package/type/uint8array/uint8array.d.ts +13 -0
- package/type/uint8array/uint8array.js +36 -0
- package/type/undefined/index.d.ts +1 -0
- package/type/undefined/index.js +44 -0
- package/type/undefined/undefined.d.ts +9 -0
- package/type/undefined/undefined.js +36 -0
- package/type/union/index.d.ts +1 -0
- package/type/union/index.js +44 -0
- package/type/union/union.d.ts +16 -0
- package/type/union/union.js +50 -0
- package/type/unknown/index.d.ts +1 -0
- package/type/unknown/index.js +44 -0
- package/type/unknown/unknown.d.ts +8 -0
- package/type/unknown/unknown.js +39 -0
- package/type/unsafe/index.d.ts +1 -0
- package/type/unsafe/index.js +44 -0
- package/type/unsafe/unsafe.d.ts +11 -0
- package/type/unsafe/unsafe.js +39 -0
- package/type/void/index.d.ts +1 -0
- package/type/void/index.js +44 -0
- package/type/void/void.d.ts +9 -0
- package/type/void/void.js +40 -0
- package/value/cast/cast.d.ts +23 -0
- package/value/{cast.js → cast/cast.js} +62 -83
- package/value/cast/index.d.ts +1 -0
- package/value/cast/index.js +44 -0
- package/value/check/check.d.ts +10 -0
- package/value/{check.js → check/check.js} +50 -45
- package/value/check/index.d.ts +1 -0
- package/value/check/index.js +44 -0
- package/value/clean/clean.d.ts +5 -0
- package/value/clean/clean.js +167 -0
- package/value/clean/index.d.ts +1 -0
- package/value/clean/index.js +44 -0
- package/value/{clone.js → clone/clone.js} +6 -6
- package/value/clone/index.d.ts +1 -0
- package/value/clone/index.js +44 -0
- package/value/convert/convert.d.ts +9 -0
- package/value/{convert.js → convert/convert.js} +93 -116
- package/value/convert/index.d.ts +1 -0
- package/value/convert/index.js +44 -0
- package/value/create/create.d.ts +31 -0
- package/value/{create.js → create/create.js} +69 -62
- package/value/create/index.d.ts +1 -0
- package/value/create/index.js +44 -0
- package/value/default/default.d.ts +5 -0
- package/value/default/default.js +175 -0
- package/value/default/index.d.ts +1 -0
- package/value/default/index.js +44 -0
- package/value/delta/delta.d.ts +41 -0
- package/value/{delta.js → delta/delta.js} +57 -53
- package/value/delta/index.d.ts +1 -0
- package/value/delta/index.js +44 -0
- package/value/{deref.d.ts → deref/deref.d.ts} +4 -2
- package/value/{deref.js → deref/deref.js} +1 -2
- package/value/deref/index.d.ts +1 -0
- package/value/deref/index.js +44 -0
- package/value/{equal.js → equal/equal.js} +14 -14
- package/value/equal/index.d.ts +1 -0
- package/value/equal/index.js +44 -0
- package/value/guard/index.d.ts +1 -0
- package/value/guard/index.js +44 -0
- package/value/{hash.d.ts → hash/hash.d.ts} +0 -13
- package/value/{hash.js → hash/hash.js} +26 -26
- package/value/hash/index.d.ts +1 -0
- package/value/hash/index.js +44 -0
- package/value/index.d.ts +17 -4
- package/value/index.js +91 -10
- package/value/mutate/index.d.ts +1 -0
- package/value/mutate/index.js +44 -0
- package/value/{mutate.d.ts → mutate/mutate.d.ts} +1 -1
- package/value/{mutate.js → mutate/mutate.js} +25 -25
- package/value/pointer/index.d.ts +1 -0
- package/value/pointer/index.js +31 -0
- package/value/pointer/pointer.d.ts +21 -0
- package/value/pointer/pointer.js +151 -0
- package/value/transform/decode.d.ts +19 -0
- package/value/transform/decode.js +211 -0
- package/value/transform/encode.d.ts +20 -0
- package/value/transform/encode.js +219 -0
- package/value/transform/has.d.ts +3 -0
- package/value/transform/has.js +146 -0
- package/value/transform/index.d.ts +3 -0
- package/value/transform/index.js +46 -0
- package/value/value/index.d.ts +1 -0
- package/value/value/index.js +31 -0
- package/value/value/value.d.ts +53 -0
- package/value/value/value.js +125 -0
- 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/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/value/{clone.d.ts → clone/clone.d.ts} +0 -0
- /package/value/{equal.d.ts → equal/equal.d.ts} +0 -0
- /package/value/{guard.d.ts → guard/guard.d.ts} +0 -0
- /package/value/{guard.js → guard/guard.js} +0 -0
package/typebox.js
DELETED
|
@@ -1,2364 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Type = exports.JsonType = exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.TypeBuilder = exports.TypeBuilderError = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralGeneratorError = exports.TemplateLiteralFinite = exports.TemplateLiteralFiniteError = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.TemplateLiteralPatternError = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyArrayResolverError = exports.KeyResolver = exports.ObjectMap = exports.Intrinsic = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.TypeExtendsError = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeBoxError = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = exports.Transform = void 0;
|
|
31
|
-
// --------------------------------------------------------------------------
|
|
32
|
-
// Symbols
|
|
33
|
-
// --------------------------------------------------------------------------
|
|
34
|
-
exports.Transform = Symbol.for('TypeBox.Transform');
|
|
35
|
-
exports.Readonly = Symbol.for('TypeBox.Readonly');
|
|
36
|
-
exports.Optional = Symbol.for('TypeBox.Optional');
|
|
37
|
-
exports.Hint = Symbol.for('TypeBox.Hint');
|
|
38
|
-
exports.Kind = Symbol.for('TypeBox.Kind');
|
|
39
|
-
// --------------------------------------------------------------------------
|
|
40
|
-
// Patterns
|
|
41
|
-
// --------------------------------------------------------------------------
|
|
42
|
-
exports.PatternBoolean = '(true|false)';
|
|
43
|
-
exports.PatternNumber = '(0|[1-9][0-9]*)';
|
|
44
|
-
exports.PatternString = '(.*)';
|
|
45
|
-
exports.PatternBooleanExact = `^${exports.PatternBoolean}$`;
|
|
46
|
-
exports.PatternNumberExact = `^${exports.PatternNumber}$`;
|
|
47
|
-
exports.PatternStringExact = `^${exports.PatternString}$`;
|
|
48
|
-
/** A registry for user defined types */
|
|
49
|
-
var TypeRegistry;
|
|
50
|
-
(function (TypeRegistry) {
|
|
51
|
-
const map = new Map();
|
|
52
|
-
/** Returns the entries in this registry */
|
|
53
|
-
function Entries() {
|
|
54
|
-
return new Map(map);
|
|
55
|
-
}
|
|
56
|
-
TypeRegistry.Entries = Entries;
|
|
57
|
-
/** Clears all user defined types */
|
|
58
|
-
function Clear() {
|
|
59
|
-
return map.clear();
|
|
60
|
-
}
|
|
61
|
-
TypeRegistry.Clear = Clear;
|
|
62
|
-
/** Deletes a registered type */
|
|
63
|
-
function Delete(kind) {
|
|
64
|
-
return map.delete(kind);
|
|
65
|
-
}
|
|
66
|
-
TypeRegistry.Delete = Delete;
|
|
67
|
-
/** Returns true if this registry contains this kind */
|
|
68
|
-
function Has(kind) {
|
|
69
|
-
return map.has(kind);
|
|
70
|
-
}
|
|
71
|
-
TypeRegistry.Has = Has;
|
|
72
|
-
/** Sets a validation function for a user defined type */
|
|
73
|
-
function Set(kind, func) {
|
|
74
|
-
map.set(kind, func);
|
|
75
|
-
}
|
|
76
|
-
TypeRegistry.Set = Set;
|
|
77
|
-
/** Gets a custom validation function for a user defined type */
|
|
78
|
-
function Get(kind) {
|
|
79
|
-
return map.get(kind);
|
|
80
|
-
}
|
|
81
|
-
TypeRegistry.Get = Get;
|
|
82
|
-
})(TypeRegistry || (exports.TypeRegistry = TypeRegistry = {}));
|
|
83
|
-
// --------------------------------------------------------------------------
|
|
84
|
-
// TypeBoxError
|
|
85
|
-
// --------------------------------------------------------------------------
|
|
86
|
-
class TypeBoxError extends Error {
|
|
87
|
-
constructor(message) {
|
|
88
|
-
super(message);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
exports.TypeBoxError = TypeBoxError;
|
|
92
|
-
/** A registry for user defined string formats */
|
|
93
|
-
var FormatRegistry;
|
|
94
|
-
(function (FormatRegistry) {
|
|
95
|
-
const map = new Map();
|
|
96
|
-
/** Returns the entries in this registry */
|
|
97
|
-
function Entries() {
|
|
98
|
-
return new Map(map);
|
|
99
|
-
}
|
|
100
|
-
FormatRegistry.Entries = Entries;
|
|
101
|
-
/** Clears all user defined string formats */
|
|
102
|
-
function Clear() {
|
|
103
|
-
return map.clear();
|
|
104
|
-
}
|
|
105
|
-
FormatRegistry.Clear = Clear;
|
|
106
|
-
/** Deletes a registered format */
|
|
107
|
-
function Delete(format) {
|
|
108
|
-
return map.delete(format);
|
|
109
|
-
}
|
|
110
|
-
FormatRegistry.Delete = Delete;
|
|
111
|
-
/** Returns true if the user defined string format exists */
|
|
112
|
-
function Has(format) {
|
|
113
|
-
return map.has(format);
|
|
114
|
-
}
|
|
115
|
-
FormatRegistry.Has = Has;
|
|
116
|
-
/** Sets a validation function for a user defined string format */
|
|
117
|
-
function Set(format, func) {
|
|
118
|
-
map.set(format, func);
|
|
119
|
-
}
|
|
120
|
-
FormatRegistry.Set = Set;
|
|
121
|
-
/** Gets a validation function for a user defined string format */
|
|
122
|
-
function Get(format) {
|
|
123
|
-
return map.get(format);
|
|
124
|
-
}
|
|
125
|
-
FormatRegistry.Get = Get;
|
|
126
|
-
})(FormatRegistry || (exports.FormatRegistry = FormatRegistry = {}));
|
|
127
|
-
// --------------------------------------------------------------------------
|
|
128
|
-
// ValueGuard
|
|
129
|
-
// --------------------------------------------------------------------------
|
|
130
|
-
/** Provides functions to type guard raw JavaScript values */
|
|
131
|
-
var ValueGuard;
|
|
132
|
-
(function (ValueGuard) {
|
|
133
|
-
/** Returns true if this value is an array */
|
|
134
|
-
function IsArray(value) {
|
|
135
|
-
return Array.isArray(value);
|
|
136
|
-
}
|
|
137
|
-
ValueGuard.IsArray = IsArray;
|
|
138
|
-
/** Returns true if this value is bigint */
|
|
139
|
-
function IsBigInt(value) {
|
|
140
|
-
return typeof value === 'bigint';
|
|
141
|
-
}
|
|
142
|
-
ValueGuard.IsBigInt = IsBigInt;
|
|
143
|
-
/** Returns true if this value is a boolean */
|
|
144
|
-
function IsBoolean(value) {
|
|
145
|
-
return typeof value === 'boolean';
|
|
146
|
-
}
|
|
147
|
-
ValueGuard.IsBoolean = IsBoolean;
|
|
148
|
-
/** Returns true if this value is a Date object */
|
|
149
|
-
function IsDate(value) {
|
|
150
|
-
return value instanceof globalThis.Date;
|
|
151
|
-
}
|
|
152
|
-
ValueGuard.IsDate = IsDate;
|
|
153
|
-
/** Returns true if this value is null */
|
|
154
|
-
function IsNull(value) {
|
|
155
|
-
return value === null;
|
|
156
|
-
}
|
|
157
|
-
ValueGuard.IsNull = IsNull;
|
|
158
|
-
/** Returns true if this value is number */
|
|
159
|
-
function IsNumber(value) {
|
|
160
|
-
return typeof value === 'number';
|
|
161
|
-
}
|
|
162
|
-
ValueGuard.IsNumber = IsNumber;
|
|
163
|
-
/** Returns true if this value is an object */
|
|
164
|
-
function IsObject(value) {
|
|
165
|
-
return typeof value === 'object' && value !== null;
|
|
166
|
-
}
|
|
167
|
-
ValueGuard.IsObject = IsObject;
|
|
168
|
-
/** Returns true if this value is string */
|
|
169
|
-
function IsString(value) {
|
|
170
|
-
return typeof value === 'string';
|
|
171
|
-
}
|
|
172
|
-
ValueGuard.IsString = IsString;
|
|
173
|
-
/** Returns true if this value is a Uint8Array */
|
|
174
|
-
function IsUint8Array(value) {
|
|
175
|
-
return value instanceof globalThis.Uint8Array;
|
|
176
|
-
}
|
|
177
|
-
ValueGuard.IsUint8Array = IsUint8Array;
|
|
178
|
-
/** Returns true if this value is undefined */
|
|
179
|
-
function IsUndefined(value) {
|
|
180
|
-
return value === undefined;
|
|
181
|
-
}
|
|
182
|
-
ValueGuard.IsUndefined = IsUndefined;
|
|
183
|
-
})(ValueGuard || (exports.ValueGuard = ValueGuard = {}));
|
|
184
|
-
// --------------------------------------------------------------------------
|
|
185
|
-
// TypeGuard
|
|
186
|
-
// --------------------------------------------------------------------------
|
|
187
|
-
class TypeGuardUnknownTypeError extends TypeBoxError {
|
|
188
|
-
}
|
|
189
|
-
exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
|
|
190
|
-
/** Provides functions to test if JavaScript values are TypeBox types */
|
|
191
|
-
var TypeGuard;
|
|
192
|
-
(function (TypeGuard) {
|
|
193
|
-
function IsPattern(value) {
|
|
194
|
-
try {
|
|
195
|
-
new RegExp(value);
|
|
196
|
-
return true;
|
|
197
|
-
}
|
|
198
|
-
catch {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
function IsControlCharacterFree(value) {
|
|
203
|
-
if (!ValueGuard.IsString(value))
|
|
204
|
-
return false;
|
|
205
|
-
for (let i = 0; i < value.length; i++) {
|
|
206
|
-
const code = value.charCodeAt(i);
|
|
207
|
-
if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
function IsAdditionalProperties(value) {
|
|
214
|
-
return IsOptionalBoolean(value) || TSchema(value);
|
|
215
|
-
}
|
|
216
|
-
function IsOptionalBigInt(value) {
|
|
217
|
-
return ValueGuard.IsUndefined(value) || ValueGuard.IsBigInt(value);
|
|
218
|
-
}
|
|
219
|
-
function IsOptionalNumber(value) {
|
|
220
|
-
return ValueGuard.IsUndefined(value) || ValueGuard.IsNumber(value);
|
|
221
|
-
}
|
|
222
|
-
function IsOptionalBoolean(value) {
|
|
223
|
-
return ValueGuard.IsUndefined(value) || ValueGuard.IsBoolean(value);
|
|
224
|
-
}
|
|
225
|
-
function IsOptionalString(value) {
|
|
226
|
-
return ValueGuard.IsUndefined(value) || ValueGuard.IsString(value);
|
|
227
|
-
}
|
|
228
|
-
function IsOptionalPattern(value) {
|
|
229
|
-
return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
|
|
230
|
-
}
|
|
231
|
-
function IsOptionalFormat(value) {
|
|
232
|
-
return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value));
|
|
233
|
-
}
|
|
234
|
-
function IsOptionalSchema(value) {
|
|
235
|
-
return ValueGuard.IsUndefined(value) || TSchema(value);
|
|
236
|
-
}
|
|
237
|
-
// ----------------------------------------------------------------
|
|
238
|
-
// Types
|
|
239
|
-
// ----------------------------------------------------------------
|
|
240
|
-
/** Returns true if the given value is TAny */
|
|
241
|
-
function TAny(schema) {
|
|
242
|
-
// prettier-ignore
|
|
243
|
-
return (TKindOf(schema, 'Any') &&
|
|
244
|
-
IsOptionalString(schema.$id));
|
|
245
|
-
}
|
|
246
|
-
TypeGuard.TAny = TAny;
|
|
247
|
-
/** Returns true if the given value is TArray */
|
|
248
|
-
function TArray(schema) {
|
|
249
|
-
return (TKindOf(schema, 'Array') &&
|
|
250
|
-
schema.type === 'array' &&
|
|
251
|
-
IsOptionalString(schema.$id) &&
|
|
252
|
-
TSchema(schema.items) &&
|
|
253
|
-
IsOptionalNumber(schema.minItems) &&
|
|
254
|
-
IsOptionalNumber(schema.maxItems) &&
|
|
255
|
-
IsOptionalBoolean(schema.uniqueItems) &&
|
|
256
|
-
IsOptionalSchema(schema.contains) &&
|
|
257
|
-
IsOptionalNumber(schema.minContains) &&
|
|
258
|
-
IsOptionalNumber(schema.maxContains));
|
|
259
|
-
}
|
|
260
|
-
TypeGuard.TArray = TArray;
|
|
261
|
-
/** Returns true if the given value is TAsyncIterator */
|
|
262
|
-
function TAsyncIterator(schema) {
|
|
263
|
-
// prettier-ignore
|
|
264
|
-
return (TKindOf(schema, 'AsyncIterator') &&
|
|
265
|
-
schema.type === 'AsyncIterator' &&
|
|
266
|
-
IsOptionalString(schema.$id) &&
|
|
267
|
-
TSchema(schema.items));
|
|
268
|
-
}
|
|
269
|
-
TypeGuard.TAsyncIterator = TAsyncIterator;
|
|
270
|
-
/** Returns true if the given value is TBigInt */
|
|
271
|
-
function TBigInt(schema) {
|
|
272
|
-
// prettier-ignore
|
|
273
|
-
return (TKindOf(schema, 'BigInt') &&
|
|
274
|
-
schema.type === 'bigint' &&
|
|
275
|
-
IsOptionalString(schema.$id) &&
|
|
276
|
-
IsOptionalBigInt(schema.exclusiveMaximum) &&
|
|
277
|
-
IsOptionalBigInt(schema.exclusiveMinimum) &&
|
|
278
|
-
IsOptionalBigInt(schema.maximum) &&
|
|
279
|
-
IsOptionalBigInt(schema.minimum) &&
|
|
280
|
-
IsOptionalBigInt(schema.multipleOf));
|
|
281
|
-
}
|
|
282
|
-
TypeGuard.TBigInt = TBigInt;
|
|
283
|
-
/** Returns true if the given value is TBoolean */
|
|
284
|
-
function TBoolean(schema) {
|
|
285
|
-
// prettier-ignore
|
|
286
|
-
return (TKindOf(schema, 'Boolean') &&
|
|
287
|
-
schema.type === 'boolean' &&
|
|
288
|
-
IsOptionalString(schema.$id));
|
|
289
|
-
}
|
|
290
|
-
TypeGuard.TBoolean = TBoolean;
|
|
291
|
-
/** Returns true if the given value is TConstructor */
|
|
292
|
-
function TConstructor(schema) {
|
|
293
|
-
// prettier-ignore
|
|
294
|
-
return (TKindOf(schema, 'Constructor') &&
|
|
295
|
-
schema.type === 'Constructor' &&
|
|
296
|
-
IsOptionalString(schema.$id) &&
|
|
297
|
-
ValueGuard.IsArray(schema.parameters) &&
|
|
298
|
-
schema.parameters.every(schema => TSchema(schema)) &&
|
|
299
|
-
TSchema(schema.returns));
|
|
300
|
-
}
|
|
301
|
-
TypeGuard.TConstructor = TConstructor;
|
|
302
|
-
/** Returns true if the given value is TDate */
|
|
303
|
-
function TDate(schema) {
|
|
304
|
-
return (TKindOf(schema, 'Date') &&
|
|
305
|
-
schema.type === 'Date' &&
|
|
306
|
-
IsOptionalString(schema.$id) &&
|
|
307
|
-
IsOptionalNumber(schema.exclusiveMaximumTimestamp) &&
|
|
308
|
-
IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
|
|
309
|
-
IsOptionalNumber(schema.maximumTimestamp) &&
|
|
310
|
-
IsOptionalNumber(schema.minimumTimestamp) &&
|
|
311
|
-
IsOptionalNumber(schema.multipleOfTimestamp));
|
|
312
|
-
}
|
|
313
|
-
TypeGuard.TDate = TDate;
|
|
314
|
-
/** Returns true if the given value is TFunction */
|
|
315
|
-
function TFunction(schema) {
|
|
316
|
-
// prettier-ignore
|
|
317
|
-
return (TKindOf(schema, 'Function') &&
|
|
318
|
-
schema.type === 'Function' &&
|
|
319
|
-
IsOptionalString(schema.$id) &&
|
|
320
|
-
ValueGuard.IsArray(schema.parameters) &&
|
|
321
|
-
schema.parameters.every(schema => TSchema(schema)) &&
|
|
322
|
-
TSchema(schema.returns));
|
|
323
|
-
}
|
|
324
|
-
TypeGuard.TFunction = TFunction;
|
|
325
|
-
/** Returns true if the given value is TInteger */
|
|
326
|
-
function TInteger(schema) {
|
|
327
|
-
return (TKindOf(schema, 'Integer') &&
|
|
328
|
-
schema.type === 'integer' &&
|
|
329
|
-
IsOptionalString(schema.$id) &&
|
|
330
|
-
IsOptionalNumber(schema.exclusiveMaximum) &&
|
|
331
|
-
IsOptionalNumber(schema.exclusiveMinimum) &&
|
|
332
|
-
IsOptionalNumber(schema.maximum) &&
|
|
333
|
-
IsOptionalNumber(schema.minimum) &&
|
|
334
|
-
IsOptionalNumber(schema.multipleOf));
|
|
335
|
-
}
|
|
336
|
-
TypeGuard.TInteger = TInteger;
|
|
337
|
-
/** Returns true if the given value is TIntersect */
|
|
338
|
-
function TIntersect(schema) {
|
|
339
|
-
// prettier-ignore
|
|
340
|
-
return (TKindOf(schema, 'Intersect') &&
|
|
341
|
-
(ValueGuard.IsString(schema.type) && schema.type !== 'object' ? false : true) &&
|
|
342
|
-
ValueGuard.IsArray(schema.allOf) &&
|
|
343
|
-
schema.allOf.every(schema => TSchema(schema) && !TTransform(schema)) &&
|
|
344
|
-
IsOptionalString(schema.type) &&
|
|
345
|
-
(IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
|
|
346
|
-
IsOptionalString(schema.$id));
|
|
347
|
-
}
|
|
348
|
-
TypeGuard.TIntersect = TIntersect;
|
|
349
|
-
/** Returns true if the given value is TIterator */
|
|
350
|
-
function TIterator(schema) {
|
|
351
|
-
// prettier-ignore
|
|
352
|
-
return (TKindOf(schema, 'Iterator') &&
|
|
353
|
-
schema.type === 'Iterator' &&
|
|
354
|
-
IsOptionalString(schema.$id) &&
|
|
355
|
-
TSchema(schema.items));
|
|
356
|
-
}
|
|
357
|
-
TypeGuard.TIterator = TIterator;
|
|
358
|
-
/** Returns true if the given value is a TKind with the given name. */
|
|
359
|
-
function TKindOf(schema, kind) {
|
|
360
|
-
return TKind(schema) && schema[exports.Kind] === kind;
|
|
361
|
-
}
|
|
362
|
-
TypeGuard.TKindOf = TKindOf;
|
|
363
|
-
/** Returns true if the given value is TKind */
|
|
364
|
-
function TKind(schema) {
|
|
365
|
-
return ValueGuard.IsObject(schema) && exports.Kind in schema && ValueGuard.IsString(schema[exports.Kind]);
|
|
366
|
-
}
|
|
367
|
-
TypeGuard.TKind = TKind;
|
|
368
|
-
/** Returns true if the given value is TLiteral<string> */
|
|
369
|
-
function TLiteralString(schema) {
|
|
370
|
-
return TLiteral(schema) && ValueGuard.IsString(schema.const);
|
|
371
|
-
}
|
|
372
|
-
TypeGuard.TLiteralString = TLiteralString;
|
|
373
|
-
/** Returns true if the given value is TLiteral<number> */
|
|
374
|
-
function TLiteralNumber(schema) {
|
|
375
|
-
return TLiteral(schema) && ValueGuard.IsNumber(schema.const);
|
|
376
|
-
}
|
|
377
|
-
TypeGuard.TLiteralNumber = TLiteralNumber;
|
|
378
|
-
/** Returns true if the given value is TLiteral<boolean> */
|
|
379
|
-
function TLiteralBoolean(schema) {
|
|
380
|
-
return TLiteral(schema) && ValueGuard.IsBoolean(schema.const);
|
|
381
|
-
}
|
|
382
|
-
TypeGuard.TLiteralBoolean = TLiteralBoolean;
|
|
383
|
-
/** Returns true if the given value is TLiteral */
|
|
384
|
-
function TLiteral(schema) {
|
|
385
|
-
// prettier-ignore
|
|
386
|
-
return (TKindOf(schema, 'Literal') &&
|
|
387
|
-
IsOptionalString(schema.$id) && (ValueGuard.IsBoolean(schema.const) ||
|
|
388
|
-
ValueGuard.IsNumber(schema.const) ||
|
|
389
|
-
ValueGuard.IsString(schema.const)));
|
|
390
|
-
}
|
|
391
|
-
TypeGuard.TLiteral = TLiteral;
|
|
392
|
-
/** Returns true if the given value is TNever */
|
|
393
|
-
function TNever(schema) {
|
|
394
|
-
// prettier-ignore
|
|
395
|
-
return (TKindOf(schema, 'Never') &&
|
|
396
|
-
ValueGuard.IsObject(schema.not) &&
|
|
397
|
-
Object.getOwnPropertyNames(schema.not).length === 0);
|
|
398
|
-
}
|
|
399
|
-
TypeGuard.TNever = TNever;
|
|
400
|
-
/** Returns true if the given value is TNot */
|
|
401
|
-
function TNot(schema) {
|
|
402
|
-
// prettier-ignore
|
|
403
|
-
return (TKindOf(schema, 'Not') &&
|
|
404
|
-
TSchema(schema.not));
|
|
405
|
-
}
|
|
406
|
-
TypeGuard.TNot = TNot;
|
|
407
|
-
/** Returns true if the given value is TNull */
|
|
408
|
-
function TNull(schema) {
|
|
409
|
-
// prettier-ignore
|
|
410
|
-
return (TKindOf(schema, 'Null') &&
|
|
411
|
-
schema.type === 'null' &&
|
|
412
|
-
IsOptionalString(schema.$id));
|
|
413
|
-
}
|
|
414
|
-
TypeGuard.TNull = TNull;
|
|
415
|
-
/** Returns true if the given value is TNumber */
|
|
416
|
-
function TNumber(schema) {
|
|
417
|
-
return (TKindOf(schema, 'Number') &&
|
|
418
|
-
schema.type === 'number' &&
|
|
419
|
-
IsOptionalString(schema.$id) &&
|
|
420
|
-
IsOptionalNumber(schema.exclusiveMaximum) &&
|
|
421
|
-
IsOptionalNumber(schema.exclusiveMinimum) &&
|
|
422
|
-
IsOptionalNumber(schema.maximum) &&
|
|
423
|
-
IsOptionalNumber(schema.minimum) &&
|
|
424
|
-
IsOptionalNumber(schema.multipleOf));
|
|
425
|
-
}
|
|
426
|
-
TypeGuard.TNumber = TNumber;
|
|
427
|
-
/** Returns true if the given value is TObject */
|
|
428
|
-
function TObject(schema) {
|
|
429
|
-
// prettier-ignore
|
|
430
|
-
return (TKindOf(schema, 'Object') &&
|
|
431
|
-
schema.type === 'object' &&
|
|
432
|
-
IsOptionalString(schema.$id) &&
|
|
433
|
-
ValueGuard.IsObject(schema.properties) &&
|
|
434
|
-
IsAdditionalProperties(schema.additionalProperties) &&
|
|
435
|
-
IsOptionalNumber(schema.minProperties) &&
|
|
436
|
-
IsOptionalNumber(schema.maxProperties) &&
|
|
437
|
-
Object.entries(schema.properties).every(([key, schema]) => IsControlCharacterFree(key) && TSchema(schema)));
|
|
438
|
-
}
|
|
439
|
-
TypeGuard.TObject = TObject;
|
|
440
|
-
/** Returns true if the given value is TPromise */
|
|
441
|
-
function TPromise(schema) {
|
|
442
|
-
// prettier-ignore
|
|
443
|
-
return (TKindOf(schema, 'Promise') &&
|
|
444
|
-
schema.type === 'Promise' &&
|
|
445
|
-
IsOptionalString(schema.$id) &&
|
|
446
|
-
TSchema(schema.item));
|
|
447
|
-
}
|
|
448
|
-
TypeGuard.TPromise = TPromise;
|
|
449
|
-
/** Returns true if the given value is TRecord */
|
|
450
|
-
function TRecord(schema) {
|
|
451
|
-
// prettier-ignore
|
|
452
|
-
return (TKindOf(schema, 'Record') &&
|
|
453
|
-
schema.type === 'object' &&
|
|
454
|
-
IsOptionalString(schema.$id) &&
|
|
455
|
-
IsAdditionalProperties(schema.additionalProperties) &&
|
|
456
|
-
ValueGuard.IsObject(schema.patternProperties) &&
|
|
457
|
-
((schema) => {
|
|
458
|
-
const keys = Object.getOwnPropertyNames(schema.patternProperties);
|
|
459
|
-
return (keys.length === 1 &&
|
|
460
|
-
IsPattern(keys[0]) &&
|
|
461
|
-
ValueGuard.IsObject(schema.patternProperties) &&
|
|
462
|
-
TSchema(schema.patternProperties[keys[0]]));
|
|
463
|
-
})(schema));
|
|
464
|
-
}
|
|
465
|
-
TypeGuard.TRecord = TRecord;
|
|
466
|
-
/** Returns true if this value is TRecursive */
|
|
467
|
-
function TRecursive(schema) {
|
|
468
|
-
return ValueGuard.IsObject(schema) && exports.Hint in schema && schema[exports.Hint] === 'Recursive';
|
|
469
|
-
}
|
|
470
|
-
TypeGuard.TRecursive = TRecursive;
|
|
471
|
-
/** Returns true if the given value is TRef */
|
|
472
|
-
function TRef(schema) {
|
|
473
|
-
// prettier-ignore
|
|
474
|
-
return (TKindOf(schema, 'Ref') &&
|
|
475
|
-
IsOptionalString(schema.$id) &&
|
|
476
|
-
ValueGuard.IsString(schema.$ref));
|
|
477
|
-
}
|
|
478
|
-
TypeGuard.TRef = TRef;
|
|
479
|
-
/** Returns true if the given value is TString */
|
|
480
|
-
function TString(schema) {
|
|
481
|
-
// prettier-ignore
|
|
482
|
-
return (TKindOf(schema, 'String') &&
|
|
483
|
-
schema.type === 'string' &&
|
|
484
|
-
IsOptionalString(schema.$id) &&
|
|
485
|
-
IsOptionalNumber(schema.minLength) &&
|
|
486
|
-
IsOptionalNumber(schema.maxLength) &&
|
|
487
|
-
IsOptionalPattern(schema.pattern) &&
|
|
488
|
-
IsOptionalFormat(schema.format));
|
|
489
|
-
}
|
|
490
|
-
TypeGuard.TString = TString;
|
|
491
|
-
/** Returns true if the given value is TSymbol */
|
|
492
|
-
function TSymbol(schema) {
|
|
493
|
-
// prettier-ignore
|
|
494
|
-
return (TKindOf(schema, 'Symbol') &&
|
|
495
|
-
schema.type === 'symbol' &&
|
|
496
|
-
IsOptionalString(schema.$id));
|
|
497
|
-
}
|
|
498
|
-
TypeGuard.TSymbol = TSymbol;
|
|
499
|
-
/** Returns true if the given value is TTemplateLiteral */
|
|
500
|
-
function TTemplateLiteral(schema) {
|
|
501
|
-
// prettier-ignore
|
|
502
|
-
return (TKindOf(schema, 'TemplateLiteral') &&
|
|
503
|
-
schema.type === 'string' &&
|
|
504
|
-
ValueGuard.IsString(schema.pattern) &&
|
|
505
|
-
schema.pattern[0] === '^' &&
|
|
506
|
-
schema.pattern[schema.pattern.length - 1] === '$');
|
|
507
|
-
}
|
|
508
|
-
TypeGuard.TTemplateLiteral = TTemplateLiteral;
|
|
509
|
-
/** Returns true if the given value is TThis */
|
|
510
|
-
function TThis(schema) {
|
|
511
|
-
// prettier-ignore
|
|
512
|
-
return (TKindOf(schema, 'This') &&
|
|
513
|
-
IsOptionalString(schema.$id) &&
|
|
514
|
-
ValueGuard.IsString(schema.$ref));
|
|
515
|
-
}
|
|
516
|
-
TypeGuard.TThis = TThis;
|
|
517
|
-
/** Returns true of this value is TTransform */
|
|
518
|
-
function TTransform(schema) {
|
|
519
|
-
return ValueGuard.IsObject(schema) && exports.Transform in schema;
|
|
520
|
-
}
|
|
521
|
-
TypeGuard.TTransform = TTransform;
|
|
522
|
-
/** Returns true if the given value is TTuple */
|
|
523
|
-
function TTuple(schema) {
|
|
524
|
-
// prettier-ignore
|
|
525
|
-
return (TKindOf(schema, 'Tuple') &&
|
|
526
|
-
schema.type === 'array' &&
|
|
527
|
-
IsOptionalString(schema.$id) &&
|
|
528
|
-
ValueGuard.IsNumber(schema.minItems) &&
|
|
529
|
-
ValueGuard.IsNumber(schema.maxItems) &&
|
|
530
|
-
schema.minItems === schema.maxItems &&
|
|
531
|
-
(( // empty
|
|
532
|
-
ValueGuard.IsUndefined(schema.items) &&
|
|
533
|
-
ValueGuard.IsUndefined(schema.additionalItems) &&
|
|
534
|
-
schema.minItems === 0) || (ValueGuard.IsArray(schema.items) &&
|
|
535
|
-
schema.items.every(schema => TSchema(schema)))));
|
|
536
|
-
}
|
|
537
|
-
TypeGuard.TTuple = TTuple;
|
|
538
|
-
/** Returns true if the given value is TUndefined */
|
|
539
|
-
function TUndefined(schema) {
|
|
540
|
-
// prettier-ignore
|
|
541
|
-
return (TKindOf(schema, 'Undefined') &&
|
|
542
|
-
schema.type === 'undefined' &&
|
|
543
|
-
IsOptionalString(schema.$id));
|
|
544
|
-
}
|
|
545
|
-
TypeGuard.TUndefined = TUndefined;
|
|
546
|
-
/** Returns true if the given value is TUnion<Literal<string | number>[]> */
|
|
547
|
-
function TUnionLiteral(schema) {
|
|
548
|
-
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
|
|
549
|
-
}
|
|
550
|
-
TypeGuard.TUnionLiteral = TUnionLiteral;
|
|
551
|
-
/** Returns true if the given value is TUnion */
|
|
552
|
-
function TUnion(schema) {
|
|
553
|
-
// prettier-ignore
|
|
554
|
-
return (TKindOf(schema, 'Union') &&
|
|
555
|
-
IsOptionalString(schema.$id) &&
|
|
556
|
-
ValueGuard.IsObject(schema) &&
|
|
557
|
-
ValueGuard.IsArray(schema.anyOf) &&
|
|
558
|
-
schema.anyOf.every(schema => TSchema(schema)));
|
|
559
|
-
}
|
|
560
|
-
TypeGuard.TUnion = TUnion;
|
|
561
|
-
/** Returns true if the given value is TUint8Array */
|
|
562
|
-
function TUint8Array(schema) {
|
|
563
|
-
// prettier-ignore
|
|
564
|
-
return (TKindOf(schema, 'Uint8Array') &&
|
|
565
|
-
schema.type === 'Uint8Array' &&
|
|
566
|
-
IsOptionalString(schema.$id) &&
|
|
567
|
-
IsOptionalNumber(schema.minByteLength) &&
|
|
568
|
-
IsOptionalNumber(schema.maxByteLength));
|
|
569
|
-
}
|
|
570
|
-
TypeGuard.TUint8Array = TUint8Array;
|
|
571
|
-
/** Returns true if the given value is TUnknown */
|
|
572
|
-
function TUnknown(schema) {
|
|
573
|
-
// prettier-ignore
|
|
574
|
-
return (TKindOf(schema, 'Unknown') &&
|
|
575
|
-
IsOptionalString(schema.$id));
|
|
576
|
-
}
|
|
577
|
-
TypeGuard.TUnknown = TUnknown;
|
|
578
|
-
/** Returns true if the given value is a raw TUnsafe */
|
|
579
|
-
function TUnsafe(schema) {
|
|
580
|
-
return TKindOf(schema, 'Unsafe');
|
|
581
|
-
}
|
|
582
|
-
TypeGuard.TUnsafe = TUnsafe;
|
|
583
|
-
/** Returns true if the given value is TVoid */
|
|
584
|
-
function TVoid(schema) {
|
|
585
|
-
// prettier-ignore
|
|
586
|
-
return (TKindOf(schema, 'Void') &&
|
|
587
|
-
schema.type === 'void' &&
|
|
588
|
-
IsOptionalString(schema.$id));
|
|
589
|
-
}
|
|
590
|
-
TypeGuard.TVoid = TVoid;
|
|
591
|
-
/** Returns true if this value has a Readonly symbol */
|
|
592
|
-
function TReadonly(schema) {
|
|
593
|
-
return ValueGuard.IsObject(schema) && schema[exports.Readonly] === 'Readonly';
|
|
594
|
-
}
|
|
595
|
-
TypeGuard.TReadonly = TReadonly;
|
|
596
|
-
/** Returns true if this value has a Optional symbol */
|
|
597
|
-
function TOptional(schema) {
|
|
598
|
-
return ValueGuard.IsObject(schema) && schema[exports.Optional] === 'Optional';
|
|
599
|
-
}
|
|
600
|
-
TypeGuard.TOptional = TOptional;
|
|
601
|
-
/** Returns true if the given value is TSchema */
|
|
602
|
-
function TSchema(schema) {
|
|
603
|
-
// prettier-ignore
|
|
604
|
-
return (ValueGuard.IsObject(schema)) && (TAny(schema) ||
|
|
605
|
-
TArray(schema) ||
|
|
606
|
-
TBoolean(schema) ||
|
|
607
|
-
TBigInt(schema) ||
|
|
608
|
-
TAsyncIterator(schema) ||
|
|
609
|
-
TConstructor(schema) ||
|
|
610
|
-
TDate(schema) ||
|
|
611
|
-
TFunction(schema) ||
|
|
612
|
-
TInteger(schema) ||
|
|
613
|
-
TIntersect(schema) ||
|
|
614
|
-
TIterator(schema) ||
|
|
615
|
-
TLiteral(schema) ||
|
|
616
|
-
TNever(schema) ||
|
|
617
|
-
TNot(schema) ||
|
|
618
|
-
TNull(schema) ||
|
|
619
|
-
TNumber(schema) ||
|
|
620
|
-
TObject(schema) ||
|
|
621
|
-
TPromise(schema) ||
|
|
622
|
-
TRecord(schema) ||
|
|
623
|
-
TRef(schema) ||
|
|
624
|
-
TString(schema) ||
|
|
625
|
-
TSymbol(schema) ||
|
|
626
|
-
TTemplateLiteral(schema) ||
|
|
627
|
-
TThis(schema) ||
|
|
628
|
-
TTuple(schema) ||
|
|
629
|
-
TUndefined(schema) ||
|
|
630
|
-
TUnion(schema) ||
|
|
631
|
-
TUint8Array(schema) ||
|
|
632
|
-
TUnknown(schema) ||
|
|
633
|
-
TUnsafe(schema) ||
|
|
634
|
-
TVoid(schema) ||
|
|
635
|
-
(TKind(schema) && TypeRegistry.Has(schema[exports.Kind])));
|
|
636
|
-
}
|
|
637
|
-
TypeGuard.TSchema = TSchema;
|
|
638
|
-
})(TypeGuard || (exports.TypeGuard = TypeGuard = {}));
|
|
639
|
-
// --------------------------------------------------------------------------
|
|
640
|
-
// ExtendsUndefined
|
|
641
|
-
// --------------------------------------------------------------------------
|
|
642
|
-
/** Fast undefined check used for properties of type undefined */
|
|
643
|
-
var ExtendsUndefined;
|
|
644
|
-
(function (ExtendsUndefined) {
|
|
645
|
-
function Check(schema) {
|
|
646
|
-
return schema[exports.Kind] === 'Intersect'
|
|
647
|
-
? schema.allOf.every((schema) => Check(schema))
|
|
648
|
-
: schema[exports.Kind] === 'Union'
|
|
649
|
-
? schema.anyOf.some((schema) => Check(schema))
|
|
650
|
-
: schema[exports.Kind] === 'Undefined'
|
|
651
|
-
? true
|
|
652
|
-
: schema[exports.Kind] === 'Not'
|
|
653
|
-
? !Check(schema.not)
|
|
654
|
-
: false;
|
|
655
|
-
}
|
|
656
|
-
ExtendsUndefined.Check = Check;
|
|
657
|
-
})(ExtendsUndefined || (exports.ExtendsUndefined = ExtendsUndefined = {}));
|
|
658
|
-
// --------------------------------------------------------------------------
|
|
659
|
-
// TypeExtends
|
|
660
|
-
// --------------------------------------------------------------------------
|
|
661
|
-
class TypeExtendsError extends TypeBoxError {
|
|
662
|
-
}
|
|
663
|
-
exports.TypeExtendsError = TypeExtendsError;
|
|
664
|
-
var TypeExtendsResult;
|
|
665
|
-
(function (TypeExtendsResult) {
|
|
666
|
-
TypeExtendsResult[TypeExtendsResult["Union"] = 0] = "Union";
|
|
667
|
-
TypeExtendsResult[TypeExtendsResult["True"] = 1] = "True";
|
|
668
|
-
TypeExtendsResult[TypeExtendsResult["False"] = 2] = "False";
|
|
669
|
-
})(TypeExtendsResult || (exports.TypeExtendsResult = TypeExtendsResult = {}));
|
|
670
|
-
var TypeExtends;
|
|
671
|
-
(function (TypeExtends) {
|
|
672
|
-
// --------------------------------------------------------------------------
|
|
673
|
-
// IntoBooleanResult
|
|
674
|
-
// --------------------------------------------------------------------------
|
|
675
|
-
function IntoBooleanResult(result) {
|
|
676
|
-
return result === TypeExtendsResult.False ? result : TypeExtendsResult.True;
|
|
677
|
-
}
|
|
678
|
-
// --------------------------------------------------------------------------
|
|
679
|
-
// Throw
|
|
680
|
-
// --------------------------------------------------------------------------
|
|
681
|
-
function Throw(message) {
|
|
682
|
-
throw new TypeExtendsError(message);
|
|
683
|
-
}
|
|
684
|
-
// --------------------------------------------------------------------------
|
|
685
|
-
// StructuralRight
|
|
686
|
-
// --------------------------------------------------------------------------
|
|
687
|
-
function IsStructuralRight(right) {
|
|
688
|
-
// prettier-ignore
|
|
689
|
-
return (TypeGuard.TNever(right) ||
|
|
690
|
-
TypeGuard.TIntersect(right) ||
|
|
691
|
-
TypeGuard.TUnion(right) ||
|
|
692
|
-
TypeGuard.TUnknown(right) ||
|
|
693
|
-
TypeGuard.TAny(right));
|
|
694
|
-
}
|
|
695
|
-
function StructuralRight(left, right) {
|
|
696
|
-
// prettier-ignore
|
|
697
|
-
return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
|
|
698
|
-
TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
|
|
699
|
-
TypeGuard.TUnion(right) ? TUnionRight(left, right) :
|
|
700
|
-
TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
|
|
701
|
-
TypeGuard.TAny(right) ? TAnyRight(left, right) :
|
|
702
|
-
Throw('StructuralRight'));
|
|
703
|
-
}
|
|
704
|
-
// --------------------------------------------------------------------------
|
|
705
|
-
// Any
|
|
706
|
-
// --------------------------------------------------------------------------
|
|
707
|
-
function TAnyRight(left, right) {
|
|
708
|
-
return TypeExtendsResult.True;
|
|
709
|
-
}
|
|
710
|
-
function TAny(left, right) {
|
|
711
|
-
// prettier-ignore
|
|
712
|
-
return (TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
|
|
713
|
-
(TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema))) ? TypeExtendsResult.True :
|
|
714
|
-
TypeGuard.TUnion(right) ? TypeExtendsResult.Union :
|
|
715
|
-
TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
|
|
716
|
-
TypeGuard.TAny(right) ? TypeExtendsResult.True :
|
|
717
|
-
TypeExtendsResult.Union);
|
|
718
|
-
}
|
|
719
|
-
// --------------------------------------------------------------------------
|
|
720
|
-
// Array
|
|
721
|
-
// --------------------------------------------------------------------------
|
|
722
|
-
function TArrayRight(left, right) {
|
|
723
|
-
// prettier-ignore
|
|
724
|
-
return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
|
|
725
|
-
TypeGuard.TAny(left) ? TypeExtendsResult.Union :
|
|
726
|
-
TypeGuard.TNever(left) ? TypeExtendsResult.True :
|
|
727
|
-
TypeExtendsResult.False);
|
|
728
|
-
}
|
|
729
|
-
function TArray(left, right) {
|
|
730
|
-
// prettier-ignore
|
|
731
|
-
return (TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
|
|
732
|
-
IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
733
|
-
!TypeGuard.TArray(right) ? TypeExtendsResult.False :
|
|
734
|
-
IntoBooleanResult(Visit(left.items, right.items)));
|
|
735
|
-
}
|
|
736
|
-
// --------------------------------------------------------------------------
|
|
737
|
-
// AsyncIterator
|
|
738
|
-
// --------------------------------------------------------------------------
|
|
739
|
-
function TAsyncIterator(left, right) {
|
|
740
|
-
// prettier-ignore
|
|
741
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
742
|
-
!TypeGuard.TAsyncIterator(right) ? TypeExtendsResult.False :
|
|
743
|
-
IntoBooleanResult(Visit(left.items, right.items)));
|
|
744
|
-
}
|
|
745
|
-
// --------------------------------------------------------------------------
|
|
746
|
-
// BigInt
|
|
747
|
-
// --------------------------------------------------------------------------
|
|
748
|
-
function TBigInt(left, right) {
|
|
749
|
-
// prettier-ignore
|
|
750
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
751
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
752
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
753
|
-
TypeGuard.TBigInt(right) ? TypeExtendsResult.True :
|
|
754
|
-
TypeExtendsResult.False);
|
|
755
|
-
}
|
|
756
|
-
// --------------------------------------------------------------------------
|
|
757
|
-
// Boolean
|
|
758
|
-
// --------------------------------------------------------------------------
|
|
759
|
-
function TBooleanRight(left, right) {
|
|
760
|
-
return TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const) ? TypeExtendsResult.True : TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
761
|
-
}
|
|
762
|
-
function TBoolean(left, right) {
|
|
763
|
-
// prettier-ignore
|
|
764
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
765
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
766
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
767
|
-
TypeGuard.TBoolean(right) ? TypeExtendsResult.True :
|
|
768
|
-
TypeExtendsResult.False);
|
|
769
|
-
}
|
|
770
|
-
// --------------------------------------------------------------------------
|
|
771
|
-
// Constructor
|
|
772
|
-
// --------------------------------------------------------------------------
|
|
773
|
-
function TConstructor(left, right) {
|
|
774
|
-
// prettier-ignore
|
|
775
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
776
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
777
|
-
!TypeGuard.TConstructor(right) ? TypeExtendsResult.False :
|
|
778
|
-
left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
|
|
779
|
-
(!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
|
|
780
|
-
IntoBooleanResult(Visit(left.returns, right.returns)));
|
|
781
|
-
}
|
|
782
|
-
// --------------------------------------------------------------------------
|
|
783
|
-
// Date
|
|
784
|
-
// --------------------------------------------------------------------------
|
|
785
|
-
function TDate(left, right) {
|
|
786
|
-
// prettier-ignore
|
|
787
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
788
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
789
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
790
|
-
TypeGuard.TDate(right) ? TypeExtendsResult.True :
|
|
791
|
-
TypeExtendsResult.False);
|
|
792
|
-
}
|
|
793
|
-
// --------------------------------------------------------------------------
|
|
794
|
-
// Function
|
|
795
|
-
// --------------------------------------------------------------------------
|
|
796
|
-
function TFunction(left, right) {
|
|
797
|
-
// prettier-ignore
|
|
798
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
799
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
800
|
-
!TypeGuard.TFunction(right) ? TypeExtendsResult.False :
|
|
801
|
-
left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
|
|
802
|
-
(!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
|
|
803
|
-
IntoBooleanResult(Visit(left.returns, right.returns)));
|
|
804
|
-
}
|
|
805
|
-
// --------------------------------------------------------------------------
|
|
806
|
-
// Integer
|
|
807
|
-
// --------------------------------------------------------------------------
|
|
808
|
-
function TIntegerRight(left, right) {
|
|
809
|
-
// prettier-ignore
|
|
810
|
-
return (TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const) ? TypeExtendsResult.True :
|
|
811
|
-
TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
|
|
812
|
-
TypeExtendsResult.False);
|
|
813
|
-
}
|
|
814
|
-
function TInteger(left, right) {
|
|
815
|
-
// prettier-ignore
|
|
816
|
-
return (TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
|
|
817
|
-
IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
818
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
819
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
820
|
-
TypeExtendsResult.False);
|
|
821
|
-
}
|
|
822
|
-
// --------------------------------------------------------------------------
|
|
823
|
-
// Intersect
|
|
824
|
-
// --------------------------------------------------------------------------
|
|
825
|
-
function TIntersectRight(left, right) {
|
|
826
|
-
// prettier-ignore
|
|
827
|
-
return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True)
|
|
828
|
-
? TypeExtendsResult.True
|
|
829
|
-
: TypeExtendsResult.False;
|
|
830
|
-
}
|
|
831
|
-
function TIntersect(left, right) {
|
|
832
|
-
// prettier-ignore
|
|
833
|
-
return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True)
|
|
834
|
-
? TypeExtendsResult.True
|
|
835
|
-
: TypeExtendsResult.False;
|
|
836
|
-
}
|
|
837
|
-
// --------------------------------------------------------------------------
|
|
838
|
-
// Iterator
|
|
839
|
-
// --------------------------------------------------------------------------
|
|
840
|
-
function TIterator(left, right) {
|
|
841
|
-
// prettier-ignore
|
|
842
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
843
|
-
!TypeGuard.TIterator(right) ? TypeExtendsResult.False :
|
|
844
|
-
IntoBooleanResult(Visit(left.items, right.items)));
|
|
845
|
-
}
|
|
846
|
-
// --------------------------------------------------------------------------
|
|
847
|
-
// Literal
|
|
848
|
-
// --------------------------------------------------------------------------
|
|
849
|
-
function TLiteral(left, right) {
|
|
850
|
-
// prettier-ignore
|
|
851
|
-
return (TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True :
|
|
852
|
-
IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
853
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
854
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
855
|
-
TypeGuard.TString(right) ? TStringRight(left, right) :
|
|
856
|
-
TypeGuard.TNumber(right) ? TNumberRight(left, right) :
|
|
857
|
-
TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
|
|
858
|
-
TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
|
|
859
|
-
TypeExtendsResult.False);
|
|
860
|
-
}
|
|
861
|
-
// --------------------------------------------------------------------------
|
|
862
|
-
// Never
|
|
863
|
-
// --------------------------------------------------------------------------
|
|
864
|
-
function TNeverRight(left, right) {
|
|
865
|
-
return TypeExtendsResult.False;
|
|
866
|
-
}
|
|
867
|
-
function TNever(left, right) {
|
|
868
|
-
return TypeExtendsResult.True;
|
|
869
|
-
}
|
|
870
|
-
// --------------------------------------------------------------------------
|
|
871
|
-
// Not
|
|
872
|
-
// --------------------------------------------------------------------------
|
|
873
|
-
function UnwrapTNot(schema) {
|
|
874
|
-
let [current, depth] = [schema, 0];
|
|
875
|
-
while (true) {
|
|
876
|
-
if (!TypeGuard.TNot(current))
|
|
877
|
-
break;
|
|
878
|
-
current = current.not;
|
|
879
|
-
depth += 1;
|
|
880
|
-
}
|
|
881
|
-
return depth % 2 === 0 ? current : exports.Type.Unknown();
|
|
882
|
-
}
|
|
883
|
-
function TNot(left, right) {
|
|
884
|
-
// TypeScript has no concept of negated types, and attempts to correctly check the negated
|
|
885
|
-
// type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
|
|
886
|
-
// the type. Instead we unwrap to either unknown or T and continue evaluating.
|
|
887
|
-
// prettier-ignore
|
|
888
|
-
return (TypeGuard.TNot(left) ? Visit(UnwrapTNot(left), right) :
|
|
889
|
-
TypeGuard.TNot(right) ? Visit(left, UnwrapTNot(right)) :
|
|
890
|
-
Throw('Invalid fallthrough for Not'));
|
|
891
|
-
}
|
|
892
|
-
// --------------------------------------------------------------------------
|
|
893
|
-
// Null
|
|
894
|
-
// --------------------------------------------------------------------------
|
|
895
|
-
function TNull(left, right) {
|
|
896
|
-
// prettier-ignore
|
|
897
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
898
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
899
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
900
|
-
TypeGuard.TNull(right) ? TypeExtendsResult.True :
|
|
901
|
-
TypeExtendsResult.False);
|
|
902
|
-
}
|
|
903
|
-
// --------------------------------------------------------------------------
|
|
904
|
-
// Number
|
|
905
|
-
// --------------------------------------------------------------------------
|
|
906
|
-
function TNumberRight(left, right) {
|
|
907
|
-
// prettier-ignore
|
|
908
|
-
return (TypeGuard.TLiteralNumber(left) ? TypeExtendsResult.True :
|
|
909
|
-
TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
|
|
910
|
-
TypeExtendsResult.False);
|
|
911
|
-
}
|
|
912
|
-
function TNumber(left, right) {
|
|
913
|
-
// prettier-ignore
|
|
914
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
915
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
916
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
917
|
-
TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
|
|
918
|
-
TypeExtendsResult.False);
|
|
919
|
-
}
|
|
920
|
-
// --------------------------------------------------------------------------
|
|
921
|
-
// Object
|
|
922
|
-
// --------------------------------------------------------------------------
|
|
923
|
-
function IsObjectPropertyCount(schema, count) {
|
|
924
|
-
return Object.getOwnPropertyNames(schema.properties).length === count;
|
|
925
|
-
}
|
|
926
|
-
function IsObjectStringLike(schema) {
|
|
927
|
-
return IsObjectArrayLike(schema);
|
|
928
|
-
}
|
|
929
|
-
function IsObjectSymbolLike(schema) {
|
|
930
|
-
// prettier-ignore
|
|
931
|
-
return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && ((TypeGuard.TString(schema.properties.description.anyOf[0]) &&
|
|
932
|
-
TypeGuard.TUndefined(schema.properties.description.anyOf[1])) || (TypeGuard.TString(schema.properties.description.anyOf[1]) &&
|
|
933
|
-
TypeGuard.TUndefined(schema.properties.description.anyOf[0]))));
|
|
934
|
-
}
|
|
935
|
-
function IsObjectNumberLike(schema) {
|
|
936
|
-
return IsObjectPropertyCount(schema, 0);
|
|
937
|
-
}
|
|
938
|
-
function IsObjectBooleanLike(schema) {
|
|
939
|
-
return IsObjectPropertyCount(schema, 0);
|
|
940
|
-
}
|
|
941
|
-
function IsObjectBigIntLike(schema) {
|
|
942
|
-
return IsObjectPropertyCount(schema, 0);
|
|
943
|
-
}
|
|
944
|
-
function IsObjectDateLike(schema) {
|
|
945
|
-
return IsObjectPropertyCount(schema, 0);
|
|
946
|
-
}
|
|
947
|
-
function IsObjectUint8ArrayLike(schema) {
|
|
948
|
-
return IsObjectArrayLike(schema);
|
|
949
|
-
}
|
|
950
|
-
function IsObjectFunctionLike(schema) {
|
|
951
|
-
const length = exports.Type.Number();
|
|
952
|
-
return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
|
|
953
|
-
}
|
|
954
|
-
function IsObjectConstructorLike(schema) {
|
|
955
|
-
return IsObjectPropertyCount(schema, 0);
|
|
956
|
-
}
|
|
957
|
-
function IsObjectArrayLike(schema) {
|
|
958
|
-
const length = exports.Type.Number();
|
|
959
|
-
return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
|
|
960
|
-
}
|
|
961
|
-
function IsObjectPromiseLike(schema) {
|
|
962
|
-
const then = exports.Type.Function([exports.Type.Any()], exports.Type.Any());
|
|
963
|
-
return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'then' in schema.properties && IntoBooleanResult(Visit(schema.properties['then'], then)) === TypeExtendsResult.True);
|
|
964
|
-
}
|
|
965
|
-
// --------------------------------------------------------------------------
|
|
966
|
-
// Property
|
|
967
|
-
// --------------------------------------------------------------------------
|
|
968
|
-
function Property(left, right) {
|
|
969
|
-
// prettier-ignore
|
|
970
|
-
return (Visit(left, right) === TypeExtendsResult.False ? TypeExtendsResult.False :
|
|
971
|
-
TypeGuard.TOptional(left) && !TypeGuard.TOptional(right) ? TypeExtendsResult.False :
|
|
972
|
-
TypeExtendsResult.True);
|
|
973
|
-
}
|
|
974
|
-
function TObjectRight(left, right) {
|
|
975
|
-
// prettier-ignore
|
|
976
|
-
return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
|
|
977
|
-
TypeGuard.TAny(left) ? TypeExtendsResult.Union : (TypeGuard.TNever(left) ||
|
|
978
|
-
(TypeGuard.TLiteralString(left) && IsObjectStringLike(right)) ||
|
|
979
|
-
(TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right)) ||
|
|
980
|
-
(TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right)) ||
|
|
981
|
-
(TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
|
|
982
|
-
(TypeGuard.TBigInt(left) && IsObjectBigIntLike(right)) ||
|
|
983
|
-
(TypeGuard.TString(left) && IsObjectStringLike(right)) ||
|
|
984
|
-
(TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
|
|
985
|
-
(TypeGuard.TNumber(left) && IsObjectNumberLike(right)) ||
|
|
986
|
-
(TypeGuard.TInteger(left) && IsObjectNumberLike(right)) ||
|
|
987
|
-
(TypeGuard.TBoolean(left) && IsObjectBooleanLike(right)) ||
|
|
988
|
-
(TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right)) ||
|
|
989
|
-
(TypeGuard.TDate(left) && IsObjectDateLike(right)) ||
|
|
990
|
-
(TypeGuard.TConstructor(left) && IsObjectConstructorLike(right)) ||
|
|
991
|
-
(TypeGuard.TFunction(left) && IsObjectFunctionLike(right))) ? TypeExtendsResult.True :
|
|
992
|
-
(TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => {
|
|
993
|
-
// When expressing a Record with literal key values, the Record is converted into a Object with
|
|
994
|
-
// the Hint assigned as `Record`. This is used to invert the extends logic.
|
|
995
|
-
return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
996
|
-
})() :
|
|
997
|
-
(TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => {
|
|
998
|
-
return IsObjectPropertyCount(right, 0)
|
|
999
|
-
? TypeExtendsResult.True
|
|
1000
|
-
: TypeExtendsResult.False;
|
|
1001
|
-
})() :
|
|
1002
|
-
TypeExtendsResult.False);
|
|
1003
|
-
}
|
|
1004
|
-
function TObject(left, right) {
|
|
1005
|
-
// prettier-ignore
|
|
1006
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1007
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
1008
|
-
!TypeGuard.TObject(right) ? TypeExtendsResult.False :
|
|
1009
|
-
(() => {
|
|
1010
|
-
for (const key of Object.getOwnPropertyNames(right.properties)) {
|
|
1011
|
-
if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) {
|
|
1012
|
-
return TypeExtendsResult.False;
|
|
1013
|
-
}
|
|
1014
|
-
if (TypeGuard.TOptional(right.properties[key])) {
|
|
1015
|
-
return TypeExtendsResult.True;
|
|
1016
|
-
}
|
|
1017
|
-
if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
|
|
1018
|
-
return TypeExtendsResult.False;
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
return TypeExtendsResult.True;
|
|
1022
|
-
})());
|
|
1023
|
-
}
|
|
1024
|
-
// --------------------------------------------------------------------------
|
|
1025
|
-
// Promise
|
|
1026
|
-
// --------------------------------------------------------------------------
|
|
1027
|
-
function TPromise(left, right) {
|
|
1028
|
-
// prettier-ignore
|
|
1029
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1030
|
-
TypeGuard.TObject(right) && IsObjectPromiseLike(right) ? TypeExtendsResult.True :
|
|
1031
|
-
!TypeGuard.TPromise(right) ? TypeExtendsResult.False :
|
|
1032
|
-
IntoBooleanResult(Visit(left.item, right.item)));
|
|
1033
|
-
}
|
|
1034
|
-
// --------------------------------------------------------------------------
|
|
1035
|
-
// Record
|
|
1036
|
-
// --------------------------------------------------------------------------
|
|
1037
|
-
function RecordKey(schema) {
|
|
1038
|
-
// prettier-ignore
|
|
1039
|
-
return (exports.PatternNumberExact in schema.patternProperties ? exports.Type.Number() :
|
|
1040
|
-
exports.PatternStringExact in schema.patternProperties ? exports.Type.String() :
|
|
1041
|
-
Throw('Unknown record key pattern'));
|
|
1042
|
-
}
|
|
1043
|
-
function RecordValue(schema) {
|
|
1044
|
-
// prettier-ignore
|
|
1045
|
-
return (exports.PatternNumberExact in schema.patternProperties ? schema.patternProperties[exports.PatternNumberExact] :
|
|
1046
|
-
exports.PatternStringExact in schema.patternProperties ? schema.patternProperties[exports.PatternStringExact] :
|
|
1047
|
-
Throw('Unable to get record value schema'));
|
|
1048
|
-
}
|
|
1049
|
-
function TRecordRight(left, right) {
|
|
1050
|
-
const [Key, Value] = [RecordKey(right), RecordValue(right)];
|
|
1051
|
-
// prettier-ignore
|
|
1052
|
-
return ((TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True) ? TypeExtendsResult.True :
|
|
1053
|
-
TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
|
|
1054
|
-
TypeGuard.TString(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
|
|
1055
|
-
TypeGuard.TArray(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
|
|
1056
|
-
TypeGuard.TObject(left) ? (() => {
|
|
1057
|
-
for (const key of Object.getOwnPropertyNames(left.properties)) {
|
|
1058
|
-
if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
|
|
1059
|
-
return TypeExtendsResult.False;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
return TypeExtendsResult.True;
|
|
1063
|
-
})() :
|
|
1064
|
-
TypeExtendsResult.False);
|
|
1065
|
-
}
|
|
1066
|
-
function TRecord(left, right) {
|
|
1067
|
-
// prettier-ignore
|
|
1068
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1069
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1070
|
-
!TypeGuard.TRecord(right) ? TypeExtendsResult.False :
|
|
1071
|
-
Visit(RecordValue(left), RecordValue(right)));
|
|
1072
|
-
}
|
|
1073
|
-
// --------------------------------------------------------------------------
|
|
1074
|
-
// String
|
|
1075
|
-
// --------------------------------------------------------------------------
|
|
1076
|
-
function TStringRight(left, right) {
|
|
1077
|
-
// prettier-ignore
|
|
1078
|
-
return (TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const) ? TypeExtendsResult.True :
|
|
1079
|
-
TypeGuard.TString(left) ? TypeExtendsResult.True :
|
|
1080
|
-
TypeExtendsResult.False);
|
|
1081
|
-
}
|
|
1082
|
-
function TString(left, right) {
|
|
1083
|
-
// prettier-ignore
|
|
1084
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1085
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1086
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
1087
|
-
TypeGuard.TString(right) ? TypeExtendsResult.True :
|
|
1088
|
-
TypeExtendsResult.False);
|
|
1089
|
-
}
|
|
1090
|
-
// --------------------------------------------------------------------------
|
|
1091
|
-
// Symbol
|
|
1092
|
-
// --------------------------------------------------------------------------
|
|
1093
|
-
function TSymbol(left, right) {
|
|
1094
|
-
// prettier-ignore
|
|
1095
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1096
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1097
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
1098
|
-
TypeGuard.TSymbol(right) ? TypeExtendsResult.True :
|
|
1099
|
-
TypeExtendsResult.False);
|
|
1100
|
-
}
|
|
1101
|
-
// --------------------------------------------------------------------------
|
|
1102
|
-
// TemplateLiteral
|
|
1103
|
-
// --------------------------------------------------------------------------
|
|
1104
|
-
function TTemplateLiteral(left, right) {
|
|
1105
|
-
// TemplateLiteral types are resolved to either unions for finite expressions or string
|
|
1106
|
-
// for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
|
|
1107
|
-
// either type and continue evaluating.
|
|
1108
|
-
// prettier-ignore
|
|
1109
|
-
return (TypeGuard.TTemplateLiteral(left) ? Visit(TemplateLiteralResolver.Resolve(left), right) :
|
|
1110
|
-
TypeGuard.TTemplateLiteral(right) ? Visit(left, TemplateLiteralResolver.Resolve(right)) :
|
|
1111
|
-
Throw('Invalid fallthrough for TemplateLiteral'));
|
|
1112
|
-
}
|
|
1113
|
-
// --------------------------------------------------------------------------
|
|
1114
|
-
// Tuple
|
|
1115
|
-
// --------------------------------------------------------------------------
|
|
1116
|
-
function IsArrayOfTuple(left, right) {
|
|
1117
|
-
// prettier-ignore
|
|
1118
|
-
return (TypeGuard.TArray(right) &&
|
|
1119
|
-
left.items !== undefined &&
|
|
1120
|
-
left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True));
|
|
1121
|
-
}
|
|
1122
|
-
function TTupleRight(left, right) {
|
|
1123
|
-
// prettier-ignore
|
|
1124
|
-
return (TypeGuard.TNever(left) ? TypeExtendsResult.True :
|
|
1125
|
-
TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
|
|
1126
|
-
TypeGuard.TAny(left) ? TypeExtendsResult.Union :
|
|
1127
|
-
TypeExtendsResult.False);
|
|
1128
|
-
}
|
|
1129
|
-
function TTuple(left, right) {
|
|
1130
|
-
// prettier-ignore
|
|
1131
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1132
|
-
TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
|
|
1133
|
-
TypeGuard.TArray(right) && IsArrayOfTuple(left, right) ? TypeExtendsResult.True :
|
|
1134
|
-
!TypeGuard.TTuple(right) ? TypeExtendsResult.False :
|
|
1135
|
-
(ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.False :
|
|
1136
|
-
(ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.True :
|
|
1137
|
-
left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True :
|
|
1138
|
-
TypeExtendsResult.False);
|
|
1139
|
-
}
|
|
1140
|
-
// --------------------------------------------------------------------------
|
|
1141
|
-
// Uint8Array
|
|
1142
|
-
// --------------------------------------------------------------------------
|
|
1143
|
-
function TUint8Array(left, right) {
|
|
1144
|
-
// prettier-ignore
|
|
1145
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1146
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1147
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
1148
|
-
TypeGuard.TUint8Array(right) ? TypeExtendsResult.True :
|
|
1149
|
-
TypeExtendsResult.False);
|
|
1150
|
-
}
|
|
1151
|
-
// --------------------------------------------------------------------------
|
|
1152
|
-
// Undefined
|
|
1153
|
-
// --------------------------------------------------------------------------
|
|
1154
|
-
function TUndefined(left, right) {
|
|
1155
|
-
// prettier-ignore
|
|
1156
|
-
return (IsStructuralRight(right) ? StructuralRight(left, right) :
|
|
1157
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1158
|
-
TypeGuard.TRecord(right) ? TRecordRight(left, right) :
|
|
1159
|
-
TypeGuard.TVoid(right) ? VoidRight(left, right) :
|
|
1160
|
-
TypeGuard.TUndefined(right) ? TypeExtendsResult.True :
|
|
1161
|
-
TypeExtendsResult.False);
|
|
1162
|
-
}
|
|
1163
|
-
// --------------------------------------------------------------------------
|
|
1164
|
-
// Union
|
|
1165
|
-
// --------------------------------------------------------------------------
|
|
1166
|
-
function TUnionRight(left, right) {
|
|
1167
|
-
// prettier-ignore
|
|
1168
|
-
return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True)
|
|
1169
|
-
? TypeExtendsResult.True
|
|
1170
|
-
: TypeExtendsResult.False;
|
|
1171
|
-
}
|
|
1172
|
-
function TUnion(left, right) {
|
|
1173
|
-
// prettier-ignore
|
|
1174
|
-
return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True)
|
|
1175
|
-
? TypeExtendsResult.True
|
|
1176
|
-
: TypeExtendsResult.False;
|
|
1177
|
-
}
|
|
1178
|
-
// --------------------------------------------------------------------------
|
|
1179
|
-
// Unknown
|
|
1180
|
-
// --------------------------------------------------------------------------
|
|
1181
|
-
function TUnknownRight(left, right) {
|
|
1182
|
-
return TypeExtendsResult.True;
|
|
1183
|
-
}
|
|
1184
|
-
function TUnknown(left, right) {
|
|
1185
|
-
// prettier-ignore
|
|
1186
|
-
return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
|
|
1187
|
-
TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
|
|
1188
|
-
TypeGuard.TUnion(right) ? TUnionRight(left, right) :
|
|
1189
|
-
TypeGuard.TAny(right) ? TAnyRight(left, right) :
|
|
1190
|
-
TypeGuard.TString(right) ? TStringRight(left, right) :
|
|
1191
|
-
TypeGuard.TNumber(right) ? TNumberRight(left, right) :
|
|
1192
|
-
TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
|
|
1193
|
-
TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
|
|
1194
|
-
TypeGuard.TArray(right) ? TArrayRight(left, right) :
|
|
1195
|
-
TypeGuard.TTuple(right) ? TTupleRight(left, right) :
|
|
1196
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1197
|
-
TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
|
|
1198
|
-
TypeExtendsResult.False);
|
|
1199
|
-
}
|
|
1200
|
-
// --------------------------------------------------------------------------
|
|
1201
|
-
// Void
|
|
1202
|
-
// --------------------------------------------------------------------------
|
|
1203
|
-
function VoidRight(left, right) {
|
|
1204
|
-
// prettier-ignore
|
|
1205
|
-
return TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
|
|
1206
|
-
TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
|
|
1207
|
-
TypeExtendsResult.False;
|
|
1208
|
-
}
|
|
1209
|
-
function TVoid(left, right) {
|
|
1210
|
-
// prettier-ignore
|
|
1211
|
-
return TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
|
|
1212
|
-
TypeGuard.TUnion(right) ? TUnionRight(left, right) :
|
|
1213
|
-
TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
|
|
1214
|
-
TypeGuard.TAny(right) ? TAnyRight(left, right) :
|
|
1215
|
-
TypeGuard.TObject(right) ? TObjectRight(left, right) :
|
|
1216
|
-
TypeGuard.TVoid(right) ? TypeExtendsResult.True :
|
|
1217
|
-
TypeExtendsResult.False;
|
|
1218
|
-
}
|
|
1219
|
-
function Visit(left, right) {
|
|
1220
|
-
// prettier-ignore
|
|
1221
|
-
return (
|
|
1222
|
-
// resolvable
|
|
1223
|
-
(TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right)) ? TTemplateLiteral(left, right) :
|
|
1224
|
-
(TypeGuard.TNot(left) || TypeGuard.TNot(right)) ? TNot(left, right) :
|
|
1225
|
-
// standard
|
|
1226
|
-
TypeGuard.TAny(left) ? TAny(left, right) :
|
|
1227
|
-
TypeGuard.TArray(left) ? TArray(left, right) :
|
|
1228
|
-
TypeGuard.TBigInt(left) ? TBigInt(left, right) :
|
|
1229
|
-
TypeGuard.TBoolean(left) ? TBoolean(left, right) :
|
|
1230
|
-
TypeGuard.TAsyncIterator(left) ? TAsyncIterator(left, right) :
|
|
1231
|
-
TypeGuard.TConstructor(left) ? TConstructor(left, right) :
|
|
1232
|
-
TypeGuard.TDate(left) ? TDate(left, right) :
|
|
1233
|
-
TypeGuard.TFunction(left) ? TFunction(left, right) :
|
|
1234
|
-
TypeGuard.TInteger(left) ? TInteger(left, right) :
|
|
1235
|
-
TypeGuard.TIntersect(left) ? TIntersect(left, right) :
|
|
1236
|
-
TypeGuard.TIterator(left) ? TIterator(left, right) :
|
|
1237
|
-
TypeGuard.TLiteral(left) ? TLiteral(left, right) :
|
|
1238
|
-
TypeGuard.TNever(left) ? TNever(left, right) :
|
|
1239
|
-
TypeGuard.TNull(left) ? TNull(left, right) :
|
|
1240
|
-
TypeGuard.TNumber(left) ? TNumber(left, right) :
|
|
1241
|
-
TypeGuard.TObject(left) ? TObject(left, right) :
|
|
1242
|
-
TypeGuard.TRecord(left) ? TRecord(left, right) :
|
|
1243
|
-
TypeGuard.TString(left) ? TString(left, right) :
|
|
1244
|
-
TypeGuard.TSymbol(left) ? TSymbol(left, right) :
|
|
1245
|
-
TypeGuard.TTuple(left) ? TTuple(left, right) :
|
|
1246
|
-
TypeGuard.TPromise(left) ? TPromise(left, right) :
|
|
1247
|
-
TypeGuard.TUint8Array(left) ? TUint8Array(left, right) :
|
|
1248
|
-
TypeGuard.TUndefined(left) ? TUndefined(left, right) :
|
|
1249
|
-
TypeGuard.TUnion(left) ? TUnion(left, right) :
|
|
1250
|
-
TypeGuard.TUnknown(left) ? TUnknown(left, right) :
|
|
1251
|
-
TypeGuard.TVoid(left) ? TVoid(left, right) :
|
|
1252
|
-
Throw(`Unknown left type operand '${left[exports.Kind]}'`));
|
|
1253
|
-
}
|
|
1254
|
-
function Extends(left, right) {
|
|
1255
|
-
return Visit(left, right);
|
|
1256
|
-
}
|
|
1257
|
-
TypeExtends.Extends = Extends;
|
|
1258
|
-
})(TypeExtends || (exports.TypeExtends = TypeExtends = {}));
|
|
1259
|
-
// --------------------------------------------------------------------------
|
|
1260
|
-
// TypeClone
|
|
1261
|
-
// --------------------------------------------------------------------------
|
|
1262
|
-
/** Specialized Clone for Types */
|
|
1263
|
-
var TypeClone;
|
|
1264
|
-
(function (TypeClone) {
|
|
1265
|
-
function ArrayType(value) {
|
|
1266
|
-
return value.map((value) => Visit(value));
|
|
1267
|
-
}
|
|
1268
|
-
function DateType(value) {
|
|
1269
|
-
return new Date(value.getTime());
|
|
1270
|
-
}
|
|
1271
|
-
function Uint8ArrayType(value) {
|
|
1272
|
-
return new Uint8Array(value);
|
|
1273
|
-
}
|
|
1274
|
-
function ObjectType(value) {
|
|
1275
|
-
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1276
|
-
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1277
|
-
return { ...clonedProperties, ...clonedSymbols };
|
|
1278
|
-
}
|
|
1279
|
-
function Visit(value) {
|
|
1280
|
-
// prettier-ignore
|
|
1281
|
-
return (ValueGuard.IsArray(value) ? ArrayType(value) :
|
|
1282
|
-
ValueGuard.IsDate(value) ? DateType(value) :
|
|
1283
|
-
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
|
|
1284
|
-
ValueGuard.IsObject(value) ? ObjectType(value) :
|
|
1285
|
-
value);
|
|
1286
|
-
}
|
|
1287
|
-
/** Clones a Rest */
|
|
1288
|
-
function Rest(schemas) {
|
|
1289
|
-
return schemas.map((schema) => Type(schema));
|
|
1290
|
-
}
|
|
1291
|
-
TypeClone.Rest = Rest;
|
|
1292
|
-
/** Clones a Type */
|
|
1293
|
-
function Type(schema, options = {}) {
|
|
1294
|
-
return { ...Visit(schema), ...options };
|
|
1295
|
-
}
|
|
1296
|
-
TypeClone.Type = Type;
|
|
1297
|
-
})(TypeClone || (exports.TypeClone = TypeClone = {}));
|
|
1298
|
-
// --------------------------------------------------------------------------
|
|
1299
|
-
// IndexedAccessor
|
|
1300
|
-
// --------------------------------------------------------------------------
|
|
1301
|
-
var IndexedAccessor;
|
|
1302
|
-
(function (IndexedAccessor) {
|
|
1303
|
-
function OptionalUnwrap(schema) {
|
|
1304
|
-
return schema.map((schema) => {
|
|
1305
|
-
const { [exports.Optional]: _, ...clone } = TypeClone.Type(schema);
|
|
1306
|
-
return clone;
|
|
1307
|
-
});
|
|
1308
|
-
}
|
|
1309
|
-
function IsIntersectOptional(schema) {
|
|
1310
|
-
return schema.every((schema) => TypeGuard.TOptional(schema));
|
|
1311
|
-
}
|
|
1312
|
-
function IsUnionOptional(schema) {
|
|
1313
|
-
return schema.some((schema) => TypeGuard.TOptional(schema));
|
|
1314
|
-
}
|
|
1315
|
-
function ResolveIntersect(schema) {
|
|
1316
|
-
return IsIntersectOptional(schema.allOf) ? exports.Type.Optional(exports.Type.Intersect(OptionalUnwrap(schema.allOf))) : schema;
|
|
1317
|
-
}
|
|
1318
|
-
function ResolveUnion(schema) {
|
|
1319
|
-
return IsUnionOptional(schema.anyOf) ? exports.Type.Optional(exports.Type.Union(OptionalUnwrap(schema.anyOf))) : schema;
|
|
1320
|
-
}
|
|
1321
|
-
function ResolveOptional(schema) {
|
|
1322
|
-
// prettier-ignore
|
|
1323
|
-
return schema[exports.Kind] === 'Intersect' ? ResolveIntersect(schema) :
|
|
1324
|
-
schema[exports.Kind] === 'Union' ? ResolveUnion(schema) :
|
|
1325
|
-
schema;
|
|
1326
|
-
}
|
|
1327
|
-
function TIntersect(schema, key) {
|
|
1328
|
-
const resolved = schema.allOf.reduce((acc, schema) => {
|
|
1329
|
-
const indexed = Visit(schema, key);
|
|
1330
|
-
return indexed[exports.Kind] === 'Never' ? acc : [...acc, indexed];
|
|
1331
|
-
}, []);
|
|
1332
|
-
return ResolveOptional(exports.Type.Intersect(resolved));
|
|
1333
|
-
}
|
|
1334
|
-
function TUnion(schema, key) {
|
|
1335
|
-
const resolved = schema.anyOf.map((schema) => Visit(schema, key));
|
|
1336
|
-
return ResolveOptional(exports.Type.Union(resolved));
|
|
1337
|
-
}
|
|
1338
|
-
function TObject(schema, key) {
|
|
1339
|
-
const property = schema.properties[key];
|
|
1340
|
-
return ValueGuard.IsUndefined(property) ? exports.Type.Never() : exports.Type.Union([property]);
|
|
1341
|
-
}
|
|
1342
|
-
function TTuple(schema, key) {
|
|
1343
|
-
const items = schema.items;
|
|
1344
|
-
if (ValueGuard.IsUndefined(items))
|
|
1345
|
-
return exports.Type.Never();
|
|
1346
|
-
const element = items[key]; //
|
|
1347
|
-
if (ValueGuard.IsUndefined(element))
|
|
1348
|
-
return exports.Type.Never();
|
|
1349
|
-
return element;
|
|
1350
|
-
}
|
|
1351
|
-
function Visit(schema, key) {
|
|
1352
|
-
// prettier-ignore
|
|
1353
|
-
return schema[exports.Kind] === 'Intersect' ? TIntersect(schema, key) :
|
|
1354
|
-
schema[exports.Kind] === 'Union' ? TUnion(schema, key) :
|
|
1355
|
-
schema[exports.Kind] === 'Object' ? TObject(schema, key) :
|
|
1356
|
-
schema[exports.Kind] === 'Tuple' ? TTuple(schema, key) :
|
|
1357
|
-
exports.Type.Never();
|
|
1358
|
-
}
|
|
1359
|
-
function Resolve(schema, keys, options = {}) {
|
|
1360
|
-
const resolved = keys.map((key) => Visit(schema, key.toString()));
|
|
1361
|
-
return ResolveOptional(exports.Type.Union(resolved, options));
|
|
1362
|
-
}
|
|
1363
|
-
IndexedAccessor.Resolve = Resolve;
|
|
1364
|
-
})(IndexedAccessor || (exports.IndexedAccessor = IndexedAccessor = {}));
|
|
1365
|
-
// --------------------------------------------------------------------------
|
|
1366
|
-
// Intrinsic
|
|
1367
|
-
// --------------------------------------------------------------------------
|
|
1368
|
-
var Intrinsic;
|
|
1369
|
-
(function (Intrinsic) {
|
|
1370
|
-
function Uncapitalize(value) {
|
|
1371
|
-
const [first, rest] = [value.slice(0, 1), value.slice(1)];
|
|
1372
|
-
return `${first.toLowerCase()}${rest}`;
|
|
1373
|
-
}
|
|
1374
|
-
function Capitalize(value) {
|
|
1375
|
-
const [first, rest] = [value.slice(0, 1), value.slice(1)];
|
|
1376
|
-
return `${first.toUpperCase()}${rest}`;
|
|
1377
|
-
}
|
|
1378
|
-
function Uppercase(value) {
|
|
1379
|
-
return value.toUpperCase();
|
|
1380
|
-
}
|
|
1381
|
-
function Lowercase(value) {
|
|
1382
|
-
return value.toLowerCase();
|
|
1383
|
-
}
|
|
1384
|
-
function IntrinsicTemplateLiteral(schema, mode) {
|
|
1385
|
-
// note: template literals require special runtime handling as they are encoded in string patterns.
|
|
1386
|
-
// This diverges from the mapped type which would otherwise map on the template literal kind.
|
|
1387
|
-
const expression = TemplateLiteralParser.ParseExact(schema.pattern);
|
|
1388
|
-
const finite = TemplateLiteralFinite.Check(expression);
|
|
1389
|
-
if (!finite)
|
|
1390
|
-
return { ...schema, pattern: IntrinsicLiteral(schema.pattern, mode) };
|
|
1391
|
-
const strings = [...TemplateLiteralGenerator.Generate(expression)];
|
|
1392
|
-
const literals = strings.map((value) => exports.Type.Literal(value));
|
|
1393
|
-
const mapped = IntrinsicRest(literals, mode);
|
|
1394
|
-
const union = exports.Type.Union(mapped);
|
|
1395
|
-
return exports.Type.TemplateLiteral([union]);
|
|
1396
|
-
}
|
|
1397
|
-
function IntrinsicLiteral(value, mode) {
|
|
1398
|
-
// prettier-ignore
|
|
1399
|
-
return typeof value === 'string' ? (mode === 'Uncapitalize' ? Uncapitalize(value) :
|
|
1400
|
-
mode === 'Capitalize' ? Capitalize(value) :
|
|
1401
|
-
mode === 'Uppercase' ? Uppercase(value) :
|
|
1402
|
-
mode === 'Lowercase' ? Lowercase(value) :
|
|
1403
|
-
value) : value.toString();
|
|
1404
|
-
}
|
|
1405
|
-
function IntrinsicRest(schema, mode) {
|
|
1406
|
-
if (schema.length === 0)
|
|
1407
|
-
return [];
|
|
1408
|
-
const [L, ...R] = schema;
|
|
1409
|
-
return [Map(L, mode), ...IntrinsicRest(R, mode)];
|
|
1410
|
-
}
|
|
1411
|
-
function Visit(schema, mode) {
|
|
1412
|
-
// prettier-ignore
|
|
1413
|
-
return TypeGuard.TTemplateLiteral(schema) ? IntrinsicTemplateLiteral(schema, mode) :
|
|
1414
|
-
TypeGuard.TUnion(schema) ? exports.Type.Union(IntrinsicRest(schema.anyOf, mode)) :
|
|
1415
|
-
TypeGuard.TLiteral(schema) ? exports.Type.Literal(IntrinsicLiteral(schema.const, mode)) :
|
|
1416
|
-
schema;
|
|
1417
|
-
}
|
|
1418
|
-
/** Applies an intrinsic string manipulation to the given type. */
|
|
1419
|
-
function Map(schema, mode) {
|
|
1420
|
-
return Visit(schema, mode);
|
|
1421
|
-
}
|
|
1422
|
-
Intrinsic.Map = Map;
|
|
1423
|
-
})(Intrinsic || (exports.Intrinsic = Intrinsic = {}));
|
|
1424
|
-
// --------------------------------------------------------------------------
|
|
1425
|
-
// ObjectMap
|
|
1426
|
-
// --------------------------------------------------------------------------
|
|
1427
|
-
var ObjectMap;
|
|
1428
|
-
(function (ObjectMap) {
|
|
1429
|
-
function TIntersect(schema, callback) {
|
|
1430
|
-
// prettier-ignore
|
|
1431
|
-
return exports.Type.Intersect(schema.allOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1432
|
-
}
|
|
1433
|
-
function TUnion(schema, callback) {
|
|
1434
|
-
// prettier-ignore
|
|
1435
|
-
return exports.Type.Union(schema.anyOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1436
|
-
}
|
|
1437
|
-
function TObject(schema, callback) {
|
|
1438
|
-
return callback(schema);
|
|
1439
|
-
}
|
|
1440
|
-
function Visit(schema, callback) {
|
|
1441
|
-
// There are cases where users need to map objects with unregistered kinds. Using a TypeGuard here would
|
|
1442
|
-
// prevent sub schema mapping as unregistered kinds will not pass TSchema checks. This is notable in the
|
|
1443
|
-
// case of TObject where unregistered property kinds cause the TObject check to fail. As mapping is only
|
|
1444
|
-
// used for composition, we use explicit checks instead.
|
|
1445
|
-
// prettier-ignore
|
|
1446
|
-
return (schema[exports.Kind] === 'Intersect' ? TIntersect(schema, callback) :
|
|
1447
|
-
schema[exports.Kind] === 'Union' ? TUnion(schema, callback) :
|
|
1448
|
-
schema[exports.Kind] === 'Object' ? TObject(schema, callback) :
|
|
1449
|
-
schema);
|
|
1450
|
-
}
|
|
1451
|
-
function Map(schema, callback, options) {
|
|
1452
|
-
return { ...Visit(TypeClone.Type(schema), callback), ...options };
|
|
1453
|
-
}
|
|
1454
|
-
ObjectMap.Map = Map;
|
|
1455
|
-
})(ObjectMap || (exports.ObjectMap = ObjectMap = {}));
|
|
1456
|
-
var KeyResolver;
|
|
1457
|
-
(function (KeyResolver) {
|
|
1458
|
-
function UnwrapPattern(key) {
|
|
1459
|
-
return key[0] === '^' && key[key.length - 1] === '$' ? key.slice(1, key.length - 1) : key;
|
|
1460
|
-
}
|
|
1461
|
-
function TIntersect(schema, options) {
|
|
1462
|
-
return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, options)], []);
|
|
1463
|
-
}
|
|
1464
|
-
function TUnion(schema, options) {
|
|
1465
|
-
const sets = schema.anyOf.map((inner) => Visit(inner, options));
|
|
1466
|
-
return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
|
|
1467
|
-
}
|
|
1468
|
-
function TObject(schema, options) {
|
|
1469
|
-
return Object.getOwnPropertyNames(schema.properties);
|
|
1470
|
-
}
|
|
1471
|
-
function TRecord(schema, options) {
|
|
1472
|
-
return options.includePatterns ? Object.getOwnPropertyNames(schema.patternProperties) : [];
|
|
1473
|
-
}
|
|
1474
|
-
function Visit(schema, options) {
|
|
1475
|
-
// prettier-ignore
|
|
1476
|
-
return (TypeGuard.TIntersect(schema) ? TIntersect(schema, options) :
|
|
1477
|
-
TypeGuard.TUnion(schema) ? TUnion(schema, options) :
|
|
1478
|
-
TypeGuard.TObject(schema) ? TObject(schema, options) :
|
|
1479
|
-
TypeGuard.TRecord(schema) ? TRecord(schema, options) :
|
|
1480
|
-
[]);
|
|
1481
|
-
}
|
|
1482
|
-
/** Resolves an array of keys in this schema */
|
|
1483
|
-
function ResolveKeys(schema, options) {
|
|
1484
|
-
return [...new Set(Visit(schema, options))];
|
|
1485
|
-
}
|
|
1486
|
-
KeyResolver.ResolveKeys = ResolveKeys;
|
|
1487
|
-
/** Resolves a regular expression pattern matching all keys in this schema */
|
|
1488
|
-
function ResolvePattern(schema) {
|
|
1489
|
-
const keys = ResolveKeys(schema, { includePatterns: true });
|
|
1490
|
-
const pattern = keys.map((key) => `(${UnwrapPattern(key)})`);
|
|
1491
|
-
return `^(${pattern.join('|')})$`;
|
|
1492
|
-
}
|
|
1493
|
-
KeyResolver.ResolvePattern = ResolvePattern;
|
|
1494
|
-
})(KeyResolver || (exports.KeyResolver = KeyResolver = {}));
|
|
1495
|
-
// --------------------------------------------------------------------------
|
|
1496
|
-
// KeyArrayResolver
|
|
1497
|
-
// --------------------------------------------------------------------------
|
|
1498
|
-
class KeyArrayResolverError extends TypeBoxError {
|
|
1499
|
-
}
|
|
1500
|
-
exports.KeyArrayResolverError = KeyArrayResolverError;
|
|
1501
|
-
var KeyArrayResolver;
|
|
1502
|
-
(function (KeyArrayResolver) {
|
|
1503
|
-
/** Resolves an array of string[] keys from the given schema or array type. */
|
|
1504
|
-
function Resolve(schema) {
|
|
1505
|
-
// prettier-ignore
|
|
1506
|
-
return Array.isArray(schema) ? schema :
|
|
1507
|
-
TypeGuard.TUnionLiteral(schema) ? schema.anyOf.map((schema) => schema.const.toString()) :
|
|
1508
|
-
TypeGuard.TLiteral(schema) ? [schema.const] :
|
|
1509
|
-
TypeGuard.TTemplateLiteral(schema) ? (() => {
|
|
1510
|
-
const expression = TemplateLiteralParser.ParseExact(schema.pattern);
|
|
1511
|
-
if (!TemplateLiteralFinite.Check(expression))
|
|
1512
|
-
throw new KeyArrayResolverError('Cannot resolve keys from infinite template expression');
|
|
1513
|
-
return [...TemplateLiteralGenerator.Generate(expression)];
|
|
1514
|
-
})() : [];
|
|
1515
|
-
}
|
|
1516
|
-
KeyArrayResolver.Resolve = Resolve;
|
|
1517
|
-
})(KeyArrayResolver || (exports.KeyArrayResolver = KeyArrayResolver = {}));
|
|
1518
|
-
// --------------------------------------------------------------------------
|
|
1519
|
-
// UnionResolver
|
|
1520
|
-
// --------------------------------------------------------------------------
|
|
1521
|
-
var UnionResolver;
|
|
1522
|
-
(function (UnionResolver) {
|
|
1523
|
-
function* TUnion(union) {
|
|
1524
|
-
for (const schema of union.anyOf) {
|
|
1525
|
-
if (schema[exports.Kind] === 'Union') {
|
|
1526
|
-
yield* TUnion(schema);
|
|
1527
|
-
}
|
|
1528
|
-
else {
|
|
1529
|
-
yield schema;
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
}
|
|
1533
|
-
/** Returns a resolved union with interior unions flattened */
|
|
1534
|
-
function Resolve(union) {
|
|
1535
|
-
return exports.Type.Union([...TUnion(union)], { ...union });
|
|
1536
|
-
}
|
|
1537
|
-
UnionResolver.Resolve = Resolve;
|
|
1538
|
-
})(UnionResolver || (exports.UnionResolver = UnionResolver = {}));
|
|
1539
|
-
// --------------------------------------------------------------------------
|
|
1540
|
-
// TemplateLiteralPattern
|
|
1541
|
-
// --------------------------------------------------------------------------
|
|
1542
|
-
class TemplateLiteralPatternError extends TypeBoxError {
|
|
1543
|
-
}
|
|
1544
|
-
exports.TemplateLiteralPatternError = TemplateLiteralPatternError;
|
|
1545
|
-
var TemplateLiteralPattern;
|
|
1546
|
-
(function (TemplateLiteralPattern) {
|
|
1547
|
-
function Throw(message) {
|
|
1548
|
-
throw new TemplateLiteralPatternError(message);
|
|
1549
|
-
}
|
|
1550
|
-
function Escape(value) {
|
|
1551
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1552
|
-
}
|
|
1553
|
-
function Visit(schema, acc) {
|
|
1554
|
-
// prettier-ignore
|
|
1555
|
-
return (TypeGuard.TTemplateLiteral(schema) ? schema.pattern.slice(1, schema.pattern.length - 1) :
|
|
1556
|
-
TypeGuard.TUnion(schema) ? `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})` :
|
|
1557
|
-
TypeGuard.TNumber(schema) ? `${acc}${exports.PatternNumber}` :
|
|
1558
|
-
TypeGuard.TInteger(schema) ? `${acc}${exports.PatternNumber}` :
|
|
1559
|
-
TypeGuard.TBigInt(schema) ? `${acc}${exports.PatternNumber}` :
|
|
1560
|
-
TypeGuard.TString(schema) ? `${acc}${exports.PatternString}` :
|
|
1561
|
-
TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` :
|
|
1562
|
-
TypeGuard.TBoolean(schema) ? `${acc}${exports.PatternBoolean}` :
|
|
1563
|
-
Throw(`Unexpected Kind '${schema[exports.Kind]}'`));
|
|
1564
|
-
}
|
|
1565
|
-
function Create(kinds) {
|
|
1566
|
-
return `^${kinds.map((schema) => Visit(schema, '')).join('')}\$`;
|
|
1567
|
-
}
|
|
1568
|
-
TemplateLiteralPattern.Create = Create;
|
|
1569
|
-
})(TemplateLiteralPattern || (exports.TemplateLiteralPattern = TemplateLiteralPattern = {}));
|
|
1570
|
-
// --------------------------------------------------------------------------------------
|
|
1571
|
-
// TemplateLiteralResolver
|
|
1572
|
-
// --------------------------------------------------------------------------------------
|
|
1573
|
-
var TemplateLiteralResolver;
|
|
1574
|
-
(function (TemplateLiteralResolver) {
|
|
1575
|
-
/** Resolves a template literal as a TUnion */
|
|
1576
|
-
function Resolve(template) {
|
|
1577
|
-
const expression = TemplateLiteralParser.ParseExact(template.pattern);
|
|
1578
|
-
if (!TemplateLiteralFinite.Check(expression))
|
|
1579
|
-
return exports.Type.String();
|
|
1580
|
-
const literals = [...TemplateLiteralGenerator.Generate(expression)].map((value) => exports.Type.Literal(value));
|
|
1581
|
-
return exports.Type.Union(literals);
|
|
1582
|
-
}
|
|
1583
|
-
TemplateLiteralResolver.Resolve = Resolve;
|
|
1584
|
-
})(TemplateLiteralResolver || (exports.TemplateLiteralResolver = TemplateLiteralResolver = {}));
|
|
1585
|
-
// --------------------------------------------------------------------------------------
|
|
1586
|
-
// TemplateLiteralParser
|
|
1587
|
-
// --------------------------------------------------------------------------------------
|
|
1588
|
-
class TemplateLiteralParserError extends TypeBoxError {
|
|
1589
|
-
}
|
|
1590
|
-
exports.TemplateLiteralParserError = TemplateLiteralParserError;
|
|
1591
|
-
var TemplateLiteralParser;
|
|
1592
|
-
(function (TemplateLiteralParser) {
|
|
1593
|
-
function IsNonEscaped(pattern, index, char) {
|
|
1594
|
-
return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
|
|
1595
|
-
}
|
|
1596
|
-
function IsOpenParen(pattern, index) {
|
|
1597
|
-
return IsNonEscaped(pattern, index, '(');
|
|
1598
|
-
}
|
|
1599
|
-
function IsCloseParen(pattern, index) {
|
|
1600
|
-
return IsNonEscaped(pattern, index, ')');
|
|
1601
|
-
}
|
|
1602
|
-
function IsSeparator(pattern, index) {
|
|
1603
|
-
return IsNonEscaped(pattern, index, '|');
|
|
1604
|
-
}
|
|
1605
|
-
function IsGroup(pattern) {
|
|
1606
|
-
if (!(IsOpenParen(pattern, 0) && IsCloseParen(pattern, pattern.length - 1)))
|
|
1607
|
-
return false;
|
|
1608
|
-
let count = 0;
|
|
1609
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
1610
|
-
if (IsOpenParen(pattern, index))
|
|
1611
|
-
count += 1;
|
|
1612
|
-
if (IsCloseParen(pattern, index))
|
|
1613
|
-
count -= 1;
|
|
1614
|
-
if (count === 0 && index !== pattern.length - 1)
|
|
1615
|
-
return false;
|
|
1616
|
-
}
|
|
1617
|
-
return true;
|
|
1618
|
-
}
|
|
1619
|
-
function InGroup(pattern) {
|
|
1620
|
-
return pattern.slice(1, pattern.length - 1);
|
|
1621
|
-
}
|
|
1622
|
-
function IsPrecedenceOr(pattern) {
|
|
1623
|
-
let count = 0;
|
|
1624
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
1625
|
-
if (IsOpenParen(pattern, index))
|
|
1626
|
-
count += 1;
|
|
1627
|
-
if (IsCloseParen(pattern, index))
|
|
1628
|
-
count -= 1;
|
|
1629
|
-
if (IsSeparator(pattern, index) && count === 0)
|
|
1630
|
-
return true;
|
|
1631
|
-
}
|
|
1632
|
-
return false;
|
|
1633
|
-
}
|
|
1634
|
-
function IsPrecedenceAnd(pattern) {
|
|
1635
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
1636
|
-
if (IsOpenParen(pattern, index))
|
|
1637
|
-
return true;
|
|
1638
|
-
}
|
|
1639
|
-
return false;
|
|
1640
|
-
}
|
|
1641
|
-
function Or(pattern) {
|
|
1642
|
-
let [count, start] = [0, 0];
|
|
1643
|
-
const expressions = [];
|
|
1644
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
1645
|
-
if (IsOpenParen(pattern, index))
|
|
1646
|
-
count += 1;
|
|
1647
|
-
if (IsCloseParen(pattern, index))
|
|
1648
|
-
count -= 1;
|
|
1649
|
-
if (IsSeparator(pattern, index) && count === 0) {
|
|
1650
|
-
const range = pattern.slice(start, index);
|
|
1651
|
-
if (range.length > 0)
|
|
1652
|
-
expressions.push(Parse(range));
|
|
1653
|
-
start = index + 1;
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
const range = pattern.slice(start);
|
|
1657
|
-
if (range.length > 0)
|
|
1658
|
-
expressions.push(Parse(range));
|
|
1659
|
-
if (expressions.length === 0)
|
|
1660
|
-
return { type: 'const', const: '' };
|
|
1661
|
-
if (expressions.length === 1)
|
|
1662
|
-
return expressions[0];
|
|
1663
|
-
return { type: 'or', expr: expressions };
|
|
1664
|
-
}
|
|
1665
|
-
function And(pattern) {
|
|
1666
|
-
function Group(value, index) {
|
|
1667
|
-
if (!IsOpenParen(value, index))
|
|
1668
|
-
throw new TemplateLiteralParserError(`TemplateLiteralParser: Index must point to open parens`);
|
|
1669
|
-
let count = 0;
|
|
1670
|
-
for (let scan = index; scan < value.length; scan++) {
|
|
1671
|
-
if (IsOpenParen(value, scan))
|
|
1672
|
-
count += 1;
|
|
1673
|
-
if (IsCloseParen(value, scan))
|
|
1674
|
-
count -= 1;
|
|
1675
|
-
if (count === 0)
|
|
1676
|
-
return [index, scan];
|
|
1677
|
-
}
|
|
1678
|
-
throw new TemplateLiteralParserError(`TemplateLiteralParser: Unclosed group parens in expression`);
|
|
1679
|
-
}
|
|
1680
|
-
function Range(pattern, index) {
|
|
1681
|
-
for (let scan = index; scan < pattern.length; scan++) {
|
|
1682
|
-
if (IsOpenParen(pattern, scan))
|
|
1683
|
-
return [index, scan];
|
|
1684
|
-
}
|
|
1685
|
-
return [index, pattern.length];
|
|
1686
|
-
}
|
|
1687
|
-
const expressions = [];
|
|
1688
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
1689
|
-
if (IsOpenParen(pattern, index)) {
|
|
1690
|
-
const [start, end] = Group(pattern, index);
|
|
1691
|
-
const range = pattern.slice(start, end + 1);
|
|
1692
|
-
expressions.push(Parse(range));
|
|
1693
|
-
index = end;
|
|
1694
|
-
}
|
|
1695
|
-
else {
|
|
1696
|
-
const [start, end] = Range(pattern, index);
|
|
1697
|
-
const range = pattern.slice(start, end);
|
|
1698
|
-
if (range.length > 0)
|
|
1699
|
-
expressions.push(Parse(range));
|
|
1700
|
-
index = end - 1;
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
// prettier-ignore
|
|
1704
|
-
return (expressions.length === 0) ? { type: 'const', const: '' } :
|
|
1705
|
-
(expressions.length === 1) ? expressions[0] :
|
|
1706
|
-
{ type: 'and', expr: expressions };
|
|
1707
|
-
}
|
|
1708
|
-
/** Parses a pattern and returns an expression tree */
|
|
1709
|
-
function Parse(pattern) {
|
|
1710
|
-
// prettier-ignore
|
|
1711
|
-
return IsGroup(pattern) ? Parse(InGroup(pattern)) :
|
|
1712
|
-
IsPrecedenceOr(pattern) ? Or(pattern) :
|
|
1713
|
-
IsPrecedenceAnd(pattern) ? And(pattern) :
|
|
1714
|
-
{ type: 'const', const: pattern };
|
|
1715
|
-
}
|
|
1716
|
-
TemplateLiteralParser.Parse = Parse;
|
|
1717
|
-
/** Parses a pattern and strips forward and trailing ^ and $ */
|
|
1718
|
-
function ParseExact(pattern) {
|
|
1719
|
-
return Parse(pattern.slice(1, pattern.length - 1));
|
|
1720
|
-
}
|
|
1721
|
-
TemplateLiteralParser.ParseExact = ParseExact;
|
|
1722
|
-
})(TemplateLiteralParser || (exports.TemplateLiteralParser = TemplateLiteralParser = {}));
|
|
1723
|
-
// --------------------------------------------------------------------------------------
|
|
1724
|
-
// TemplateLiteralFinite
|
|
1725
|
-
// --------------------------------------------------------------------------------------
|
|
1726
|
-
class TemplateLiteralFiniteError extends TypeBoxError {
|
|
1727
|
-
}
|
|
1728
|
-
exports.TemplateLiteralFiniteError = TemplateLiteralFiniteError;
|
|
1729
|
-
var TemplateLiteralFinite;
|
|
1730
|
-
(function (TemplateLiteralFinite) {
|
|
1731
|
-
function Throw(message) {
|
|
1732
|
-
throw new TemplateLiteralFiniteError(message);
|
|
1733
|
-
}
|
|
1734
|
-
function IsNumber(expression) {
|
|
1735
|
-
// prettier-ignore
|
|
1736
|
-
return (expression.type === 'or' &&
|
|
1737
|
-
expression.expr.length === 2 &&
|
|
1738
|
-
expression.expr[0].type === 'const' &&
|
|
1739
|
-
expression.expr[0].const === '0' &&
|
|
1740
|
-
expression.expr[1].type === 'const' &&
|
|
1741
|
-
expression.expr[1].const === '[1-9][0-9]*');
|
|
1742
|
-
}
|
|
1743
|
-
function IsBoolean(expression) {
|
|
1744
|
-
// prettier-ignore
|
|
1745
|
-
return (expression.type === 'or' &&
|
|
1746
|
-
expression.expr.length === 2 &&
|
|
1747
|
-
expression.expr[0].type === 'const' &&
|
|
1748
|
-
expression.expr[0].const === 'true' &&
|
|
1749
|
-
expression.expr[1].type === 'const' &&
|
|
1750
|
-
expression.expr[1].const === 'false');
|
|
1751
|
-
}
|
|
1752
|
-
function IsString(expression) {
|
|
1753
|
-
return expression.type === 'const' && expression.const === '.*';
|
|
1754
|
-
}
|
|
1755
|
-
function Check(expression) {
|
|
1756
|
-
// prettier-ignore
|
|
1757
|
-
return IsBoolean(expression) ? true :
|
|
1758
|
-
IsNumber(expression) || IsString(expression) ? false :
|
|
1759
|
-
(expression.type === 'and') ? expression.expr.every((expr) => Check(expr)) :
|
|
1760
|
-
(expression.type === 'or') ? expression.expr.every((expr) => Check(expr)) :
|
|
1761
|
-
(expression.type === 'const') ? true :
|
|
1762
|
-
Throw(`Unknown expression type`);
|
|
1763
|
-
}
|
|
1764
|
-
TemplateLiteralFinite.Check = Check;
|
|
1765
|
-
})(TemplateLiteralFinite || (exports.TemplateLiteralFinite = TemplateLiteralFinite = {}));
|
|
1766
|
-
// --------------------------------------------------------------------------------------
|
|
1767
|
-
// TemplateLiteralGenerator
|
|
1768
|
-
// --------------------------------------------------------------------------------------
|
|
1769
|
-
class TemplateLiteralGeneratorError extends TypeBoxError {
|
|
1770
|
-
}
|
|
1771
|
-
exports.TemplateLiteralGeneratorError = TemplateLiteralGeneratorError;
|
|
1772
|
-
var TemplateLiteralGenerator;
|
|
1773
|
-
(function (TemplateLiteralGenerator) {
|
|
1774
|
-
function* Reduce(buffer) {
|
|
1775
|
-
if (buffer.length === 1)
|
|
1776
|
-
return yield* buffer[0];
|
|
1777
|
-
for (const left of buffer[0]) {
|
|
1778
|
-
for (const right of Reduce(buffer.slice(1))) {
|
|
1779
|
-
yield `${left}${right}`;
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
function* And(expression) {
|
|
1784
|
-
return yield* Reduce(expression.expr.map((expr) => [...Generate(expr)]));
|
|
1785
|
-
}
|
|
1786
|
-
function* Or(expression) {
|
|
1787
|
-
for (const expr of expression.expr)
|
|
1788
|
-
yield* Generate(expr);
|
|
1789
|
-
}
|
|
1790
|
-
function* Const(expression) {
|
|
1791
|
-
return yield expression.const;
|
|
1792
|
-
}
|
|
1793
|
-
function* Generate(expression) {
|
|
1794
|
-
// prettier-ignore
|
|
1795
|
-
return (expression.type === 'and' ? yield* And(expression) :
|
|
1796
|
-
expression.type === 'or' ? yield* Or(expression) :
|
|
1797
|
-
expression.type === 'const' ? yield* Const(expression) :
|
|
1798
|
-
(() => { throw new TemplateLiteralGeneratorError('Unknown expression'); })());
|
|
1799
|
-
}
|
|
1800
|
-
TemplateLiteralGenerator.Generate = Generate;
|
|
1801
|
-
})(TemplateLiteralGenerator || (exports.TemplateLiteralGenerator = TemplateLiteralGenerator = {}));
|
|
1802
|
-
// ---------------------------------------------------------------------
|
|
1803
|
-
// TemplateLiteralDslParser
|
|
1804
|
-
// ---------------------------------------------------------------------
|
|
1805
|
-
var TemplateLiteralDslParser;
|
|
1806
|
-
(function (TemplateLiteralDslParser) {
|
|
1807
|
-
function* ParseUnion(template) {
|
|
1808
|
-
const trim = template.trim().replace(/"|'/g, '');
|
|
1809
|
-
// prettier-ignore
|
|
1810
|
-
return (trim === 'boolean' ? yield exports.Type.Boolean() :
|
|
1811
|
-
trim === 'number' ? yield exports.Type.Number() :
|
|
1812
|
-
trim === 'bigint' ? yield exports.Type.BigInt() :
|
|
1813
|
-
trim === 'string' ? yield exports.Type.String() :
|
|
1814
|
-
yield (() => {
|
|
1815
|
-
const literals = trim.split('|').map((literal) => exports.Type.Literal(literal.trim()));
|
|
1816
|
-
return (literals.length === 0 ? exports.Type.Never() :
|
|
1817
|
-
literals.length === 1 ? literals[0] :
|
|
1818
|
-
exports.Type.Union(literals));
|
|
1819
|
-
})());
|
|
1820
|
-
}
|
|
1821
|
-
function* ParseTerminal(template) {
|
|
1822
|
-
if (template[1] !== '{') {
|
|
1823
|
-
const L = exports.Type.Literal('$');
|
|
1824
|
-
const R = ParseLiteral(template.slice(1));
|
|
1825
|
-
return yield* [L, ...R];
|
|
1826
|
-
}
|
|
1827
|
-
for (let i = 2; i < template.length; i++) {
|
|
1828
|
-
if (template[i] === '}') {
|
|
1829
|
-
const L = ParseUnion(template.slice(2, i));
|
|
1830
|
-
const R = ParseLiteral(template.slice(i + 1));
|
|
1831
|
-
return yield* [...L, ...R];
|
|
1832
|
-
}
|
|
1833
|
-
}
|
|
1834
|
-
yield exports.Type.Literal(template);
|
|
1835
|
-
}
|
|
1836
|
-
function* ParseLiteral(template) {
|
|
1837
|
-
for (let i = 0; i < template.length; i++) {
|
|
1838
|
-
if (template[i] === '$') {
|
|
1839
|
-
const L = exports.Type.Literal(template.slice(0, i));
|
|
1840
|
-
const R = ParseTerminal(template.slice(i));
|
|
1841
|
-
return yield* [L, ...R];
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
yield exports.Type.Literal(template);
|
|
1845
|
-
}
|
|
1846
|
-
function Parse(template_dsl) {
|
|
1847
|
-
return [...ParseLiteral(template_dsl)];
|
|
1848
|
-
}
|
|
1849
|
-
TemplateLiteralDslParser.Parse = Parse;
|
|
1850
|
-
})(TemplateLiteralDslParser || (exports.TemplateLiteralDslParser = TemplateLiteralDslParser = {}));
|
|
1851
|
-
// ---------------------------------------------------------------------
|
|
1852
|
-
// TransformBuilder
|
|
1853
|
-
// ---------------------------------------------------------------------
|
|
1854
|
-
class TransformDecodeBuilder {
|
|
1855
|
-
constructor(schema) {
|
|
1856
|
-
this.schema = schema;
|
|
1857
|
-
}
|
|
1858
|
-
Decode(decode) {
|
|
1859
|
-
return new TransformEncodeBuilder(this.schema, decode);
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
exports.TransformDecodeBuilder = TransformDecodeBuilder;
|
|
1863
|
-
class TransformEncodeBuilder {
|
|
1864
|
-
constructor(schema, decode) {
|
|
1865
|
-
this.schema = schema;
|
|
1866
|
-
this.decode = decode;
|
|
1867
|
-
}
|
|
1868
|
-
Encode(encode) {
|
|
1869
|
-
const schema = TypeClone.Type(this.schema);
|
|
1870
|
-
// prettier-ignore
|
|
1871
|
-
return (TypeGuard.TTransform(schema) ? (() => {
|
|
1872
|
-
const Encode = (value) => schema[exports.Transform].Encode(encode(value));
|
|
1873
|
-
const Decode = (value) => this.decode(schema[exports.Transform].Decode(value));
|
|
1874
|
-
const Codec = { Encode: Encode, Decode: Decode };
|
|
1875
|
-
return { ...schema, [exports.Transform]: Codec };
|
|
1876
|
-
})() : (() => {
|
|
1877
|
-
const Codec = { Decode: this.decode, Encode: encode };
|
|
1878
|
-
return { ...schema, [exports.Transform]: Codec };
|
|
1879
|
-
})());
|
|
1880
|
-
}
|
|
1881
|
-
}
|
|
1882
|
-
exports.TransformEncodeBuilder = TransformEncodeBuilder;
|
|
1883
|
-
// --------------------------------------------------------------------------
|
|
1884
|
-
// TypeOrdinal: Used for auto $id generation
|
|
1885
|
-
// --------------------------------------------------------------------------
|
|
1886
|
-
let TypeOrdinal = 0;
|
|
1887
|
-
// --------------------------------------------------------------------------
|
|
1888
|
-
// TypeBuilder
|
|
1889
|
-
// --------------------------------------------------------------------------
|
|
1890
|
-
class TypeBuilderError extends TypeBoxError {
|
|
1891
|
-
}
|
|
1892
|
-
exports.TypeBuilderError = TypeBuilderError;
|
|
1893
|
-
class TypeBuilder {
|
|
1894
|
-
/** `[Internal]` Creates a schema without `static` and `params` types */
|
|
1895
|
-
Create(schema) {
|
|
1896
|
-
return schema;
|
|
1897
|
-
}
|
|
1898
|
-
/** `[Internal]` Throws a TypeBuilder error with the given message */
|
|
1899
|
-
Throw(message) {
|
|
1900
|
-
throw new TypeBuilderError(message);
|
|
1901
|
-
}
|
|
1902
|
-
/** `[Internal]` Discards property keys from the given record type */
|
|
1903
|
-
Discard(record, keys) {
|
|
1904
|
-
return keys.reduce((acc, key) => {
|
|
1905
|
-
const { [key]: _, ...rest } = acc;
|
|
1906
|
-
return rest;
|
|
1907
|
-
}, record);
|
|
1908
|
-
}
|
|
1909
|
-
/** `[Json]` Omits compositing symbols from this schema */
|
|
1910
|
-
Strict(schema) {
|
|
1911
|
-
return JSON.parse(JSON.stringify(schema));
|
|
1912
|
-
}
|
|
1913
|
-
}
|
|
1914
|
-
exports.TypeBuilder = TypeBuilder;
|
|
1915
|
-
// --------------------------------------------------------------------------
|
|
1916
|
-
// JsonTypeBuilder
|
|
1917
|
-
// --------------------------------------------------------------------------
|
|
1918
|
-
class JsonTypeBuilder extends TypeBuilder {
|
|
1919
|
-
// ------------------------------------------------------------------------
|
|
1920
|
-
// Modifiers
|
|
1921
|
-
// ------------------------------------------------------------------------
|
|
1922
|
-
/** `[Json]` Creates a Readonly and Optional property */
|
|
1923
|
-
ReadonlyOptional(schema) {
|
|
1924
|
-
return this.Readonly(this.Optional(schema));
|
|
1925
|
-
}
|
|
1926
|
-
/** `[Json]` Creates a Readonly property */
|
|
1927
|
-
Readonly(schema) {
|
|
1928
|
-
return { ...TypeClone.Type(schema), [exports.Readonly]: 'Readonly' };
|
|
1929
|
-
}
|
|
1930
|
-
/** `[Json]` Creates an Optional property */
|
|
1931
|
-
Optional(schema) {
|
|
1932
|
-
return { ...TypeClone.Type(schema), [exports.Optional]: 'Optional' };
|
|
1933
|
-
}
|
|
1934
|
-
// ------------------------------------------------------------------------
|
|
1935
|
-
// Types
|
|
1936
|
-
// ------------------------------------------------------------------------
|
|
1937
|
-
/** `[Json]` Creates an Any type */
|
|
1938
|
-
Any(options = {}) {
|
|
1939
|
-
return this.Create({ ...options, [exports.Kind]: 'Any' });
|
|
1940
|
-
}
|
|
1941
|
-
/** `[Json]` Creates an Array type */
|
|
1942
|
-
Array(schema, options = {}) {
|
|
1943
|
-
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Type(schema) });
|
|
1944
|
-
}
|
|
1945
|
-
/** `[Json]` Creates a Boolean type */
|
|
1946
|
-
Boolean(options = {}) {
|
|
1947
|
-
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
1948
|
-
}
|
|
1949
|
-
/** `[Json]` Intrinsic function to Capitalize LiteralString types */
|
|
1950
|
-
Capitalize(schema, options = {}) {
|
|
1951
|
-
return { ...Intrinsic.Map(TypeClone.Type(schema), 'Capitalize'), ...options };
|
|
1952
|
-
}
|
|
1953
|
-
/** `[Json]` Creates a Composite object type */
|
|
1954
|
-
Composite(objects, options) {
|
|
1955
|
-
const intersect = exports.Type.Intersect(objects, {});
|
|
1956
|
-
const keys = KeyResolver.ResolveKeys(intersect, { includePatterns: false });
|
|
1957
|
-
const properties = keys.reduce((acc, key) => ({ ...acc, [key]: exports.Type.Index(intersect, [key]) }), {});
|
|
1958
|
-
return exports.Type.Object(properties, options);
|
|
1959
|
-
}
|
|
1960
|
-
/** `[Json]` Creates a Enum type */
|
|
1961
|
-
Enum(item, options = {}) {
|
|
1962
|
-
if (ValueGuard.IsUndefined(item))
|
|
1963
|
-
return this.Throw('Enum undefined or empty');
|
|
1964
|
-
// prettier-ignore
|
|
1965
|
-
const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
|
|
1966
|
-
const values2 = [...new Set(values1)];
|
|
1967
|
-
const anyOf = values2.map((value) => exports.Type.Literal(value));
|
|
1968
|
-
return this.Union(anyOf, { ...options, [exports.Hint]: 'Enum' });
|
|
1969
|
-
}
|
|
1970
|
-
/** `[Json]` Creates a Conditional type */
|
|
1971
|
-
Extends(left, right, trueType, falseType, options = {}) {
|
|
1972
|
-
switch (TypeExtends.Extends(left, right)) {
|
|
1973
|
-
case TypeExtendsResult.Union:
|
|
1974
|
-
return this.Union([TypeClone.Type(trueType, options), TypeClone.Type(falseType, options)]);
|
|
1975
|
-
case TypeExtendsResult.True:
|
|
1976
|
-
return TypeClone.Type(trueType, options);
|
|
1977
|
-
case TypeExtendsResult.False:
|
|
1978
|
-
return TypeClone.Type(falseType, options);
|
|
1979
|
-
}
|
|
1980
|
-
}
|
|
1981
|
-
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
|
|
1982
|
-
Exclude(unionType, excludedMembers, options = {}) {
|
|
1983
|
-
// prettier-ignore
|
|
1984
|
-
return (TypeGuard.TTemplateLiteral(unionType) ? this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options) :
|
|
1985
|
-
TypeGuard.TTemplateLiteral(excludedMembers) ? this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options) :
|
|
1986
|
-
TypeGuard.TUnion(unionType) ? (() => {
|
|
1987
|
-
const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False);
|
|
1988
|
-
return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
|
|
1989
|
-
})() :
|
|
1990
|
-
TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) :
|
|
1991
|
-
TypeClone.Type(unionType, options));
|
|
1992
|
-
}
|
|
1993
|
-
/** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
|
|
1994
|
-
Extract(type, union, options = {}) {
|
|
1995
|
-
// prettier-ignore
|
|
1996
|
-
return (TypeGuard.TTemplateLiteral(type) ? this.Extract(TemplateLiteralResolver.Resolve(type), union, options) :
|
|
1997
|
-
TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union), options) :
|
|
1998
|
-
TypeGuard.TUnion(type) ? (() => {
|
|
1999
|
-
const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False);
|
|
2000
|
-
return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
|
|
2001
|
-
})() :
|
|
2002
|
-
TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Type(type, options) :
|
|
2003
|
-
this.Never(options));
|
|
2004
|
-
}
|
|
2005
|
-
/** `[Json]` Returns an Indexed property type for the given keys */
|
|
2006
|
-
Index(schema, unresolved, options = {}) {
|
|
2007
|
-
// prettier-ignore
|
|
2008
|
-
return (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved) ? (() => {
|
|
2009
|
-
return TypeClone.Type(schema.items, options);
|
|
2010
|
-
})() :
|
|
2011
|
-
TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved) ? (() => {
|
|
2012
|
-
const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
|
|
2013
|
-
const cloned = items.map((schema) => TypeClone.Type(schema));
|
|
2014
|
-
return this.Union(cloned, options);
|
|
2015
|
-
})() : (() => {
|
|
2016
|
-
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2017
|
-
const clone = TypeClone.Type(schema);
|
|
2018
|
-
return IndexedAccessor.Resolve(clone, keys, options);
|
|
2019
|
-
})());
|
|
2020
|
-
}
|
|
2021
|
-
/** `[Json]` Creates an Integer type */
|
|
2022
|
-
Integer(options = {}) {
|
|
2023
|
-
return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
|
|
2024
|
-
}
|
|
2025
|
-
/** `[Json]` Creates an Intersect type */
|
|
2026
|
-
Intersect(allOf, options = {}) {
|
|
2027
|
-
if (allOf.length === 0)
|
|
2028
|
-
return exports.Type.Never();
|
|
2029
|
-
if (allOf.length === 1)
|
|
2030
|
-
return TypeClone.Type(allOf[0], options);
|
|
2031
|
-
if (allOf.some((schema) => TypeGuard.TTransform(schema)))
|
|
2032
|
-
this.Throw('Cannot intersect transform types');
|
|
2033
|
-
const objects = allOf.every((schema) => TypeGuard.TObject(schema));
|
|
2034
|
-
const cloned = TypeClone.Rest(allOf);
|
|
2035
|
-
// prettier-ignore
|
|
2036
|
-
const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties)
|
|
2037
|
-
? { unevaluatedProperties: TypeClone.Type(options.unevaluatedProperties) }
|
|
2038
|
-
: {};
|
|
2039
|
-
return options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects
|
|
2040
|
-
? this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned })
|
|
2041
|
-
: this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', allOf: cloned });
|
|
2042
|
-
}
|
|
2043
|
-
/** `[Json]` Creates a KeyOf type */
|
|
2044
|
-
KeyOf(schema, options = {}) {
|
|
2045
|
-
// prettier-ignore
|
|
2046
|
-
return (TypeGuard.TRecord(schema) ? (() => {
|
|
2047
|
-
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
2048
|
-
return (pattern === exports.PatternNumberExact ? this.Number(options) :
|
|
2049
|
-
pattern === exports.PatternStringExact ? this.String(options) :
|
|
2050
|
-
this.Throw('Unable to resolve key type from Record key pattern'));
|
|
2051
|
-
})() :
|
|
2052
|
-
TypeGuard.TTuple(schema) ? (() => {
|
|
2053
|
-
const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
|
|
2054
|
-
const literals = items.map((_, index) => exports.Type.Literal(index.toString()));
|
|
2055
|
-
return this.Union(literals, options);
|
|
2056
|
-
})() :
|
|
2057
|
-
TypeGuard.TArray(schema) ? (() => {
|
|
2058
|
-
return this.Number(options);
|
|
2059
|
-
})() : (() => {
|
|
2060
|
-
const keys = KeyResolver.ResolveKeys(schema, { includePatterns: false });
|
|
2061
|
-
if (keys.length === 0)
|
|
2062
|
-
return this.Never(options);
|
|
2063
|
-
const literals = keys.map((key) => this.Literal(key));
|
|
2064
|
-
return this.Union(literals, options);
|
|
2065
|
-
})());
|
|
2066
|
-
}
|
|
2067
|
-
/** `[Json]` Creates a Literal type */
|
|
2068
|
-
Literal(value, options = {}) {
|
|
2069
|
-
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
2070
|
-
}
|
|
2071
|
-
/** `[Json]` Intrinsic function to Lowercase LiteralString types */
|
|
2072
|
-
Lowercase(schema, options = {}) {
|
|
2073
|
-
return { ...Intrinsic.Map(TypeClone.Type(schema), 'Lowercase'), ...options };
|
|
2074
|
-
}
|
|
2075
|
-
/** `[Json]` Creates a Never type */
|
|
2076
|
-
Never(options = {}) {
|
|
2077
|
-
return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
|
|
2078
|
-
}
|
|
2079
|
-
/** `[Json]` Creates a Not type */
|
|
2080
|
-
Not(schema, options) {
|
|
2081
|
-
return this.Create({ ...options, [exports.Kind]: 'Not', not: TypeClone.Type(schema) });
|
|
2082
|
-
}
|
|
2083
|
-
/** `[Json]` Creates a Null type */
|
|
2084
|
-
Null(options = {}) {
|
|
2085
|
-
return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
|
|
2086
|
-
}
|
|
2087
|
-
/** `[Json]` Creates a Number type */
|
|
2088
|
-
Number(options = {}) {
|
|
2089
|
-
return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
|
|
2090
|
-
}
|
|
2091
|
-
/** `[Json]` Creates an Object type */
|
|
2092
|
-
Object(properties, options = {}) {
|
|
2093
|
-
const propertyKeys = Object.getOwnPropertyNames(properties);
|
|
2094
|
-
const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]));
|
|
2095
|
-
const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
|
|
2096
|
-
const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Type(options.additionalProperties) } : {};
|
|
2097
|
-
const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(properties[key]) }), {});
|
|
2098
|
-
return requiredKeys.length > 0
|
|
2099
|
-
? this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys })
|
|
2100
|
-
: this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
|
|
2101
|
-
}
|
|
2102
|
-
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
2103
|
-
Omit(schema, unresolved, options = {}) {
|
|
2104
|
-
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2105
|
-
// prettier-ignore
|
|
2106
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2107
|
-
if (ValueGuard.IsArray(object.required)) {
|
|
2108
|
-
object.required = object.required.filter((key) => !keys.includes(key));
|
|
2109
|
-
if (object.required.length === 0)
|
|
2110
|
-
delete object.required;
|
|
2111
|
-
}
|
|
2112
|
-
for (const key of Object.getOwnPropertyNames(object.properties)) {
|
|
2113
|
-
if (keys.includes(key))
|
|
2114
|
-
delete object.properties[key];
|
|
2115
|
-
}
|
|
2116
|
-
return this.Create(object);
|
|
2117
|
-
}, options);
|
|
2118
|
-
}
|
|
2119
|
-
/** `[Json]` Constructs a type where all properties are optional */
|
|
2120
|
-
Partial(schema, options = {}) {
|
|
2121
|
-
// prettier-ignore
|
|
2122
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2123
|
-
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2124
|
-
return { ...acc, [key]: this.Optional(object.properties[key]) };
|
|
2125
|
-
}, {});
|
|
2126
|
-
return this.Object(properties, this.Discard(object, ['required']) /* object used as options to retain other constraints */);
|
|
2127
|
-
}, options);
|
|
2128
|
-
}
|
|
2129
|
-
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
2130
|
-
Pick(schema, unresolved, options = {}) {
|
|
2131
|
-
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2132
|
-
// prettier-ignore
|
|
2133
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2134
|
-
if (ValueGuard.IsArray(object.required)) {
|
|
2135
|
-
object.required = object.required.filter((key) => keys.includes(key));
|
|
2136
|
-
if (object.required.length === 0)
|
|
2137
|
-
delete object.required;
|
|
2138
|
-
}
|
|
2139
|
-
for (const key of Object.getOwnPropertyNames(object.properties)) {
|
|
2140
|
-
if (!keys.includes(key))
|
|
2141
|
-
delete object.properties[key];
|
|
2142
|
-
}
|
|
2143
|
-
return this.Create(object);
|
|
2144
|
-
}, options);
|
|
2145
|
-
}
|
|
2146
|
-
/** `[Json]` Creates a Record type */
|
|
2147
|
-
Record(key, schema, options = {}) {
|
|
2148
|
-
// prettier-ignore
|
|
2149
|
-
return (TypeGuard.TTemplateLiteral(key) ? (() => {
|
|
2150
|
-
const expression = TemplateLiteralParser.ParseExact(key.pattern);
|
|
2151
|
-
// prettier-ignore
|
|
2152
|
-
return TemplateLiteralFinite.Check(expression)
|
|
2153
|
-
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(schema) }), {}), options))
|
|
2154
|
-
: this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Type(schema) } });
|
|
2155
|
-
})() :
|
|
2156
|
-
TypeGuard.TUnion(key) ? (() => {
|
|
2157
|
-
const union = UnionResolver.Resolve(key);
|
|
2158
|
-
if (TypeGuard.TUnionLiteral(union)) {
|
|
2159
|
-
const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Type(schema) }), {});
|
|
2160
|
-
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2161
|
-
}
|
|
2162
|
-
else
|
|
2163
|
-
this.Throw('Record key of type union contains non-literal types');
|
|
2164
|
-
})() :
|
|
2165
|
-
TypeGuard.TLiteral(key) ? (() => {
|
|
2166
|
-
// prettier-ignore
|
|
2167
|
-
return (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const))
|
|
2168
|
-
? this.Object({ [key.const]: TypeClone.Type(schema) }, options)
|
|
2169
|
-
: this.Throw('Record key of type literal is not of type string or number');
|
|
2170
|
-
})() :
|
|
2171
|
-
TypeGuard.TInteger(key) || TypeGuard.TNumber(key) ? (() => {
|
|
2172
|
-
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [exports.PatternNumberExact]: TypeClone.Type(schema) } });
|
|
2173
|
-
})() :
|
|
2174
|
-
TypeGuard.TString(key) ? (() => {
|
|
2175
|
-
const pattern = ValueGuard.IsUndefined(key.pattern) ? exports.PatternStringExact : key.pattern;
|
|
2176
|
-
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(schema) } });
|
|
2177
|
-
})() :
|
|
2178
|
-
this.Never());
|
|
2179
|
-
}
|
|
2180
|
-
/** `[Json]` Creates a Recursive type */
|
|
2181
|
-
Recursive(callback, options = {}) {
|
|
2182
|
-
if (ValueGuard.IsUndefined(options.$id))
|
|
2183
|
-
options.$id = `T${TypeOrdinal++}`;
|
|
2184
|
-
const thisType = callback({ [exports.Kind]: 'This', $ref: `${options.$id}` });
|
|
2185
|
-
thisType.$id = options.$id;
|
|
2186
|
-
return this.Create({ ...options, [exports.Hint]: 'Recursive', ...thisType });
|
|
2187
|
-
}
|
|
2188
|
-
/** `[Json]` Creates a Ref type. */
|
|
2189
|
-
Ref(unresolved, options = {}) {
|
|
2190
|
-
if (ValueGuard.IsString(unresolved))
|
|
2191
|
-
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved });
|
|
2192
|
-
if (ValueGuard.IsUndefined(unresolved.$id))
|
|
2193
|
-
this.Throw('Reference target type must specify an $id');
|
|
2194
|
-
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved.$id });
|
|
2195
|
-
}
|
|
2196
|
-
/** `[Json]` Constructs a type where all properties are required */
|
|
2197
|
-
Required(schema, options = {}) {
|
|
2198
|
-
// prettier-ignore
|
|
2199
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2200
|
-
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2201
|
-
return { ...acc, [key]: this.Discard(object.properties[key], [exports.Optional]) };
|
|
2202
|
-
}, {});
|
|
2203
|
-
return this.Object(properties, object /* object used as options to retain other constraints */);
|
|
2204
|
-
}, options);
|
|
2205
|
-
}
|
|
2206
|
-
/** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
|
|
2207
|
-
Rest(schema) {
|
|
2208
|
-
return (TypeGuard.TTuple(schema) && !ValueGuard.IsUndefined(schema.items) ? TypeClone.Rest(schema.items) : TypeGuard.TIntersect(schema) ? TypeClone.Rest(schema.allOf) : TypeGuard.TUnion(schema) ? TypeClone.Rest(schema.anyOf) : []);
|
|
2209
|
-
}
|
|
2210
|
-
/** `[Json]` Creates a String type */
|
|
2211
|
-
String(options = {}) {
|
|
2212
|
-
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
2213
|
-
}
|
|
2214
|
-
/** `[Json]` Creates a TemplateLiteral type */
|
|
2215
|
-
TemplateLiteral(unresolved, options = {}) {
|
|
2216
|
-
// prettier-ignore
|
|
2217
|
-
const pattern = ValueGuard.IsString(unresolved)
|
|
2218
|
-
? TemplateLiteralPattern.Create(TemplateLiteralDslParser.Parse(unresolved))
|
|
2219
|
-
: TemplateLiteralPattern.Create(unresolved);
|
|
2220
|
-
return this.Create({ ...options, [exports.Kind]: 'TemplateLiteral', type: 'string', pattern });
|
|
2221
|
-
}
|
|
2222
|
-
/** `[Json]` Creates a Transform type */
|
|
2223
|
-
Transform(schema) {
|
|
2224
|
-
return new TransformDecodeBuilder(schema);
|
|
2225
|
-
}
|
|
2226
|
-
/** `[Json]` Creates a Tuple type */
|
|
2227
|
-
Tuple(items, options = {}) {
|
|
2228
|
-
const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
|
|
2229
|
-
const clonedItems = TypeClone.Rest(items);
|
|
2230
|
-
// prettier-ignore
|
|
2231
|
-
const schema = (items.length > 0 ?
|
|
2232
|
-
{ ...options, [exports.Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
|
|
2233
|
-
{ ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
2234
|
-
return this.Create(schema);
|
|
2235
|
-
}
|
|
2236
|
-
/** `[Json]` Intrinsic function to Uncapitalize LiteralString types */
|
|
2237
|
-
Uncapitalize(schema, options = {}) {
|
|
2238
|
-
return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uncapitalize'), ...options };
|
|
2239
|
-
}
|
|
2240
|
-
/** `[Json]` Creates a Union type */
|
|
2241
|
-
Union(union, options = {}) {
|
|
2242
|
-
// prettier-ignore
|
|
2243
|
-
return TypeGuard.TTemplateLiteral(union)
|
|
2244
|
-
? TemplateLiteralResolver.Resolve(union)
|
|
2245
|
-
: (() => {
|
|
2246
|
-
const anyOf = union;
|
|
2247
|
-
if (anyOf.length === 0)
|
|
2248
|
-
return this.Never(options);
|
|
2249
|
-
if (anyOf.length === 1)
|
|
2250
|
-
return this.Create(TypeClone.Type(anyOf[0], options));
|
|
2251
|
-
const clonedAnyOf = TypeClone.Rest(anyOf);
|
|
2252
|
-
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
|
|
2253
|
-
})();
|
|
2254
|
-
}
|
|
2255
|
-
/** `[Json]` Creates an Unknown type */
|
|
2256
|
-
Unknown(options = {}) {
|
|
2257
|
-
return this.Create({ ...options, [exports.Kind]: 'Unknown' });
|
|
2258
|
-
}
|
|
2259
|
-
/** `[Json]` Creates a Unsafe type that will infers as the generic argument T */
|
|
2260
|
-
Unsafe(options = {}) {
|
|
2261
|
-
return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
|
|
2262
|
-
}
|
|
2263
|
-
/** `[Json]` Intrinsic function to Uppercase LiteralString types */
|
|
2264
|
-
Uppercase(schema, options = {}) {
|
|
2265
|
-
return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uppercase'), ...options };
|
|
2266
|
-
}
|
|
2267
|
-
}
|
|
2268
|
-
exports.JsonTypeBuilder = JsonTypeBuilder;
|
|
2269
|
-
// --------------------------------------------------------------------------
|
|
2270
|
-
// JavaScriptTypeBuilder
|
|
2271
|
-
// --------------------------------------------------------------------------
|
|
2272
|
-
class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
2273
|
-
/** `[JavaScript]` Creates a AsyncIterator type */
|
|
2274
|
-
AsyncIterator(items, options = {}) {
|
|
2275
|
-
return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Type(items) });
|
|
2276
|
-
}
|
|
2277
|
-
/** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
|
|
2278
|
-
Awaited(schema, options = {}) {
|
|
2279
|
-
// prettier-ignore
|
|
2280
|
-
const Unwrap = (rest) => rest.length > 0 ? (() => {
|
|
2281
|
-
const [L, ...R] = rest;
|
|
2282
|
-
return [this.Awaited(L), ...Unwrap(R)];
|
|
2283
|
-
})() : rest;
|
|
2284
|
-
// prettier-ignore
|
|
2285
|
-
return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(Unwrap(schema.allOf)) :
|
|
2286
|
-
TypeGuard.TUnion(schema) ? exports.Type.Union(Unwrap(schema.anyOf)) :
|
|
2287
|
-
TypeGuard.TPromise(schema) ? this.Awaited(schema.item) :
|
|
2288
|
-
TypeClone.Type(schema, options));
|
|
2289
|
-
}
|
|
2290
|
-
/** `[JavaScript]` Creates a BigInt type */
|
|
2291
|
-
BigInt(options = {}) {
|
|
2292
|
-
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
|
|
2293
|
-
}
|
|
2294
|
-
/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
|
|
2295
|
-
ConstructorParameters(schema, options = {}) {
|
|
2296
|
-
return this.Tuple([...schema.parameters], { ...options });
|
|
2297
|
-
}
|
|
2298
|
-
/** `[JavaScript]` Creates a Constructor type */
|
|
2299
|
-
Constructor(parameters, returns, options) {
|
|
2300
|
-
const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
|
|
2301
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2302
|
-
}
|
|
2303
|
-
/** `[JavaScript]` Creates a Date type */
|
|
2304
|
-
Date(options = {}) {
|
|
2305
|
-
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'Date' });
|
|
2306
|
-
}
|
|
2307
|
-
/** `[JavaScript]` Creates a Function type */
|
|
2308
|
-
Function(parameters, returns, options) {
|
|
2309
|
-
const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
|
|
2310
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
2311
|
-
}
|
|
2312
|
-
/** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
|
|
2313
|
-
InstanceType(schema, options = {}) {
|
|
2314
|
-
return TypeClone.Type(schema.returns, options);
|
|
2315
|
-
}
|
|
2316
|
-
/** `[JavaScript]` Creates an Iterator type */
|
|
2317
|
-
Iterator(items, options = {}) {
|
|
2318
|
-
return this.Create({ ...options, [exports.Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Type(items) });
|
|
2319
|
-
}
|
|
2320
|
-
/** `[JavaScript]` Extracts the Parameters from the given Function type */
|
|
2321
|
-
Parameters(schema, options = {}) {
|
|
2322
|
-
return this.Tuple(schema.parameters, { ...options });
|
|
2323
|
-
}
|
|
2324
|
-
/** `[JavaScript]` Creates a Promise type */
|
|
2325
|
-
Promise(item, options = {}) {
|
|
2326
|
-
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Type(item) });
|
|
2327
|
-
}
|
|
2328
|
-
/** `[Extended]` Creates a String type */
|
|
2329
|
-
RegExp(unresolved, options = {}) {
|
|
2330
|
-
const pattern = ValueGuard.IsString(unresolved) ? unresolved : unresolved.source;
|
|
2331
|
-
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern });
|
|
2332
|
-
}
|
|
2333
|
-
/**
|
|
2334
|
-
* @deprecated Use `Type.RegExp`
|
|
2335
|
-
*/
|
|
2336
|
-
RegEx(regex, options = {}) {
|
|
2337
|
-
return this.RegExp(regex, options);
|
|
2338
|
-
}
|
|
2339
|
-
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
2340
|
-
ReturnType(schema, options = {}) {
|
|
2341
|
-
return TypeClone.Type(schema.returns, options);
|
|
2342
|
-
}
|
|
2343
|
-
/** `[JavaScript]` Creates a Symbol type */
|
|
2344
|
-
Symbol(options) {
|
|
2345
|
-
return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'symbol' });
|
|
2346
|
-
}
|
|
2347
|
-
/** `[JavaScript]` Creates a Undefined type */
|
|
2348
|
-
Undefined(options = {}) {
|
|
2349
|
-
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'undefined' });
|
|
2350
|
-
}
|
|
2351
|
-
/** `[JavaScript]` Creates a Uint8Array type */
|
|
2352
|
-
Uint8Array(options = {}) {
|
|
2353
|
-
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'Uint8Array' });
|
|
2354
|
-
}
|
|
2355
|
-
/** `[JavaScript]` Creates a Void type */
|
|
2356
|
-
Void(options = {}) {
|
|
2357
|
-
return this.Create({ ...options, [exports.Kind]: 'Void', type: 'void' });
|
|
2358
|
-
}
|
|
2359
|
-
}
|
|
2360
|
-
exports.JavaScriptTypeBuilder = JavaScriptTypeBuilder;
|
|
2361
|
-
/** Json Type Builder with Static Resolution for TypeScript */
|
|
2362
|
-
exports.JsonType = new JsonTypeBuilder();
|
|
2363
|
-
/** JavaScript Type Builder with Static Resolution for TypeScript */
|
|
2364
|
-
exports.Type = new JavaScriptTypeBuilder();
|