@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/compiler/compiler.d.ts
CHANGED
|
@@ -1,31 +1,38 @@
|
|
|
1
|
-
import { ValueErrorIterator } from '../errors/
|
|
2
|
-
import
|
|
1
|
+
import { ValueErrorIterator } from '../errors/index';
|
|
2
|
+
import type { TSchema } from '../type/schema/index';
|
|
3
|
+
import type { Static, StaticDecode, StaticEncode } from '../type/static/index';
|
|
3
4
|
export type CheckFunction = (value: unknown) => boolean;
|
|
4
|
-
export declare class TypeCheck<T extends
|
|
5
|
+
export declare class TypeCheck<T extends TSchema> {
|
|
5
6
|
private readonly schema;
|
|
6
7
|
private readonly references;
|
|
7
8
|
private readonly checkFunc;
|
|
8
9
|
private readonly code;
|
|
9
10
|
private readonly hasTransform;
|
|
10
|
-
constructor(schema: T, references:
|
|
11
|
+
constructor(schema: T, references: TSchema[], checkFunc: CheckFunction, code: string);
|
|
11
12
|
/** Returns the generated assertion code used to validate this type. */
|
|
12
13
|
Code(): string;
|
|
13
14
|
/** Returns an iterator for each error in this value. */
|
|
14
15
|
Errors(value: unknown): ValueErrorIterator;
|
|
15
16
|
/** Returns true if the value matches the compiled type. */
|
|
16
|
-
Check(value: unknown): value is
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
Check(value: unknown): value is Static<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Decodes a value or throws if error
|
|
20
|
+
* @deprecated Use raw `Decode` from `Value` module
|
|
21
|
+
*/
|
|
22
|
+
Decode(value: unknown): StaticDecode<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Encodes a value or throws if error
|
|
25
|
+
* @deprecated Use raw `Encode` from `Value` module
|
|
26
|
+
*/
|
|
27
|
+
Encode(value: unknown): StaticEncode<T>;
|
|
21
28
|
}
|
|
22
|
-
export declare class TypeCompilerUnknownTypeError extends
|
|
23
|
-
readonly schema:
|
|
24
|
-
constructor(schema:
|
|
29
|
+
export declare class TypeCompilerUnknownTypeError extends Error {
|
|
30
|
+
readonly schema: TSchema;
|
|
31
|
+
constructor(schema: TSchema);
|
|
25
32
|
}
|
|
26
|
-
export declare class TypeCompilerTypeGuardError extends
|
|
27
|
-
readonly schema:
|
|
28
|
-
constructor(schema:
|
|
33
|
+
export declare class TypeCompilerTypeGuardError extends Error {
|
|
34
|
+
readonly schema: TSchema;
|
|
35
|
+
constructor(schema: TSchema);
|
|
29
36
|
}
|
|
30
37
|
export declare namespace Policy {
|
|
31
38
|
function IsExactOptionalProperty(value: string, key: string, expression: string): string;
|
|
@@ -41,9 +48,9 @@ export interface TypeCompilerCodegenOptions {
|
|
|
41
48
|
/** Compiles Types for Runtime Type Checking */
|
|
42
49
|
export declare namespace TypeCompiler {
|
|
43
50
|
/** Generates the code used to assert this type and returns it as a string */
|
|
44
|
-
function Code<T extends
|
|
51
|
+
function Code<T extends TSchema>(schema: T, references: TSchema[], options?: TypeCompilerCodegenOptions): string;
|
|
45
52
|
/** Generates the code used to assert this type and returns it as a string */
|
|
46
|
-
function Code<T extends
|
|
53
|
+
function Code<T extends TSchema>(schema: T, options?: TypeCompilerCodegenOptions): string;
|
|
47
54
|
/** Compiles a TypeBox type for optimal runtime type checking. Types must be valid TypeBox types of TSchema */
|
|
48
|
-
function Compile<T extends
|
|
55
|
+
function Compile<T extends TSchema>(schema: T, references?: TSchema[]): TypeCheck<T>;
|
|
49
56
|
}
|
package/compiler/compiler.js
CHANGED
|
@@ -28,23 +28,28 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.TypeCompiler = exports.Policy = exports.TypeCompilerTypeGuardError = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0;
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
31
|
+
const index_1 = require("../value/transform/index");
|
|
32
|
+
const index_2 = require("../value/guard/index");
|
|
33
|
+
const index_3 = require("../errors/index");
|
|
34
|
+
const index_4 = require("../system/index");
|
|
35
|
+
const index_5 = require("../value/deref/index");
|
|
36
|
+
const index_6 = require("../value/hash/index");
|
|
37
|
+
const index_7 = require("../type/symbols/index");
|
|
38
|
+
const type_1 = require("../type/guard/type");
|
|
39
|
+
const index_8 = require("../type/registry/index");
|
|
40
|
+
const index_9 = require("../type/keyof/index");
|
|
41
|
+
const extends_undefined_1 = require("../type/extends/extends-undefined");
|
|
42
|
+
const index_10 = require("../type/never/index");
|
|
43
|
+
// ------------------------------------------------------------------
|
|
39
44
|
// TypeCheck
|
|
40
|
-
//
|
|
45
|
+
// ------------------------------------------------------------------
|
|
41
46
|
class TypeCheck {
|
|
42
47
|
constructor(schema, references, checkFunc, code) {
|
|
43
48
|
this.schema = schema;
|
|
44
49
|
this.references = references;
|
|
45
50
|
this.checkFunc = checkFunc;
|
|
46
51
|
this.code = code;
|
|
47
|
-
this.hasTransform =
|
|
52
|
+
this.hasTransform = (0, index_1.HasTransform)(schema, references);
|
|
48
53
|
}
|
|
49
54
|
/** Returns the generated assertion code used to validate this type. */
|
|
50
55
|
Code() {
|
|
@@ -52,30 +57,36 @@ class TypeCheck {
|
|
|
52
57
|
}
|
|
53
58
|
/** Returns an iterator for each error in this value. */
|
|
54
59
|
Errors(value) {
|
|
55
|
-
return (0,
|
|
60
|
+
return (0, index_3.Errors)(this.schema, this.references, value);
|
|
56
61
|
}
|
|
57
62
|
/** Returns true if the value matches the compiled type. */
|
|
58
63
|
Check(value) {
|
|
59
64
|
return this.checkFunc(value);
|
|
60
65
|
}
|
|
61
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* Decodes a value or throws if error
|
|
68
|
+
* @deprecated Use raw `Decode` from `Value` module
|
|
69
|
+
*/
|
|
62
70
|
Decode(value) {
|
|
63
71
|
if (!this.checkFunc(value))
|
|
64
|
-
throw new
|
|
65
|
-
return this.hasTransform ?
|
|
72
|
+
throw new index_1.TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
73
|
+
return this.hasTransform ? (0, index_1.Decode)(this.schema, this.references, value) : value;
|
|
66
74
|
}
|
|
67
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Encodes a value or throws if error
|
|
77
|
+
* @deprecated Use raw `Encode` from `Value` module
|
|
78
|
+
*/
|
|
68
79
|
Encode(value) {
|
|
69
|
-
const encoded = this.hasTransform ?
|
|
80
|
+
const encoded = this.hasTransform ? (0, index_1.Encode)(this.schema, this.references, value) : value;
|
|
70
81
|
if (!this.checkFunc(encoded))
|
|
71
|
-
throw new
|
|
82
|
+
throw new index_1.TransformEncodeCheckError(this.schema, value, this.Errors(value).First());
|
|
72
83
|
return encoded;
|
|
73
84
|
}
|
|
74
85
|
}
|
|
75
86
|
exports.TypeCheck = TypeCheck;
|
|
76
|
-
//
|
|
87
|
+
// ------------------------------------------------------------------
|
|
77
88
|
// Character
|
|
78
|
-
//
|
|
89
|
+
// ------------------------------------------------------------------
|
|
79
90
|
var Character;
|
|
80
91
|
(function (Character) {
|
|
81
92
|
function DollarSign(code) {
|
|
@@ -95,9 +106,9 @@ var Character;
|
|
|
95
106
|
}
|
|
96
107
|
Character.IsNumeric = IsNumeric;
|
|
97
108
|
})(Character || (Character = {}));
|
|
98
|
-
//
|
|
109
|
+
// ------------------------------------------------------------------
|
|
99
110
|
// MemberExpression
|
|
100
|
-
//
|
|
111
|
+
// ------------------------------------------------------------------
|
|
101
112
|
var MemberExpression;
|
|
102
113
|
(function (MemberExpression) {
|
|
103
114
|
function IsFirstCharacterNumeric(value) {
|
|
@@ -124,9 +135,9 @@ var MemberExpression;
|
|
|
124
135
|
}
|
|
125
136
|
MemberExpression.Encode = Encode;
|
|
126
137
|
})(MemberExpression || (MemberExpression = {}));
|
|
127
|
-
//
|
|
138
|
+
// ------------------------------------------------------------------
|
|
128
139
|
// Identifier
|
|
129
|
-
//
|
|
140
|
+
// ------------------------------------------------------------------
|
|
130
141
|
var Identifier;
|
|
131
142
|
(function (Identifier) {
|
|
132
143
|
function Encode($id) {
|
|
@@ -144,9 +155,9 @@ var Identifier;
|
|
|
144
155
|
}
|
|
145
156
|
Identifier.Encode = Encode;
|
|
146
157
|
})(Identifier || (Identifier = {}));
|
|
147
|
-
//
|
|
158
|
+
// ------------------------------------------------------------------
|
|
148
159
|
// LiteralString
|
|
149
|
-
//
|
|
160
|
+
// ------------------------------------------------------------------
|
|
150
161
|
var LiteralString;
|
|
151
162
|
(function (LiteralString) {
|
|
152
163
|
function Escape(content) {
|
|
@@ -154,80 +165,80 @@ var LiteralString;
|
|
|
154
165
|
}
|
|
155
166
|
LiteralString.Escape = Escape;
|
|
156
167
|
})(LiteralString || (LiteralString = {}));
|
|
157
|
-
//
|
|
168
|
+
// ------------------------------------------------------------------
|
|
158
169
|
// Errors
|
|
159
|
-
//
|
|
160
|
-
class TypeCompilerUnknownTypeError extends
|
|
170
|
+
// ------------------------------------------------------------------
|
|
171
|
+
class TypeCompilerUnknownTypeError extends Error {
|
|
161
172
|
constructor(schema) {
|
|
162
173
|
super('Unknown type');
|
|
163
174
|
this.schema = schema;
|
|
164
175
|
}
|
|
165
176
|
}
|
|
166
177
|
exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
167
|
-
class TypeCompilerTypeGuardError extends
|
|
178
|
+
class TypeCompilerTypeGuardError extends Error {
|
|
168
179
|
constructor(schema) {
|
|
169
180
|
super('Preflight validation check failed to guard for the given schema');
|
|
170
181
|
this.schema = schema;
|
|
171
182
|
}
|
|
172
183
|
}
|
|
173
184
|
exports.TypeCompilerTypeGuardError = TypeCompilerTypeGuardError;
|
|
174
|
-
//
|
|
185
|
+
// ------------------------------------------------------------------
|
|
175
186
|
// Policy
|
|
176
|
-
//
|
|
187
|
+
// ------------------------------------------------------------------
|
|
177
188
|
var Policy;
|
|
178
189
|
(function (Policy) {
|
|
179
190
|
function IsExactOptionalProperty(value, key, expression) {
|
|
180
|
-
return
|
|
191
|
+
return index_4.TypeSystemPolicy.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`;
|
|
181
192
|
}
|
|
182
193
|
Policy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
183
194
|
function IsObjectLike(value) {
|
|
184
|
-
return !
|
|
195
|
+
return !index_4.TypeSystemPolicy.AllowArrayObject ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
185
196
|
}
|
|
186
197
|
Policy.IsObjectLike = IsObjectLike;
|
|
187
198
|
function IsRecordLike(value) {
|
|
188
|
-
return !
|
|
199
|
+
return !index_4.TypeSystemPolicy.AllowArrayObject
|
|
189
200
|
? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
|
|
190
201
|
: `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`;
|
|
191
202
|
}
|
|
192
203
|
Policy.IsRecordLike = IsRecordLike;
|
|
193
204
|
function IsNumberLike(value) {
|
|
194
|
-
return !
|
|
205
|
+
return !index_4.TypeSystemPolicy.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
|
|
195
206
|
}
|
|
196
207
|
Policy.IsNumberLike = IsNumberLike;
|
|
197
208
|
function IsVoidLike(value) {
|
|
198
|
-
return
|
|
209
|
+
return index_4.TypeSystemPolicy.AllowNullVoid ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`;
|
|
199
210
|
}
|
|
200
211
|
Policy.IsVoidLike = IsVoidLike;
|
|
201
212
|
})(Policy || (exports.Policy = Policy = {}));
|
|
202
213
|
/** Compiles Types for Runtime Type Checking */
|
|
203
214
|
var TypeCompiler;
|
|
204
215
|
(function (TypeCompiler) {
|
|
205
|
-
//
|
|
216
|
+
// ----------------------------------------------------------------
|
|
206
217
|
// Guards
|
|
207
|
-
//
|
|
218
|
+
// ----------------------------------------------------------------
|
|
208
219
|
function IsAnyOrUnknown(schema) {
|
|
209
|
-
return schema[
|
|
220
|
+
return schema[index_7.Kind] === 'Any' || schema[index_7.Kind] === 'Unknown';
|
|
210
221
|
}
|
|
211
|
-
//
|
|
222
|
+
// ----------------------------------------------------------------
|
|
212
223
|
// Types
|
|
213
|
-
//
|
|
224
|
+
// ----------------------------------------------------------------
|
|
214
225
|
function* TAny(schema, references, value) {
|
|
215
226
|
yield 'true';
|
|
216
227
|
}
|
|
217
228
|
function* TArray(schema, references, value) {
|
|
218
229
|
yield `Array.isArray(${value})`;
|
|
219
230
|
const [parameter, accumulator] = [CreateParameter('value', 'any'), CreateParameter('acc', 'number')];
|
|
220
|
-
if ((0,
|
|
231
|
+
if ((0, index_2.IsNumber)(schema.maxItems))
|
|
221
232
|
yield `${value}.length <= ${schema.maxItems}`;
|
|
222
|
-
if ((0,
|
|
233
|
+
if ((0, index_2.IsNumber)(schema.minItems))
|
|
223
234
|
yield `${value}.length >= ${schema.minItems}`;
|
|
224
235
|
const elementExpression = CreateExpression(schema.items, references, 'value');
|
|
225
236
|
yield `${value}.every((${parameter}) => ${elementExpression})`;
|
|
226
|
-
if (
|
|
227
|
-
const containsSchema =
|
|
237
|
+
if ((0, type_1.TSchema)(schema.contains) || (0, index_2.IsNumber)(schema.minContains) || (0, index_2.IsNumber)(schema.maxContains)) {
|
|
238
|
+
const containsSchema = (0, type_1.TSchema)(schema.contains) ? schema.contains : (0, index_10.Never)();
|
|
228
239
|
const checkExpression = CreateExpression(containsSchema, references, 'value');
|
|
229
|
-
const checkMinContains = (0,
|
|
230
|
-
const checkMaxContains = (0,
|
|
240
|
+
const checkMinContains = (0, index_2.IsNumber)(schema.minContains) ? [`(count >= ${schema.minContains})`] : [];
|
|
241
|
+
const checkMaxContains = (0, index_2.IsNumber)(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : [];
|
|
231
242
|
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`;
|
|
232
243
|
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ');
|
|
233
244
|
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`;
|
|
@@ -243,15 +254,15 @@ var TypeCompiler;
|
|
|
243
254
|
}
|
|
244
255
|
function* TBigInt(schema, references, value) {
|
|
245
256
|
yield `(typeof ${value} === 'bigint')`;
|
|
246
|
-
if ((0,
|
|
257
|
+
if ((0, index_2.IsBigInt)(schema.exclusiveMaximum))
|
|
247
258
|
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
248
|
-
if ((0,
|
|
259
|
+
if ((0, index_2.IsBigInt)(schema.exclusiveMinimum))
|
|
249
260
|
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
250
|
-
if ((0,
|
|
261
|
+
if ((0, index_2.IsBigInt)(schema.maximum))
|
|
251
262
|
yield `${value} <= BigInt(${schema.maximum})`;
|
|
252
|
-
if ((0,
|
|
263
|
+
if ((0, index_2.IsBigInt)(schema.minimum))
|
|
253
264
|
yield `${value} >= BigInt(${schema.minimum})`;
|
|
254
|
-
if ((0,
|
|
265
|
+
if ((0, index_2.IsBigInt)(schema.multipleOf))
|
|
255
266
|
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
256
267
|
}
|
|
257
268
|
function* TBoolean(schema, references, value) {
|
|
@@ -262,15 +273,15 @@ var TypeCompiler;
|
|
|
262
273
|
}
|
|
263
274
|
function* TDate(schema, references, value) {
|
|
264
275
|
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
265
|
-
if ((0,
|
|
276
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMaximumTimestamp))
|
|
266
277
|
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
267
|
-
if ((0,
|
|
278
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMinimumTimestamp))
|
|
268
279
|
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
269
|
-
if ((0,
|
|
280
|
+
if ((0, index_2.IsNumber)(schema.maximumTimestamp))
|
|
270
281
|
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
271
|
-
if ((0,
|
|
282
|
+
if ((0, index_2.IsNumber)(schema.minimumTimestamp))
|
|
272
283
|
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
273
|
-
if ((0,
|
|
284
|
+
if ((0, index_2.IsNumber)(schema.multipleOfTimestamp))
|
|
274
285
|
yield `(${value}.getTime() % ${schema.multipleOfTimestamp}) === 0`;
|
|
275
286
|
}
|
|
276
287
|
function* TFunction(schema, references, value) {
|
|
@@ -278,26 +289,26 @@ var TypeCompiler;
|
|
|
278
289
|
}
|
|
279
290
|
function* TInteger(schema, references, value) {
|
|
280
291
|
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
281
|
-
if ((0,
|
|
292
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMaximum))
|
|
282
293
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
283
|
-
if ((0,
|
|
294
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMinimum))
|
|
284
295
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
285
|
-
if ((0,
|
|
296
|
+
if ((0, index_2.IsNumber)(schema.maximum))
|
|
286
297
|
yield `${value} <= ${schema.maximum}`;
|
|
287
|
-
if ((0,
|
|
298
|
+
if ((0, index_2.IsNumber)(schema.minimum))
|
|
288
299
|
yield `${value} >= ${schema.minimum}`;
|
|
289
|
-
if ((0,
|
|
300
|
+
if ((0, index_2.IsNumber)(schema.multipleOf))
|
|
290
301
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
291
302
|
}
|
|
292
303
|
function* TIntersect(schema, references, value) {
|
|
293
304
|
const check1 = schema.allOf.map((schema) => CreateExpression(schema, references, value)).join(' && ');
|
|
294
305
|
if (schema.unevaluatedProperties === false) {
|
|
295
|
-
const keyCheck = CreateVariable(`${new RegExp(
|
|
306
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_9.KeyOfStringResolvePattern)(schema))};`);
|
|
296
307
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`;
|
|
297
308
|
yield `(${check1} && ${check2})`;
|
|
298
309
|
}
|
|
299
|
-
else if (
|
|
300
|
-
const keyCheck = CreateVariable(`${new RegExp(
|
|
310
|
+
else if ((0, type_1.TSchema)(schema.unevaluatedProperties)) {
|
|
311
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_9.KeyOfStringResolvePattern)(schema))};`);
|
|
301
312
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
302
313
|
yield `(${check1} && ${check2})`;
|
|
303
314
|
}
|
|
@@ -328,22 +339,22 @@ var TypeCompiler;
|
|
|
328
339
|
}
|
|
329
340
|
function* TNumber(schema, references, value) {
|
|
330
341
|
yield Policy.IsNumberLike(value);
|
|
331
|
-
if ((0,
|
|
342
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMaximum))
|
|
332
343
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
333
|
-
if ((0,
|
|
344
|
+
if ((0, index_2.IsNumber)(schema.exclusiveMinimum))
|
|
334
345
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
335
|
-
if ((0,
|
|
346
|
+
if ((0, index_2.IsNumber)(schema.maximum))
|
|
336
347
|
yield `${value} <= ${schema.maximum}`;
|
|
337
|
-
if ((0,
|
|
348
|
+
if ((0, index_2.IsNumber)(schema.minimum))
|
|
338
349
|
yield `${value} >= ${schema.minimum}`;
|
|
339
|
-
if ((0,
|
|
350
|
+
if ((0, index_2.IsNumber)(schema.multipleOf))
|
|
340
351
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
341
352
|
}
|
|
342
353
|
function* TObject(schema, references, value) {
|
|
343
354
|
yield Policy.IsObjectLike(value);
|
|
344
|
-
if ((0,
|
|
355
|
+
if ((0, index_2.IsNumber)(schema.minProperties))
|
|
345
356
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
346
|
-
if ((0,
|
|
357
|
+
if ((0, index_2.IsNumber)(schema.maxProperties))
|
|
347
358
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
348
359
|
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
349
360
|
for (const knownKey of knownKeys) {
|
|
@@ -351,7 +362,7 @@ var TypeCompiler;
|
|
|
351
362
|
const property = schema.properties[knownKey];
|
|
352
363
|
if (schema.required && schema.required.includes(knownKey)) {
|
|
353
364
|
yield* Visit(property, references, memberExpression);
|
|
354
|
-
if (
|
|
365
|
+
if ((0, extends_undefined_1.ExtendsUndefinedCheck)(property) || IsAnyOrUnknown(property))
|
|
355
366
|
yield `('${knownKey}' in ${value})`;
|
|
356
367
|
}
|
|
357
368
|
else {
|
|
@@ -379,19 +390,19 @@ var TypeCompiler;
|
|
|
379
390
|
}
|
|
380
391
|
function* TRecord(schema, references, value) {
|
|
381
392
|
yield Policy.IsRecordLike(value);
|
|
382
|
-
if ((0,
|
|
393
|
+
if ((0, index_2.IsNumber)(schema.minProperties))
|
|
383
394
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
384
|
-
if ((0,
|
|
395
|
+
if ((0, index_2.IsNumber)(schema.maxProperties))
|
|
385
396
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
386
397
|
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
387
398
|
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
388
399
|
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
389
|
-
const check2 =
|
|
400
|
+
const check2 = (0, type_1.TSchema)(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
390
401
|
const expression = `(${variable}.test(key) ? ${check1} : ${check2})`;
|
|
391
402
|
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
|
392
403
|
}
|
|
393
404
|
function* TRef(schema, references, value) {
|
|
394
|
-
const target = (0,
|
|
405
|
+
const target = (0, index_5.Deref)(schema, references);
|
|
395
406
|
// Reference: If we have seen this reference before we can just yield and return the function call.
|
|
396
407
|
// If this isn't the case we defer to visit to generate and set the function for subsequent passes.
|
|
397
408
|
if (state.functions.has(schema.$ref))
|
|
@@ -400,9 +411,9 @@ var TypeCompiler;
|
|
|
400
411
|
}
|
|
401
412
|
function* TString(schema, references, value) {
|
|
402
413
|
yield `(typeof ${value} === 'string')`;
|
|
403
|
-
if ((0,
|
|
414
|
+
if ((0, index_2.IsNumber)(schema.maxLength))
|
|
404
415
|
yield `${value}.length <= ${schema.maxLength}`;
|
|
405
|
-
if ((0,
|
|
416
|
+
if ((0, index_2.IsNumber)(schema.minLength))
|
|
406
417
|
yield `${value}.length >= ${schema.minLength}`;
|
|
407
418
|
if (schema.pattern !== undefined) {
|
|
408
419
|
const variable = CreateVariable(`${new RegExp(schema.pattern)};`);
|
|
@@ -443,9 +454,9 @@ var TypeCompiler;
|
|
|
443
454
|
}
|
|
444
455
|
function* TUint8Array(schema, references, value) {
|
|
445
456
|
yield `${value} instanceof Uint8Array`;
|
|
446
|
-
if ((0,
|
|
457
|
+
if ((0, index_2.IsNumber)(schema.maxByteLength))
|
|
447
458
|
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
448
|
-
if ((0,
|
|
459
|
+
if ((0, index_2.IsNumber)(schema.minByteLength))
|
|
449
460
|
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
450
461
|
}
|
|
451
462
|
function* TUnknown(schema, references, value) {
|
|
@@ -457,15 +468,15 @@ var TypeCompiler;
|
|
|
457
468
|
function* TKind(schema, references, value) {
|
|
458
469
|
const instance = state.instances.size;
|
|
459
470
|
state.instances.set(instance, schema);
|
|
460
|
-
yield `kind('${schema[
|
|
471
|
+
yield `kind('${schema[index_7.Kind]}', ${instance}, ${value})`;
|
|
461
472
|
}
|
|
462
473
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
463
|
-
const references_ = (0,
|
|
474
|
+
const references_ = (0, index_2.IsString)(schema.$id) ? [...references, schema] : references;
|
|
464
475
|
const schema_ = schema;
|
|
465
|
-
//
|
|
476
|
+
// --------------------------------------------------------------
|
|
466
477
|
// Hoisting
|
|
467
|
-
//
|
|
468
|
-
if (useHoisting && (0,
|
|
478
|
+
// --------------------------------------------------------------
|
|
479
|
+
if (useHoisting && (0, index_2.IsString)(schema.$id)) {
|
|
469
480
|
const functionName = CreateFunctionName(schema.$id);
|
|
470
481
|
if (state.functions.has(functionName)) {
|
|
471
482
|
return yield `${functionName}(${value})`;
|
|
@@ -476,7 +487,7 @@ var TypeCompiler;
|
|
|
476
487
|
return yield `${functionName}(${value})`;
|
|
477
488
|
}
|
|
478
489
|
}
|
|
479
|
-
switch (schema_[
|
|
490
|
+
switch (schema_[index_7.Kind]) {
|
|
480
491
|
case 'Any':
|
|
481
492
|
return yield* TAny(schema_, references_, value);
|
|
482
493
|
case 'Array':
|
|
@@ -538,24 +549,24 @@ var TypeCompiler;
|
|
|
538
549
|
case 'Void':
|
|
539
550
|
return yield* TVoid(schema_, references_, value);
|
|
540
551
|
default:
|
|
541
|
-
if (!
|
|
552
|
+
if (!index_8.TypeRegistry.Has(schema_[index_7.Kind]))
|
|
542
553
|
throw new TypeCompilerUnknownTypeError(schema);
|
|
543
554
|
return yield* TKind(schema_, references_, value);
|
|
544
555
|
}
|
|
545
556
|
}
|
|
546
|
-
//
|
|
557
|
+
// ----------------------------------------------------------------
|
|
547
558
|
// Compiler State
|
|
548
|
-
//
|
|
559
|
+
// ----------------------------------------------------------------
|
|
549
560
|
// prettier-ignore
|
|
550
561
|
const state = {
|
|
551
|
-
language: 'javascript',
|
|
552
|
-
functions: new Map(),
|
|
553
|
-
variables: new Map(),
|
|
562
|
+
language: 'javascript', // target language
|
|
563
|
+
functions: new Map(), // local functions
|
|
564
|
+
variables: new Map(), // local variables
|
|
554
565
|
instances: new Map() // exterior kind instances
|
|
555
566
|
};
|
|
556
|
-
//
|
|
567
|
+
// ----------------------------------------------------------------
|
|
557
568
|
// Compiler Factory
|
|
558
|
-
//
|
|
569
|
+
// ----------------------------------------------------------------
|
|
559
570
|
function CreateExpression(schema, references, value, useHoisting = true) {
|
|
560
571
|
return `(${[...Visit(schema, references, value, useHoisting)].join(' && ')})`;
|
|
561
572
|
}
|
|
@@ -581,9 +592,9 @@ var TypeCompiler;
|
|
|
581
592
|
function CreateReturns(type) {
|
|
582
593
|
return state.language === 'typescript' ? `: ${type}` : '';
|
|
583
594
|
}
|
|
584
|
-
//
|
|
595
|
+
// ----------------------------------------------------------------
|
|
585
596
|
// Compile
|
|
586
|
-
//
|
|
597
|
+
// ----------------------------------------------------------------
|
|
587
598
|
function Build(schema, references, options) {
|
|
588
599
|
const functionCode = CreateFunction('check', schema, references, 'value'); // will populate functions and variables
|
|
589
600
|
const parameter = CreateParameter('value', 'any');
|
|
@@ -591,7 +602,7 @@ var TypeCompiler;
|
|
|
591
602
|
const functions = [...state.functions.values()];
|
|
592
603
|
const variables = [...state.variables.values()];
|
|
593
604
|
// prettier-ignore
|
|
594
|
-
const checkFunction = (0,
|
|
605
|
+
const checkFunction = (0, index_2.IsString)(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
595
606
|
? `return function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
596
607
|
: `return ${functionCode}`;
|
|
597
608
|
return [...variables, ...functions, checkFunction].join('\n');
|
|
@@ -600,8 +611,8 @@ var TypeCompiler;
|
|
|
600
611
|
function Code(...args) {
|
|
601
612
|
const defaults = { language: 'javascript' };
|
|
602
613
|
// prettier-ignore
|
|
603
|
-
const [schema, references, options] = (args.length === 2 && (0,
|
|
604
|
-
args.length === 2 && !(0,
|
|
614
|
+
const [schema, references, options] = (args.length === 2 && (0, index_2.IsArray)(args[1]) ? [args[0], args[1], defaults] :
|
|
615
|
+
args.length === 2 && !(0, index_2.IsArray)(args[1]) ? [args[0], [], args[1]] :
|
|
605
616
|
args.length === 3 ? [args[0], args[1], args[2]] :
|
|
606
617
|
args.length === 1 ? [args[0], [], defaults] :
|
|
607
618
|
[null, [], defaults]);
|
|
@@ -610,10 +621,10 @@ var TypeCompiler;
|
|
|
610
621
|
state.variables.clear();
|
|
611
622
|
state.functions.clear();
|
|
612
623
|
state.instances.clear();
|
|
613
|
-
if (!
|
|
624
|
+
if (!(0, type_1.TSchema)(schema))
|
|
614
625
|
throw new TypeCompilerTypeGuardError(schema);
|
|
615
626
|
for (const schema of references)
|
|
616
|
-
if (!
|
|
627
|
+
if (!(0, type_1.TSchema)(schema))
|
|
617
628
|
throw new TypeCompilerTypeGuardError(schema);
|
|
618
629
|
return Build(schema, references, options);
|
|
619
630
|
}
|
|
@@ -624,20 +635,20 @@ var TypeCompiler;
|
|
|
624
635
|
const compiledFunction = globalThis.Function('kind', 'format', 'hash', generatedCode);
|
|
625
636
|
const instances = new Map(state.instances);
|
|
626
637
|
function typeRegistryFunction(kind, instance, value) {
|
|
627
|
-
if (!
|
|
638
|
+
if (!index_8.TypeRegistry.Has(kind) || !instances.has(instance))
|
|
628
639
|
return false;
|
|
629
|
-
const checkFunc =
|
|
640
|
+
const checkFunc = index_8.TypeRegistry.Get(kind);
|
|
630
641
|
const schema = instances.get(instance);
|
|
631
642
|
return checkFunc(schema, value);
|
|
632
643
|
}
|
|
633
644
|
function formatRegistryFunction(format, value) {
|
|
634
|
-
if (!
|
|
645
|
+
if (!index_8.FormatRegistry.Has(format))
|
|
635
646
|
return false;
|
|
636
|
-
const checkFunc =
|
|
647
|
+
const checkFunc = index_8.FormatRegistry.Get(format);
|
|
637
648
|
return checkFunc(value);
|
|
638
649
|
}
|
|
639
650
|
function hashFunction(value) {
|
|
640
|
-
return (0,
|
|
651
|
+
return (0, index_6.Hash)(value);
|
|
641
652
|
}
|
|
642
653
|
const checkFunction = compiledFunction(typeRegistryFunction, formatRegistryFunction, hashFunction);
|
|
643
654
|
return new TypeCheck(schema, references, checkFunction, generatedCode);
|
package/errors/errors.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { TSchema } from '../type/schema/index';
|
|
2
2
|
export declare enum ValueErrorType {
|
|
3
3
|
ArrayContains = 0,
|
|
4
4
|
ArrayMaxContains = 1,
|
|
@@ -66,14 +66,14 @@ export declare enum ValueErrorType {
|
|
|
66
66
|
}
|
|
67
67
|
export interface ValueError {
|
|
68
68
|
type: ValueErrorType;
|
|
69
|
-
schema:
|
|
69
|
+
schema: TSchema;
|
|
70
70
|
path: string;
|
|
71
71
|
value: unknown;
|
|
72
72
|
message: string;
|
|
73
73
|
}
|
|
74
|
-
export declare class ValueErrorsUnknownTypeError extends
|
|
75
|
-
readonly schema:
|
|
76
|
-
constructor(schema:
|
|
74
|
+
export declare class ValueErrorsUnknownTypeError extends Error {
|
|
75
|
+
readonly schema: TSchema;
|
|
76
|
+
constructor(schema: TSchema);
|
|
77
77
|
}
|
|
78
78
|
export declare function EscapeKey(key: string): string;
|
|
79
79
|
export declare class ValueErrorIterator {
|
|
@@ -84,6 +84,6 @@ export declare class ValueErrorIterator {
|
|
|
84
84
|
First(): ValueError | undefined;
|
|
85
85
|
}
|
|
86
86
|
/** Returns an iterator for each error in this value. */
|
|
87
|
-
export declare function Errors<T extends
|
|
87
|
+
export declare function Errors<T extends TSchema>(schema: T, references: TSchema[], value: unknown): ValueErrorIterator;
|
|
88
88
|
/** Returns an iterator for each error in this value. */
|
|
89
|
-
export declare function Errors<T extends
|
|
89
|
+
export declare function Errors<T extends TSchema>(schema: T, value: unknown): ValueErrorIterator;
|