@sinclair/typebox 0.28.17 → 0.28.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/compiler/compiler.d.ts +1 -1
- package/compiler/compiler.js +15 -10
- package/package.json +1 -1
- package/value/create.d.ts +6 -0
- package/value/create.js +28 -11
package/compiler/compiler.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare class TypeCompilerTypeGuardError extends Error {
|
|
|
27
27
|
constructor(schema: Types.TSchema);
|
|
28
28
|
}
|
|
29
29
|
export interface TypeCompilerOptions {
|
|
30
|
-
language
|
|
30
|
+
language?: 'typescript' | 'javascript';
|
|
31
31
|
}
|
|
32
32
|
/** Compiles Types for Runtime Type Checking */
|
|
33
33
|
export declare namespace TypeCompiler {
|
package/compiler/compiler.js
CHANGED
|
@@ -207,7 +207,7 @@ var TypeCompiler;
|
|
|
207
207
|
if (schema.uniqueItems === true)
|
|
208
208
|
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 })())`;
|
|
209
209
|
const expression = CreateExpression(schema.items, references, 'value');
|
|
210
|
-
const parameter = CreateParameter('value');
|
|
210
|
+
const parameter = CreateParameter('value', 'any');
|
|
211
211
|
yield `${value}.every((${parameter}) => ${expression})`;
|
|
212
212
|
}
|
|
213
213
|
function* BigInt(schema, references, value) {
|
|
@@ -521,20 +521,24 @@ var TypeCompiler;
|
|
|
521
521
|
functions: new Set(),
|
|
522
522
|
customs: new Map(), // custom type data
|
|
523
523
|
};
|
|
524
|
-
function CreateExpression(schema, references, value) {
|
|
525
|
-
return `(${[...Visit(schema, references, value)].join(' && ')})`;
|
|
526
|
-
}
|
|
527
524
|
function CreateFunctionName($id) {
|
|
528
525
|
return `check_${Identifier.Encode($id)}`;
|
|
529
526
|
}
|
|
530
|
-
function
|
|
531
|
-
|
|
527
|
+
function CreateExpression(schema, references, value) {
|
|
528
|
+
return `(${[...Visit(schema, references, value)].join(' && ')})`;
|
|
529
|
+
}
|
|
530
|
+
function CreateParameter(name, type) {
|
|
531
|
+
const annotation = state.language === 'typescript' ? `: ${type}` : '';
|
|
532
532
|
return `${name}${annotation}`;
|
|
533
533
|
}
|
|
534
|
+
function CreateReturns(type) {
|
|
535
|
+
return state.language === 'typescript' ? `: ${type}` : '';
|
|
536
|
+
}
|
|
534
537
|
function CreateFunction(name, schema, references, value) {
|
|
535
538
|
const expression = [...Visit(schema, references, value, true)].map((condition) => ` ${condition}`).join(' &&\n');
|
|
536
|
-
const parameter = CreateParameter('value');
|
|
537
|
-
|
|
539
|
+
const parameter = CreateParameter('value', 'any');
|
|
540
|
+
const returns = CreateReturns('boolean');
|
|
541
|
+
return `function ${name}(${parameter})${returns} {\n return (\n${expression}\n )\n}`;
|
|
538
542
|
}
|
|
539
543
|
function PushFunction(functionBody) {
|
|
540
544
|
state.variables.add(functionBody);
|
|
@@ -553,10 +557,11 @@ var TypeCompiler;
|
|
|
553
557
|
function Build(schema, references) {
|
|
554
558
|
const check = CreateFunction('check', schema, references, 'value'); // interior visit
|
|
555
559
|
const locals = GetLocals();
|
|
556
|
-
const parameter = CreateParameter('value');
|
|
560
|
+
const parameter = CreateParameter('value', 'any');
|
|
561
|
+
const returns = CreateReturns('boolean');
|
|
557
562
|
// prettier-ignore
|
|
558
563
|
return IsString(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
559
|
-
? `${locals.join('\n')}\nreturn function check(${parameter}) {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
564
|
+
? `${locals.join('\n')}\nreturn function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
560
565
|
: `${locals.join('\n')}\nreturn ${check}`;
|
|
561
566
|
}
|
|
562
567
|
/** Returns the generated assertion code used to validate this type. */
|
package/package.json
CHANGED
package/value/create.d.ts
CHANGED
|
@@ -19,8 +19,14 @@ export declare class ValueCreateDereferenceError extends Error {
|
|
|
19
19
|
readonly schema: Types.TRef | Types.TThis;
|
|
20
20
|
constructor(schema: Types.TRef | Types.TThis);
|
|
21
21
|
}
|
|
22
|
+
export declare class ValueCreateRecursiveInstantiationError extends Error {
|
|
23
|
+
readonly schema: Types.TSchema;
|
|
24
|
+
readonly recursiveMaxDepth: number;
|
|
25
|
+
constructor(schema: Types.TSchema, recursiveMaxDepth: number);
|
|
26
|
+
}
|
|
22
27
|
export declare namespace ValueCreate {
|
|
23
28
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
24
29
|
function Visit(schema: Types.TSchema, references: Types.TSchema[]): unknown;
|
|
30
|
+
/** Creates a value from the given schema and references */
|
|
25
31
|
function Create<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
|
26
32
|
}
|
package/value/create.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCreate = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
30
|
+
exports.ValueCreate = exports.ValueCreateRecursiveInstantiationError = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const check_1 = require("./check");
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
@@ -68,6 +68,14 @@ class ValueCreateDereferenceError extends Error {
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
exports.ValueCreateDereferenceError = ValueCreateDereferenceError;
|
|
71
|
+
class ValueCreateRecursiveInstantiationError extends Error {
|
|
72
|
+
constructor(schema, recursiveMaxDepth) {
|
|
73
|
+
super('ValueCreate: Value cannot be created as recursive type may produce value of infinite size. Consider using a default.');
|
|
74
|
+
this.schema = schema;
|
|
75
|
+
this.recursiveMaxDepth = recursiveMaxDepth;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.ValueCreateRecursiveInstantiationError = ValueCreateRecursiveInstantiationError;
|
|
71
79
|
// --------------------------------------------------------------------------
|
|
72
80
|
// ValueCreate
|
|
73
81
|
// --------------------------------------------------------------------------
|
|
@@ -99,7 +107,7 @@ var ValueCreate;
|
|
|
99
107
|
}
|
|
100
108
|
else if (schema.minItems !== undefined) {
|
|
101
109
|
return globalThis.Array.from({ length: schema.minItems }).map((item) => {
|
|
102
|
-
return
|
|
110
|
+
return Visit(schema.items, references);
|
|
103
111
|
});
|
|
104
112
|
}
|
|
105
113
|
else {
|
|
@@ -127,7 +135,7 @@ var ValueCreate;
|
|
|
127
135
|
return schema.default;
|
|
128
136
|
}
|
|
129
137
|
else {
|
|
130
|
-
const value =
|
|
138
|
+
const value = Visit(schema.returns, references);
|
|
131
139
|
if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
|
|
132
140
|
return class {
|
|
133
141
|
constructor() {
|
|
@@ -160,7 +168,7 @@ var ValueCreate;
|
|
|
160
168
|
return schema.default;
|
|
161
169
|
}
|
|
162
170
|
else {
|
|
163
|
-
return () =>
|
|
171
|
+
return () => Visit(schema.returns, references);
|
|
164
172
|
}
|
|
165
173
|
}
|
|
166
174
|
function Integer(schema, references) {
|
|
@@ -237,7 +245,7 @@ var ValueCreate;
|
|
|
237
245
|
const required = new Set(schema.required);
|
|
238
246
|
return (schema.default ||
|
|
239
247
|
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
240
|
-
return required.has(key) ? { ...acc, [key]:
|
|
248
|
+
return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
|
|
241
249
|
}, {}));
|
|
242
250
|
}
|
|
243
251
|
}
|
|
@@ -246,7 +254,7 @@ var ValueCreate;
|
|
|
246
254
|
return schema.default;
|
|
247
255
|
}
|
|
248
256
|
else {
|
|
249
|
-
return globalThis.Promise.resolve(
|
|
257
|
+
return globalThis.Promise.resolve(Visit(schema.item, references));
|
|
250
258
|
}
|
|
251
259
|
}
|
|
252
260
|
function Record(schema, references) {
|
|
@@ -257,7 +265,7 @@ var ValueCreate;
|
|
|
257
265
|
else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) {
|
|
258
266
|
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
259
267
|
return propertyKeys.reduce((acc, key) => {
|
|
260
|
-
return { ...acc, [key]:
|
|
268
|
+
return { ...acc, [key]: Visit(valueSchema, references) };
|
|
261
269
|
}, {});
|
|
262
270
|
}
|
|
263
271
|
else {
|
|
@@ -269,7 +277,7 @@ var ValueCreate;
|
|
|
269
277
|
return schema.default;
|
|
270
278
|
}
|
|
271
279
|
else {
|
|
272
|
-
const index = references.findIndex((foreign) => foreign.$id === schema.$
|
|
280
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
273
281
|
if (index === -1)
|
|
274
282
|
throw new ValueCreateDereferenceError(schema);
|
|
275
283
|
const target = references[index];
|
|
@@ -329,11 +337,13 @@ var ValueCreate;
|
|
|
329
337
|
return sequence.next().value;
|
|
330
338
|
}
|
|
331
339
|
function This(schema, references) {
|
|
340
|
+
if (recursiveDepth++ > recursiveMaxDepth)
|
|
341
|
+
throw new ValueCreateRecursiveInstantiationError(schema, recursiveMaxDepth);
|
|
332
342
|
if ('default' in schema) {
|
|
333
343
|
return schema.default;
|
|
334
344
|
}
|
|
335
345
|
else {
|
|
336
|
-
const index = references.findIndex((foreign) => foreign.$id === schema.$
|
|
346
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
337
347
|
if (index === -1)
|
|
338
348
|
throw new ValueCreateDereferenceError(schema);
|
|
339
349
|
const target = references[index];
|
|
@@ -348,7 +358,7 @@ var ValueCreate;
|
|
|
348
358
|
return [];
|
|
349
359
|
}
|
|
350
360
|
else {
|
|
351
|
-
return globalThis.Array.from({ length: schema.minItems }).map((_, index) =>
|
|
361
|
+
return globalThis.Array.from({ length: schema.minItems }).map((_, index) => Visit(schema.items[index], references));
|
|
352
362
|
}
|
|
353
363
|
}
|
|
354
364
|
function Undefined(schema, references) {
|
|
@@ -367,7 +377,7 @@ var ValueCreate;
|
|
|
367
377
|
throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
|
|
368
378
|
}
|
|
369
379
|
else {
|
|
370
|
-
return
|
|
380
|
+
return Visit(schema.anyOf[0], references);
|
|
371
381
|
}
|
|
372
382
|
}
|
|
373
383
|
function Uint8Array(schema, references) {
|
|
@@ -473,7 +483,14 @@ var ValueCreate;
|
|
|
473
483
|
}
|
|
474
484
|
}
|
|
475
485
|
ValueCreate.Visit = Visit;
|
|
486
|
+
// --------------------------------------------------------
|
|
487
|
+
// State
|
|
488
|
+
// --------------------------------------------------------
|
|
489
|
+
const recursiveMaxDepth = 512;
|
|
490
|
+
let recursiveDepth = 0;
|
|
491
|
+
/** Creates a value from the given schema and references */
|
|
476
492
|
function Create(schema, references) {
|
|
493
|
+
recursiveDepth = 0;
|
|
477
494
|
return Visit(schema, references);
|
|
478
495
|
}
|
|
479
496
|
ValueCreate.Create = Create;
|