@sinclair/typebox 0.28.18 → 0.28.20
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/errors/errors.d.ts +1 -1
- package/errors/errors.js +3 -3
- package/package.json +1 -1
- package/value/create.d.ts +6 -0
- package/value/create.js +28 -11
package/errors/errors.d.ts
CHANGED
package/errors/errors.js
CHANGED
|
@@ -69,7 +69,7 @@ var ValueErrorType;
|
|
|
69
69
|
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 30] = "NumberMultipleOf";
|
|
70
70
|
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 31] = "NumberExclusiveMinimum";
|
|
71
71
|
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 32] = "NumberExclusiveMaximum";
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
+
ValueErrorType[ValueErrorType["NumberMinimum"] = 33] = "NumberMinimum";
|
|
73
73
|
ValueErrorType[ValueErrorType["NumberMaximum"] = 34] = "NumberMaximum";
|
|
74
74
|
ValueErrorType[ValueErrorType["Object"] = 35] = "Object";
|
|
75
75
|
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 36] = "ObjectMinProperties";
|
|
@@ -337,10 +337,10 @@ var ValueErrors;
|
|
|
337
337
|
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
338
338
|
}
|
|
339
339
|
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
340
|
-
yield { type: ValueErrorType.
|
|
340
|
+
yield { type: ValueErrorType.NumberMinimum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
341
341
|
}
|
|
342
342
|
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
343
|
-
yield { type: ValueErrorType.
|
|
343
|
+
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
344
344
|
}
|
|
345
345
|
}
|
|
346
346
|
function* Object(schema, references, path, value) {
|
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;
|