@sinclair/typebox 0.26.0-dev.2 → 0.26.0-dev.4

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
@@ -1,430 +1,430 @@
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.ValueCreate = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
31
- const Types = require("../typebox");
32
- const check_1 = require("./check");
33
- // --------------------------------------------------------------------------
34
- // Errors
35
- // --------------------------------------------------------------------------
36
- class ValueCreateUnknownTypeError extends Error {
37
- constructor(schema) {
38
- super('ValueCreate: Unknown type');
39
- this.schema = schema;
40
- }
41
- }
42
- exports.ValueCreateUnknownTypeError = ValueCreateUnknownTypeError;
43
- class ValueCreateNeverTypeError extends Error {
44
- constructor(schema) {
45
- super('ValueCreate: Never types cannot be created');
46
- this.schema = schema;
47
- }
48
- }
49
- exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
50
- class ValueCreateIntersectTypeError extends Error {
51
- constructor(schema) {
52
- super('ValueCreate: Can only create values for intersected objects and non-varying primitive types. Consider using a default value.');
53
- this.schema = schema;
54
- }
55
- }
56
- exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError;
57
- // --------------------------------------------------------------------------
58
- // ValueCreate
59
- // --------------------------------------------------------------------------
60
- var ValueCreate;
61
- (function (ValueCreate) {
62
- function Any(schema) {
63
- if ('default' in schema) {
64
- return schema.default;
65
- }
66
- else {
67
- return {};
68
- }
69
- }
70
- function Array(schema) {
71
- if (schema.uniqueItems === true && schema.default === undefined) {
72
- throw new Error('ValueCreate.Array: Arrays with uniqueItems require a default value');
73
- }
74
- else if ('default' in schema) {
75
- return schema.default;
76
- }
77
- else if (schema.minItems !== undefined) {
78
- return globalThis.Array.from({ length: schema.minItems }).map((item) => {
79
- return ValueCreate.Create(schema.items);
80
- });
81
- }
82
- else {
83
- return [];
84
- }
85
- }
86
- function BigInt(schema) {
87
- if ('default' in schema) {
88
- return schema.default;
89
- }
90
- else {
91
- return globalThis.BigInt(0);
92
- }
93
- }
94
- function Boolean(schema) {
95
- if ('default' in schema) {
96
- return schema.default;
97
- }
98
- else {
99
- return false;
100
- }
101
- }
102
- function Constructor(schema) {
103
- if ('default' in schema) {
104
- return schema.default;
105
- }
106
- else {
107
- const value = ValueCreate.Create(schema.returns);
108
- if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
109
- return class {
110
- constructor() {
111
- for (const [key, val] of globalThis.Object.entries(value)) {
112
- const self = this;
113
- self[key] = val;
114
- }
115
- }
116
- };
117
- }
118
- else {
119
- return class {
120
- };
121
- }
122
- }
123
- }
124
- function Date(schema) {
125
- if ('default' in schema) {
126
- return schema.default;
127
- }
128
- else if (schema.minimumTimestamp !== undefined) {
129
- return new globalThis.Date(schema.minimumTimestamp);
130
- }
131
- else {
132
- return new globalThis.Date(0);
133
- }
134
- }
135
- function Function(schema) {
136
- if ('default' in schema) {
137
- return schema.default;
138
- }
139
- else {
140
- return () => ValueCreate.Create(schema.returns);
141
- }
142
- }
143
- function Integer(schema) {
144
- if ('default' in schema) {
145
- return schema.default;
146
- }
147
- else if (schema.minimum !== undefined) {
148
- return schema.minimum;
149
- }
150
- else {
151
- return 0;
152
- }
153
- }
154
- function Intersect(schema) {
155
- if ('default' in schema) {
156
- return schema.default;
157
- }
158
- else {
159
- const value = schema.type === 'object' ? schema.allOf.reduce((acc, schema) => ({ ...acc, ...Visit(schema) }), {}) : schema.allOf.reduce((_, schema) => Visit(schema), undefined);
160
- if (!check_1.ValueCheck.Check(schema, value))
161
- throw new ValueCreateIntersectTypeError(schema);
162
- return value;
163
- }
164
- }
165
- function Literal(schema) {
166
- if ('default' in schema) {
167
- return schema.default;
168
- }
169
- else {
170
- return schema.const;
171
- }
172
- }
173
- function Never(schema) {
174
- throw new ValueCreateNeverTypeError(schema);
175
- }
176
- function Not(schema) {
177
- if ('default' in schema) {
178
- return schema.default;
179
- }
180
- else {
181
- return Visit(schema.allOf[1]);
182
- }
183
- }
184
- function Null(schema) {
185
- if ('default' in schema) {
186
- return schema.default;
187
- }
188
- else {
189
- return null;
190
- }
191
- }
192
- function Number(schema) {
193
- if ('default' in schema) {
194
- return schema.default;
195
- }
196
- else if (schema.minimum !== undefined) {
197
- return schema.minimum;
198
- }
199
- else {
200
- return 0;
201
- }
202
- }
203
- function Object(schema) {
204
- if ('default' in schema) {
205
- return schema.default;
206
- }
207
- else {
208
- const required = new Set(schema.required);
209
- return (schema.default ||
210
- globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
211
- return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema) } : { ...acc };
212
- }, {}));
213
- }
214
- }
215
- function Promise(schema) {
216
- if ('default' in schema) {
217
- return schema.default;
218
- }
219
- else {
220
- return globalThis.Promise.resolve(ValueCreate.Create(schema.item));
221
- }
222
- }
223
- function Record(schema) {
224
- const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
225
- if ('default' in schema) {
226
- return schema.default;
227
- }
228
- else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
229
- const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
230
- return propertyKeys.reduce((acc, key) => {
231
- return { ...acc, [key]: Create(valueSchema) };
232
- }, {});
233
- }
234
- else {
235
- return {};
236
- }
237
- }
238
- function Ref(schema) {
239
- if ('default' in schema) {
240
- return schema.default;
241
- }
242
- else {
243
- return Visit(Types.ReferenceRegistry.DerefOne(schema));
244
- }
245
- }
246
- function Self(schema) {
247
- if ('default' in schema) {
248
- return schema.default;
249
- }
250
- else {
251
- return Visit(Types.ReferenceRegistry.DerefOne(schema));
252
- }
253
- }
254
- function String(schema) {
255
- if (schema.pattern !== undefined) {
256
- if (!('default' in schema)) {
257
- throw new Error('ValueCreate.String: String types with patterns must specify a default value');
258
- }
259
- else {
260
- return schema.default;
261
- }
262
- }
263
- else if (schema.format !== undefined) {
264
- if (!('default' in schema)) {
265
- throw new Error('ValueCreate.String: String types with formats must specify a default value');
266
- }
267
- else {
268
- return schema.default;
269
- }
270
- }
271
- else {
272
- if ('default' in schema) {
273
- return schema.default;
274
- }
275
- else if (schema.minLength !== undefined) {
276
- return globalThis.Array.from({ length: schema.minLength })
277
- .map(() => '.')
278
- .join('');
279
- }
280
- else {
281
- return '';
282
- }
283
- }
284
- }
285
- function Symbol(schema) {
286
- if ('default' in schema) {
287
- return schema.default;
288
- }
289
- else if ('value' in schema) {
290
- return globalThis.Symbol.for(schema.value);
291
- }
292
- else {
293
- return globalThis.Symbol();
294
- }
295
- }
296
- function Tuple(schema) {
297
- if ('default' in schema) {
298
- return schema.default;
299
- }
300
- if (schema.items === undefined) {
301
- return [];
302
- }
303
- else {
304
- return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index]));
305
- }
306
- }
307
- function Undefined(schema) {
308
- if ('default' in schema) {
309
- return schema.default;
310
- }
311
- else {
312
- return undefined;
313
- }
314
- }
315
- function Union(schema) {
316
- if ('default' in schema) {
317
- return schema.default;
318
- }
319
- else if (schema.anyOf.length === 0) {
320
- throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
321
- }
322
- else {
323
- return ValueCreate.Create(schema.anyOf[0]);
324
- }
325
- }
326
- function Uint8Array(schema) {
327
- if ('default' in schema) {
328
- return schema.default;
329
- }
330
- else if (schema.minByteLength !== undefined) {
331
- return new globalThis.Uint8Array(schema.minByteLength);
332
- }
333
- else {
334
- return new globalThis.Uint8Array(0);
335
- }
336
- }
337
- function Unknown(schema) {
338
- if ('default' in schema) {
339
- return schema.default;
340
- }
341
- else {
342
- return {};
343
- }
344
- }
345
- function Void(schema) {
346
- if ('default' in schema) {
347
- return schema.default;
348
- }
349
- else {
350
- return void 0;
351
- }
352
- }
353
- function UserDefined(schema) {
354
- if ('default' in schema) {
355
- return schema.default;
356
- }
357
- else {
358
- throw new Error('ValueCreate.UserDefined: User defined types must specify a default value');
359
- }
360
- }
361
- /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
362
- function Visit(schema) {
363
- const anySchema = schema;
364
- switch (anySchema[Types.Kind]) {
365
- case 'Any':
366
- return Any(anySchema);
367
- case 'Array':
368
- return Array(anySchema);
369
- case 'BigInt':
370
- return BigInt(anySchema);
371
- case 'Boolean':
372
- return Boolean(anySchema);
373
- case 'Constructor':
374
- return Constructor(anySchema);
375
- case 'Date':
376
- return Date(anySchema);
377
- case 'Function':
378
- return Function(anySchema);
379
- case 'Integer':
380
- return Integer(anySchema);
381
- case 'Intersect':
382
- return Intersect(anySchema);
383
- case 'Literal':
384
- return Literal(anySchema);
385
- case 'Never':
386
- return Never(anySchema);
387
- case 'Not':
388
- return Not(anySchema);
389
- case 'Null':
390
- return Null(anySchema);
391
- case 'Number':
392
- return Number(anySchema);
393
- case 'Object':
394
- return Object(anySchema);
395
- case 'Promise':
396
- return Promise(anySchema);
397
- case 'Record':
398
- return Record(anySchema);
399
- case 'Ref':
400
- return Ref(anySchema);
401
- case 'Self':
402
- return Self(anySchema);
403
- case 'String':
404
- return String(anySchema);
405
- case 'Symbol':
406
- return Symbol(anySchema);
407
- case 'Tuple':
408
- return Tuple(anySchema);
409
- case 'Undefined':
410
- return Undefined(anySchema);
411
- case 'Union':
412
- return Union(anySchema);
413
- case 'Uint8Array':
414
- return Uint8Array(anySchema);
415
- case 'Unknown':
416
- return Unknown(anySchema);
417
- case 'Void':
418
- return Void(anySchema);
419
- default:
420
- if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
421
- throw new ValueCreateUnknownTypeError(anySchema);
422
- return UserDefined(anySchema);
423
- }
424
- }
425
- ValueCreate.Visit = Visit;
426
- function Create(schema) {
427
- return Visit(schema);
428
- }
429
- ValueCreate.Create = Create;
430
- })(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {}));
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.ValueCreate = exports.ValueCreateIntersectTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
31
+ const Types = require("../typebox");
32
+ const check_1 = require("./check");
33
+ // --------------------------------------------------------------------------
34
+ // Errors
35
+ // --------------------------------------------------------------------------
36
+ class ValueCreateUnknownTypeError extends Error {
37
+ constructor(schema) {
38
+ super('ValueCreate: Unknown type');
39
+ this.schema = schema;
40
+ }
41
+ }
42
+ exports.ValueCreateUnknownTypeError = ValueCreateUnknownTypeError;
43
+ class ValueCreateNeverTypeError extends Error {
44
+ constructor(schema) {
45
+ super('ValueCreate: Never types cannot be created');
46
+ this.schema = schema;
47
+ }
48
+ }
49
+ exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
50
+ class ValueCreateIntersectTypeError extends Error {
51
+ constructor(schema) {
52
+ super('ValueCreate: Can only create values for intersected objects and non-varying primitive types. Consider using a default value.');
53
+ this.schema = schema;
54
+ }
55
+ }
56
+ exports.ValueCreateIntersectTypeError = ValueCreateIntersectTypeError;
57
+ // --------------------------------------------------------------------------
58
+ // ValueCreate
59
+ // --------------------------------------------------------------------------
60
+ var ValueCreate;
61
+ (function (ValueCreate) {
62
+ function Any(schema) {
63
+ if ('default' in schema) {
64
+ return schema.default;
65
+ }
66
+ else {
67
+ return {};
68
+ }
69
+ }
70
+ function Array(schema) {
71
+ if (schema.uniqueItems === true && schema.default === undefined) {
72
+ throw new Error('ValueCreate.Array: Arrays with uniqueItems require a default value');
73
+ }
74
+ else if ('default' in schema) {
75
+ return schema.default;
76
+ }
77
+ else if (schema.minItems !== undefined) {
78
+ return globalThis.Array.from({ length: schema.minItems }).map((item) => {
79
+ return ValueCreate.Create(schema.items);
80
+ });
81
+ }
82
+ else {
83
+ return [];
84
+ }
85
+ }
86
+ function BigInt(schema) {
87
+ if ('default' in schema) {
88
+ return schema.default;
89
+ }
90
+ else {
91
+ return globalThis.BigInt(0);
92
+ }
93
+ }
94
+ function Boolean(schema) {
95
+ if ('default' in schema) {
96
+ return schema.default;
97
+ }
98
+ else {
99
+ return false;
100
+ }
101
+ }
102
+ function Constructor(schema) {
103
+ if ('default' in schema) {
104
+ return schema.default;
105
+ }
106
+ else {
107
+ const value = ValueCreate.Create(schema.returns);
108
+ if (typeof value === 'object' && !globalThis.Array.isArray(value)) {
109
+ return class {
110
+ constructor() {
111
+ for (const [key, val] of globalThis.Object.entries(value)) {
112
+ const self = this;
113
+ self[key] = val;
114
+ }
115
+ }
116
+ };
117
+ }
118
+ else {
119
+ return class {
120
+ };
121
+ }
122
+ }
123
+ }
124
+ function Date(schema) {
125
+ if ('default' in schema) {
126
+ return schema.default;
127
+ }
128
+ else if (schema.minimumTimestamp !== undefined) {
129
+ return new globalThis.Date(schema.minimumTimestamp);
130
+ }
131
+ else {
132
+ return new globalThis.Date(0);
133
+ }
134
+ }
135
+ function Function(schema) {
136
+ if ('default' in schema) {
137
+ return schema.default;
138
+ }
139
+ else {
140
+ return () => ValueCreate.Create(schema.returns);
141
+ }
142
+ }
143
+ function Integer(schema) {
144
+ if ('default' in schema) {
145
+ return schema.default;
146
+ }
147
+ else if (schema.minimum !== undefined) {
148
+ return schema.minimum;
149
+ }
150
+ else {
151
+ return 0;
152
+ }
153
+ }
154
+ function Intersect(schema) {
155
+ if ('default' in schema) {
156
+ return schema.default;
157
+ }
158
+ else {
159
+ const value = schema.type === 'object' ? schema.allOf.reduce((acc, schema) => ({ ...acc, ...Visit(schema) }), {}) : schema.allOf.reduce((_, schema) => Visit(schema), undefined);
160
+ if (!check_1.ValueCheck.Check(schema, value))
161
+ throw new ValueCreateIntersectTypeError(schema);
162
+ return value;
163
+ }
164
+ }
165
+ function Literal(schema) {
166
+ if ('default' in schema) {
167
+ return schema.default;
168
+ }
169
+ else {
170
+ return schema.const;
171
+ }
172
+ }
173
+ function Never(schema) {
174
+ throw new ValueCreateNeverTypeError(schema);
175
+ }
176
+ function Not(schema) {
177
+ if ('default' in schema) {
178
+ return schema.default;
179
+ }
180
+ else {
181
+ return Visit(schema.allOf[1]);
182
+ }
183
+ }
184
+ function Null(schema) {
185
+ if ('default' in schema) {
186
+ return schema.default;
187
+ }
188
+ else {
189
+ return null;
190
+ }
191
+ }
192
+ function Number(schema) {
193
+ if ('default' in schema) {
194
+ return schema.default;
195
+ }
196
+ else if (schema.minimum !== undefined) {
197
+ return schema.minimum;
198
+ }
199
+ else {
200
+ return 0;
201
+ }
202
+ }
203
+ function Object(schema) {
204
+ if ('default' in schema) {
205
+ return schema.default;
206
+ }
207
+ else {
208
+ const required = new Set(schema.required);
209
+ return (schema.default ||
210
+ globalThis.Object.entries(schema.properties).reduce((acc, [key, schema]) => {
211
+ return required.has(key) ? { ...acc, [key]: ValueCreate.Create(schema) } : { ...acc };
212
+ }, {}));
213
+ }
214
+ }
215
+ function Promise(schema) {
216
+ if ('default' in schema) {
217
+ return schema.default;
218
+ }
219
+ else {
220
+ return globalThis.Promise.resolve(ValueCreate.Create(schema.item));
221
+ }
222
+ }
223
+ function Record(schema) {
224
+ const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
225
+ if ('default' in schema) {
226
+ return schema.default;
227
+ }
228
+ else if (!(keyPattern === '^.*$' || keyPattern === '^(0|[1-9][0-9]*)$')) {
229
+ const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
230
+ return propertyKeys.reduce((acc, key) => {
231
+ return { ...acc, [key]: Create(valueSchema) };
232
+ }, {});
233
+ }
234
+ else {
235
+ return {};
236
+ }
237
+ }
238
+ function Ref(schema) {
239
+ if ('default' in schema) {
240
+ return schema.default;
241
+ }
242
+ else {
243
+ return Visit(Types.ReferenceRegistry.DerefOne(schema));
244
+ }
245
+ }
246
+ function Self(schema) {
247
+ if ('default' in schema) {
248
+ return schema.default;
249
+ }
250
+ else {
251
+ return Visit(Types.ReferenceRegistry.DerefOne(schema));
252
+ }
253
+ }
254
+ function String(schema) {
255
+ if (schema.pattern !== undefined) {
256
+ if (!('default' in schema)) {
257
+ throw new Error('ValueCreate.String: String types with patterns must specify a default value');
258
+ }
259
+ else {
260
+ return schema.default;
261
+ }
262
+ }
263
+ else if (schema.format !== undefined) {
264
+ if (!('default' in schema)) {
265
+ throw new Error('ValueCreate.String: String types with formats must specify a default value');
266
+ }
267
+ else {
268
+ return schema.default;
269
+ }
270
+ }
271
+ else {
272
+ if ('default' in schema) {
273
+ return schema.default;
274
+ }
275
+ else if (schema.minLength !== undefined) {
276
+ return globalThis.Array.from({ length: schema.minLength })
277
+ .map(() => '.')
278
+ .join('');
279
+ }
280
+ else {
281
+ return '';
282
+ }
283
+ }
284
+ }
285
+ function Symbol(schema) {
286
+ if ('default' in schema) {
287
+ return schema.default;
288
+ }
289
+ else if ('value' in schema) {
290
+ return globalThis.Symbol.for(schema.value);
291
+ }
292
+ else {
293
+ return globalThis.Symbol();
294
+ }
295
+ }
296
+ function Tuple(schema) {
297
+ if ('default' in schema) {
298
+ return schema.default;
299
+ }
300
+ if (schema.items === undefined) {
301
+ return [];
302
+ }
303
+ else {
304
+ return globalThis.Array.from({ length: schema.minItems }).map((_, index) => ValueCreate.Create(schema.items[index]));
305
+ }
306
+ }
307
+ function Undefined(schema) {
308
+ if ('default' in schema) {
309
+ return schema.default;
310
+ }
311
+ else {
312
+ return undefined;
313
+ }
314
+ }
315
+ function Union(schema) {
316
+ if ('default' in schema) {
317
+ return schema.default;
318
+ }
319
+ else if (schema.anyOf.length === 0) {
320
+ throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
321
+ }
322
+ else {
323
+ return ValueCreate.Create(schema.anyOf[0]);
324
+ }
325
+ }
326
+ function Uint8Array(schema) {
327
+ if ('default' in schema) {
328
+ return schema.default;
329
+ }
330
+ else if (schema.minByteLength !== undefined) {
331
+ return new globalThis.Uint8Array(schema.minByteLength);
332
+ }
333
+ else {
334
+ return new globalThis.Uint8Array(0);
335
+ }
336
+ }
337
+ function Unknown(schema) {
338
+ if ('default' in schema) {
339
+ return schema.default;
340
+ }
341
+ else {
342
+ return {};
343
+ }
344
+ }
345
+ function Void(schema) {
346
+ if ('default' in schema) {
347
+ return schema.default;
348
+ }
349
+ else {
350
+ return void 0;
351
+ }
352
+ }
353
+ function UserDefined(schema) {
354
+ if ('default' in schema) {
355
+ return schema.default;
356
+ }
357
+ else {
358
+ throw new Error('ValueCreate.UserDefined: User defined types must specify a default value');
359
+ }
360
+ }
361
+ /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
362
+ function Visit(schema) {
363
+ const anySchema = schema;
364
+ switch (anySchema[Types.Kind]) {
365
+ case 'Any':
366
+ return Any(anySchema);
367
+ case 'Array':
368
+ return Array(anySchema);
369
+ case 'BigInt':
370
+ return BigInt(anySchema);
371
+ case 'Boolean':
372
+ return Boolean(anySchema);
373
+ case 'Constructor':
374
+ return Constructor(anySchema);
375
+ case 'Date':
376
+ return Date(anySchema);
377
+ case 'Function':
378
+ return Function(anySchema);
379
+ case 'Integer':
380
+ return Integer(anySchema);
381
+ case 'Intersect':
382
+ return Intersect(anySchema);
383
+ case 'Literal':
384
+ return Literal(anySchema);
385
+ case 'Never':
386
+ return Never(anySchema);
387
+ case 'Not':
388
+ return Not(anySchema);
389
+ case 'Null':
390
+ return Null(anySchema);
391
+ case 'Number':
392
+ return Number(anySchema);
393
+ case 'Object':
394
+ return Object(anySchema);
395
+ case 'Promise':
396
+ return Promise(anySchema);
397
+ case 'Record':
398
+ return Record(anySchema);
399
+ case 'Ref':
400
+ return Ref(anySchema);
401
+ case 'Self':
402
+ return Self(anySchema);
403
+ case 'String':
404
+ return String(anySchema);
405
+ case 'Symbol':
406
+ return Symbol(anySchema);
407
+ case 'Tuple':
408
+ return Tuple(anySchema);
409
+ case 'Undefined':
410
+ return Undefined(anySchema);
411
+ case 'Union':
412
+ return Union(anySchema);
413
+ case 'Uint8Array':
414
+ return Uint8Array(anySchema);
415
+ case 'Unknown':
416
+ return Unknown(anySchema);
417
+ case 'Void':
418
+ return Void(anySchema);
419
+ default:
420
+ if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
421
+ throw new ValueCreateUnknownTypeError(anySchema);
422
+ return UserDefined(anySchema);
423
+ }
424
+ }
425
+ ValueCreate.Visit = Visit;
426
+ function Create(schema) {
427
+ return Visit(schema);
428
+ }
429
+ ValueCreate.Create = Create;
430
+ })(ValueCreate = exports.ValueCreate || (exports.ValueCreate = {}));