@sinclair/typebox 0.30.4 → 0.31.0-dev-1
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 +22 -14
- package/compiler/compiler.js +133 -106
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +2 -1
- package/errors/errors.d.ts +56 -61
- package/errors/errors.js +222 -279
- package/package.json +5 -3
- package/readme.md +588 -507
- package/system/system.d.ts +34 -8
- package/system/system.js +214 -11
- package/typebox.d.ts +180 -106
- package/typebox.js +794 -907
- package/value/cast.d.ts +6 -12
- package/value/cast.js +81 -163
- package/value/check.d.ts +1 -5
- package/value/check.js +59 -103
- package/value/clone.js +6 -29
- package/value/convert.d.ts +2 -5
- package/value/convert.js +55 -106
- package/value/create.d.ts +6 -10
- package/value/create.js +54 -68
- package/value/delta.js +22 -22
- package/value/deref.d.ts +7 -0
- package/value/deref.js +46 -0
- package/value/equal.js +10 -10
- package/value/guard.js +1 -1
- package/value/hash.js +14 -14
- package/value/mutate.js +17 -17
- package/value/pointer.js +2 -2
- package/value/transform.d.ts +42 -0
- package/value/transform.js +512 -0
- package/value/value.d.ts +8 -0
- package/value/value.js +18 -0
package/value/create.js
CHANGED
|
@@ -27,58 +27,52 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Create = exports.ValueCreateRecursiveInstantiationError = exports.
|
|
30
|
+
exports.Create = exports.ValueCreateRecursiveInstantiationError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNotTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
|
+
const guard_1 = require("./guard");
|
|
32
|
+
const check_1 = require("./check");
|
|
33
|
+
const deref_1 = require("./deref");
|
|
31
34
|
const Types = require("../typebox");
|
|
32
|
-
const ValueCheck = require("./check");
|
|
33
|
-
const ValueGuard = require("./guard");
|
|
34
35
|
// --------------------------------------------------------------------------
|
|
35
36
|
// Errors
|
|
36
37
|
// --------------------------------------------------------------------------
|
|
37
|
-
class ValueCreateUnknownTypeError extends
|
|
38
|
+
class ValueCreateUnknownTypeError extends Types.TypeBoxError {
|
|
38
39
|
constructor(schema) {
|
|
39
|
-
super('
|
|
40
|
+
super('Unknown type');
|
|
40
41
|
this.schema = schema;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
exports.ValueCreateUnknownTypeError = ValueCreateUnknownTypeError;
|
|
44
|
-
class ValueCreateNeverTypeError extends
|
|
45
|
+
class ValueCreateNeverTypeError extends Types.TypeBoxError {
|
|
45
46
|
constructor(schema) {
|
|
46
|
-
super('
|
|
47
|
+
super('Never types cannot be created');
|
|
47
48
|
this.schema = schema;
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
|
|
51
|
-
class ValueCreateNotTypeError extends
|
|
52
|
+
class ValueCreateNotTypeError extends Types.TypeBoxError {
|
|
52
53
|
constructor(schema) {
|
|
53
|
-
super('
|
|
54
|
+
super('Not types must have a default value');
|
|
54
55
|
this.schema = schema;
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
exports.ValueCreateNotTypeError = ValueCreateNotTypeError;
|
|
58
|
-
class ValueCreateIntersectTypeError extends
|
|
59
|
+
class ValueCreateIntersectTypeError extends Types.TypeBoxError {
|
|
59
60
|
constructor(schema) {
|
|
60
|
-
super('
|
|
61
|
+
super('Intersect produced invalid value. Consider using a default value.');
|
|
61
62
|
this.schema = schema;
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError;
|
|
65
|
-
class ValueCreateTempateLiteralTypeError extends
|
|
66
|
+
class ValueCreateTempateLiteralTypeError extends Types.TypeBoxError {
|
|
66
67
|
constructor(schema) {
|
|
67
|
-
super('
|
|
68
|
+
super('Can only create template literal values from patterns that produce finite sequences. Consider using a default value.');
|
|
68
69
|
this.schema = schema;
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
exports.ValueCreateTempateLiteralTypeError = ValueCreateTempateLiteralTypeError;
|
|
72
|
-
class
|
|
73
|
-
constructor(schema) {
|
|
74
|
-
super(`ValueCreate: Unable to dereference type with $id '${schema.$ref}'`);
|
|
75
|
-
this.schema = schema;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
exports.ValueCreateDereferenceError = ValueCreateDereferenceError;
|
|
79
|
-
class ValueCreateRecursiveInstantiationError extends Error {
|
|
73
|
+
class ValueCreateRecursiveInstantiationError extends Types.TypeBoxError {
|
|
80
74
|
constructor(schema, recursiveMaxDepth) {
|
|
81
|
-
super('
|
|
75
|
+
super('Value cannot be created as recursive type may produce value of infinite size. Consider using a default.');
|
|
82
76
|
this.schema = schema;
|
|
83
77
|
this.recursiveMaxDepth = recursiveMaxDepth;
|
|
84
78
|
}
|
|
@@ -88,7 +82,7 @@ exports.ValueCreateRecursiveInstantiationError = ValueCreateRecursiveInstantiati
|
|
|
88
82
|
// Types
|
|
89
83
|
// --------------------------------------------------------------------------
|
|
90
84
|
function TAny(schema, references) {
|
|
91
|
-
if (
|
|
85
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
92
86
|
return schema.default;
|
|
93
87
|
}
|
|
94
88
|
else {
|
|
@@ -96,10 +90,10 @@ function TAny(schema, references) {
|
|
|
96
90
|
}
|
|
97
91
|
}
|
|
98
92
|
function TArray(schema, references) {
|
|
99
|
-
if (schema.uniqueItems === true && !
|
|
93
|
+
if (schema.uniqueItems === true && !(0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
100
94
|
throw new Error('ValueCreate.Array: Array with the uniqueItems constraint requires a default value');
|
|
101
95
|
}
|
|
102
|
-
else if ('contains' in schema && !
|
|
96
|
+
else if ('contains' in schema && !(0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
103
97
|
throw new Error('ValueCreate.Array: Array with the contains constraint requires a default value');
|
|
104
98
|
}
|
|
105
99
|
else if ('default' in schema) {
|
|
@@ -115,7 +109,7 @@ function TArray(schema, references) {
|
|
|
115
109
|
}
|
|
116
110
|
}
|
|
117
111
|
function TAsyncIterator(schema, references) {
|
|
118
|
-
if (
|
|
112
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
119
113
|
return schema.default;
|
|
120
114
|
}
|
|
121
115
|
else {
|
|
@@ -123,7 +117,7 @@ function TAsyncIterator(schema, references) {
|
|
|
123
117
|
}
|
|
124
118
|
}
|
|
125
119
|
function TBigInt(schema, references) {
|
|
126
|
-
if (
|
|
120
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
127
121
|
return schema.default;
|
|
128
122
|
}
|
|
129
123
|
else {
|
|
@@ -131,7 +125,7 @@ function TBigInt(schema, references) {
|
|
|
131
125
|
}
|
|
132
126
|
}
|
|
133
127
|
function TBoolean(schema, references) {
|
|
134
|
-
if (
|
|
128
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
135
129
|
return schema.default;
|
|
136
130
|
}
|
|
137
131
|
else {
|
|
@@ -139,7 +133,7 @@ function TBoolean(schema, references) {
|
|
|
139
133
|
}
|
|
140
134
|
}
|
|
141
135
|
function TConstructor(schema, references) {
|
|
142
|
-
if (
|
|
136
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
143
137
|
return schema.default;
|
|
144
138
|
}
|
|
145
139
|
else {
|
|
@@ -161,7 +155,7 @@ function TConstructor(schema, references) {
|
|
|
161
155
|
}
|
|
162
156
|
}
|
|
163
157
|
function TDate(schema, references) {
|
|
164
|
-
if (
|
|
158
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
165
159
|
return schema.default;
|
|
166
160
|
}
|
|
167
161
|
else if (schema.minimumTimestamp !== undefined) {
|
|
@@ -172,7 +166,7 @@ function TDate(schema, references) {
|
|
|
172
166
|
}
|
|
173
167
|
}
|
|
174
168
|
function TFunction(schema, references) {
|
|
175
|
-
if (
|
|
169
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
176
170
|
return schema.default;
|
|
177
171
|
}
|
|
178
172
|
else {
|
|
@@ -180,7 +174,7 @@ function TFunction(schema, references) {
|
|
|
180
174
|
}
|
|
181
175
|
}
|
|
182
176
|
function TInteger(schema, references) {
|
|
183
|
-
if (
|
|
177
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
184
178
|
return schema.default;
|
|
185
179
|
}
|
|
186
180
|
else if (schema.minimum !== undefined) {
|
|
@@ -191,7 +185,7 @@ function TInteger(schema, references) {
|
|
|
191
185
|
}
|
|
192
186
|
}
|
|
193
187
|
function TIntersect(schema, references) {
|
|
194
|
-
if (
|
|
188
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
195
189
|
return schema.default;
|
|
196
190
|
}
|
|
197
191
|
else {
|
|
@@ -202,13 +196,13 @@ function TIntersect(schema, references) {
|
|
|
202
196
|
const next = Visit(schema, references);
|
|
203
197
|
return typeof next === 'object' ? { ...acc, ...next } : next;
|
|
204
198
|
}, {});
|
|
205
|
-
if (!
|
|
199
|
+
if (!(0, check_1.Check)(schema, references, value))
|
|
206
200
|
throw new ValueCreateIntersectTypeError(schema);
|
|
207
201
|
return value;
|
|
208
202
|
}
|
|
209
203
|
}
|
|
210
204
|
function TIterator(schema, references) {
|
|
211
|
-
if (
|
|
205
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
212
206
|
return schema.default;
|
|
213
207
|
}
|
|
214
208
|
else {
|
|
@@ -216,7 +210,7 @@ function TIterator(schema, references) {
|
|
|
216
210
|
}
|
|
217
211
|
}
|
|
218
212
|
function TLiteral(schema, references) {
|
|
219
|
-
if (
|
|
213
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
220
214
|
return schema.default;
|
|
221
215
|
}
|
|
222
216
|
else {
|
|
@@ -227,7 +221,7 @@ function TNever(schema, references) {
|
|
|
227
221
|
throw new ValueCreateNeverTypeError(schema);
|
|
228
222
|
}
|
|
229
223
|
function TNot(schema, references) {
|
|
230
|
-
if (
|
|
224
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
231
225
|
return schema.default;
|
|
232
226
|
}
|
|
233
227
|
else {
|
|
@@ -235,7 +229,7 @@ function TNot(schema, references) {
|
|
|
235
229
|
}
|
|
236
230
|
}
|
|
237
231
|
function TNull(schema, references) {
|
|
238
|
-
if (
|
|
232
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
239
233
|
return schema.default;
|
|
240
234
|
}
|
|
241
235
|
else {
|
|
@@ -243,7 +237,7 @@ function TNull(schema, references) {
|
|
|
243
237
|
}
|
|
244
238
|
}
|
|
245
239
|
function TNumber(schema, references) {
|
|
246
|
-
if (
|
|
240
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
247
241
|
return schema.default;
|
|
248
242
|
}
|
|
249
243
|
else if (schema.minimum !== undefined) {
|
|
@@ -254,7 +248,7 @@ function TNumber(schema, references) {
|
|
|
254
248
|
}
|
|
255
249
|
}
|
|
256
250
|
function TObject(schema, references) {
|
|
257
|
-
if (
|
|
251
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
258
252
|
return schema.default;
|
|
259
253
|
}
|
|
260
254
|
else {
|
|
@@ -266,7 +260,7 @@ function TObject(schema, references) {
|
|
|
266
260
|
}
|
|
267
261
|
}
|
|
268
262
|
function TPromise(schema, references) {
|
|
269
|
-
if (
|
|
263
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
270
264
|
return schema.default;
|
|
271
265
|
}
|
|
272
266
|
else {
|
|
@@ -275,7 +269,7 @@ function TPromise(schema, references) {
|
|
|
275
269
|
}
|
|
276
270
|
function TRecord(schema, references) {
|
|
277
271
|
const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
|
|
278
|
-
if (
|
|
272
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
279
273
|
return schema.default;
|
|
280
274
|
}
|
|
281
275
|
else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) {
|
|
@@ -289,20 +283,16 @@ function TRecord(schema, references) {
|
|
|
289
283
|
}
|
|
290
284
|
}
|
|
291
285
|
function TRef(schema, references) {
|
|
292
|
-
if (
|
|
286
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
293
287
|
return schema.default;
|
|
294
288
|
}
|
|
295
289
|
else {
|
|
296
|
-
|
|
297
|
-
if (index === -1)
|
|
298
|
-
throw new ValueCreateDereferenceError(schema);
|
|
299
|
-
const target = references[index];
|
|
300
|
-
return Visit(target, references);
|
|
290
|
+
return Visit((0, deref_1.Deref)(schema, references), references);
|
|
301
291
|
}
|
|
302
292
|
}
|
|
303
293
|
function TString(schema, references) {
|
|
304
294
|
if (schema.pattern !== undefined) {
|
|
305
|
-
if (!
|
|
295
|
+
if (!(0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
306
296
|
throw new Error('ValueCreate.String: String types with patterns must specify a default value');
|
|
307
297
|
}
|
|
308
298
|
else {
|
|
@@ -310,7 +300,7 @@ function TString(schema, references) {
|
|
|
310
300
|
}
|
|
311
301
|
}
|
|
312
302
|
else if (schema.format !== undefined) {
|
|
313
|
-
if (!
|
|
303
|
+
if (!(0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
314
304
|
throw new Error('ValueCreate.String: String types with formats must specify a default value');
|
|
315
305
|
}
|
|
316
306
|
else {
|
|
@@ -318,7 +308,7 @@ function TString(schema, references) {
|
|
|
318
308
|
}
|
|
319
309
|
}
|
|
320
310
|
else {
|
|
321
|
-
if (
|
|
311
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
322
312
|
return schema.default;
|
|
323
313
|
}
|
|
324
314
|
else if (schema.minLength !== undefined) {
|
|
@@ -332,7 +322,7 @@ function TString(schema, references) {
|
|
|
332
322
|
}
|
|
333
323
|
}
|
|
334
324
|
function TSymbol(schema, references) {
|
|
335
|
-
if (
|
|
325
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
336
326
|
return schema.default;
|
|
337
327
|
}
|
|
338
328
|
else if ('value' in schema) {
|
|
@@ -343,7 +333,7 @@ function TSymbol(schema, references) {
|
|
|
343
333
|
}
|
|
344
334
|
}
|
|
345
335
|
function TTemplateLiteral(schema, references) {
|
|
346
|
-
if (
|
|
336
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
347
337
|
return schema.default;
|
|
348
338
|
}
|
|
349
339
|
const expression = Types.TemplateLiteralParser.ParseExact(schema.pattern);
|
|
@@ -355,19 +345,15 @@ function TTemplateLiteral(schema, references) {
|
|
|
355
345
|
function TThis(schema, references) {
|
|
356
346
|
if (recursiveDepth++ > recursiveMaxDepth)
|
|
357
347
|
throw new ValueCreateRecursiveInstantiationError(schema, recursiveMaxDepth);
|
|
358
|
-
if (
|
|
348
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
359
349
|
return schema.default;
|
|
360
350
|
}
|
|
361
351
|
else {
|
|
362
|
-
|
|
363
|
-
if (index === -1)
|
|
364
|
-
throw new ValueCreateDereferenceError(schema);
|
|
365
|
-
const target = references[index];
|
|
366
|
-
return Visit(target, references);
|
|
352
|
+
return Visit((0, deref_1.Deref)(schema, references), references);
|
|
367
353
|
}
|
|
368
354
|
}
|
|
369
355
|
function TTuple(schema, references) {
|
|
370
|
-
if (
|
|
356
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
371
357
|
return schema.default;
|
|
372
358
|
}
|
|
373
359
|
if (schema.items === undefined) {
|
|
@@ -378,7 +364,7 @@ function TTuple(schema, references) {
|
|
|
378
364
|
}
|
|
379
365
|
}
|
|
380
366
|
function TUndefined(schema, references) {
|
|
381
|
-
if (
|
|
367
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
382
368
|
return schema.default;
|
|
383
369
|
}
|
|
384
370
|
else {
|
|
@@ -386,7 +372,7 @@ function TUndefined(schema, references) {
|
|
|
386
372
|
}
|
|
387
373
|
}
|
|
388
374
|
function TUnion(schema, references) {
|
|
389
|
-
if (
|
|
375
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
390
376
|
return schema.default;
|
|
391
377
|
}
|
|
392
378
|
else if (schema.anyOf.length === 0) {
|
|
@@ -397,7 +383,7 @@ function TUnion(schema, references) {
|
|
|
397
383
|
}
|
|
398
384
|
}
|
|
399
385
|
function TUint8Array(schema, references) {
|
|
400
|
-
if (
|
|
386
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
401
387
|
return schema.default;
|
|
402
388
|
}
|
|
403
389
|
else if (schema.minByteLength !== undefined) {
|
|
@@ -408,7 +394,7 @@ function TUint8Array(schema, references) {
|
|
|
408
394
|
}
|
|
409
395
|
}
|
|
410
396
|
function TUnknown(schema, references) {
|
|
411
|
-
if (
|
|
397
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
412
398
|
return schema.default;
|
|
413
399
|
}
|
|
414
400
|
else {
|
|
@@ -416,7 +402,7 @@ function TUnknown(schema, references) {
|
|
|
416
402
|
}
|
|
417
403
|
}
|
|
418
404
|
function TVoid(schema, references) {
|
|
419
|
-
if (
|
|
405
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
420
406
|
return schema.default;
|
|
421
407
|
}
|
|
422
408
|
else {
|
|
@@ -424,15 +410,15 @@ function TVoid(schema, references) {
|
|
|
424
410
|
}
|
|
425
411
|
}
|
|
426
412
|
function TKind(schema, references) {
|
|
427
|
-
if (
|
|
413
|
+
if ((0, guard_1.HasPropertyKey)(schema, 'default')) {
|
|
428
414
|
return schema.default;
|
|
429
415
|
}
|
|
430
416
|
else {
|
|
431
|
-
throw new Error('
|
|
417
|
+
throw new Error('User defined types must specify a default value');
|
|
432
418
|
}
|
|
433
419
|
}
|
|
434
420
|
function Visit(schema, references) {
|
|
435
|
-
const references_ =
|
|
421
|
+
const references_ = (0, guard_1.IsString)(schema.$id) ? [...references, schema] : references;
|
|
436
422
|
const schema_ = schema;
|
|
437
423
|
switch (schema_[Types.Kind]) {
|
|
438
424
|
case 'Any':
|
package/value/delta.js
CHANGED
|
@@ -28,10 +28,10 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Patch = exports.Diff = exports.ValueDeltaUnableToDiffUnknownValue = exports.ValueDeltaObjectWithSymbolKeyError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
|
|
31
|
+
const guard_1 = require("./guard");
|
|
31
32
|
const typebox_1 = require("../typebox");
|
|
32
33
|
const pointer_1 = require("./pointer");
|
|
33
|
-
const
|
|
34
|
-
const ValueClone = require("./clone");
|
|
34
|
+
const clone_1 = require("./clone");
|
|
35
35
|
exports.Insert = typebox_1.Type.Object({
|
|
36
36
|
type: typebox_1.Type.Literal('insert'),
|
|
37
37
|
path: typebox_1.Type.String(),
|
|
@@ -52,14 +52,14 @@ exports.Edit = typebox_1.Type.Union([exports.Insert, exports.Update, exports.Del
|
|
|
52
52
|
// --------------------------------------------------------------------------
|
|
53
53
|
class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
54
54
|
constructor(key) {
|
|
55
|
-
super('
|
|
55
|
+
super('Cannot diff objects with symbol keys');
|
|
56
56
|
this.key = key;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
exports.ValueDeltaObjectWithSymbolKeyError = ValueDeltaObjectWithSymbolKeyError;
|
|
60
60
|
class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
61
61
|
constructor(value) {
|
|
62
|
-
super('
|
|
62
|
+
super('Unable to create diff edits for unknown value');
|
|
63
63
|
this.value = value;
|
|
64
64
|
}
|
|
65
65
|
}
|
|
@@ -80,38 +80,38 @@ function CreateDelete(path) {
|
|
|
80
80
|
// Diffing Generators
|
|
81
81
|
// --------------------------------------------------------------------------
|
|
82
82
|
function* ObjectType(path, current, next) {
|
|
83
|
-
if (!
|
|
83
|
+
if (!(0, guard_1.IsPlainObject)(next))
|
|
84
84
|
return yield CreateUpdate(path, next);
|
|
85
85
|
const currentKeys = [...Object.keys(current), ...Object.getOwnPropertySymbols(current)];
|
|
86
86
|
const nextKeys = [...Object.keys(next), ...Object.getOwnPropertySymbols(next)];
|
|
87
87
|
for (const key of currentKeys) {
|
|
88
|
-
if (
|
|
88
|
+
if ((0, guard_1.IsSymbol)(key))
|
|
89
89
|
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
90
|
-
if (
|
|
90
|
+
if ((0, guard_1.IsUndefined)(next[key]) && nextKeys.includes(key))
|
|
91
91
|
yield CreateUpdate(`${path}/${String(key)}`, undefined);
|
|
92
92
|
}
|
|
93
93
|
for (const key of nextKeys) {
|
|
94
|
-
if (
|
|
94
|
+
if ((0, guard_1.IsUndefined)(current[key]) || (0, guard_1.IsUndefined)(next[key]))
|
|
95
95
|
continue;
|
|
96
|
-
if (
|
|
96
|
+
if ((0, guard_1.IsSymbol)(key))
|
|
97
97
|
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
98
98
|
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
99
99
|
}
|
|
100
100
|
for (const key of nextKeys) {
|
|
101
|
-
if (
|
|
101
|
+
if ((0, guard_1.IsSymbol)(key))
|
|
102
102
|
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
103
|
-
if (
|
|
103
|
+
if ((0, guard_1.IsUndefined)(current[key]))
|
|
104
104
|
yield CreateInsert(`${path}/${String(key)}`, next[key]);
|
|
105
105
|
}
|
|
106
106
|
for (const key of currentKeys.reverse()) {
|
|
107
|
-
if (
|
|
107
|
+
if ((0, guard_1.IsSymbol)(key))
|
|
108
108
|
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
109
|
-
if (
|
|
109
|
+
if ((0, guard_1.IsUndefined)(next[key]) && !nextKeys.includes(key))
|
|
110
110
|
yield CreateDelete(`${path}/${String(key)}`);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
function* ArrayType(path, current, next) {
|
|
114
|
-
if (!
|
|
114
|
+
if (!(0, guard_1.IsArray)(next))
|
|
115
115
|
return yield CreateUpdate(path, next);
|
|
116
116
|
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
117
117
|
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
@@ -128,7 +128,7 @@ function* ArrayType(path, current, next) {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
function* TypedArrayType(path, current, next) {
|
|
131
|
-
if (!
|
|
131
|
+
if (!(0, guard_1.IsTypedArray)(next) || current.length !== next.length || Object.getPrototypeOf(current).constructor.name !== Object.getPrototypeOf(next).constructor.name)
|
|
132
132
|
return yield CreateUpdate(path, next);
|
|
133
133
|
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
134
134
|
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
@@ -140,13 +140,13 @@ function* ValueType(path, current, next) {
|
|
|
140
140
|
yield CreateUpdate(path, next);
|
|
141
141
|
}
|
|
142
142
|
function* Visit(path, current, next) {
|
|
143
|
-
if (
|
|
143
|
+
if ((0, guard_1.IsPlainObject)(current))
|
|
144
144
|
return yield* ObjectType(path, current, next);
|
|
145
|
-
if (
|
|
145
|
+
if ((0, guard_1.IsArray)(current))
|
|
146
146
|
return yield* ArrayType(path, current, next);
|
|
147
|
-
if (
|
|
147
|
+
if ((0, guard_1.IsTypedArray)(current))
|
|
148
148
|
return yield* TypedArrayType(path, current, next);
|
|
149
|
-
if (
|
|
149
|
+
if ((0, guard_1.IsValueType)(current))
|
|
150
150
|
return yield* ValueType(path, current, next);
|
|
151
151
|
throw new ValueDeltaUnableToDiffUnknownValue(current);
|
|
152
152
|
}
|
|
@@ -168,12 +168,12 @@ function IsIdentity(edits) {
|
|
|
168
168
|
}
|
|
169
169
|
function Patch(current, edits) {
|
|
170
170
|
if (IsRootUpdate(edits)) {
|
|
171
|
-
return
|
|
171
|
+
return (0, clone_1.Clone)(edits[0].value);
|
|
172
172
|
}
|
|
173
173
|
if (IsIdentity(edits)) {
|
|
174
|
-
return
|
|
174
|
+
return (0, clone_1.Clone)(current);
|
|
175
175
|
}
|
|
176
|
-
const clone =
|
|
176
|
+
const clone = (0, clone_1.Clone)(current);
|
|
177
177
|
for (const edit of edits) {
|
|
178
178
|
switch (edit.type) {
|
|
179
179
|
case 'insert': {
|
package/value/deref.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypeBoxError, TSchema, TRef, TThis } from '../typebox';
|
|
2
|
+
export declare class TypeDereferenceError extends TypeBoxError {
|
|
3
|
+
readonly schema: TRef | TThis;
|
|
4
|
+
constructor(schema: TRef | TThis);
|
|
5
|
+
}
|
|
6
|
+
/** Dereferences a schema from the references array or throws if not found */
|
|
7
|
+
export declare function Deref(schema: TRef | TThis, references: TSchema[]): TSchema;
|
package/value/deref.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
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.Deref = exports.TypeDereferenceError = void 0;
|
|
31
|
+
const typebox_1 = require("../typebox");
|
|
32
|
+
class TypeDereferenceError extends typebox_1.TypeBoxError {
|
|
33
|
+
constructor(schema) {
|
|
34
|
+
super(`Unable to dereference schema with $id '${schema.$id}'`);
|
|
35
|
+
this.schema = schema;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.TypeDereferenceError = TypeDereferenceError;
|
|
39
|
+
/** Dereferences a schema from the references array or throws if not found */
|
|
40
|
+
function Deref(schema, references) {
|
|
41
|
+
const index = references.findIndex((target) => target.$id === schema.$ref);
|
|
42
|
+
if (index === -1)
|
|
43
|
+
throw new TypeDereferenceError(schema);
|
|
44
|
+
return references[index];
|
|
45
|
+
}
|
|
46
|
+
exports.Deref = Deref;
|
package/value/equal.js
CHANGED
|
@@ -28,12 +28,12 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Equal = void 0;
|
|
31
|
-
const
|
|
31
|
+
const guard_1 = require("./guard");
|
|
32
32
|
// --------------------------------------------------------------------------
|
|
33
33
|
// Equality Checks
|
|
34
34
|
// --------------------------------------------------------------------------
|
|
35
35
|
function ObjectType(left, right) {
|
|
36
|
-
if (!
|
|
36
|
+
if (!(0, guard_1.IsPlainObject)(right))
|
|
37
37
|
return false;
|
|
38
38
|
const leftKeys = [...Object.keys(left), ...Object.getOwnPropertySymbols(left)];
|
|
39
39
|
const rightKeys = [...Object.keys(right), ...Object.getOwnPropertySymbols(right)];
|
|
@@ -42,15 +42,15 @@ function ObjectType(left, right) {
|
|
|
42
42
|
return leftKeys.every((key) => Equal(left[key], right[key]));
|
|
43
43
|
}
|
|
44
44
|
function DateType(left, right) {
|
|
45
|
-
return
|
|
45
|
+
return (0, guard_1.IsDate)(right) && left.getTime() === right.getTime();
|
|
46
46
|
}
|
|
47
47
|
function ArrayType(left, right) {
|
|
48
|
-
if (!
|
|
48
|
+
if (!(0, guard_1.IsArray)(right) || left.length !== right.length)
|
|
49
49
|
return false;
|
|
50
50
|
return left.every((value, index) => Equal(value, right[index]));
|
|
51
51
|
}
|
|
52
52
|
function TypedArrayType(left, right) {
|
|
53
|
-
if (!
|
|
53
|
+
if (!(0, guard_1.IsTypedArray)(right) || left.length !== right.length || Object.getPrototypeOf(left).constructor.name !== Object.getPrototypeOf(right).constructor.name)
|
|
54
54
|
return false;
|
|
55
55
|
return left.every((value, index) => Equal(value, right[index]));
|
|
56
56
|
}
|
|
@@ -62,15 +62,15 @@ function ValueType(left, right) {
|
|
|
62
62
|
// --------------------------------------------------------------------------
|
|
63
63
|
/** Returns true if the left value deep-equals the right */
|
|
64
64
|
function Equal(left, right) {
|
|
65
|
-
if (
|
|
65
|
+
if ((0, guard_1.IsPlainObject)(left))
|
|
66
66
|
return ObjectType(left, right);
|
|
67
|
-
if (
|
|
67
|
+
if ((0, guard_1.IsDate)(left))
|
|
68
68
|
return DateType(left, right);
|
|
69
|
-
if (
|
|
69
|
+
if ((0, guard_1.IsTypedArray)(left))
|
|
70
70
|
return TypedArrayType(left, right);
|
|
71
|
-
if (
|
|
71
|
+
if ((0, guard_1.IsArray)(left))
|
|
72
72
|
return ArrayType(left, right);
|
|
73
|
-
if (
|
|
73
|
+
if ((0, guard_1.IsValueType)(left))
|
|
74
74
|
return ValueType(left, right);
|
|
75
75
|
throw new Error('ValueEquals: Unable to compare value');
|
|
76
76
|
}
|
package/value/guard.js
CHANGED
|
@@ -61,7 +61,7 @@ function IsUint8Array(value) {
|
|
|
61
61
|
exports.IsUint8Array = IsUint8Array;
|
|
62
62
|
/** Returns true if this value is a Date */
|
|
63
63
|
function IsDate(value) {
|
|
64
|
-
return value instanceof Date;
|
|
64
|
+
return value instanceof Date && Number.isFinite(value.getTime());
|
|
65
65
|
}
|
|
66
66
|
exports.IsDate = IsDate;
|
|
67
67
|
// --------------------------------------------------------------------------
|