@sinclair/typebox 0.24.7 → 0.24.10
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 +6 -5
- package/compiler/compiler.js +108 -101
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +0 -1
- package/package.json +1 -1
- package/readme.md +32 -19
- package/typebox.d.ts +1 -3
- package/typebox.js +1 -1
- package/value/cast.d.ts +5 -0
- package/value/cast.js +249 -0
- package/value/check.d.ts +2 -3
- package/value/check.js +181 -119
- package/value/create.d.ts +3 -4
- package/value/create.js +91 -101
- package/value/errors.d.ts +10 -0
- package/{compiler → value}/errors.js +77 -92
- package/value/index.d.ts +1 -0
- package/value/value.d.ts +17 -13
- package/value/value.js +20 -32
- package/compiler/errors.d.ts +0 -10
- package/value/clone.d.ts +0 -3
- package/value/clone.js +0 -94
- package/value/delta.d.ts +0 -13
- package/value/delta.js +0 -191
- package/value/pointer.d.ts +0 -12
- package/value/pointer.js +0 -110
- package/value/reflect.d.ts +0 -2
- package/value/reflect.js +0 -42
- package/value/upcast.d.ts +0 -4
- package/value/upcast.js +0 -247
package/value/create.js
CHANGED
|
@@ -27,13 +27,11 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
30
|
+
exports.ValueCreate = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
-
var
|
|
33
|
-
(function (
|
|
34
|
-
|
|
35
|
-
let recursionDepth = 0;
|
|
36
|
-
function Any(schema) {
|
|
32
|
+
var ValueCreate;
|
|
33
|
+
(function (ValueCreate) {
|
|
34
|
+
function Any(schema, references) {
|
|
37
35
|
if (schema.default !== undefined) {
|
|
38
36
|
return schema.default;
|
|
39
37
|
}
|
|
@@ -41,20 +39,20 @@ var CreateValue;
|
|
|
41
39
|
return {};
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
|
-
function Array(schema) {
|
|
42
|
+
function Array(schema, references) {
|
|
45
43
|
if (schema.default !== undefined) {
|
|
46
44
|
return schema.default;
|
|
47
45
|
}
|
|
48
46
|
else if (schema.minItems !== undefined) {
|
|
49
47
|
return globalThis.Array.from({ length: schema.minItems }).map((item) => {
|
|
50
|
-
return
|
|
48
|
+
return ValueCreate.Create(schema.items, references);
|
|
51
49
|
});
|
|
52
50
|
}
|
|
53
51
|
else {
|
|
54
52
|
return [];
|
|
55
53
|
}
|
|
56
54
|
}
|
|
57
|
-
function Boolean(schema) {
|
|
55
|
+
function Boolean(schema, references) {
|
|
58
56
|
if (schema.default !== undefined) {
|
|
59
57
|
return schema.default;
|
|
60
58
|
}
|
|
@@ -62,18 +60,18 @@ var CreateValue;
|
|
|
62
60
|
return false;
|
|
63
61
|
}
|
|
64
62
|
}
|
|
65
|
-
function Constructor(schema) {
|
|
63
|
+
function Constructor(schema, references) {
|
|
66
64
|
if (schema.default !== undefined) {
|
|
67
65
|
return schema.default;
|
|
68
66
|
}
|
|
69
67
|
else {
|
|
70
|
-
const value =
|
|
68
|
+
const value = ValueCreate.Create(schema.returns, references);
|
|
71
69
|
if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
|
|
72
70
|
return class {
|
|
73
71
|
constructor() {
|
|
74
72
|
for (const [key, val] of globalThis.Object.entries(value)) {
|
|
75
|
-
const
|
|
76
|
-
|
|
73
|
+
const self = this;
|
|
74
|
+
self[key] = val;
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
};
|
|
@@ -84,7 +82,7 @@ var CreateValue;
|
|
|
84
82
|
}
|
|
85
83
|
}
|
|
86
84
|
}
|
|
87
|
-
function Enum(schema) {
|
|
85
|
+
function Enum(schema, references) {
|
|
88
86
|
if (schema.default !== undefined) {
|
|
89
87
|
return schema.default;
|
|
90
88
|
}
|
|
@@ -95,15 +93,15 @@ var CreateValue;
|
|
|
95
93
|
return schema.anyOf[0].const;
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
|
-
function Function(schema) {
|
|
96
|
+
function Function(schema, references) {
|
|
99
97
|
if (schema.default !== undefined) {
|
|
100
98
|
return schema.default;
|
|
101
99
|
}
|
|
102
100
|
else {
|
|
103
|
-
return () =>
|
|
101
|
+
return () => ValueCreate.Create(schema.returns, references);
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
|
-
function Integer(schema) {
|
|
104
|
+
function Integer(schema, references) {
|
|
107
105
|
if (schema.default !== undefined) {
|
|
108
106
|
return schema.default;
|
|
109
107
|
}
|
|
@@ -114,24 +112,13 @@ var CreateValue;
|
|
|
114
112
|
return 0;
|
|
115
113
|
}
|
|
116
114
|
}
|
|
117
|
-
function
|
|
118
|
-
if (schema.default !== undefined) {
|
|
119
|
-
return schema.default;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
return (schema.default ||
|
|
123
|
-
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
124
|
-
return { ...acc, [key]: CreateValue.Create(schema) };
|
|
125
|
-
}, {}));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
function Literal(schema) {
|
|
115
|
+
function Literal(schema, references) {
|
|
129
116
|
return schema.const;
|
|
130
117
|
}
|
|
131
|
-
function Null(schema) {
|
|
118
|
+
function Null(schema, references) {
|
|
132
119
|
return null;
|
|
133
120
|
}
|
|
134
|
-
function Number(schema) {
|
|
121
|
+
function Number(schema, references) {
|
|
135
122
|
if (schema.default !== undefined) {
|
|
136
123
|
return schema.default;
|
|
137
124
|
}
|
|
@@ -142,35 +129,27 @@ var CreateValue;
|
|
|
142
129
|
return 0;
|
|
143
130
|
}
|
|
144
131
|
}
|
|
145
|
-
function Object(schema) {
|
|
132
|
+
function Object(schema, references) {
|
|
146
133
|
if (schema.default !== undefined) {
|
|
147
134
|
return schema.default;
|
|
148
135
|
}
|
|
149
136
|
else {
|
|
150
|
-
// StackOverflow Prevention
|
|
151
|
-
if (schema['$dynamicAnchor'] !== undefined) {
|
|
152
|
-
for (const [key, value] of globalThis.Object.entries(schema.properties)) {
|
|
153
|
-
if (value['$dynamicRef'] !== undefined && schema.required && schema.required.includes(key)) {
|
|
154
|
-
throw Error(`Cannot create recursive object with immediate recursive property`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
137
|
const required = new Set(schema.required);
|
|
159
138
|
return (schema.default ||
|
|
160
139
|
globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
161
|
-
return required.has(key) ? { ...acc, [key]:
|
|
140
|
+
return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema, references) } : { ...acc };
|
|
162
141
|
}, {}));
|
|
163
142
|
}
|
|
164
143
|
}
|
|
165
|
-
function Promise(schema) {
|
|
144
|
+
function Promise(schema, references) {
|
|
166
145
|
if (schema.default !== undefined) {
|
|
167
146
|
return schema.default;
|
|
168
147
|
}
|
|
169
148
|
else {
|
|
170
|
-
return globalThis.Promise.resolve(
|
|
149
|
+
return globalThis.Promise.resolve(ValueCreate.Create(schema.item, references));
|
|
171
150
|
}
|
|
172
151
|
}
|
|
173
|
-
function Record(schema) {
|
|
152
|
+
function Record(schema, references) {
|
|
174
153
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
175
154
|
if (schema.default !== undefined) {
|
|
176
155
|
return schema.default;
|
|
@@ -178,33 +157,47 @@ var CreateValue;
|
|
|
178
157
|
else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
|
|
179
158
|
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
180
159
|
return propertyKeys.reduce((acc, key) => {
|
|
181
|
-
return { ...acc, [key]: Create(valueSchema) };
|
|
160
|
+
return { ...acc, [key]: Create(valueSchema, references) };
|
|
182
161
|
}, {});
|
|
183
162
|
}
|
|
184
163
|
else {
|
|
185
164
|
return {};
|
|
186
165
|
}
|
|
187
166
|
}
|
|
188
|
-
function Recursive(schema) {
|
|
167
|
+
function Recursive(schema, references) {
|
|
189
168
|
if (schema.default !== undefined) {
|
|
190
169
|
return schema.default;
|
|
191
170
|
}
|
|
192
171
|
else {
|
|
193
|
-
throw new Error('
|
|
172
|
+
throw new Error('ValueCreate.Recursive: Recursive types require a default value');
|
|
194
173
|
}
|
|
195
174
|
}
|
|
196
|
-
function Ref(schema) {
|
|
175
|
+
function Ref(schema, references) {
|
|
197
176
|
if (schema.default !== undefined) {
|
|
198
177
|
return schema.default;
|
|
199
178
|
}
|
|
200
179
|
else {
|
|
201
|
-
|
|
180
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
181
|
+
if (reference === undefined)
|
|
182
|
+
throw new Error(`ValueCreate.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
183
|
+
return Visit(reference, references);
|
|
202
184
|
}
|
|
203
185
|
}
|
|
204
|
-
function
|
|
186
|
+
function Self(schema, references) {
|
|
187
|
+
if (schema.default !== undefined) {
|
|
188
|
+
return schema.default;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
192
|
+
if (reference === undefined)
|
|
193
|
+
throw new Error(`ValueCreate.Self: Cannot locate schema with $id '${schema.$ref}'`);
|
|
194
|
+
return Visit(reference, references);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function String(schema, references) {
|
|
205
198
|
if (schema.pattern !== undefined) {
|
|
206
199
|
if (schema.default === undefined) {
|
|
207
|
-
throw Error('String types with patterns must specify a default value');
|
|
200
|
+
throw new Error('ValueCreate.String: String types with patterns must specify a default value');
|
|
208
201
|
}
|
|
209
202
|
else {
|
|
210
203
|
return schema.default;
|
|
@@ -214,12 +207,17 @@ var CreateValue;
|
|
|
214
207
|
if (schema.default !== undefined) {
|
|
215
208
|
return schema.default;
|
|
216
209
|
}
|
|
210
|
+
else if (schema.minLength !== undefined) {
|
|
211
|
+
return globalThis.Array.from({ length: schema.minLength })
|
|
212
|
+
.map(() => '.')
|
|
213
|
+
.join('');
|
|
214
|
+
}
|
|
217
215
|
else {
|
|
218
216
|
return '';
|
|
219
217
|
}
|
|
220
218
|
}
|
|
221
219
|
}
|
|
222
|
-
function Tuple(schema) {
|
|
220
|
+
function Tuple(schema, references) {
|
|
223
221
|
if (schema.default !== undefined) {
|
|
224
222
|
return schema.default;
|
|
225
223
|
}
|
|
@@ -227,35 +225,35 @@ var CreateValue;
|
|
|
227
225
|
return [];
|
|
228
226
|
}
|
|
229
227
|
else {
|
|
230
|
-
return globalThis.Array.from({ length: schema.minItems }).map((_, index) =>
|
|
228
|
+
return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index], references));
|
|
231
229
|
}
|
|
232
230
|
}
|
|
233
|
-
function Undefined(schema) {
|
|
231
|
+
function Undefined(schema, references) {
|
|
234
232
|
return undefined;
|
|
235
233
|
}
|
|
236
|
-
function Union(schema) {
|
|
234
|
+
function Union(schema, references) {
|
|
237
235
|
if (schema.default !== undefined) {
|
|
238
236
|
return schema.default;
|
|
239
237
|
}
|
|
240
238
|
else if (schema.anyOf.length === 0) {
|
|
241
|
-
throw Error('Cannot
|
|
239
|
+
throw new Error('ValueCreate: Cannot create Union with zero variants');
|
|
242
240
|
}
|
|
243
241
|
else {
|
|
244
|
-
return
|
|
242
|
+
return ValueCreate.Create(schema.anyOf[0], references);
|
|
245
243
|
}
|
|
246
244
|
}
|
|
247
|
-
function Uint8Array(schema) {
|
|
245
|
+
function Uint8Array(schema, references) {
|
|
248
246
|
if (schema.default !== undefined) {
|
|
249
247
|
return schema.default;
|
|
250
248
|
}
|
|
251
|
-
else if (schema.minByteLength) {
|
|
249
|
+
else if (schema.minByteLength !== undefined) {
|
|
252
250
|
return new globalThis.Uint8Array(schema.minByteLength);
|
|
253
251
|
}
|
|
254
252
|
else {
|
|
255
253
|
return new globalThis.Uint8Array(0);
|
|
256
254
|
}
|
|
257
255
|
}
|
|
258
|
-
function Unknown(schema) {
|
|
256
|
+
function Unknown(schema, references) {
|
|
259
257
|
if (schema.default !== undefined) {
|
|
260
258
|
return schema.default;
|
|
261
259
|
}
|
|
@@ -263,75 +261,67 @@ var CreateValue;
|
|
|
263
261
|
return {};
|
|
264
262
|
}
|
|
265
263
|
}
|
|
266
|
-
function Void(schema) {
|
|
264
|
+
function Void(schema, references) {
|
|
267
265
|
return null;
|
|
268
266
|
}
|
|
269
267
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
270
|
-
function Visit(schema) {
|
|
271
|
-
|
|
272
|
-
if (recursionDepth >= 1000)
|
|
273
|
-
throw new Error('Cannot create value as schema contains a infinite expansion');
|
|
268
|
+
function Visit(schema, references) {
|
|
269
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
274
270
|
const anySchema = schema;
|
|
275
|
-
if (anySchema.$id !== undefined)
|
|
276
|
-
referenceMap.set(anySchema.$id, anySchema);
|
|
277
271
|
switch (anySchema[Types.Kind]) {
|
|
278
272
|
case 'Any':
|
|
279
|
-
return Any(anySchema);
|
|
273
|
+
return Any(anySchema, anyReferences);
|
|
280
274
|
case 'Array':
|
|
281
|
-
return Array(anySchema);
|
|
275
|
+
return Array(anySchema, anyReferences);
|
|
282
276
|
case 'Boolean':
|
|
283
|
-
return Boolean(anySchema);
|
|
277
|
+
return Boolean(anySchema, anyReferences);
|
|
284
278
|
case 'Constructor':
|
|
285
|
-
return Constructor(anySchema);
|
|
279
|
+
return Constructor(anySchema, anyReferences);
|
|
286
280
|
case 'Enum':
|
|
287
|
-
return Enum(anySchema);
|
|
281
|
+
return Enum(anySchema, anyReferences);
|
|
288
282
|
case 'Function':
|
|
289
|
-
return Function(anySchema);
|
|
283
|
+
return Function(anySchema, anyReferences);
|
|
290
284
|
case 'Integer':
|
|
291
|
-
return Integer(anySchema);
|
|
292
|
-
case 'Intersect':
|
|
293
|
-
return Intersect(anySchema);
|
|
285
|
+
return Integer(anySchema, anyReferences);
|
|
294
286
|
case 'Literal':
|
|
295
|
-
return Literal(anySchema);
|
|
287
|
+
return Literal(anySchema, anyReferences);
|
|
296
288
|
case 'Null':
|
|
297
|
-
return Null(anySchema);
|
|
289
|
+
return Null(anySchema, anyReferences);
|
|
298
290
|
case 'Number':
|
|
299
|
-
return Number(anySchema);
|
|
291
|
+
return Number(anySchema, anyReferences);
|
|
300
292
|
case 'Object':
|
|
301
|
-
return Object(anySchema);
|
|
293
|
+
return Object(anySchema, anyReferences);
|
|
302
294
|
case 'Promise':
|
|
303
|
-
return Promise(anySchema);
|
|
295
|
+
return Promise(anySchema, anyReferences);
|
|
304
296
|
case 'Record':
|
|
305
|
-
return Record(anySchema);
|
|
297
|
+
return Record(anySchema, anyReferences);
|
|
306
298
|
case 'Rec':
|
|
307
|
-
return Recursive(anySchema);
|
|
299
|
+
return Recursive(anySchema, anyReferences);
|
|
308
300
|
case 'Ref':
|
|
309
|
-
return Ref(anySchema);
|
|
301
|
+
return Ref(anySchema, anyReferences);
|
|
302
|
+
case 'Self':
|
|
303
|
+
return Self(anySchema, anyReferences);
|
|
310
304
|
case 'String':
|
|
311
|
-
return String(anySchema);
|
|
305
|
+
return String(anySchema, anyReferences);
|
|
312
306
|
case 'Tuple':
|
|
313
|
-
return Tuple(anySchema);
|
|
307
|
+
return Tuple(anySchema, anyReferences);
|
|
314
308
|
case 'Undefined':
|
|
315
|
-
return Undefined(anySchema);
|
|
309
|
+
return Undefined(anySchema, anyReferences);
|
|
316
310
|
case 'Union':
|
|
317
|
-
return Union(anySchema);
|
|
311
|
+
return Union(anySchema, anyReferences);
|
|
318
312
|
case 'Uint8Array':
|
|
319
|
-
return Uint8Array(anySchema);
|
|
313
|
+
return Uint8Array(anySchema, anyReferences);
|
|
320
314
|
case 'Unknown':
|
|
321
|
-
return Unknown(anySchema);
|
|
315
|
+
return Unknown(anySchema, anyReferences);
|
|
322
316
|
case 'Void':
|
|
323
|
-
return Void(anySchema);
|
|
324
|
-
case 'Self':
|
|
325
|
-
return Visit(referenceMap.get(anySchema.$ref));
|
|
317
|
+
return Void(anySchema, anyReferences);
|
|
326
318
|
default:
|
|
327
|
-
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
319
|
+
throw new Error(`ValueCreate: Unknown schema kind '${schema[Types.Kind]}'`);
|
|
328
320
|
}
|
|
329
321
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
recursionDepth = 0;
|
|
334
|
-
return Visit(schema);
|
|
322
|
+
ValueCreate.Visit = Visit;
|
|
323
|
+
function Create(schema, references) {
|
|
324
|
+
return Visit(schema, references);
|
|
335
325
|
}
|
|
336
|
-
|
|
337
|
-
})(
|
|
326
|
+
ValueCreate.Create = Create;
|
|
327
|
+
})(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {}));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as Types from '../typebox';
|
|
2
|
+
export interface ValueError {
|
|
3
|
+
schema: Types.TSchema;
|
|
4
|
+
path: string;
|
|
5
|
+
value: unknown;
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
export declare namespace ValueErrors {
|
|
9
|
+
function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
|
|
10
|
+
}
|