@sinclair/typebox 0.32.0-dev-18 → 0.32.0-dev-19
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/build/import/compiler/compiler.mjs +14 -8
- package/build/import/errors/errors.mjs +15 -11
- package/build/import/type/awaited/awaited.mjs +4 -4
- package/build/import/type/deref/deref.mjs +12 -12
- package/build/import/type/exclude/exclude.mjs +8 -5
- package/build/import/type/extends/extends-check.mjs +167 -167
- package/build/import/type/extends/extends.mjs +3 -3
- package/build/import/type/extract/extract.mjs +5 -5
- package/build/import/type/guard/type.d.mts +50 -50
- package/build/import/type/guard/type.mjs +136 -136
- package/build/import/type/indexed/indexed-property-keys.mjs +7 -7
- package/build/import/type/indexed/indexed.mjs +9 -9
- package/build/import/type/intersect/intersect-create.mjs +4 -4
- package/build/import/type/intersect/intersect-evaluated.mjs +1 -1
- package/build/import/type/intersect/intersect.mjs +2 -2
- package/build/import/type/intrinsic/intrinsic.mjs +5 -5
- package/build/import/type/keyof/keyof-property-keys.mjs +7 -7
- package/build/import/type/keyof/keyof.mjs +2 -2
- package/build/import/type/mapped/mapped.mjs +16 -16
- package/build/import/type/modifiers/modifiers.mjs +3 -3
- package/build/import/type/object/object.mjs +3 -3
- package/build/import/type/omit/omit.mjs +6 -6
- package/build/import/type/partial/partial.mjs +5 -5
- package/build/import/type/pick/pick.mjs +6 -6
- package/build/import/type/record/record.mjs +7 -7
- package/build/import/type/required/required.mjs +5 -5
- package/build/import/type/rest/rest.mjs +6 -2
- package/build/import/type/template-literal/pattern.mjs +9 -9
- package/build/import/type/transform/transform.mjs +1 -1
- package/build/import/type/union/union-evaluated.mjs +2 -2
- package/build/import/value/check/check.mjs +18 -11
- package/build/import/value/clean/clean.mjs +9 -7
- package/build/import/value/clone/clone.mjs +7 -4
- package/build/import/value/convert/convert.mjs +6 -2
- package/build/import/value/default/default.mjs +12 -4
- package/build/import/value/transform/decode.mjs +12 -6
- package/build/import/value/transform/encode.mjs +12 -6
- package/build/import/value/transform/has.mjs +24 -18
- package/build/require/compiler/compiler.js +77 -71
- package/build/require/errors/errors.js +58 -54
- package/build/require/type/awaited/awaited.js +3 -3
- package/build/require/type/deref/deref.js +11 -11
- package/build/require/type/exclude/exclude.js +7 -4
- package/build/require/type/extends/extends-check.js +167 -167
- package/build/require/type/extends/extends.js +2 -2
- package/build/require/type/extract/extract.js +4 -4
- package/build/require/type/guard/type.d.ts +50 -50
- package/build/require/type/guard/type.js +184 -184
- package/build/require/type/indexed/indexed-property-keys.js +6 -6
- package/build/require/type/indexed/indexed.js +9 -9
- package/build/require/type/intersect/intersect-create.js +3 -3
- package/build/require/type/intersect/intersect-evaluated.js +2 -2
- package/build/require/type/intersect/intersect.js +1 -1
- package/build/require/type/intrinsic/intrinsic.js +4 -4
- package/build/require/type/keyof/keyof-property-keys.js +6 -6
- package/build/require/type/keyof/keyof.js +1 -1
- package/build/require/type/mapped/mapped.js +15 -15
- package/build/require/type/modifiers/modifiers.js +2 -2
- package/build/require/type/object/object.js +2 -2
- package/build/require/type/omit/omit.js +5 -5
- package/build/require/type/partial/partial.js +4 -4
- package/build/require/type/pick/pick.js +5 -5
- package/build/require/type/record/record.js +6 -6
- package/build/require/type/required/required.js +4 -4
- package/build/require/type/rest/rest.js +5 -1
- package/build/require/type/template-literal/pattern.js +8 -8
- package/build/require/type/transform/transform.js +1 -1
- package/build/require/type/union/union-evaluated.js +1 -1
- package/build/require/value/check/check.js +63 -56
- package/build/require/value/clean/clean.js +31 -29
- package/build/require/value/clone/clone.js +7 -4
- package/build/require/value/convert/convert.js +42 -38
- package/build/require/value/default/default.js +24 -16
- package/build/require/value/transform/decode.js +27 -21
- package/build/require/value/transform/encode.js +28 -22
- package/build/require/value/transform/has.js +29 -23
- package/package.json +1 -1
- package/readme.md +5 -5
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
import { TTransform as IsTransformType, TSchema as IsSchemaType } from '../../type/guard/type.mjs';
|
|
2
1
|
import { Kind, TransformKind } from '../../type/symbols/index.mjs';
|
|
3
|
-
import { IsPlainObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
4
2
|
import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
|
|
5
3
|
import { Index } from '../../type/indexed/index.mjs';
|
|
6
4
|
import { Deref } from '../deref/index.mjs';
|
|
7
5
|
import { Check } from '../check/index.mjs';
|
|
8
6
|
// ------------------------------------------------------------------
|
|
7
|
+
// ValueGuard
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
import { IsPlainObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
// TypeGuard
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
14
|
+
// ------------------------------------------------------------------
|
|
9
15
|
// Errors
|
|
10
16
|
// ------------------------------------------------------------------
|
|
11
17
|
// thrown externally
|
|
@@ -35,7 +41,7 @@ export class TransformDecodeError extends Error {
|
|
|
35
41
|
// prettier-ignore
|
|
36
42
|
function Default(schema, value) {
|
|
37
43
|
try {
|
|
38
|
-
return
|
|
44
|
+
return IsTransform(schema) ? schema[TransformKind].Decode(value) : value;
|
|
39
45
|
}
|
|
40
46
|
catch (error) {
|
|
41
47
|
throw new TransformDecodeError(schema, value, error);
|
|
@@ -57,7 +63,7 @@ function TIntersect(schema, references, value) {
|
|
|
57
63
|
? { ...value, [key]: Visit(Index(schema, [key]), references, value[key]) }
|
|
58
64
|
: value;
|
|
59
65
|
}, value);
|
|
60
|
-
if (!
|
|
66
|
+
if (!IsTransform(schema.unevaluatedProperties)) {
|
|
61
67
|
return Default(schema, knownProperties);
|
|
62
68
|
}
|
|
63
69
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -82,7 +88,7 @@ function TObject(schema, references, value) {
|
|
|
82
88
|
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
|
|
83
89
|
: value;
|
|
84
90
|
}, value);
|
|
85
|
-
if (!
|
|
91
|
+
if (!IsSchema(schema.additionalProperties)) {
|
|
86
92
|
return Default(schema, knownProperties);
|
|
87
93
|
}
|
|
88
94
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -105,7 +111,7 @@ function TRecord(schema, references, value) {
|
|
|
105
111
|
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, value[key]) }
|
|
106
112
|
: value;
|
|
107
113
|
}, value);
|
|
108
|
-
if (!
|
|
114
|
+
if (!IsSchema(schema.additionalProperties)) {
|
|
109
115
|
return Default(schema, knownProperties);
|
|
110
116
|
}
|
|
111
117
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
import { TTransform as IsTransformType, TSchema as IsSchemaType } from '../../type/guard/type.mjs';
|
|
2
1
|
import { Kind, TransformKind } from '../../type/symbols/index.mjs';
|
|
3
|
-
import { IsPlainObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
4
2
|
import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
|
|
5
3
|
import { Index } from '../../type/indexed/index.mjs';
|
|
6
4
|
import { Deref } from '../deref/index.mjs';
|
|
7
5
|
import { Check } from '../check/index.mjs';
|
|
8
6
|
// ------------------------------------------------------------------
|
|
7
|
+
// ValueGuard
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
import { IsPlainObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
// TypeGuard
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
14
|
+
// ------------------------------------------------------------------
|
|
9
15
|
// Errors
|
|
10
16
|
// ------------------------------------------------------------------
|
|
11
17
|
export class TransformEncodeCheckError extends Error {
|
|
@@ -34,7 +40,7 @@ export class TransformEncodeError extends Error {
|
|
|
34
40
|
// prettier-ignore
|
|
35
41
|
function Default(schema, value) {
|
|
36
42
|
try {
|
|
37
|
-
return
|
|
43
|
+
return IsTransform(schema) ? schema[TransformKind].Encode(value) : value;
|
|
38
44
|
}
|
|
39
45
|
catch (error) {
|
|
40
46
|
throw new TransformEncodeError(schema, value, error);
|
|
@@ -58,7 +64,7 @@ function TIntersect(schema, references, value) {
|
|
|
58
64
|
? { ...value, [key]: Visit(Index(schema, [key]), references, value[key]) }
|
|
59
65
|
: value;
|
|
60
66
|
}, defaulted);
|
|
61
|
-
if (!
|
|
67
|
+
if (!IsTransform(schema.unevaluatedProperties)) {
|
|
62
68
|
return Default(schema, knownProperties);
|
|
63
69
|
}
|
|
64
70
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -84,7 +90,7 @@ function TObject(schema, references, value) {
|
|
|
84
90
|
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
|
|
85
91
|
: value;
|
|
86
92
|
}, defaulted);
|
|
87
|
-
if (!
|
|
93
|
+
if (!IsSchema(schema.additionalProperties)) {
|
|
88
94
|
return knownProperties;
|
|
89
95
|
}
|
|
90
96
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -107,7 +113,7 @@ function TRecord(schema, references, value) {
|
|
|
107
113
|
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, value[key]) }
|
|
108
114
|
: value;
|
|
109
115
|
}, defaulted);
|
|
110
|
-
if (!
|
|
116
|
+
if (!IsSchema(schema.additionalProperties)) {
|
|
111
117
|
return Default(schema, knownProperties);
|
|
112
118
|
}
|
|
113
119
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
@@ -1,70 +1,76 @@
|
|
|
1
|
-
import { TTransform as IsTransformType, TSchema as IsSchemaType } from '../../type/guard/type.mjs';
|
|
2
|
-
import { IsString, IsUndefined } from '../guard/index.mjs';
|
|
3
1
|
import { Deref } from '../deref/index.mjs';
|
|
4
2
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
3
|
+
// ------------------------------------------------------------------
|
|
4
|
+
// TypeGuard
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// ValueGuard
|
|
9
|
+
// ------------------------------------------------------------------
|
|
10
|
+
import { IsString, IsUndefined } from '../guard/index.mjs';
|
|
5
11
|
// prettier-ignore
|
|
6
12
|
function TArray(schema, references) {
|
|
7
|
-
return
|
|
13
|
+
return IsTransform(schema) || Visit(schema.items, references);
|
|
8
14
|
}
|
|
9
15
|
// prettier-ignore
|
|
10
16
|
function TAsyncIterator(schema, references) {
|
|
11
|
-
return
|
|
17
|
+
return IsTransform(schema) || Visit(schema.items, references);
|
|
12
18
|
}
|
|
13
19
|
// prettier-ignore
|
|
14
20
|
function TConstructor(schema, references) {
|
|
15
|
-
return
|
|
21
|
+
return IsTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
16
22
|
}
|
|
17
23
|
// prettier-ignore
|
|
18
24
|
function TFunction(schema, references) {
|
|
19
|
-
return
|
|
25
|
+
return IsTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
20
26
|
}
|
|
21
27
|
// prettier-ignore
|
|
22
28
|
function TIntersect(schema, references) {
|
|
23
|
-
return
|
|
29
|
+
return IsTransform(schema) || IsTransform(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
|
|
24
30
|
}
|
|
25
31
|
// prettier-ignore
|
|
26
32
|
function TIterator(schema, references) {
|
|
27
|
-
return
|
|
33
|
+
return IsTransform(schema) || Visit(schema.items, references);
|
|
28
34
|
}
|
|
29
35
|
// prettier-ignore
|
|
30
36
|
function TNot(schema, references) {
|
|
31
|
-
return
|
|
37
|
+
return IsTransform(schema) || Visit(schema.not, references);
|
|
32
38
|
}
|
|
33
39
|
// prettier-ignore
|
|
34
40
|
function TObject(schema, references) {
|
|
35
|
-
return (
|
|
41
|
+
return (IsTransform(schema) ||
|
|
36
42
|
Object.values(schema.properties).some((schema) => Visit(schema, references)) ||
|
|
37
|
-
(
|
|
43
|
+
(IsSchema(schema.additionalProperties) && Visit(schema.additionalProperties, references)));
|
|
38
44
|
}
|
|
39
45
|
// prettier-ignore
|
|
40
46
|
function TPromise(schema, references) {
|
|
41
|
-
return
|
|
47
|
+
return IsTransform(schema) || Visit(schema.item, references);
|
|
42
48
|
}
|
|
43
49
|
// prettier-ignore
|
|
44
50
|
function TRecord(schema, references) {
|
|
45
51
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
46
52
|
const property = schema.patternProperties[pattern];
|
|
47
|
-
return
|
|
53
|
+
return IsTransform(schema) || Visit(property, references) || (IsSchema(schema.additionalProperties) && IsTransform(schema.additionalProperties));
|
|
48
54
|
}
|
|
49
55
|
// prettier-ignore
|
|
50
56
|
function TRef(schema, references) {
|
|
51
|
-
if (
|
|
57
|
+
if (IsTransform(schema))
|
|
52
58
|
return true;
|
|
53
59
|
return Visit(Deref(schema, references), references);
|
|
54
60
|
}
|
|
55
61
|
// prettier-ignore
|
|
56
62
|
function TThis(schema, references) {
|
|
57
|
-
if (
|
|
63
|
+
if (IsTransform(schema))
|
|
58
64
|
return true;
|
|
59
65
|
return Visit(Deref(schema, references), references);
|
|
60
66
|
}
|
|
61
67
|
// prettier-ignore
|
|
62
68
|
function TTuple(schema, references) {
|
|
63
|
-
return
|
|
69
|
+
return IsTransform(schema) || (!IsUndefined(schema.items) && schema.items.some((schema) => Visit(schema, references)));
|
|
64
70
|
}
|
|
65
71
|
// prettier-ignore
|
|
66
72
|
function TUnion(schema, references) {
|
|
67
|
-
return
|
|
73
|
+
return IsTransform(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
68
74
|
}
|
|
69
75
|
// prettier-ignore
|
|
70
76
|
function Visit(schema, references) {
|
|
@@ -104,7 +110,7 @@ function Visit(schema, references) {
|
|
|
104
110
|
case 'Union':
|
|
105
111
|
return TUnion(schema_, references_);
|
|
106
112
|
default:
|
|
107
|
-
return
|
|
113
|
+
return IsTransform(schema);
|
|
108
114
|
}
|
|
109
115
|
}
|
|
110
116
|
const visited = new Set();
|
|
@@ -3,17 +3,23 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.TypeCompiler = exports.Policy = exports.TypeCompilerTypeGuardError = exports.TypeCompilerUnknownTypeError = exports.TypeCheck = void 0;
|
|
5
5
|
const index_1 = require("../value/transform/index");
|
|
6
|
-
const index_2 = require("../
|
|
7
|
-
const index_3 = require("../
|
|
8
|
-
const index_4 = require("../
|
|
9
|
-
const index_5 = require("../value/
|
|
10
|
-
const index_6 = require("../
|
|
11
|
-
const index_7 = require("../type/
|
|
12
|
-
const
|
|
13
|
-
const index_8 = require("../type/registry/index");
|
|
14
|
-
const index_9 = require("../type/keyof/index");
|
|
6
|
+
const index_2 = require("../errors/index");
|
|
7
|
+
const index_3 = require("../system/index");
|
|
8
|
+
const index_4 = require("../value/deref/index");
|
|
9
|
+
const index_5 = require("../value/hash/index");
|
|
10
|
+
const index_6 = require("../type/symbols/index");
|
|
11
|
+
const index_7 = require("../type/registry/index");
|
|
12
|
+
const index_8 = require("../type/keyof/index");
|
|
15
13
|
const extends_undefined_1 = require("../type/extends/extends-undefined");
|
|
16
|
-
const
|
|
14
|
+
const index_9 = require("../type/never/index");
|
|
15
|
+
// ------------------------------------------------------------------
|
|
16
|
+
// ValueGuard
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
const index_10 = require("../value/guard/index");
|
|
19
|
+
// ------------------------------------------------------------------
|
|
20
|
+
// TypeGuard
|
|
21
|
+
// ------------------------------------------------------------------
|
|
22
|
+
const type_1 = require("../type/guard/type");
|
|
17
23
|
// ------------------------------------------------------------------
|
|
18
24
|
// TypeCheck
|
|
19
25
|
// ------------------------------------------------------------------
|
|
@@ -31,7 +37,7 @@ class TypeCheck {
|
|
|
31
37
|
}
|
|
32
38
|
/** Returns an iterator for each error in this value. */
|
|
33
39
|
Errors(value) {
|
|
34
|
-
return (0,
|
|
40
|
+
return (0, index_2.Errors)(this.schema, this.references, value);
|
|
35
41
|
}
|
|
36
42
|
/** Returns true if the value matches the compiled type. */
|
|
37
43
|
Check(value) {
|
|
@@ -156,25 +162,25 @@ exports.TypeCompilerTypeGuardError = TypeCompilerTypeGuardError;
|
|
|
156
162
|
var Policy;
|
|
157
163
|
(function (Policy) {
|
|
158
164
|
function IsExactOptionalProperty(value, key, expression) {
|
|
159
|
-
return
|
|
165
|
+
return index_3.TypeSystemPolicy.ExactOptionalPropertyTypes ? `('${key}' in ${value} ? ${expression} : true)` : `(${MemberExpression.Encode(value, key)} !== undefined ? ${expression} : true)`;
|
|
160
166
|
}
|
|
161
167
|
Policy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
162
168
|
function IsObjectLike(value) {
|
|
163
|
-
return !
|
|
169
|
+
return !index_3.TypeSystemPolicy.AllowArrayObject ? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))` : `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
164
170
|
}
|
|
165
171
|
Policy.IsObjectLike = IsObjectLike;
|
|
166
172
|
function IsRecordLike(value) {
|
|
167
|
-
return !
|
|
173
|
+
return !index_3.TypeSystemPolicy.AllowArrayObject
|
|
168
174
|
? `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
|
|
169
175
|
: `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`;
|
|
170
176
|
}
|
|
171
177
|
Policy.IsRecordLike = IsRecordLike;
|
|
172
178
|
function IsNumberLike(value) {
|
|
173
|
-
return !
|
|
179
|
+
return !index_3.TypeSystemPolicy.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`;
|
|
174
180
|
}
|
|
175
181
|
Policy.IsNumberLike = IsNumberLike;
|
|
176
182
|
function IsVoidLike(value) {
|
|
177
|
-
return
|
|
183
|
+
return index_3.TypeSystemPolicy.AllowNullVoid ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`;
|
|
178
184
|
}
|
|
179
185
|
Policy.IsVoidLike = IsVoidLike;
|
|
180
186
|
})(Policy || (exports.Policy = Policy = {}));
|
|
@@ -185,7 +191,7 @@ var TypeCompiler;
|
|
|
185
191
|
// Guards
|
|
186
192
|
// ----------------------------------------------------------------
|
|
187
193
|
function IsAnyOrUnknown(schema) {
|
|
188
|
-
return schema[
|
|
194
|
+
return schema[index_6.Kind] === 'Any' || schema[index_6.Kind] === 'Unknown';
|
|
189
195
|
}
|
|
190
196
|
// ----------------------------------------------------------------
|
|
191
197
|
// Types
|
|
@@ -196,17 +202,17 @@ var TypeCompiler;
|
|
|
196
202
|
function* TArray(schema, references, value) {
|
|
197
203
|
yield `Array.isArray(${value})`;
|
|
198
204
|
const [parameter, accumulator] = [CreateParameter('value', 'any'), CreateParameter('acc', 'number')];
|
|
199
|
-
if ((0,
|
|
205
|
+
if ((0, index_10.IsNumber)(schema.maxItems))
|
|
200
206
|
yield `${value}.length <= ${schema.maxItems}`;
|
|
201
|
-
if ((0,
|
|
207
|
+
if ((0, index_10.IsNumber)(schema.minItems))
|
|
202
208
|
yield `${value}.length >= ${schema.minItems}`;
|
|
203
209
|
const elementExpression = CreateExpression(schema.items, references, 'value');
|
|
204
210
|
yield `${value}.every((${parameter}) => ${elementExpression})`;
|
|
205
|
-
if ((0, type_1.
|
|
206
|
-
const containsSchema = (0, type_1.
|
|
211
|
+
if ((0, type_1.IsSchema)(schema.contains) || (0, index_10.IsNumber)(schema.minContains) || (0, index_10.IsNumber)(schema.maxContains)) {
|
|
212
|
+
const containsSchema = (0, type_1.IsSchema)(schema.contains) ? schema.contains : (0, index_9.Never)();
|
|
207
213
|
const checkExpression = CreateExpression(containsSchema, references, 'value');
|
|
208
|
-
const checkMinContains = (0,
|
|
209
|
-
const checkMaxContains = (0,
|
|
214
|
+
const checkMinContains = (0, index_10.IsNumber)(schema.minContains) ? [`(count >= ${schema.minContains})`] : [];
|
|
215
|
+
const checkMaxContains = (0, index_10.IsNumber)(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : [];
|
|
210
216
|
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`;
|
|
211
217
|
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ');
|
|
212
218
|
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`;
|
|
@@ -222,15 +228,15 @@ var TypeCompiler;
|
|
|
222
228
|
}
|
|
223
229
|
function* TBigInt(schema, references, value) {
|
|
224
230
|
yield `(typeof ${value} === 'bigint')`;
|
|
225
|
-
if ((0,
|
|
231
|
+
if ((0, index_10.IsBigInt)(schema.exclusiveMaximum))
|
|
226
232
|
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
227
|
-
if ((0,
|
|
233
|
+
if ((0, index_10.IsBigInt)(schema.exclusiveMinimum))
|
|
228
234
|
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
229
|
-
if ((0,
|
|
235
|
+
if ((0, index_10.IsBigInt)(schema.maximum))
|
|
230
236
|
yield `${value} <= BigInt(${schema.maximum})`;
|
|
231
|
-
if ((0,
|
|
237
|
+
if ((0, index_10.IsBigInt)(schema.minimum))
|
|
232
238
|
yield `${value} >= BigInt(${schema.minimum})`;
|
|
233
|
-
if ((0,
|
|
239
|
+
if ((0, index_10.IsBigInt)(schema.multipleOf))
|
|
234
240
|
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
235
241
|
}
|
|
236
242
|
function* TBoolean(schema, references, value) {
|
|
@@ -241,15 +247,15 @@ var TypeCompiler;
|
|
|
241
247
|
}
|
|
242
248
|
function* TDate(schema, references, value) {
|
|
243
249
|
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
244
|
-
if ((0,
|
|
250
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMaximumTimestamp))
|
|
245
251
|
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
246
|
-
if ((0,
|
|
252
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMinimumTimestamp))
|
|
247
253
|
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
248
|
-
if ((0,
|
|
254
|
+
if ((0, index_10.IsNumber)(schema.maximumTimestamp))
|
|
249
255
|
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
250
|
-
if ((0,
|
|
256
|
+
if ((0, index_10.IsNumber)(schema.minimumTimestamp))
|
|
251
257
|
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
252
|
-
if ((0,
|
|
258
|
+
if ((0, index_10.IsNumber)(schema.multipleOfTimestamp))
|
|
253
259
|
yield `(${value}.getTime() % ${schema.multipleOfTimestamp}) === 0`;
|
|
254
260
|
}
|
|
255
261
|
function* TFunction(schema, references, value) {
|
|
@@ -257,26 +263,26 @@ var TypeCompiler;
|
|
|
257
263
|
}
|
|
258
264
|
function* TInteger(schema, references, value) {
|
|
259
265
|
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
260
|
-
if ((0,
|
|
266
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMaximum))
|
|
261
267
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
262
|
-
if ((0,
|
|
268
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMinimum))
|
|
263
269
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
264
|
-
if ((0,
|
|
270
|
+
if ((0, index_10.IsNumber)(schema.maximum))
|
|
265
271
|
yield `${value} <= ${schema.maximum}`;
|
|
266
|
-
if ((0,
|
|
272
|
+
if ((0, index_10.IsNumber)(schema.minimum))
|
|
267
273
|
yield `${value} >= ${schema.minimum}`;
|
|
268
|
-
if ((0,
|
|
274
|
+
if ((0, index_10.IsNumber)(schema.multipleOf))
|
|
269
275
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
270
276
|
}
|
|
271
277
|
function* TIntersect(schema, references, value) {
|
|
272
278
|
const check1 = schema.allOf.map((schema) => CreateExpression(schema, references, value)).join(' && ');
|
|
273
279
|
if (schema.unevaluatedProperties === false) {
|
|
274
|
-
const keyCheck = CreateVariable(`${new RegExp((0,
|
|
280
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_8.KeyOfPattern)(schema))};`);
|
|
275
281
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`;
|
|
276
282
|
yield `(${check1} && ${check2})`;
|
|
277
283
|
}
|
|
278
|
-
else if ((0, type_1.
|
|
279
|
-
const keyCheck = CreateVariable(`${new RegExp((0,
|
|
284
|
+
else if ((0, type_1.IsSchema)(schema.unevaluatedProperties)) {
|
|
285
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_8.KeyOfPattern)(schema))};`);
|
|
280
286
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
281
287
|
yield `(${check1} && ${check2})`;
|
|
282
288
|
}
|
|
@@ -307,22 +313,22 @@ var TypeCompiler;
|
|
|
307
313
|
}
|
|
308
314
|
function* TNumber(schema, references, value) {
|
|
309
315
|
yield Policy.IsNumberLike(value);
|
|
310
|
-
if ((0,
|
|
316
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMaximum))
|
|
311
317
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
312
|
-
if ((0,
|
|
318
|
+
if ((0, index_10.IsNumber)(schema.exclusiveMinimum))
|
|
313
319
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
314
|
-
if ((0,
|
|
320
|
+
if ((0, index_10.IsNumber)(schema.maximum))
|
|
315
321
|
yield `${value} <= ${schema.maximum}`;
|
|
316
|
-
if ((0,
|
|
322
|
+
if ((0, index_10.IsNumber)(schema.minimum))
|
|
317
323
|
yield `${value} >= ${schema.minimum}`;
|
|
318
|
-
if ((0,
|
|
324
|
+
if ((0, index_10.IsNumber)(schema.multipleOf))
|
|
319
325
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
320
326
|
}
|
|
321
327
|
function* TObject(schema, references, value) {
|
|
322
328
|
yield Policy.IsObjectLike(value);
|
|
323
|
-
if ((0,
|
|
329
|
+
if ((0, index_10.IsNumber)(schema.minProperties))
|
|
324
330
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
325
|
-
if ((0,
|
|
331
|
+
if ((0, index_10.IsNumber)(schema.maxProperties))
|
|
326
332
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
327
333
|
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
328
334
|
for (const knownKey of knownKeys) {
|
|
@@ -358,19 +364,19 @@ var TypeCompiler;
|
|
|
358
364
|
}
|
|
359
365
|
function* TRecord(schema, references, value) {
|
|
360
366
|
yield Policy.IsRecordLike(value);
|
|
361
|
-
if ((0,
|
|
367
|
+
if ((0, index_10.IsNumber)(schema.minProperties))
|
|
362
368
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
363
|
-
if ((0,
|
|
369
|
+
if ((0, index_10.IsNumber)(schema.maxProperties))
|
|
364
370
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
365
371
|
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
366
372
|
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
367
373
|
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
368
|
-
const check2 = (0, type_1.
|
|
374
|
+
const check2 = (0, type_1.IsSchema)(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
369
375
|
const expression = `(${variable}.test(key) ? ${check1} : ${check2})`;
|
|
370
376
|
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
|
371
377
|
}
|
|
372
378
|
function* TRef(schema, references, value) {
|
|
373
|
-
const target = (0,
|
|
379
|
+
const target = (0, index_4.Deref)(schema, references);
|
|
374
380
|
// Reference: If we have seen this reference before we can just yield and return the function call.
|
|
375
381
|
// If this isn't the case we defer to visit to generate and set the function for subsequent passes.
|
|
376
382
|
if (state.functions.has(schema.$ref))
|
|
@@ -379,9 +385,9 @@ var TypeCompiler;
|
|
|
379
385
|
}
|
|
380
386
|
function* TString(schema, references, value) {
|
|
381
387
|
yield `(typeof ${value} === 'string')`;
|
|
382
|
-
if ((0,
|
|
388
|
+
if ((0, index_10.IsNumber)(schema.maxLength))
|
|
383
389
|
yield `${value}.length <= ${schema.maxLength}`;
|
|
384
|
-
if ((0,
|
|
390
|
+
if ((0, index_10.IsNumber)(schema.minLength))
|
|
385
391
|
yield `${value}.length >= ${schema.minLength}`;
|
|
386
392
|
if (schema.pattern !== undefined) {
|
|
387
393
|
const variable = CreateVariable(`${new RegExp(schema.pattern)};`);
|
|
@@ -422,9 +428,9 @@ var TypeCompiler;
|
|
|
422
428
|
}
|
|
423
429
|
function* TUint8Array(schema, references, value) {
|
|
424
430
|
yield `${value} instanceof Uint8Array`;
|
|
425
|
-
if ((0,
|
|
431
|
+
if ((0, index_10.IsNumber)(schema.maxByteLength))
|
|
426
432
|
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
427
|
-
if ((0,
|
|
433
|
+
if ((0, index_10.IsNumber)(schema.minByteLength))
|
|
428
434
|
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
429
435
|
}
|
|
430
436
|
function* TUnknown(schema, references, value) {
|
|
@@ -436,15 +442,15 @@ var TypeCompiler;
|
|
|
436
442
|
function* TKind(schema, references, value) {
|
|
437
443
|
const instance = state.instances.size;
|
|
438
444
|
state.instances.set(instance, schema);
|
|
439
|
-
yield `kind('${schema[
|
|
445
|
+
yield `kind('${schema[index_6.Kind]}', ${instance}, ${value})`;
|
|
440
446
|
}
|
|
441
447
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
442
|
-
const references_ = (0,
|
|
448
|
+
const references_ = (0, index_10.IsString)(schema.$id) ? [...references, schema] : references;
|
|
443
449
|
const schema_ = schema;
|
|
444
450
|
// --------------------------------------------------------------
|
|
445
451
|
// Hoisting
|
|
446
452
|
// --------------------------------------------------------------
|
|
447
|
-
if (useHoisting && (0,
|
|
453
|
+
if (useHoisting && (0, index_10.IsString)(schema.$id)) {
|
|
448
454
|
const functionName = CreateFunctionName(schema.$id);
|
|
449
455
|
if (state.functions.has(functionName)) {
|
|
450
456
|
return yield `${functionName}(${value})`;
|
|
@@ -455,7 +461,7 @@ var TypeCompiler;
|
|
|
455
461
|
return yield `${functionName}(${value})`;
|
|
456
462
|
}
|
|
457
463
|
}
|
|
458
|
-
switch (schema_[
|
|
464
|
+
switch (schema_[index_6.Kind]) {
|
|
459
465
|
case 'Any':
|
|
460
466
|
return yield* TAny(schema_, references_, value);
|
|
461
467
|
case 'Array':
|
|
@@ -517,7 +523,7 @@ var TypeCompiler;
|
|
|
517
523
|
case 'Void':
|
|
518
524
|
return yield* TVoid(schema_, references_, value);
|
|
519
525
|
default:
|
|
520
|
-
if (!
|
|
526
|
+
if (!index_7.TypeRegistry.Has(schema_[index_6.Kind]))
|
|
521
527
|
throw new TypeCompilerUnknownTypeError(schema);
|
|
522
528
|
return yield* TKind(schema_, references_, value);
|
|
523
529
|
}
|
|
@@ -570,7 +576,7 @@ var TypeCompiler;
|
|
|
570
576
|
const functions = [...state.functions.values()];
|
|
571
577
|
const variables = [...state.variables.values()];
|
|
572
578
|
// prettier-ignore
|
|
573
|
-
const checkFunction = (0,
|
|
579
|
+
const checkFunction = (0, index_10.IsString)(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
574
580
|
? `return function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
575
581
|
: `return ${functionCode}`;
|
|
576
582
|
return [...variables, ...functions, checkFunction].join('\n');
|
|
@@ -579,8 +585,8 @@ var TypeCompiler;
|
|
|
579
585
|
function Code(...args) {
|
|
580
586
|
const defaults = { language: 'javascript' };
|
|
581
587
|
// prettier-ignore
|
|
582
|
-
const [schema, references, options] = (args.length === 2 && (0,
|
|
583
|
-
args.length === 2 && !(0,
|
|
588
|
+
const [schema, references, options] = (args.length === 2 && (0, index_10.IsArray)(args[1]) ? [args[0], args[1], defaults] :
|
|
589
|
+
args.length === 2 && !(0, index_10.IsArray)(args[1]) ? [args[0], [], args[1]] :
|
|
584
590
|
args.length === 3 ? [args[0], args[1], args[2]] :
|
|
585
591
|
args.length === 1 ? [args[0], [], defaults] :
|
|
586
592
|
[null, [], defaults]);
|
|
@@ -589,10 +595,10 @@ var TypeCompiler;
|
|
|
589
595
|
state.variables.clear();
|
|
590
596
|
state.functions.clear();
|
|
591
597
|
state.instances.clear();
|
|
592
|
-
if (!(0, type_1.
|
|
598
|
+
if (!(0, type_1.IsSchema)(schema))
|
|
593
599
|
throw new TypeCompilerTypeGuardError(schema);
|
|
594
600
|
for (const schema of references)
|
|
595
|
-
if (!(0, type_1.
|
|
601
|
+
if (!(0, type_1.IsSchema)(schema))
|
|
596
602
|
throw new TypeCompilerTypeGuardError(schema);
|
|
597
603
|
return Build(schema, references, options);
|
|
598
604
|
}
|
|
@@ -603,20 +609,20 @@ var TypeCompiler;
|
|
|
603
609
|
const compiledFunction = globalThis.Function('kind', 'format', 'hash', generatedCode);
|
|
604
610
|
const instances = new Map(state.instances);
|
|
605
611
|
function typeRegistryFunction(kind, instance, value) {
|
|
606
|
-
if (!
|
|
612
|
+
if (!index_7.TypeRegistry.Has(kind) || !instances.has(instance))
|
|
607
613
|
return false;
|
|
608
|
-
const checkFunc =
|
|
614
|
+
const checkFunc = index_7.TypeRegistry.Get(kind);
|
|
609
615
|
const schema = instances.get(instance);
|
|
610
616
|
return checkFunc(schema, value);
|
|
611
617
|
}
|
|
612
618
|
function formatRegistryFunction(format, value) {
|
|
613
|
-
if (!
|
|
619
|
+
if (!index_7.FormatRegistry.Has(format))
|
|
614
620
|
return false;
|
|
615
|
-
const checkFunc =
|
|
621
|
+
const checkFunc = index_7.FormatRegistry.Get(format);
|
|
616
622
|
return checkFunc(value);
|
|
617
623
|
}
|
|
618
624
|
function hashFunction(value) {
|
|
619
|
-
return (0,
|
|
625
|
+
return (0, index_5.Hash)(value);
|
|
620
626
|
}
|
|
621
627
|
const checkFunction = compiledFunction(typeRegistryFunction, formatRegistryFunction, hashFunction);
|
|
622
628
|
return new TypeCheck(schema, references, checkFunction, generatedCode);
|