@sinclair/typebox 0.29.5 → 0.30.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/value/create.js CHANGED
@@ -27,9 +27,10 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCreate = exports.ValueCreateRecursiveInstantiationError = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNotTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
30
+ exports.Create = exports.Visit = exports.ValueCreateRecursiveInstantiationError = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNotTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
31
31
  const Types = require("../typebox");
32
- const check_1 = require("./check");
32
+ const ValueCheck = require("./check");
33
+ const ValueGuard = require("./guard");
33
34
  // --------------------------------------------------------------------------
34
35
  // Errors
35
36
  // --------------------------------------------------------------------------
@@ -84,421 +85,432 @@ class ValueCreateRecursiveInstantiationError extends Error {
84
85
  }
85
86
  exports.ValueCreateRecursiveInstantiationError = ValueCreateRecursiveInstantiationError;
86
87
  // --------------------------------------------------------------------------
87
- // ValueCreate
88
+ // Types
88
89
  // --------------------------------------------------------------------------
89
- var ValueCreate;
90
- (function (ValueCreate) {
91
- // --------------------------------------------------------
92
- // Guards
93
- // --------------------------------------------------------
94
- function IsString(value) {
95
- return typeof value === 'string';
96
- }
97
- // --------------------------------------------------------
98
- // Types
99
- // --------------------------------------------------------
100
- function Any(schema, references) {
101
- if ('default' in schema) {
102
- return schema.default;
103
- }
104
- else {
105
- return {};
106
- }
90
+ function TAny(schema, references) {
91
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
92
+ return schema.default;
107
93
  }
108
- function Array(schema, references) {
109
- if (schema.uniqueItems === true && schema.default === undefined) {
110
- throw new Error('ValueCreate.Array: Arrays with uniqueItems require a default value');
111
- }
112
- else if ('default' in schema) {
113
- return schema.default;
114
- }
115
- else if (schema.minItems !== undefined) {
116
- return globalThis.Array.from({ length: schema.minItems }).map((item) => {
117
- return Visit(schema.items, references);
118
- });
119
- }
120
- else {
121
- return [];
122
- }
94
+ else {
95
+ return {};
123
96
  }
124
- function BigInt(schema, references) {
125
- if ('default' in schema) {
126
- return schema.default;
127
- }
128
- else {
129
- return globalThis.BigInt(0);
130
- }
97
+ }
98
+ function TArray(schema, references) {
99
+ if (schema.uniqueItems === true && !ValueGuard.HasPropertyKey(schema, 'default')) {
100
+ throw new Error('ValueCreate.Array: Array with the uniqueItems constraint requires a default value');
131
101
  }
132
- function Boolean(schema, references) {
133
- if ('default' in schema) {
134
- return schema.default;
135
- }
136
- else {
137
- return false;
138
- }
102
+ else if ('contains' in schema && !ValueGuard.HasPropertyKey(schema, 'default')) {
103
+ throw new Error('ValueCreate.Array: Array with the contains constraint requires a default value');
139
104
  }
140
- function Constructor(schema, references) {
141
- if ('default' in schema) {
142
- return schema.default;
143
- }
144
- else {
145
- const value = Visit(schema.returns, references);
146
- if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
147
- return class {
148
- constructor() {
149
- for (const [key, val] of globalThis.Object.entries(value)) {
150
- const self = this;
151
- self[key] = val;
152
- }
153
- }
154
- };
155
- }
156
- else {
157
- return class {
158
- };
159
- }
160
- }
105
+ else if ('default' in schema) {
106
+ return schema.default;
161
107
  }
162
- function Date(schema, references) {
163
- if ('default' in schema) {
164
- return schema.default;
165
- }
166
- else if (schema.minimumTimestamp !== undefined) {
167
- return new globalThis.Date(schema.minimumTimestamp);
168
- }
169
- else {
170
- return new globalThis.Date(0);
171
- }
108
+ else if (schema.minItems !== undefined) {
109
+ return Array.from({ length: schema.minItems }).map((item) => {
110
+ return Visit(schema.items, references);
111
+ });
172
112
  }
173
- function Function(schema, references) {
174
- if ('default' in schema) {
175
- return schema.default;
176
- }
177
- else {
178
- return () => Visit(schema.returns, references);
179
- }
113
+ else {
114
+ return [];
180
115
  }
181
- function Integer(schema, references) {
182
- if ('default' in schema) {
183
- return schema.default;
184
- }
185
- else if (schema.minimum !== undefined) {
186
- return schema.minimum;
187
- }
188
- else {
189
- return 0;
190
- }
116
+ }
117
+ function TAsyncIterator(schema, references) {
118
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
119
+ return schema.default;
191
120
  }
192
- function Intersect(schema, references) {
193
- if ('default' in schema) {
194
- return schema.default;
195
- }
196
- else {
197
- // Note: The best we can do here is attempt to instance each sub type and apply through object assign. For non-object
198
- // sub types, we just escape the assignment and just return the value. In the latter case, this is typically going to
199
- // be a consequence of an illogical intersection.
200
- const value = schema.allOf.reduce((acc, schema) => {
201
- const next = Visit(schema, references);
202
- return typeof next === 'object' ? { ...acc, ...next } : next;
203
- }, {});
204
- if (!check_1.ValueCheck.Check(schema, references, value))
205
- throw new ValueCreateIntersectTypeError(schema);
206
- return value;
207
- }
121
+ else {
122
+ return (async function* () { })();
208
123
  }
209
- function Literal(schema, references) {
210
- if ('default' in schema) {
211
- return schema.default;
212
- }
213
- else {
214
- return schema.const;
215
- }
124
+ }
125
+ function TBigInt(schema, references) {
126
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
127
+ return schema.default;
216
128
  }
217
- function Never(schema, references) {
218
- throw new ValueCreateNeverTypeError(schema);
129
+ else {
130
+ return BigInt(0);
219
131
  }
220
- function Not(schema, references) {
221
- if ('default' in schema) {
222
- return schema.default;
223
- }
224
- else {
225
- throw new ValueCreateNotTypeError(schema);
226
- }
132
+ }
133
+ function TBoolean(schema, references) {
134
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
135
+ return schema.default;
227
136
  }
228
- function Null(schema, references) {
229
- if ('default' in schema) {
230
- return schema.default;
231
- }
232
- else {
233
- return null;
234
- }
137
+ else {
138
+ return false;
235
139
  }
236
- function Number(schema, references) {
237
- if ('default' in schema) {
238
- return schema.default;
239
- }
240
- else if (schema.minimum !== undefined) {
241
- return schema.minimum;
140
+ }
141
+ function TConstructor(schema, references) {
142
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
143
+ return schema.default;
144
+ }
145
+ else {
146
+ const value = Visit(schema.returns, references);
147
+ if (typeof value === 'object' && !Array.isArray(value)) {
148
+ return class {
149
+ constructor() {
150
+ for (const [key, val] of Object.entries(value)) {
151
+ const self = this;
152
+ self[key] = val;
153
+ }
154
+ }
155
+ };
242
156
  }
243
157
  else {
244
- return 0;
158
+ return class {
159
+ };
245
160
  }
246
161
  }
247
- function Object(schema, references) {
248
- if ('default' in schema) {
249
- return schema.default;
250
- }
251
- else {
252
- const required = new Set(schema.required);
253
- return (schema.default ||
254
- globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
255
- return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
256
- }, {}));
257
- }
162
+ }
163
+ function TDate(schema, references) {
164
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
165
+ return schema.default;
258
166
  }
259
- function Promise(schema, references) {
260
- if ('default' in schema) {
261
- return schema.default;
262
- }
263
- else {
264
- return globalThis.Promise.resolve(Visit(schema.item, references));
265
- }
167
+ else if (schema.minimumTimestamp !== undefined) {
168
+ return new Date(schema.minimumTimestamp);
266
169
  }
267
- function Record(schema, references) {
268
- const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
269
- if ('default' in schema) {
270
- return schema.default;
271
- }
272
- else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) {
273
- const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
274
- return propertyKeys.reduce((acc, key) => {
275
- return { ...acc, [key]: Visit(valueSchema, references) };
276
- }, {});
277
- }
278
- else {
279
- return {};
280
- }
170
+ else {
171
+ return new Date(0);
281
172
  }
282
- function Ref(schema, references) {
283
- if ('default' in schema) {
284
- return schema.default;
285
- }
286
- else {
287
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
288
- if (index === -1)
289
- throw new ValueCreateDereferenceError(schema);
290
- const target = references[index];
291
- return Visit(target, references);
292
- }
173
+ }
174
+ function TFunction(schema, references) {
175
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
176
+ return schema.default;
293
177
  }
294
- function String(schema, references) {
295
- if (schema.pattern !== undefined) {
296
- if (!('default' in schema)) {
297
- throw new Error('ValueCreate.String: String types with patterns must specify a default value');
298
- }
299
- else {
300
- return schema.default;
301
- }
302
- }
303
- else if (schema.format !== undefined) {
304
- if (!('default' in schema)) {
305
- throw new Error('ValueCreate.String: String types with formats must specify a default value');
306
- }
307
- else {
308
- return schema.default;
309
- }
310
- }
311
- else {
312
- if ('default' in schema) {
313
- return schema.default;
314
- }
315
- else if (schema.minLength !== undefined) {
316
- return globalThis.Array.from({ length: schema.minLength })
317
- .map(() => '.')
318
- .join('');
319
- }
320
- else {
321
- return '';
322
- }
323
- }
178
+ else {
179
+ return () => Visit(schema.returns, references);
324
180
  }
325
- function Symbol(schema, references) {
326
- if ('default' in schema) {
327
- return schema.default;
328
- }
329
- else if ('value' in schema) {
330
- return globalThis.Symbol.for(schema.value);
331
- }
332
- else {
333
- return globalThis.Symbol();
334
- }
181
+ }
182
+ function TInteger(schema, references) {
183
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
184
+ return schema.default;
335
185
  }
336
- function TemplateLiteral(schema, references) {
337
- if ('default' in schema) {
338
- return schema.default;
339
- }
340
- const expression = Types.TemplateLiteralParser.ParseExact(schema.pattern);
341
- if (!Types.TemplateLiteralFinite.Check(expression))
342
- throw new ValueCreateTempateLiteralTypeError(schema);
343
- const sequence = Types.TemplateLiteralGenerator.Generate(expression);
344
- return sequence.next().value;
345
- }
346
- function This(schema, references) {
347
- if (recursiveDepth++ > recursiveMaxDepth)
348
- throw new ValueCreateRecursiveInstantiationError(schema, recursiveMaxDepth);
349
- if ('default' in schema) {
350
- return schema.default;
351
- }
352
- else {
353
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
354
- if (index === -1)
355
- throw new ValueCreateDereferenceError(schema);
356
- const target = references[index];
357
- return Visit(target, references);
358
- }
186
+ else if (schema.minimum !== undefined) {
187
+ return schema.minimum;
359
188
  }
360
- function Tuple(schema, references) {
361
- if ('default' in schema) {
362
- return schema.default;
363
- }
364
- if (schema.items === undefined) {
365
- return [];
366
- }
367
- else {
368
- return globalThis.Array.from({ length: schema.minItems }).map((_, index) => Visit(schema.items[index], references));
369
- }
189
+ else {
190
+ return 0;
370
191
  }
371
- function Undefined(schema, references) {
372
- if ('default' in schema) {
373
- return schema.default;
192
+ }
193
+ function TIntersect(schema, references) {
194
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
195
+ return schema.default;
196
+ }
197
+ else {
198
+ // Note: The best we can do here is attempt to instance each sub type and apply through object assign. For non-object
199
+ // sub types, we just escape the assignment and just return the value. In the latter case, this is typically going to
200
+ // be a consequence of an illogical intersection.
201
+ const value = schema.allOf.reduce((acc, schema) => {
202
+ const next = Visit(schema, references);
203
+ return typeof next === 'object' ? { ...acc, ...next } : next;
204
+ }, {});
205
+ if (!ValueCheck.Check(schema, references, value))
206
+ throw new ValueCreateIntersectTypeError(schema);
207
+ return value;
208
+ }
209
+ }
210
+ function TIterator(schema, references) {
211
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
212
+ return schema.default;
213
+ }
214
+ else {
215
+ return (function* () { })();
216
+ }
217
+ }
218
+ function TLiteral(schema, references) {
219
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
220
+ return schema.default;
221
+ }
222
+ else {
223
+ return schema.const;
224
+ }
225
+ }
226
+ function TNever(schema, references) {
227
+ throw new ValueCreateNeverTypeError(schema);
228
+ }
229
+ function TNot(schema, references) {
230
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
231
+ return schema.default;
232
+ }
233
+ else {
234
+ throw new ValueCreateNotTypeError(schema);
235
+ }
236
+ }
237
+ function TNull(schema, references) {
238
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
239
+ return schema.default;
240
+ }
241
+ else {
242
+ return null;
243
+ }
244
+ }
245
+ function TNumber(schema, references) {
246
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
247
+ return schema.default;
248
+ }
249
+ else if (schema.minimum !== undefined) {
250
+ return schema.minimum;
251
+ }
252
+ else {
253
+ return 0;
254
+ }
255
+ }
256
+ function TObject(schema, references) {
257
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
258
+ return schema.default;
259
+ }
260
+ else {
261
+ const required = new Set(schema.required);
262
+ return (schema.default ||
263
+ Object.entries(schema.properties).reduce((acc, [key, schema]) => {
264
+ return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
265
+ }, {}));
266
+ }
267
+ }
268
+ function TPromise(schema, references) {
269
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
270
+ return schema.default;
271
+ }
272
+ else {
273
+ return Promise.resolve(Visit(schema.item, references));
274
+ }
275
+ }
276
+ function TRecord(schema, references) {
277
+ const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
278
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
279
+ return schema.default;
280
+ }
281
+ else if (!(keyPattern === Types.PatternStringExact || keyPattern === Types.PatternNumberExact)) {
282
+ const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
283
+ return propertyKeys.reduce((acc, key) => {
284
+ return { ...acc, [key]: Visit(valueSchema, references) };
285
+ }, {});
286
+ }
287
+ else {
288
+ return {};
289
+ }
290
+ }
291
+ function TRef(schema, references) {
292
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
293
+ return schema.default;
294
+ }
295
+ else {
296
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
297
+ if (index === -1)
298
+ throw new ValueCreateDereferenceError(schema);
299
+ const target = references[index];
300
+ return Visit(target, references);
301
+ }
302
+ }
303
+ function TString(schema, references) {
304
+ if (schema.pattern !== undefined) {
305
+ if (!ValueGuard.HasPropertyKey(schema, 'default')) {
306
+ throw new Error('ValueCreate.String: String types with patterns must specify a default value');
374
307
  }
375
308
  else {
376
- return undefined;
377
- }
378
- }
379
- function Union(schema, references) {
380
- if ('default' in schema) {
381
309
  return schema.default;
382
310
  }
383
- else if (schema.anyOf.length === 0) {
384
- throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
311
+ }
312
+ else if (schema.format !== undefined) {
313
+ if (!ValueGuard.HasPropertyKey(schema, 'default')) {
314
+ throw new Error('ValueCreate.String: String types with formats must specify a default value');
385
315
  }
386
316
  else {
387
- return Visit(schema.anyOf[0], references);
317
+ return schema.default;
388
318
  }
389
319
  }
390
- function Uint8Array(schema, references) {
391
- if ('default' in schema) {
320
+ else {
321
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
392
322
  return schema.default;
393
323
  }
394
- else if (schema.minByteLength !== undefined) {
395
- return new globalThis.Uint8Array(schema.minByteLength);
324
+ else if (schema.minLength !== undefined) {
325
+ return Array.from({ length: schema.minLength })
326
+ .map(() => '.')
327
+ .join('');
396
328
  }
397
329
  else {
398
- return new globalThis.Uint8Array(0);
330
+ return '';
399
331
  }
400
332
  }
401
- function Unknown(schema, references) {
402
- if ('default' in schema) {
403
- return schema.default;
404
- }
405
- else {
406
- return {};
407
- }
333
+ }
334
+ function TSymbol(schema, references) {
335
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
336
+ return schema.default;
408
337
  }
409
- function Void(schema, references) {
410
- if ('default' in schema) {
411
- return schema.default;
412
- }
413
- else {
414
- return void 0;
415
- }
338
+ else if ('value' in schema) {
339
+ return Symbol.for(schema.value);
416
340
  }
417
- function UserDefined(schema, references) {
418
- if ('default' in schema) {
419
- return schema.default;
420
- }
421
- else {
422
- throw new Error('ValueCreate.UserDefined: User defined types must specify a default value');
423
- }
341
+ else {
342
+ return Symbol();
424
343
  }
425
- /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
426
- function Visit(schema, references) {
427
- const references_ = IsString(schema.$id) ? [...references, schema] : references;
428
- const schema_ = schema;
429
- switch (schema_[Types.Kind]) {
430
- case 'Any':
431
- return Any(schema_, references_);
432
- case 'Array':
433
- return Array(schema_, references_);
434
- case 'BigInt':
435
- return BigInt(schema_, references_);
436
- case 'Boolean':
437
- return Boolean(schema_, references_);
438
- case 'Constructor':
439
- return Constructor(schema_, references_);
440
- case 'Date':
441
- return Date(schema_, references_);
442
- case 'Function':
443
- return Function(schema_, references_);
444
- case 'Integer':
445
- return Integer(schema_, references_);
446
- case 'Intersect':
447
- return Intersect(schema_, references_);
448
- case 'Literal':
449
- return Literal(schema_, references_);
450
- case 'Never':
451
- return Never(schema_, references_);
452
- case 'Not':
453
- return Not(schema_, references_);
454
- case 'Null':
455
- return Null(schema_, references_);
456
- case 'Number':
457
- return Number(schema_, references_);
458
- case 'Object':
459
- return Object(schema_, references_);
460
- case 'Promise':
461
- return Promise(schema_, references_);
462
- case 'Record':
463
- return Record(schema_, references_);
464
- case 'Ref':
465
- return Ref(schema_, references_);
466
- case 'String':
467
- return String(schema_, references_);
468
- case 'Symbol':
469
- return Symbol(schema_, references_);
470
- case 'TemplateLiteral':
471
- return TemplateLiteral(schema_, references_);
472
- case 'This':
473
- return This(schema_, references_);
474
- case 'Tuple':
475
- return Tuple(schema_, references_);
476
- case 'Undefined':
477
- return Undefined(schema_, references_);
478
- case 'Union':
479
- return Union(schema_, references_);
480
- case 'Uint8Array':
481
- return Uint8Array(schema_, references_);
482
- case 'Unknown':
483
- return Unknown(schema_, references_);
484
- case 'Void':
485
- return Void(schema_, references_);
486
- default:
487
- if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
488
- throw new ValueCreateUnknownTypeError(schema_);
489
- return UserDefined(schema_, references_);
490
- }
344
+ }
345
+ function TTemplateLiteral(schema, references) {
346
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
347
+ return schema.default;
348
+ }
349
+ const expression = Types.TemplateLiteralParser.ParseExact(schema.pattern);
350
+ if (!Types.TemplateLiteralFinite.Check(expression))
351
+ throw new ValueCreateTempateLiteralTypeError(schema);
352
+ const sequence = Types.TemplateLiteralGenerator.Generate(expression);
353
+ return sequence.next().value;
354
+ }
355
+ function TThis(schema, references) {
356
+ if (recursiveDepth++ > recursiveMaxDepth)
357
+ throw new ValueCreateRecursiveInstantiationError(schema, recursiveMaxDepth);
358
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
359
+ return schema.default;
360
+ }
361
+ else {
362
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
363
+ if (index === -1)
364
+ throw new ValueCreateDereferenceError(schema);
365
+ const target = references[index];
366
+ return Visit(target, references);
367
+ }
368
+ }
369
+ function TTuple(schema, references) {
370
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
371
+ return schema.default;
372
+ }
373
+ if (schema.items === undefined) {
374
+ return [];
375
+ }
376
+ else {
377
+ return Array.from({ length: schema.minItems }).map((_, index) => Visit(schema.items[index], references));
378
+ }
379
+ }
380
+ function TUndefined(schema, references) {
381
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
382
+ return schema.default;
383
+ }
384
+ else {
385
+ return undefined;
491
386
  }
492
- ValueCreate.Visit = Visit;
493
- // --------------------------------------------------------
494
- // State
495
- // --------------------------------------------------------
496
- const recursiveMaxDepth = 512;
497
- let recursiveDepth = 0;
498
- /** Creates a value from the given schema and references */
499
- function Create(schema, references) {
500
- recursiveDepth = 0;
501
- return Visit(schema, references);
502
- }
503
- ValueCreate.Create = Create;
504
- })(ValueCreate || (exports.ValueCreate = ValueCreate = {}));
387
+ }
388
+ function TUnion(schema, references) {
389
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
390
+ return schema.default;
391
+ }
392
+ else if (schema.anyOf.length === 0) {
393
+ throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
394
+ }
395
+ else {
396
+ return Visit(schema.anyOf[0], references);
397
+ }
398
+ }
399
+ function TUint8Array(schema, references) {
400
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
401
+ return schema.default;
402
+ }
403
+ else if (schema.minByteLength !== undefined) {
404
+ return new Uint8Array(schema.minByteLength);
405
+ }
406
+ else {
407
+ return new Uint8Array(0);
408
+ }
409
+ }
410
+ function TUnknown(schema, references) {
411
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
412
+ return schema.default;
413
+ }
414
+ else {
415
+ return {};
416
+ }
417
+ }
418
+ function TVoid(schema, references) {
419
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
420
+ return schema.default;
421
+ }
422
+ else {
423
+ return void 0;
424
+ }
425
+ }
426
+ function TKind(schema, references) {
427
+ if (ValueGuard.HasPropertyKey(schema, 'default')) {
428
+ return schema.default;
429
+ }
430
+ else {
431
+ throw new Error('ValueCreate: User defined types must specify a default value');
432
+ }
433
+ }
434
+ /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
435
+ function Visit(schema, references) {
436
+ const references_ = ValueGuard.IsString(schema.$id) ? [...references, schema] : references;
437
+ const schema_ = schema;
438
+ switch (schema_[Types.Kind]) {
439
+ case 'Any':
440
+ return TAny(schema_, references_);
441
+ case 'Array':
442
+ return TArray(schema_, references_);
443
+ case 'AsyncIterator':
444
+ return TAsyncIterator(schema_, references_);
445
+ case 'BigInt':
446
+ return TBigInt(schema_, references_);
447
+ case 'Boolean':
448
+ return TBoolean(schema_, references_);
449
+ case 'Constructor':
450
+ return TConstructor(schema_, references_);
451
+ case 'Date':
452
+ return TDate(schema_, references_);
453
+ case 'Function':
454
+ return TFunction(schema_, references_);
455
+ case 'Integer':
456
+ return TInteger(schema_, references_);
457
+ case 'Intersect':
458
+ return TIntersect(schema_, references_);
459
+ case 'Iterator':
460
+ return TIterator(schema_, references_);
461
+ case 'Literal':
462
+ return TLiteral(schema_, references_);
463
+ case 'Never':
464
+ return TNever(schema_, references_);
465
+ case 'Not':
466
+ return TNot(schema_, references_);
467
+ case 'Null':
468
+ return TNull(schema_, references_);
469
+ case 'Number':
470
+ return TNumber(schema_, references_);
471
+ case 'Object':
472
+ return TObject(schema_, references_);
473
+ case 'Promise':
474
+ return TPromise(schema_, references_);
475
+ case 'Record':
476
+ return TRecord(schema_, references_);
477
+ case 'Ref':
478
+ return TRef(schema_, references_);
479
+ case 'String':
480
+ return TString(schema_, references_);
481
+ case 'Symbol':
482
+ return TSymbol(schema_, references_);
483
+ case 'TemplateLiteral':
484
+ return TTemplateLiteral(schema_, references_);
485
+ case 'This':
486
+ return TThis(schema_, references_);
487
+ case 'Tuple':
488
+ return TTuple(schema_, references_);
489
+ case 'Undefined':
490
+ return TUndefined(schema_, references_);
491
+ case 'Union':
492
+ return TUnion(schema_, references_);
493
+ case 'Uint8Array':
494
+ return TUint8Array(schema_, references_);
495
+ case 'Unknown':
496
+ return TUnknown(schema_, references_);
497
+ case 'Void':
498
+ return TVoid(schema_, references_);
499
+ default:
500
+ if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
501
+ throw new ValueCreateUnknownTypeError(schema_);
502
+ return TKind(schema_, references_);
503
+ }
504
+ }
505
+ exports.Visit = Visit;
506
+ // --------------------------------------------------------------------------
507
+ // State
508
+ // --------------------------------------------------------------------------
509
+ const recursiveMaxDepth = 512;
510
+ let recursiveDepth = 0;
511
+ /** Creates a value from the given schema */
512
+ function Create(...args) {
513
+ recursiveDepth = 0;
514
+ return args.length === 2 ? Visit(args[0], args[1]) : Visit(args[0], []);
515
+ }
516
+ exports.Create = Create;