@sinclair/typebox 0.26.0-dev.2 → 0.26.0-dev.4
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 +33 -33
- package/compiler/compiler.js +536 -536
- package/compiler/index.d.ts +2 -2
- package/compiler/index.js +47 -47
- package/errors/errors.d.ts +84 -84
- package/errors/errors.js +570 -570
- package/errors/index.d.ts +1 -1
- package/errors/index.js +44 -44
- package/package.json +2 -2
- package/readme.md +1 -7
- package/system/index.d.ts +1 -1
- package/system/index.js +44 -44
- package/system/system.d.ts +20 -20
- package/system/system.js +69 -69
- package/typebox.d.ts +656 -649
- package/typebox.js +1889 -1883
- package/value/cast.d.ts +26 -26
- package/value/cast.js +348 -348
- package/value/check.d.ts +8 -8
- package/value/check.js +450 -450
- package/value/clone.d.ts +3 -3
- package/value/clone.js +71 -71
- package/value/convert.d.ts +13 -13
- package/value/convert.js +345 -345
- package/value/create.d.ts +18 -18
- package/value/create.js +430 -430
- package/value/delta.d.ts +43 -43
- package/value/delta.js +204 -204
- package/value/equal.d.ts +3 -3
- package/value/equal.js +80 -80
- package/value/hash.d.ts +8 -8
- package/value/hash.js +208 -208
- package/value/index.d.ts +5 -5
- package/value/index.js +56 -56
- package/value/is.d.ts +11 -11
- package/value/is.js +53 -53
- package/value/pointer.d.ts +24 -24
- package/value/pointer.js +142 -142
- package/value/value.d.ts +26 -26
- package/value/value.js +93 -93
package/compiler/compiler.js
CHANGED
|
@@ -1,536 +1,536 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/compiler
|
|
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.TypeCompiler = exports.TypeCompilerPreflightCheckError = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
|
|
31
|
-
const Types = require("../typebox");
|
|
32
|
-
const index_1 = require("../errors/index");
|
|
33
|
-
const index_2 = require("../system/index");
|
|
34
|
-
const hash_1 = require("../value/hash");
|
|
35
|
-
// -------------------------------------------------------------------
|
|
36
|
-
// TypeCheck
|
|
37
|
-
// -------------------------------------------------------------------
|
|
38
|
-
class TypeCheck {
|
|
39
|
-
constructor(schema, checkFunc, code) {
|
|
40
|
-
this.schema = schema;
|
|
41
|
-
this.checkFunc = checkFunc;
|
|
42
|
-
this.code = code;
|
|
43
|
-
}
|
|
44
|
-
/** Returns the generated assertion code used to validate this type. */
|
|
45
|
-
Code() {
|
|
46
|
-
return this.code;
|
|
47
|
-
}
|
|
48
|
-
/** Returns an iterator for each error in this value. */
|
|
49
|
-
Errors(value) {
|
|
50
|
-
return index_1.ValueErrors.Errors(this.schema, value);
|
|
51
|
-
}
|
|
52
|
-
/** Returns true if the value matches the compiled type. */
|
|
53
|
-
Check(value) {
|
|
54
|
-
return this.checkFunc(value);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.TypeCheck = TypeCheck;
|
|
58
|
-
// -------------------------------------------------------------------
|
|
59
|
-
// Character
|
|
60
|
-
// -------------------------------------------------------------------
|
|
61
|
-
var Character;
|
|
62
|
-
(function (Character) {
|
|
63
|
-
function DollarSign(code) {
|
|
64
|
-
return code === 36;
|
|
65
|
-
}
|
|
66
|
-
Character.DollarSign = DollarSign;
|
|
67
|
-
function Underscore(code) {
|
|
68
|
-
return code === 95;
|
|
69
|
-
}
|
|
70
|
-
Character.Underscore = Underscore;
|
|
71
|
-
function Numeric(code) {
|
|
72
|
-
return code >= 48 && code <= 57;
|
|
73
|
-
}
|
|
74
|
-
Character.Numeric = Numeric;
|
|
75
|
-
function Alpha(code) {
|
|
76
|
-
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
77
|
-
}
|
|
78
|
-
Character.Alpha = Alpha;
|
|
79
|
-
})(Character || (Character = {}));
|
|
80
|
-
// -------------------------------------------------------------------
|
|
81
|
-
// Identifier
|
|
82
|
-
// -------------------------------------------------------------------
|
|
83
|
-
var Identifier;
|
|
84
|
-
(function (Identifier) {
|
|
85
|
-
function Encode($id) {
|
|
86
|
-
const buffer = [];
|
|
87
|
-
for (let i = 0; i < $id.length; i++) {
|
|
88
|
-
const code = $id.charCodeAt(i);
|
|
89
|
-
if (Character.Numeric(code) || Character.Alpha(code)) {
|
|
90
|
-
buffer.push($id.charAt(i));
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
buffer.push(`_${code}_`);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return buffer.join('').replace(/__/g, '_');
|
|
97
|
-
}
|
|
98
|
-
Identifier.Encode = Encode;
|
|
99
|
-
})(Identifier || (Identifier = {}));
|
|
100
|
-
// -------------------------------------------------------------------
|
|
101
|
-
// MemberExpression
|
|
102
|
-
// -------------------------------------------------------------------
|
|
103
|
-
var MemberExpression;
|
|
104
|
-
(function (MemberExpression) {
|
|
105
|
-
function Check(propertyName) {
|
|
106
|
-
if (propertyName.length === 0)
|
|
107
|
-
return false;
|
|
108
|
-
{
|
|
109
|
-
const code = propertyName.charCodeAt(0);
|
|
110
|
-
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
for (let i = 1; i < propertyName.length; i++) {
|
|
115
|
-
const code = propertyName.charCodeAt(i);
|
|
116
|
-
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
function Encode(object, key) {
|
|
123
|
-
return !Check(key) ? `${object}['${key}']` : `${object}.${key}`;
|
|
124
|
-
}
|
|
125
|
-
MemberExpression.Encode = Encode;
|
|
126
|
-
})(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {}));
|
|
127
|
-
// -------------------------------------------------------------------
|
|
128
|
-
// TypeCompiler
|
|
129
|
-
// -------------------------------------------------------------------
|
|
130
|
-
class TypeCompilerUnknownTypeError extends Error {
|
|
131
|
-
constructor(schema) {
|
|
132
|
-
super('TypeCompiler: Unknown type');
|
|
133
|
-
this.schema = schema;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
137
|
-
class TypeCompilerPreflightCheckError extends Error {
|
|
138
|
-
constructor(schema) {
|
|
139
|
-
super('TypeCompiler: Preflight validation check failed for given schema');
|
|
140
|
-
this.schema = schema;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
exports.TypeCompilerPreflightCheckError = TypeCompilerPreflightCheckError;
|
|
144
|
-
/** Compiles Types for Runtime Type Checking */
|
|
145
|
-
var TypeCompiler;
|
|
146
|
-
(function (TypeCompiler) {
|
|
147
|
-
// -------------------------------------------------------------------
|
|
148
|
-
// Guards
|
|
149
|
-
// -------------------------------------------------------------------
|
|
150
|
-
function IsBigInt(value) {
|
|
151
|
-
return typeof value === 'bigint';
|
|
152
|
-
}
|
|
153
|
-
function IsNumber(value) {
|
|
154
|
-
return typeof value === 'number' && globalThis.Number.isFinite(value);
|
|
155
|
-
}
|
|
156
|
-
// -------------------------------------------------------------------
|
|
157
|
-
// Types
|
|
158
|
-
// -------------------------------------------------------------------
|
|
159
|
-
function* Any(schema, value) {
|
|
160
|
-
yield 'true';
|
|
161
|
-
}
|
|
162
|
-
function* Array(schema, value) {
|
|
163
|
-
const expression = CreateExpression(schema.items, 'value');
|
|
164
|
-
yield `Array.isArray(${value}) && ${value}.every(value => ${expression})`;
|
|
165
|
-
if (IsNumber(schema.minItems))
|
|
166
|
-
yield `${value}.length >= ${schema.minItems}`;
|
|
167
|
-
if (IsNumber(schema.maxItems))
|
|
168
|
-
yield `${value}.length <= ${schema.maxItems}`;
|
|
169
|
-
if (schema.uniqueItems === true)
|
|
170
|
-
yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`;
|
|
171
|
-
}
|
|
172
|
-
function* BigInt(schema, value) {
|
|
173
|
-
yield `(typeof ${value} === 'bigint')`;
|
|
174
|
-
if (IsBigInt(schema.multipleOf))
|
|
175
|
-
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
176
|
-
if (IsBigInt(schema.exclusiveMinimum))
|
|
177
|
-
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
178
|
-
if (IsBigInt(schema.exclusiveMaximum))
|
|
179
|
-
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
180
|
-
if (IsBigInt(schema.minimum))
|
|
181
|
-
yield `${value} >= BigInt(${schema.minimum})`;
|
|
182
|
-
if (IsBigInt(schema.maximum))
|
|
183
|
-
yield `${value} <= BigInt(${schema.maximum})`;
|
|
184
|
-
}
|
|
185
|
-
function* Boolean(schema, value) {
|
|
186
|
-
yield `typeof ${value} === 'boolean'`;
|
|
187
|
-
}
|
|
188
|
-
function* Constructor(schema, value) {
|
|
189
|
-
yield* Visit(schema.returns, `${value}.prototype`);
|
|
190
|
-
}
|
|
191
|
-
function* Date(schema, value) {
|
|
192
|
-
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
193
|
-
if (IsNumber(schema.exclusiveMinimumTimestamp))
|
|
194
|
-
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
195
|
-
if (IsNumber(schema.exclusiveMaximumTimestamp))
|
|
196
|
-
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
197
|
-
if (IsNumber(schema.minimumTimestamp))
|
|
198
|
-
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
199
|
-
if (IsNumber(schema.maximumTimestamp))
|
|
200
|
-
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
201
|
-
}
|
|
202
|
-
function* Function(schema, value) {
|
|
203
|
-
yield `typeof ${value} === 'function'`;
|
|
204
|
-
}
|
|
205
|
-
function* Integer(schema, value) {
|
|
206
|
-
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
207
|
-
if (IsNumber(schema.multipleOf))
|
|
208
|
-
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
209
|
-
if (IsNumber(schema.exclusiveMinimum))
|
|
210
|
-
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
211
|
-
if (IsNumber(schema.exclusiveMaximum))
|
|
212
|
-
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
213
|
-
if (IsNumber(schema.minimum))
|
|
214
|
-
yield `${value} >= ${schema.minimum}`;
|
|
215
|
-
if (IsNumber(schema.maximum))
|
|
216
|
-
yield `${value} <= ${schema.maximum}`;
|
|
217
|
-
}
|
|
218
|
-
function* Intersect(schema, value) {
|
|
219
|
-
if (schema.unevaluatedProperties === undefined) {
|
|
220
|
-
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
221
|
-
yield `${expressions.join(' && ')}`;
|
|
222
|
-
}
|
|
223
|
-
else if (schema.unevaluatedProperties === false) {
|
|
224
|
-
// prettier-ignore
|
|
225
|
-
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
|
|
226
|
-
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
227
|
-
const expression1 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key))`;
|
|
228
|
-
yield `${expressions.join(' && ')} && ${expression1}`;
|
|
229
|
-
}
|
|
230
|
-
else if (typeof schema.unevaluatedProperties === 'object') {
|
|
231
|
-
// prettier-ignore
|
|
232
|
-
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
|
|
233
|
-
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
234
|
-
const expression1 = CreateExpression(schema.unevaluatedProperties, 'value[key]');
|
|
235
|
-
const expression2 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key) || ${expression1})`;
|
|
236
|
-
yield `${expressions.join(' && ')} && ${expression2}`;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
function* Literal(schema, value) {
|
|
240
|
-
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
241
|
-
yield `${value} === ${schema.const}`;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
yield `${value} === '${schema.const}'`;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function* Never(schema, value) {
|
|
248
|
-
yield `false`;
|
|
249
|
-
}
|
|
250
|
-
function* Not(schema, value) {
|
|
251
|
-
const left = CreateExpression(schema.allOf[0].not, value);
|
|
252
|
-
const right = CreateExpression(schema.allOf[1], value);
|
|
253
|
-
yield `!${left} && ${right}`;
|
|
254
|
-
}
|
|
255
|
-
function* Null(schema, value) {
|
|
256
|
-
yield `${value} === null`;
|
|
257
|
-
}
|
|
258
|
-
function* Number(schema, value) {
|
|
259
|
-
yield `typeof ${value} === 'number'`;
|
|
260
|
-
if (!index_2.TypeSystem.AllowNaN)
|
|
261
|
-
yield `Number.isFinite(${value})`;
|
|
262
|
-
if (IsNumber(schema.multipleOf))
|
|
263
|
-
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
264
|
-
if (IsNumber(schema.exclusiveMinimum))
|
|
265
|
-
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
266
|
-
if (IsNumber(schema.exclusiveMaximum))
|
|
267
|
-
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
268
|
-
if (IsNumber(schema.minimum))
|
|
269
|
-
yield `${value} >= ${schema.minimum}`;
|
|
270
|
-
if (IsNumber(schema.maximum))
|
|
271
|
-
yield `${value} <= ${schema.maximum}`;
|
|
272
|
-
}
|
|
273
|
-
function* Object(schema, value) {
|
|
274
|
-
yield `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
275
|
-
if (!index_2.TypeSystem.AllowArrayObjects)
|
|
276
|
-
yield `!Array.isArray(${value})`;
|
|
277
|
-
if (IsNumber(schema.minProperties))
|
|
278
|
-
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
279
|
-
if (IsNumber(schema.maxProperties))
|
|
280
|
-
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
281
|
-
const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
282
|
-
for (const schemaKey of schemaKeys) {
|
|
283
|
-
const memberExpression = MemberExpression.Encode(value, schemaKey);
|
|
284
|
-
const property = schema.properties[schemaKey];
|
|
285
|
-
if (schema.required && schema.required.includes(schemaKey)) {
|
|
286
|
-
yield* Visit(property, memberExpression);
|
|
287
|
-
if (Types.ExtendsUndefined.Check(property))
|
|
288
|
-
yield `('${schemaKey}' in ${value})`;
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
const expression = CreateExpression(property, memberExpression);
|
|
292
|
-
yield `('${schemaKey}' in ${value} ? ${expression} : true)`;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
if (schema.additionalProperties === false) {
|
|
296
|
-
if (schema.required && schema.required.length === schemaKeys.length) {
|
|
297
|
-
yield `Object.getOwnPropertyNames(${value}).length === ${schemaKeys.length}`;
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
301
|
-
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (typeof schema.additionalProperties === 'object') {
|
|
305
|
-
const expression = CreateExpression(schema.additionalProperties, 'value[key]');
|
|
306
|
-
const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
307
|
-
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
function* Promise(schema, value) {
|
|
311
|
-
yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
|
|
312
|
-
}
|
|
313
|
-
function* Record(schema, value) {
|
|
314
|
-
yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date))`;
|
|
315
|
-
if (!index_2.TypeSystem.AllowArrayObjects)
|
|
316
|
-
yield `!Array.isArray(${value})`;
|
|
317
|
-
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
318
|
-
const local = PushLocal(`new RegExp(/${keyPattern}/)`);
|
|
319
|
-
yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
|
|
320
|
-
const expression = CreateExpression(valueSchema, 'value');
|
|
321
|
-
yield `Object.values(${value}).every(value => ${expression})`;
|
|
322
|
-
}
|
|
323
|
-
function* Ref(schema, value) {
|
|
324
|
-
// Reference: If we have seen this reference before we can just yield and return
|
|
325
|
-
// the function call. If this isn't the case we defer to visit to generate and
|
|
326
|
-
// set the function for subsequent passes. Consider for refactor.
|
|
327
|
-
if (state_local_function_names.has(schema.$ref))
|
|
328
|
-
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
329
|
-
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), value);
|
|
330
|
-
}
|
|
331
|
-
function* Self(schema, value) {
|
|
332
|
-
const func = CreateFunctionName(schema.$ref);
|
|
333
|
-
yield `${func}(${value})`;
|
|
334
|
-
}
|
|
335
|
-
function* String(schema, value) {
|
|
336
|
-
yield `(typeof ${value} === 'string')`;
|
|
337
|
-
if (IsNumber(schema.minLength))
|
|
338
|
-
yield `${value}.length >= ${schema.minLength}`;
|
|
339
|
-
if (IsNumber(schema.maxLength))
|
|
340
|
-
yield `${value}.length <= ${schema.maxLength}`;
|
|
341
|
-
if (schema.pattern !== undefined) {
|
|
342
|
-
const local = PushLocal(`${new RegExp(schema.pattern)};`);
|
|
343
|
-
yield `${local}.test(${value})`;
|
|
344
|
-
}
|
|
345
|
-
if (schema.format !== undefined) {
|
|
346
|
-
yield `format('${schema.format}', ${value})`;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
function* Symbol(schema, value) {
|
|
350
|
-
yield `(typeof ${value} === 'symbol')`;
|
|
351
|
-
}
|
|
352
|
-
function* Tuple(schema, value) {
|
|
353
|
-
yield `(Array.isArray(${value}))`;
|
|
354
|
-
if (schema.items === undefined)
|
|
355
|
-
return yield `${value}.length === 0`;
|
|
356
|
-
yield `(${value}.length === ${schema.maxItems})`;
|
|
357
|
-
for (let i = 0; i < schema.items.length; i++) {
|
|
358
|
-
const expression = CreateExpression(schema.items[i], `${value}[${i}]`);
|
|
359
|
-
yield `${expression}`;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
function* Undefined(schema, value) {
|
|
363
|
-
yield `${value} === undefined`;
|
|
364
|
-
}
|
|
365
|
-
function* Union(schema, value) {
|
|
366
|
-
const expressions = schema.anyOf.map((schema) => CreateExpression(schema, value));
|
|
367
|
-
yield `(${expressions.join(' || ')})`;
|
|
368
|
-
}
|
|
369
|
-
function* Uint8Array(schema, value) {
|
|
370
|
-
yield `${value} instanceof Uint8Array`;
|
|
371
|
-
if (IsNumber(schema.maxByteLength))
|
|
372
|
-
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
373
|
-
if (IsNumber(schema.minByteLength))
|
|
374
|
-
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
375
|
-
}
|
|
376
|
-
function* Unknown(schema, value) {
|
|
377
|
-
yield 'true';
|
|
378
|
-
}
|
|
379
|
-
function* Void(schema, value) {
|
|
380
|
-
if (index_2.TypeSystem.AllowVoidNull) {
|
|
381
|
-
yield `(${value} === undefined || ${value} === null)`;
|
|
382
|
-
}
|
|
383
|
-
else {
|
|
384
|
-
yield `${value} === undefined`;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
function* UserDefined(schema, value) {
|
|
388
|
-
const schema_key = `schema_key_${state_remote_custom_types.size}`;
|
|
389
|
-
state_remote_custom_types.set(schema_key, schema);
|
|
390
|
-
yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`;
|
|
391
|
-
}
|
|
392
|
-
function* Visit(schema, value) {
|
|
393
|
-
// Reference: Referenced schemas can originate from either additional schemas
|
|
394
|
-
// or inline in the schema itself. Ideally the recursive path should align to
|
|
395
|
-
// reference path. Consider for refactor.
|
|
396
|
-
if (schema.$id && !state_local_function_names.has(schema.$id)) {
|
|
397
|
-
state_local_function_names.add(schema.$id);
|
|
398
|
-
const name = CreateFunctionName(schema.$id);
|
|
399
|
-
const body = CreateFunction(name, schema, 'value');
|
|
400
|
-
PushFunction(body);
|
|
401
|
-
yield `${name}(${value})`;
|
|
402
|
-
return;
|
|
403
|
-
}
|
|
404
|
-
const anySchema = schema;
|
|
405
|
-
switch (anySchema[Types.Kind]) {
|
|
406
|
-
case 'Any':
|
|
407
|
-
return yield* Any(anySchema, value);
|
|
408
|
-
case 'Array':
|
|
409
|
-
return yield* Array(anySchema, value);
|
|
410
|
-
case 'BigInt':
|
|
411
|
-
return yield* BigInt(anySchema, value);
|
|
412
|
-
case 'Boolean':
|
|
413
|
-
return yield* Boolean(anySchema, value);
|
|
414
|
-
case 'Constructor':
|
|
415
|
-
return yield* Constructor(anySchema, value);
|
|
416
|
-
case 'Date':
|
|
417
|
-
return yield* Date(anySchema, value);
|
|
418
|
-
case 'Function':
|
|
419
|
-
return yield* Function(anySchema, value);
|
|
420
|
-
case 'Integer':
|
|
421
|
-
return yield* Integer(anySchema, value);
|
|
422
|
-
case 'Intersect':
|
|
423
|
-
return yield* Intersect(anySchema, value);
|
|
424
|
-
case 'Literal':
|
|
425
|
-
return yield* Literal(anySchema, value);
|
|
426
|
-
case 'Never':
|
|
427
|
-
return yield* Never(anySchema, value);
|
|
428
|
-
case 'Not':
|
|
429
|
-
return yield* Not(anySchema, value);
|
|
430
|
-
case 'Null':
|
|
431
|
-
return yield* Null(anySchema, value);
|
|
432
|
-
case 'Number':
|
|
433
|
-
return yield* Number(anySchema, value);
|
|
434
|
-
case 'Object':
|
|
435
|
-
return yield* Object(anySchema, value);
|
|
436
|
-
case 'Promise':
|
|
437
|
-
return yield* Promise(anySchema, value);
|
|
438
|
-
case 'Record':
|
|
439
|
-
return yield* Record(anySchema, value);
|
|
440
|
-
case 'Ref':
|
|
441
|
-
return yield* Ref(anySchema, value);
|
|
442
|
-
case 'Self':
|
|
443
|
-
return yield* Self(anySchema, value);
|
|
444
|
-
case 'String':
|
|
445
|
-
return yield* String(anySchema, value);
|
|
446
|
-
case 'Symbol':
|
|
447
|
-
return yield* Symbol(anySchema, value);
|
|
448
|
-
case 'Tuple':
|
|
449
|
-
return yield* Tuple(anySchema, value);
|
|
450
|
-
case 'Undefined':
|
|
451
|
-
return yield* Undefined(anySchema, value);
|
|
452
|
-
case 'Union':
|
|
453
|
-
return yield* Union(anySchema, value);
|
|
454
|
-
case 'Uint8Array':
|
|
455
|
-
return yield* Uint8Array(anySchema, value);
|
|
456
|
-
case 'Unknown':
|
|
457
|
-
return yield* Unknown(anySchema, value);
|
|
458
|
-
case 'Void':
|
|
459
|
-
return yield* Void(anySchema, value);
|
|
460
|
-
default:
|
|
461
|
-
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
462
|
-
throw new TypeCompilerUnknownTypeError(schema);
|
|
463
|
-
return yield* UserDefined(anySchema, value);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
// -------------------------------------------------------------------
|
|
467
|
-
// Compiler State
|
|
468
|
-
// -------------------------------------------------------------------
|
|
469
|
-
const state_local_variables = new Set(); // local variables and functions
|
|
470
|
-
const state_local_function_names = new Set(); // local function names used call ref validators
|
|
471
|
-
const state_remote_custom_types = new Map(); // remote custom types used during compilation
|
|
472
|
-
function ResetCompiler() {
|
|
473
|
-
state_local_variables.clear();
|
|
474
|
-
state_local_function_names.clear();
|
|
475
|
-
state_remote_custom_types.clear();
|
|
476
|
-
}
|
|
477
|
-
function CreateExpression(schema, value) {
|
|
478
|
-
return `(${[...Visit(schema, value)].join(' && ')})`;
|
|
479
|
-
}
|
|
480
|
-
function CreateFunctionName($id) {
|
|
481
|
-
return `check_${Identifier.Encode($id)}`;
|
|
482
|
-
}
|
|
483
|
-
function CreateFunction(name, schema, value) {
|
|
484
|
-
const expression = [...Visit(schema, value)].map((condition) => ` ${condition}`).join(' &&\n');
|
|
485
|
-
return `function ${name}(value) {\n return (\n${expression}\n )\n}`;
|
|
486
|
-
}
|
|
487
|
-
function PushFunction(functionBody) {
|
|
488
|
-
state_local_variables.add(functionBody);
|
|
489
|
-
}
|
|
490
|
-
function PushLocal(expression) {
|
|
491
|
-
const local = `local_${state_local_variables.size}`;
|
|
492
|
-
state_local_variables.add(`const ${local} = ${expression}`);
|
|
493
|
-
return local;
|
|
494
|
-
}
|
|
495
|
-
function GetLocals() {
|
|
496
|
-
return [...state_local_variables.values()];
|
|
497
|
-
}
|
|
498
|
-
// -------------------------------------------------------------------
|
|
499
|
-
// Compile
|
|
500
|
-
// -------------------------------------------------------------------
|
|
501
|
-
function Build(schema) {
|
|
502
|
-
ResetCompiler();
|
|
503
|
-
const check = CreateFunction('check', schema, 'value');
|
|
504
|
-
const locals = GetLocals();
|
|
505
|
-
return `${locals.join('\n')}\nreturn ${check}`;
|
|
506
|
-
}
|
|
507
|
-
/** Returns the generated assertion code used to validate this type. */
|
|
508
|
-
function Code(schema) {
|
|
509
|
-
if (!Types.TypeGuard.TSchema(schema))
|
|
510
|
-
throw new TypeCompilerPreflightCheckError(schema);
|
|
511
|
-
return Build(schema);
|
|
512
|
-
}
|
|
513
|
-
TypeCompiler.Code = Code;
|
|
514
|
-
/** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
|
|
515
|
-
function Compile(schema) {
|
|
516
|
-
const code = Code(schema);
|
|
517
|
-
const custom_schemas = new Map(state_remote_custom_types);
|
|
518
|
-
const compiledFunction = globalThis.Function('custom', 'format', 'hash', code);
|
|
519
|
-
const checkFunction = compiledFunction((kind, schema_key, value) => {
|
|
520
|
-
if (!Types.TypeRegistry.Has(kind) || !custom_schemas.has(schema_key))
|
|
521
|
-
return false;
|
|
522
|
-
const schema = custom_schemas.get(schema_key);
|
|
523
|
-
const func = Types.TypeRegistry.Get(kind);
|
|
524
|
-
return func(schema, value);
|
|
525
|
-
}, (format, value) => {
|
|
526
|
-
if (!Types.FormatRegistry.Has(format))
|
|
527
|
-
return false;
|
|
528
|
-
const func = Types.FormatRegistry.Get(format);
|
|
529
|
-
return func(value);
|
|
530
|
-
}, (value) => {
|
|
531
|
-
return hash_1.ValueHash.Create(value);
|
|
532
|
-
});
|
|
533
|
-
return new TypeCheck(schema, checkFunction, code);
|
|
534
|
-
}
|
|
535
|
-
TypeCompiler.Compile = Compile;
|
|
536
|
-
})(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/compiler
|
|
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.TypeCompiler = exports.TypeCompilerPreflightCheckError = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
const index_1 = require("../errors/index");
|
|
33
|
+
const index_2 = require("../system/index");
|
|
34
|
+
const hash_1 = require("../value/hash");
|
|
35
|
+
// -------------------------------------------------------------------
|
|
36
|
+
// TypeCheck
|
|
37
|
+
// -------------------------------------------------------------------
|
|
38
|
+
class TypeCheck {
|
|
39
|
+
constructor(schema, checkFunc, code) {
|
|
40
|
+
this.schema = schema;
|
|
41
|
+
this.checkFunc = checkFunc;
|
|
42
|
+
this.code = code;
|
|
43
|
+
}
|
|
44
|
+
/** Returns the generated assertion code used to validate this type. */
|
|
45
|
+
Code() {
|
|
46
|
+
return this.code;
|
|
47
|
+
}
|
|
48
|
+
/** Returns an iterator for each error in this value. */
|
|
49
|
+
Errors(value) {
|
|
50
|
+
return index_1.ValueErrors.Errors(this.schema, value);
|
|
51
|
+
}
|
|
52
|
+
/** Returns true if the value matches the compiled type. */
|
|
53
|
+
Check(value) {
|
|
54
|
+
return this.checkFunc(value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.TypeCheck = TypeCheck;
|
|
58
|
+
// -------------------------------------------------------------------
|
|
59
|
+
// Character
|
|
60
|
+
// -------------------------------------------------------------------
|
|
61
|
+
var Character;
|
|
62
|
+
(function (Character) {
|
|
63
|
+
function DollarSign(code) {
|
|
64
|
+
return code === 36;
|
|
65
|
+
}
|
|
66
|
+
Character.DollarSign = DollarSign;
|
|
67
|
+
function Underscore(code) {
|
|
68
|
+
return code === 95;
|
|
69
|
+
}
|
|
70
|
+
Character.Underscore = Underscore;
|
|
71
|
+
function Numeric(code) {
|
|
72
|
+
return code >= 48 && code <= 57;
|
|
73
|
+
}
|
|
74
|
+
Character.Numeric = Numeric;
|
|
75
|
+
function Alpha(code) {
|
|
76
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
77
|
+
}
|
|
78
|
+
Character.Alpha = Alpha;
|
|
79
|
+
})(Character || (Character = {}));
|
|
80
|
+
// -------------------------------------------------------------------
|
|
81
|
+
// Identifier
|
|
82
|
+
// -------------------------------------------------------------------
|
|
83
|
+
var Identifier;
|
|
84
|
+
(function (Identifier) {
|
|
85
|
+
function Encode($id) {
|
|
86
|
+
const buffer = [];
|
|
87
|
+
for (let i = 0; i < $id.length; i++) {
|
|
88
|
+
const code = $id.charCodeAt(i);
|
|
89
|
+
if (Character.Numeric(code) || Character.Alpha(code)) {
|
|
90
|
+
buffer.push($id.charAt(i));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
buffer.push(`_${code}_`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return buffer.join('').replace(/__/g, '_');
|
|
97
|
+
}
|
|
98
|
+
Identifier.Encode = Encode;
|
|
99
|
+
})(Identifier || (Identifier = {}));
|
|
100
|
+
// -------------------------------------------------------------------
|
|
101
|
+
// MemberExpression
|
|
102
|
+
// -------------------------------------------------------------------
|
|
103
|
+
var MemberExpression;
|
|
104
|
+
(function (MemberExpression) {
|
|
105
|
+
function Check(propertyName) {
|
|
106
|
+
if (propertyName.length === 0)
|
|
107
|
+
return false;
|
|
108
|
+
{
|
|
109
|
+
const code = propertyName.charCodeAt(0);
|
|
110
|
+
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (let i = 1; i < propertyName.length; i++) {
|
|
115
|
+
const code = propertyName.charCodeAt(i);
|
|
116
|
+
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
function Encode(object, key) {
|
|
123
|
+
return !Check(key) ? `${object}['${key}']` : `${object}.${key}`;
|
|
124
|
+
}
|
|
125
|
+
MemberExpression.Encode = Encode;
|
|
126
|
+
})(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {}));
|
|
127
|
+
// -------------------------------------------------------------------
|
|
128
|
+
// TypeCompiler
|
|
129
|
+
// -------------------------------------------------------------------
|
|
130
|
+
class TypeCompilerUnknownTypeError extends Error {
|
|
131
|
+
constructor(schema) {
|
|
132
|
+
super('TypeCompiler: Unknown type');
|
|
133
|
+
this.schema = schema;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
137
|
+
class TypeCompilerPreflightCheckError extends Error {
|
|
138
|
+
constructor(schema) {
|
|
139
|
+
super('TypeCompiler: Preflight validation check failed for given schema');
|
|
140
|
+
this.schema = schema;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.TypeCompilerPreflightCheckError = TypeCompilerPreflightCheckError;
|
|
144
|
+
/** Compiles Types for Runtime Type Checking */
|
|
145
|
+
var TypeCompiler;
|
|
146
|
+
(function (TypeCompiler) {
|
|
147
|
+
// -------------------------------------------------------------------
|
|
148
|
+
// Guards
|
|
149
|
+
// -------------------------------------------------------------------
|
|
150
|
+
function IsBigInt(value) {
|
|
151
|
+
return typeof value === 'bigint';
|
|
152
|
+
}
|
|
153
|
+
function IsNumber(value) {
|
|
154
|
+
return typeof value === 'number' && globalThis.Number.isFinite(value);
|
|
155
|
+
}
|
|
156
|
+
// -------------------------------------------------------------------
|
|
157
|
+
// Types
|
|
158
|
+
// -------------------------------------------------------------------
|
|
159
|
+
function* Any(schema, value) {
|
|
160
|
+
yield 'true';
|
|
161
|
+
}
|
|
162
|
+
function* Array(schema, value) {
|
|
163
|
+
const expression = CreateExpression(schema.items, 'value');
|
|
164
|
+
yield `Array.isArray(${value}) && ${value}.every(value => ${expression})`;
|
|
165
|
+
if (IsNumber(schema.minItems))
|
|
166
|
+
yield `${value}.length >= ${schema.minItems}`;
|
|
167
|
+
if (IsNumber(schema.maxItems))
|
|
168
|
+
yield `${value}.length <= ${schema.maxItems}`;
|
|
169
|
+
if (schema.uniqueItems === true)
|
|
170
|
+
yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`;
|
|
171
|
+
}
|
|
172
|
+
function* BigInt(schema, value) {
|
|
173
|
+
yield `(typeof ${value} === 'bigint')`;
|
|
174
|
+
if (IsBigInt(schema.multipleOf))
|
|
175
|
+
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
176
|
+
if (IsBigInt(schema.exclusiveMinimum))
|
|
177
|
+
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
178
|
+
if (IsBigInt(schema.exclusiveMaximum))
|
|
179
|
+
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
180
|
+
if (IsBigInt(schema.minimum))
|
|
181
|
+
yield `${value} >= BigInt(${schema.minimum})`;
|
|
182
|
+
if (IsBigInt(schema.maximum))
|
|
183
|
+
yield `${value} <= BigInt(${schema.maximum})`;
|
|
184
|
+
}
|
|
185
|
+
function* Boolean(schema, value) {
|
|
186
|
+
yield `typeof ${value} === 'boolean'`;
|
|
187
|
+
}
|
|
188
|
+
function* Constructor(schema, value) {
|
|
189
|
+
yield* Visit(schema.returns, `${value}.prototype`);
|
|
190
|
+
}
|
|
191
|
+
function* Date(schema, value) {
|
|
192
|
+
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
193
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp))
|
|
194
|
+
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
195
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp))
|
|
196
|
+
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
197
|
+
if (IsNumber(schema.minimumTimestamp))
|
|
198
|
+
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
199
|
+
if (IsNumber(schema.maximumTimestamp))
|
|
200
|
+
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
201
|
+
}
|
|
202
|
+
function* Function(schema, value) {
|
|
203
|
+
yield `typeof ${value} === 'function'`;
|
|
204
|
+
}
|
|
205
|
+
function* Integer(schema, value) {
|
|
206
|
+
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
207
|
+
if (IsNumber(schema.multipleOf))
|
|
208
|
+
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
209
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
210
|
+
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
211
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
212
|
+
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
213
|
+
if (IsNumber(schema.minimum))
|
|
214
|
+
yield `${value} >= ${schema.minimum}`;
|
|
215
|
+
if (IsNumber(schema.maximum))
|
|
216
|
+
yield `${value} <= ${schema.maximum}`;
|
|
217
|
+
}
|
|
218
|
+
function* Intersect(schema, value) {
|
|
219
|
+
if (schema.unevaluatedProperties === undefined) {
|
|
220
|
+
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
221
|
+
yield `${expressions.join(' && ')}`;
|
|
222
|
+
}
|
|
223
|
+
else if (schema.unevaluatedProperties === false) {
|
|
224
|
+
// prettier-ignore
|
|
225
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
|
|
226
|
+
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
227
|
+
const expression1 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key))`;
|
|
228
|
+
yield `${expressions.join(' && ')} && ${expression1}`;
|
|
229
|
+
}
|
|
230
|
+
else if (typeof schema.unevaluatedProperties === 'object') {
|
|
231
|
+
// prettier-ignore
|
|
232
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
|
|
233
|
+
const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
|
|
234
|
+
const expression1 = CreateExpression(schema.unevaluatedProperties, 'value[key]');
|
|
235
|
+
const expression2 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key) || ${expression1})`;
|
|
236
|
+
yield `${expressions.join(' && ')} && ${expression2}`;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function* Literal(schema, value) {
|
|
240
|
+
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
241
|
+
yield `${value} === ${schema.const}`;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
yield `${value} === '${schema.const}'`;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function* Never(schema, value) {
|
|
248
|
+
yield `false`;
|
|
249
|
+
}
|
|
250
|
+
function* Not(schema, value) {
|
|
251
|
+
const left = CreateExpression(schema.allOf[0].not, value);
|
|
252
|
+
const right = CreateExpression(schema.allOf[1], value);
|
|
253
|
+
yield `!${left} && ${right}`;
|
|
254
|
+
}
|
|
255
|
+
function* Null(schema, value) {
|
|
256
|
+
yield `${value} === null`;
|
|
257
|
+
}
|
|
258
|
+
function* Number(schema, value) {
|
|
259
|
+
yield `typeof ${value} === 'number'`;
|
|
260
|
+
if (!index_2.TypeSystem.AllowNaN)
|
|
261
|
+
yield `Number.isFinite(${value})`;
|
|
262
|
+
if (IsNumber(schema.multipleOf))
|
|
263
|
+
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
264
|
+
if (IsNumber(schema.exclusiveMinimum))
|
|
265
|
+
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
266
|
+
if (IsNumber(schema.exclusiveMaximum))
|
|
267
|
+
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
268
|
+
if (IsNumber(schema.minimum))
|
|
269
|
+
yield `${value} >= ${schema.minimum}`;
|
|
270
|
+
if (IsNumber(schema.maximum))
|
|
271
|
+
yield `${value} <= ${schema.maximum}`;
|
|
272
|
+
}
|
|
273
|
+
function* Object(schema, value) {
|
|
274
|
+
yield `(typeof ${value} === 'object' && ${value} !== null)`;
|
|
275
|
+
if (!index_2.TypeSystem.AllowArrayObjects)
|
|
276
|
+
yield `!Array.isArray(${value})`;
|
|
277
|
+
if (IsNumber(schema.minProperties))
|
|
278
|
+
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
279
|
+
if (IsNumber(schema.maxProperties))
|
|
280
|
+
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
281
|
+
const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
282
|
+
for (const schemaKey of schemaKeys) {
|
|
283
|
+
const memberExpression = MemberExpression.Encode(value, schemaKey);
|
|
284
|
+
const property = schema.properties[schemaKey];
|
|
285
|
+
if (schema.required && schema.required.includes(schemaKey)) {
|
|
286
|
+
yield* Visit(property, memberExpression);
|
|
287
|
+
if (Types.ExtendsUndefined.Check(property))
|
|
288
|
+
yield `('${schemaKey}' in ${value})`;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const expression = CreateExpression(property, memberExpression);
|
|
292
|
+
yield `('${schemaKey}' in ${value} ? ${expression} : true)`;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (schema.additionalProperties === false) {
|
|
296
|
+
if (schema.required && schema.required.length === schemaKeys.length) {
|
|
297
|
+
yield `Object.getOwnPropertyNames(${value}).length === ${schemaKeys.length}`;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
301
|
+
yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
305
|
+
const expression = CreateExpression(schema.additionalProperties, 'value[key]');
|
|
306
|
+
const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
307
|
+
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
function* Promise(schema, value) {
|
|
311
|
+
yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
|
|
312
|
+
}
|
|
313
|
+
function* Record(schema, value) {
|
|
314
|
+
yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date))`;
|
|
315
|
+
if (!index_2.TypeSystem.AllowArrayObjects)
|
|
316
|
+
yield `!Array.isArray(${value})`;
|
|
317
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
318
|
+
const local = PushLocal(`new RegExp(/${keyPattern}/)`);
|
|
319
|
+
yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
|
|
320
|
+
const expression = CreateExpression(valueSchema, 'value');
|
|
321
|
+
yield `Object.values(${value}).every(value => ${expression})`;
|
|
322
|
+
}
|
|
323
|
+
function* Ref(schema, value) {
|
|
324
|
+
// Reference: If we have seen this reference before we can just yield and return
|
|
325
|
+
// the function call. If this isn't the case we defer to visit to generate and
|
|
326
|
+
// set the function for subsequent passes. Consider for refactor.
|
|
327
|
+
if (state_local_function_names.has(schema.$ref))
|
|
328
|
+
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
329
|
+
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), value);
|
|
330
|
+
}
|
|
331
|
+
function* Self(schema, value) {
|
|
332
|
+
const func = CreateFunctionName(schema.$ref);
|
|
333
|
+
yield `${func}(${value})`;
|
|
334
|
+
}
|
|
335
|
+
function* String(schema, value) {
|
|
336
|
+
yield `(typeof ${value} === 'string')`;
|
|
337
|
+
if (IsNumber(schema.minLength))
|
|
338
|
+
yield `${value}.length >= ${schema.minLength}`;
|
|
339
|
+
if (IsNumber(schema.maxLength))
|
|
340
|
+
yield `${value}.length <= ${schema.maxLength}`;
|
|
341
|
+
if (schema.pattern !== undefined) {
|
|
342
|
+
const local = PushLocal(`${new RegExp(schema.pattern)};`);
|
|
343
|
+
yield `${local}.test(${value})`;
|
|
344
|
+
}
|
|
345
|
+
if (schema.format !== undefined) {
|
|
346
|
+
yield `format('${schema.format}', ${value})`;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function* Symbol(schema, value) {
|
|
350
|
+
yield `(typeof ${value} === 'symbol')`;
|
|
351
|
+
}
|
|
352
|
+
function* Tuple(schema, value) {
|
|
353
|
+
yield `(Array.isArray(${value}))`;
|
|
354
|
+
if (schema.items === undefined)
|
|
355
|
+
return yield `${value}.length === 0`;
|
|
356
|
+
yield `(${value}.length === ${schema.maxItems})`;
|
|
357
|
+
for (let i = 0; i < schema.items.length; i++) {
|
|
358
|
+
const expression = CreateExpression(schema.items[i], `${value}[${i}]`);
|
|
359
|
+
yield `${expression}`;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function* Undefined(schema, value) {
|
|
363
|
+
yield `${value} === undefined`;
|
|
364
|
+
}
|
|
365
|
+
function* Union(schema, value) {
|
|
366
|
+
const expressions = schema.anyOf.map((schema) => CreateExpression(schema, value));
|
|
367
|
+
yield `(${expressions.join(' || ')})`;
|
|
368
|
+
}
|
|
369
|
+
function* Uint8Array(schema, value) {
|
|
370
|
+
yield `${value} instanceof Uint8Array`;
|
|
371
|
+
if (IsNumber(schema.maxByteLength))
|
|
372
|
+
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
373
|
+
if (IsNumber(schema.minByteLength))
|
|
374
|
+
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
375
|
+
}
|
|
376
|
+
function* Unknown(schema, value) {
|
|
377
|
+
yield 'true';
|
|
378
|
+
}
|
|
379
|
+
function* Void(schema, value) {
|
|
380
|
+
if (index_2.TypeSystem.AllowVoidNull) {
|
|
381
|
+
yield `(${value} === undefined || ${value} === null)`;
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
yield `${value} === undefined`;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
function* UserDefined(schema, value) {
|
|
388
|
+
const schema_key = `schema_key_${state_remote_custom_types.size}`;
|
|
389
|
+
state_remote_custom_types.set(schema_key, schema);
|
|
390
|
+
yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`;
|
|
391
|
+
}
|
|
392
|
+
function* Visit(schema, value) {
|
|
393
|
+
// Reference: Referenced schemas can originate from either additional schemas
|
|
394
|
+
// or inline in the schema itself. Ideally the recursive path should align to
|
|
395
|
+
// reference path. Consider for refactor.
|
|
396
|
+
if (schema.$id && !state_local_function_names.has(schema.$id)) {
|
|
397
|
+
state_local_function_names.add(schema.$id);
|
|
398
|
+
const name = CreateFunctionName(schema.$id);
|
|
399
|
+
const body = CreateFunction(name, schema, 'value');
|
|
400
|
+
PushFunction(body);
|
|
401
|
+
yield `${name}(${value})`;
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
const anySchema = schema;
|
|
405
|
+
switch (anySchema[Types.Kind]) {
|
|
406
|
+
case 'Any':
|
|
407
|
+
return yield* Any(anySchema, value);
|
|
408
|
+
case 'Array':
|
|
409
|
+
return yield* Array(anySchema, value);
|
|
410
|
+
case 'BigInt':
|
|
411
|
+
return yield* BigInt(anySchema, value);
|
|
412
|
+
case 'Boolean':
|
|
413
|
+
return yield* Boolean(anySchema, value);
|
|
414
|
+
case 'Constructor':
|
|
415
|
+
return yield* Constructor(anySchema, value);
|
|
416
|
+
case 'Date':
|
|
417
|
+
return yield* Date(anySchema, value);
|
|
418
|
+
case 'Function':
|
|
419
|
+
return yield* Function(anySchema, value);
|
|
420
|
+
case 'Integer':
|
|
421
|
+
return yield* Integer(anySchema, value);
|
|
422
|
+
case 'Intersect':
|
|
423
|
+
return yield* Intersect(anySchema, value);
|
|
424
|
+
case 'Literal':
|
|
425
|
+
return yield* Literal(anySchema, value);
|
|
426
|
+
case 'Never':
|
|
427
|
+
return yield* Never(anySchema, value);
|
|
428
|
+
case 'Not':
|
|
429
|
+
return yield* Not(anySchema, value);
|
|
430
|
+
case 'Null':
|
|
431
|
+
return yield* Null(anySchema, value);
|
|
432
|
+
case 'Number':
|
|
433
|
+
return yield* Number(anySchema, value);
|
|
434
|
+
case 'Object':
|
|
435
|
+
return yield* Object(anySchema, value);
|
|
436
|
+
case 'Promise':
|
|
437
|
+
return yield* Promise(anySchema, value);
|
|
438
|
+
case 'Record':
|
|
439
|
+
return yield* Record(anySchema, value);
|
|
440
|
+
case 'Ref':
|
|
441
|
+
return yield* Ref(anySchema, value);
|
|
442
|
+
case 'Self':
|
|
443
|
+
return yield* Self(anySchema, value);
|
|
444
|
+
case 'String':
|
|
445
|
+
return yield* String(anySchema, value);
|
|
446
|
+
case 'Symbol':
|
|
447
|
+
return yield* Symbol(anySchema, value);
|
|
448
|
+
case 'Tuple':
|
|
449
|
+
return yield* Tuple(anySchema, value);
|
|
450
|
+
case 'Undefined':
|
|
451
|
+
return yield* Undefined(anySchema, value);
|
|
452
|
+
case 'Union':
|
|
453
|
+
return yield* Union(anySchema, value);
|
|
454
|
+
case 'Uint8Array':
|
|
455
|
+
return yield* Uint8Array(anySchema, value);
|
|
456
|
+
case 'Unknown':
|
|
457
|
+
return yield* Unknown(anySchema, value);
|
|
458
|
+
case 'Void':
|
|
459
|
+
return yield* Void(anySchema, value);
|
|
460
|
+
default:
|
|
461
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
462
|
+
throw new TypeCompilerUnknownTypeError(schema);
|
|
463
|
+
return yield* UserDefined(anySchema, value);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
// -------------------------------------------------------------------
|
|
467
|
+
// Compiler State
|
|
468
|
+
// -------------------------------------------------------------------
|
|
469
|
+
const state_local_variables = new Set(); // local variables and functions
|
|
470
|
+
const state_local_function_names = new Set(); // local function names used call ref validators
|
|
471
|
+
const state_remote_custom_types = new Map(); // remote custom types used during compilation
|
|
472
|
+
function ResetCompiler() {
|
|
473
|
+
state_local_variables.clear();
|
|
474
|
+
state_local_function_names.clear();
|
|
475
|
+
state_remote_custom_types.clear();
|
|
476
|
+
}
|
|
477
|
+
function CreateExpression(schema, value) {
|
|
478
|
+
return `(${[...Visit(schema, value)].join(' && ')})`;
|
|
479
|
+
}
|
|
480
|
+
function CreateFunctionName($id) {
|
|
481
|
+
return `check_${Identifier.Encode($id)}`;
|
|
482
|
+
}
|
|
483
|
+
function CreateFunction(name, schema, value) {
|
|
484
|
+
const expression = [...Visit(schema, value)].map((condition) => ` ${condition}`).join(' &&\n');
|
|
485
|
+
return `function ${name}(value) {\n return (\n${expression}\n )\n}`;
|
|
486
|
+
}
|
|
487
|
+
function PushFunction(functionBody) {
|
|
488
|
+
state_local_variables.add(functionBody);
|
|
489
|
+
}
|
|
490
|
+
function PushLocal(expression) {
|
|
491
|
+
const local = `local_${state_local_variables.size}`;
|
|
492
|
+
state_local_variables.add(`const ${local} = ${expression}`);
|
|
493
|
+
return local;
|
|
494
|
+
}
|
|
495
|
+
function GetLocals() {
|
|
496
|
+
return [...state_local_variables.values()];
|
|
497
|
+
}
|
|
498
|
+
// -------------------------------------------------------------------
|
|
499
|
+
// Compile
|
|
500
|
+
// -------------------------------------------------------------------
|
|
501
|
+
function Build(schema) {
|
|
502
|
+
ResetCompiler();
|
|
503
|
+
const check = CreateFunction('check', schema, 'value');
|
|
504
|
+
const locals = GetLocals();
|
|
505
|
+
return `${locals.join('\n')}\nreturn ${check}`;
|
|
506
|
+
}
|
|
507
|
+
/** Returns the generated assertion code used to validate this type. */
|
|
508
|
+
function Code(schema) {
|
|
509
|
+
if (!Types.TypeGuard.TSchema(schema))
|
|
510
|
+
throw new TypeCompilerPreflightCheckError(schema);
|
|
511
|
+
return Build(schema);
|
|
512
|
+
}
|
|
513
|
+
TypeCompiler.Code = Code;
|
|
514
|
+
/** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
|
|
515
|
+
function Compile(schema) {
|
|
516
|
+
const code = Code(schema);
|
|
517
|
+
const custom_schemas = new Map(state_remote_custom_types);
|
|
518
|
+
const compiledFunction = globalThis.Function('custom', 'format', 'hash', code);
|
|
519
|
+
const checkFunction = compiledFunction((kind, schema_key, value) => {
|
|
520
|
+
if (!Types.TypeRegistry.Has(kind) || !custom_schemas.has(schema_key))
|
|
521
|
+
return false;
|
|
522
|
+
const schema = custom_schemas.get(schema_key);
|
|
523
|
+
const func = Types.TypeRegistry.Get(kind);
|
|
524
|
+
return func(schema, value);
|
|
525
|
+
}, (format, value) => {
|
|
526
|
+
if (!Types.FormatRegistry.Has(format))
|
|
527
|
+
return false;
|
|
528
|
+
const func = Types.FormatRegistry.Get(format);
|
|
529
|
+
return func(value);
|
|
530
|
+
}, (value) => {
|
|
531
|
+
return hash_1.ValueHash.Create(value);
|
|
532
|
+
});
|
|
533
|
+
return new TypeCheck(schema, checkFunction, code);
|
|
534
|
+
}
|
|
535
|
+
TypeCompiler.Compile = Compile;
|
|
536
|
+
})(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
|