@sinclair/typebox 0.32.6 → 0.32.8
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/build/import/value/cast/cast.mjs +1 -1
- package/build/import/value/create/create.mjs +42 -35
- package/build/import/value/default/default.mjs +2 -1
- package/build/require/value/cast/cast.js +1 -1
- package/build/require/value/create/create.js +56 -49
- package/build/require/value/default/default.js +15 -14
- package/package.json +1 -1
|
@@ -53,7 +53,7 @@ function SelectUnion(union, references, value) {
|
|
|
53
53
|
}
|
|
54
54
|
function CastUnion(union, references, value) {
|
|
55
55
|
if ('default' in union) {
|
|
56
|
-
return union.default;
|
|
56
|
+
return typeof value === 'function' ? union.default : Clone(union.default);
|
|
57
57
|
}
|
|
58
58
|
else {
|
|
59
59
|
const schema = SelectUnion(union, references, value);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HasPropertyKey, IsString } from '../guard/index.mjs';
|
|
2
2
|
import { Check } from '../check/index.mjs';
|
|
3
|
+
import { Clone } from '../clone/index.mjs';
|
|
3
4
|
import { Deref } from '../deref/index.mjs';
|
|
4
5
|
import { TemplateLiteralGenerate, IsTemplateLiteralFinite } from '../../type/template-literal/index.mjs';
|
|
5
6
|
import { PatternStringExact, PatternNumberExact } from '../../type/patterns/index.mjs';
|
|
@@ -17,11 +18,17 @@ export class ValueCreateError extends TypeBoxError {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
// ------------------------------------------------------------------
|
|
21
|
+
// Default
|
|
22
|
+
// ------------------------------------------------------------------
|
|
23
|
+
function FromDefault(value) {
|
|
24
|
+
return typeof value === 'function' ? value : Clone(value);
|
|
25
|
+
}
|
|
26
|
+
// ------------------------------------------------------------------
|
|
20
27
|
// Create
|
|
21
28
|
// ------------------------------------------------------------------
|
|
22
29
|
function FromAny(schema, references) {
|
|
23
30
|
if (HasPropertyKey(schema, 'default')) {
|
|
24
|
-
return schema.default;
|
|
31
|
+
return FromDefault(schema.default);
|
|
25
32
|
}
|
|
26
33
|
else {
|
|
27
34
|
return {};
|
|
@@ -35,7 +42,7 @@ function FromArray(schema, references) {
|
|
|
35
42
|
throw new ValueCreateError(schema, 'Array with the contains constraint requires a default value');
|
|
36
43
|
}
|
|
37
44
|
else if ('default' in schema) {
|
|
38
|
-
return schema.default;
|
|
45
|
+
return FromDefault(schema.default);
|
|
39
46
|
}
|
|
40
47
|
else if (schema.minItems !== undefined) {
|
|
41
48
|
return Array.from({ length: schema.minItems }).map((item) => {
|
|
@@ -48,7 +55,7 @@ function FromArray(schema, references) {
|
|
|
48
55
|
}
|
|
49
56
|
function FromAsyncIterator(schema, references) {
|
|
50
57
|
if (HasPropertyKey(schema, 'default')) {
|
|
51
|
-
return schema.default;
|
|
58
|
+
return FromDefault(schema.default);
|
|
52
59
|
}
|
|
53
60
|
else {
|
|
54
61
|
return (async function* () { })();
|
|
@@ -56,7 +63,7 @@ function FromAsyncIterator(schema, references) {
|
|
|
56
63
|
}
|
|
57
64
|
function FromBigInt(schema, references) {
|
|
58
65
|
if (HasPropertyKey(schema, 'default')) {
|
|
59
|
-
return schema.default;
|
|
66
|
+
return FromDefault(schema.default);
|
|
60
67
|
}
|
|
61
68
|
else {
|
|
62
69
|
return BigInt(0);
|
|
@@ -64,7 +71,7 @@ function FromBigInt(schema, references) {
|
|
|
64
71
|
}
|
|
65
72
|
function FromBoolean(schema, references) {
|
|
66
73
|
if (HasPropertyKey(schema, 'default')) {
|
|
67
|
-
return schema.default;
|
|
74
|
+
return FromDefault(schema.default);
|
|
68
75
|
}
|
|
69
76
|
else {
|
|
70
77
|
return false;
|
|
@@ -72,7 +79,7 @@ function FromBoolean(schema, references) {
|
|
|
72
79
|
}
|
|
73
80
|
function FromConstructor(schema, references) {
|
|
74
81
|
if (HasPropertyKey(schema, 'default')) {
|
|
75
|
-
return schema.default;
|
|
82
|
+
return FromDefault(schema.default);
|
|
76
83
|
}
|
|
77
84
|
else {
|
|
78
85
|
const value = Visit(schema.returns, references);
|
|
@@ -94,7 +101,7 @@ function FromConstructor(schema, references) {
|
|
|
94
101
|
}
|
|
95
102
|
function FromDate(schema, references) {
|
|
96
103
|
if (HasPropertyKey(schema, 'default')) {
|
|
97
|
-
return schema.default;
|
|
104
|
+
return FromDefault(schema.default);
|
|
98
105
|
}
|
|
99
106
|
else if (schema.minimumTimestamp !== undefined) {
|
|
100
107
|
return new Date(schema.minimumTimestamp);
|
|
@@ -105,7 +112,7 @@ function FromDate(schema, references) {
|
|
|
105
112
|
}
|
|
106
113
|
function FromFunction(schema, references) {
|
|
107
114
|
if (HasPropertyKey(schema, 'default')) {
|
|
108
|
-
return schema.default;
|
|
115
|
+
return FromDefault(schema.default);
|
|
109
116
|
}
|
|
110
117
|
else {
|
|
111
118
|
return () => Visit(schema.returns, references);
|
|
@@ -113,7 +120,7 @@ function FromFunction(schema, references) {
|
|
|
113
120
|
}
|
|
114
121
|
function FromInteger(schema, references) {
|
|
115
122
|
if (HasPropertyKey(schema, 'default')) {
|
|
116
|
-
return schema.default;
|
|
123
|
+
return FromDefault(schema.default);
|
|
117
124
|
}
|
|
118
125
|
else if (schema.minimum !== undefined) {
|
|
119
126
|
return schema.minimum;
|
|
@@ -124,7 +131,7 @@ function FromInteger(schema, references) {
|
|
|
124
131
|
}
|
|
125
132
|
function FromIntersect(schema, references) {
|
|
126
133
|
if (HasPropertyKey(schema, 'default')) {
|
|
127
|
-
return schema.default;
|
|
134
|
+
return FromDefault(schema.default);
|
|
128
135
|
}
|
|
129
136
|
else {
|
|
130
137
|
// --------------------------------------------------------------
|
|
@@ -145,7 +152,7 @@ function FromIntersect(schema, references) {
|
|
|
145
152
|
}
|
|
146
153
|
function FromIterator(schema, references) {
|
|
147
154
|
if (HasPropertyKey(schema, 'default')) {
|
|
148
|
-
return schema.default;
|
|
155
|
+
return FromDefault(schema.default);
|
|
149
156
|
}
|
|
150
157
|
else {
|
|
151
158
|
return (function* () { })();
|
|
@@ -153,7 +160,7 @@ function FromIterator(schema, references) {
|
|
|
153
160
|
}
|
|
154
161
|
function FromLiteral(schema, references) {
|
|
155
162
|
if (HasPropertyKey(schema, 'default')) {
|
|
156
|
-
return schema.default;
|
|
163
|
+
return FromDefault(schema.default);
|
|
157
164
|
}
|
|
158
165
|
else {
|
|
159
166
|
return schema.const;
|
|
@@ -161,7 +168,7 @@ function FromLiteral(schema, references) {
|
|
|
161
168
|
}
|
|
162
169
|
function FromNever(schema, references) {
|
|
163
170
|
if (HasPropertyKey(schema, 'default')) {
|
|
164
|
-
return schema.default;
|
|
171
|
+
return FromDefault(schema.default);
|
|
165
172
|
}
|
|
166
173
|
else {
|
|
167
174
|
throw new ValueCreateError(schema, 'Never types cannot be created. Consider using a default value.');
|
|
@@ -169,7 +176,7 @@ function FromNever(schema, references) {
|
|
|
169
176
|
}
|
|
170
177
|
function FromNot(schema, references) {
|
|
171
178
|
if (HasPropertyKey(schema, 'default')) {
|
|
172
|
-
return schema.default;
|
|
179
|
+
return FromDefault(schema.default);
|
|
173
180
|
}
|
|
174
181
|
else {
|
|
175
182
|
throw new ValueCreateError(schema, 'Not types must have a default value');
|
|
@@ -177,7 +184,7 @@ function FromNot(schema, references) {
|
|
|
177
184
|
}
|
|
178
185
|
function FromNull(schema, references) {
|
|
179
186
|
if (HasPropertyKey(schema, 'default')) {
|
|
180
|
-
return schema.default;
|
|
187
|
+
return FromDefault(schema.default);
|
|
181
188
|
}
|
|
182
189
|
else {
|
|
183
190
|
return null;
|
|
@@ -185,7 +192,7 @@ function FromNull(schema, references) {
|
|
|
185
192
|
}
|
|
186
193
|
function FromNumber(schema, references) {
|
|
187
194
|
if (HasPropertyKey(schema, 'default')) {
|
|
188
|
-
return schema.default;
|
|
195
|
+
return FromDefault(schema.default);
|
|
189
196
|
}
|
|
190
197
|
else if (schema.minimum !== undefined) {
|
|
191
198
|
return schema.minimum;
|
|
@@ -196,11 +203,11 @@ function FromNumber(schema, references) {
|
|
|
196
203
|
}
|
|
197
204
|
function FromObject(schema, references) {
|
|
198
205
|
if (HasPropertyKey(schema, 'default')) {
|
|
199
|
-
return schema.default;
|
|
206
|
+
return FromDefault(schema.default);
|
|
200
207
|
}
|
|
201
208
|
else {
|
|
202
209
|
const required = new Set(schema.required);
|
|
203
|
-
return (schema.default ||
|
|
210
|
+
return (FromDefault(schema.default) ||
|
|
204
211
|
Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
205
212
|
return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
|
|
206
213
|
}, {}));
|
|
@@ -208,7 +215,7 @@ function FromObject(schema, references) {
|
|
|
208
215
|
}
|
|
209
216
|
function FromPromise(schema, references) {
|
|
210
217
|
if (HasPropertyKey(schema, 'default')) {
|
|
211
|
-
return schema.default;
|
|
218
|
+
return FromDefault(schema.default);
|
|
212
219
|
}
|
|
213
220
|
else {
|
|
214
221
|
return Promise.resolve(Visit(schema.item, references));
|
|
@@ -217,7 +224,7 @@ function FromPromise(schema, references) {
|
|
|
217
224
|
function FromRecord(schema, references) {
|
|
218
225
|
const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
|
|
219
226
|
if (HasPropertyKey(schema, 'default')) {
|
|
220
|
-
return schema.default;
|
|
227
|
+
return FromDefault(schema.default);
|
|
221
228
|
}
|
|
222
229
|
else if (!(keyPattern === PatternStringExact || keyPattern === PatternNumberExact)) {
|
|
223
230
|
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
@@ -231,7 +238,7 @@ function FromRecord(schema, references) {
|
|
|
231
238
|
}
|
|
232
239
|
function FromRef(schema, references) {
|
|
233
240
|
if (HasPropertyKey(schema, 'default')) {
|
|
234
|
-
return schema.default;
|
|
241
|
+
return FromDefault(schema.default);
|
|
235
242
|
}
|
|
236
243
|
else {
|
|
237
244
|
return Visit(Deref(schema, references), references);
|
|
@@ -239,7 +246,7 @@ function FromRef(schema, references) {
|
|
|
239
246
|
}
|
|
240
247
|
function FromRegExp(schema, references) {
|
|
241
248
|
if (HasPropertyKey(schema, 'default')) {
|
|
242
|
-
return schema.default;
|
|
249
|
+
return FromDefault(schema.default);
|
|
243
250
|
}
|
|
244
251
|
else {
|
|
245
252
|
throw new ValueCreateError(schema, 'RegExp types cannot be created. Consider using a default value.');
|
|
@@ -251,7 +258,7 @@ function FromString(schema, references) {
|
|
|
251
258
|
throw new ValueCreateError(schema, 'String types with patterns must specify a default value');
|
|
252
259
|
}
|
|
253
260
|
else {
|
|
254
|
-
return schema.default;
|
|
261
|
+
return FromDefault(schema.default);
|
|
255
262
|
}
|
|
256
263
|
}
|
|
257
264
|
else if (schema.format !== undefined) {
|
|
@@ -259,12 +266,12 @@ function FromString(schema, references) {
|
|
|
259
266
|
throw new ValueCreateError(schema, 'String types with formats must specify a default value');
|
|
260
267
|
}
|
|
261
268
|
else {
|
|
262
|
-
return schema.default;
|
|
269
|
+
return FromDefault(schema.default);
|
|
263
270
|
}
|
|
264
271
|
}
|
|
265
272
|
else {
|
|
266
273
|
if (HasPropertyKey(schema, 'default')) {
|
|
267
|
-
return schema.default;
|
|
274
|
+
return FromDefault(schema.default);
|
|
268
275
|
}
|
|
269
276
|
else if (schema.minLength !== undefined) {
|
|
270
277
|
// prettier-ignore
|
|
@@ -277,7 +284,7 @@ function FromString(schema, references) {
|
|
|
277
284
|
}
|
|
278
285
|
function FromSymbol(schema, references) {
|
|
279
286
|
if (HasPropertyKey(schema, 'default')) {
|
|
280
|
-
return schema.default;
|
|
287
|
+
return FromDefault(schema.default);
|
|
281
288
|
}
|
|
282
289
|
else if ('value' in schema) {
|
|
283
290
|
return Symbol.for(schema.value);
|
|
@@ -288,7 +295,7 @@ function FromSymbol(schema, references) {
|
|
|
288
295
|
}
|
|
289
296
|
function FromTemplateLiteral(schema, references) {
|
|
290
297
|
if (HasPropertyKey(schema, 'default')) {
|
|
291
|
-
return schema.default;
|
|
298
|
+
return FromDefault(schema.default);
|
|
292
299
|
}
|
|
293
300
|
if (!IsTemplateLiteralFinite(schema))
|
|
294
301
|
throw new ValueCreateError(schema, 'Can only create template literals that produce a finite variants. Consider using a default value.');
|
|
@@ -299,7 +306,7 @@ function FromThis(schema, references) {
|
|
|
299
306
|
if (recursiveDepth++ > recursiveMaxDepth)
|
|
300
307
|
throw new ValueCreateError(schema, 'Cannot create recursive type as it appears possibly infinite. Consider using a default.');
|
|
301
308
|
if (HasPropertyKey(schema, 'default')) {
|
|
302
|
-
return schema.default;
|
|
309
|
+
return FromDefault(schema.default);
|
|
303
310
|
}
|
|
304
311
|
else {
|
|
305
312
|
return Visit(Deref(schema, references), references);
|
|
@@ -307,7 +314,7 @@ function FromThis(schema, references) {
|
|
|
307
314
|
}
|
|
308
315
|
function FromTuple(schema, references) {
|
|
309
316
|
if (HasPropertyKey(schema, 'default')) {
|
|
310
|
-
return schema.default;
|
|
317
|
+
return FromDefault(schema.default);
|
|
311
318
|
}
|
|
312
319
|
if (schema.items === undefined) {
|
|
313
320
|
return [];
|
|
@@ -318,7 +325,7 @@ function FromTuple(schema, references) {
|
|
|
318
325
|
}
|
|
319
326
|
function FromUndefined(schema, references) {
|
|
320
327
|
if (HasPropertyKey(schema, 'default')) {
|
|
321
|
-
return schema.default;
|
|
328
|
+
return FromDefault(schema.default);
|
|
322
329
|
}
|
|
323
330
|
else {
|
|
324
331
|
return undefined;
|
|
@@ -326,7 +333,7 @@ function FromUndefined(schema, references) {
|
|
|
326
333
|
}
|
|
327
334
|
function FromUnion(schema, references) {
|
|
328
335
|
if (HasPropertyKey(schema, 'default')) {
|
|
329
|
-
return schema.default;
|
|
336
|
+
return FromDefault(schema.default);
|
|
330
337
|
}
|
|
331
338
|
else if (schema.anyOf.length === 0) {
|
|
332
339
|
throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
|
|
@@ -337,7 +344,7 @@ function FromUnion(schema, references) {
|
|
|
337
344
|
}
|
|
338
345
|
function FromUint8Array(schema, references) {
|
|
339
346
|
if (HasPropertyKey(schema, 'default')) {
|
|
340
|
-
return schema.default;
|
|
347
|
+
return FromDefault(schema.default);
|
|
341
348
|
}
|
|
342
349
|
else if (schema.minByteLength !== undefined) {
|
|
343
350
|
return new Uint8Array(schema.minByteLength);
|
|
@@ -348,7 +355,7 @@ function FromUint8Array(schema, references) {
|
|
|
348
355
|
}
|
|
349
356
|
function FromUnknown(schema, references) {
|
|
350
357
|
if (HasPropertyKey(schema, 'default')) {
|
|
351
|
-
return schema.default;
|
|
358
|
+
return FromDefault(schema.default);
|
|
352
359
|
}
|
|
353
360
|
else {
|
|
354
361
|
return {};
|
|
@@ -356,7 +363,7 @@ function FromUnknown(schema, references) {
|
|
|
356
363
|
}
|
|
357
364
|
function FromVoid(schema, references) {
|
|
358
365
|
if (HasPropertyKey(schema, 'default')) {
|
|
359
|
-
return schema.default;
|
|
366
|
+
return FromDefault(schema.default);
|
|
360
367
|
}
|
|
361
368
|
else {
|
|
362
369
|
return void 0;
|
|
@@ -364,7 +371,7 @@ function FromVoid(schema, references) {
|
|
|
364
371
|
}
|
|
365
372
|
function FromKind(schema, references) {
|
|
366
373
|
if (HasPropertyKey(schema, 'default')) {
|
|
367
|
-
return schema.default;
|
|
374
|
+
return FromDefault(schema.default);
|
|
368
375
|
}
|
|
369
376
|
else {
|
|
370
377
|
throw new Error('User defined types must specify a default value');
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Check } from '../check/index.mjs';
|
|
2
|
+
import { Clone } from '../clone/index.mjs';
|
|
2
3
|
import { Deref } from '../deref/index.mjs';
|
|
3
4
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
4
5
|
// ------------------------------------------------------------------
|
|
@@ -13,7 +14,7 @@ import { IsSchema } from '../../type/guard/type.mjs';
|
|
|
13
14
|
// ValueOrDefault
|
|
14
15
|
// ------------------------------------------------------------------
|
|
15
16
|
function ValueOrDefault(schema, value) {
|
|
16
|
-
return
|
|
17
|
+
return value === undefined && 'default' in schema ? Clone(schema.default) : value;
|
|
17
18
|
}
|
|
18
19
|
// ------------------------------------------------------------------
|
|
19
20
|
// IsCheckable
|
|
@@ -57,7 +57,7 @@ function SelectUnion(union, references, value) {
|
|
|
57
57
|
}
|
|
58
58
|
function CastUnion(union, references, value) {
|
|
59
59
|
if ('default' in union) {
|
|
60
|
-
return union.default;
|
|
60
|
+
return typeof value === 'function' ? union.default : (0, index_6.Clone)(union.default);
|
|
61
61
|
}
|
|
62
62
|
else {
|
|
63
63
|
const schema = SelectUnion(union, references, value);
|
|
@@ -4,16 +4,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.Create = exports.ValueCreateError = void 0;
|
|
5
5
|
const index_1 = require("../guard/index");
|
|
6
6
|
const index_2 = require("../check/index");
|
|
7
|
-
const index_3 = require("../
|
|
8
|
-
const index_4 = require("
|
|
9
|
-
const index_5 = require("../../type/
|
|
10
|
-
const index_6 = require("../../type/
|
|
11
|
-
const index_7 = require("../../type/
|
|
12
|
-
const index_8 = require("../../type/
|
|
7
|
+
const index_3 = require("../clone/index");
|
|
8
|
+
const index_4 = require("../deref/index");
|
|
9
|
+
const index_5 = require("../../type/template-literal/index");
|
|
10
|
+
const index_6 = require("../../type/patterns/index");
|
|
11
|
+
const index_7 = require("../../type/registry/index");
|
|
12
|
+
const index_8 = require("../../type/symbols/index");
|
|
13
|
+
const index_9 = require("../../type/error/index");
|
|
13
14
|
// ------------------------------------------------------------------
|
|
14
15
|
// Errors
|
|
15
16
|
// ------------------------------------------------------------------
|
|
16
|
-
class ValueCreateError extends
|
|
17
|
+
class ValueCreateError extends index_9.TypeBoxError {
|
|
17
18
|
constructor(schema, message) {
|
|
18
19
|
super(message);
|
|
19
20
|
this.schema = schema;
|
|
@@ -21,11 +22,17 @@ class ValueCreateError extends index_8.TypeBoxError {
|
|
|
21
22
|
}
|
|
22
23
|
exports.ValueCreateError = ValueCreateError;
|
|
23
24
|
// ------------------------------------------------------------------
|
|
25
|
+
// Default
|
|
26
|
+
// ------------------------------------------------------------------
|
|
27
|
+
function FromDefault(value) {
|
|
28
|
+
return typeof value === 'function' ? value : (0, index_3.Clone)(value);
|
|
29
|
+
}
|
|
30
|
+
// ------------------------------------------------------------------
|
|
24
31
|
// Create
|
|
25
32
|
// ------------------------------------------------------------------
|
|
26
33
|
function FromAny(schema, references) {
|
|
27
34
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
28
|
-
return schema.default;
|
|
35
|
+
return FromDefault(schema.default);
|
|
29
36
|
}
|
|
30
37
|
else {
|
|
31
38
|
return {};
|
|
@@ -39,7 +46,7 @@ function FromArray(schema, references) {
|
|
|
39
46
|
throw new ValueCreateError(schema, 'Array with the contains constraint requires a default value');
|
|
40
47
|
}
|
|
41
48
|
else if ('default' in schema) {
|
|
42
|
-
return schema.default;
|
|
49
|
+
return FromDefault(schema.default);
|
|
43
50
|
}
|
|
44
51
|
else if (schema.minItems !== undefined) {
|
|
45
52
|
return Array.from({ length: schema.minItems }).map((item) => {
|
|
@@ -52,7 +59,7 @@ function FromArray(schema, references) {
|
|
|
52
59
|
}
|
|
53
60
|
function FromAsyncIterator(schema, references) {
|
|
54
61
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
55
|
-
return schema.default;
|
|
62
|
+
return FromDefault(schema.default);
|
|
56
63
|
}
|
|
57
64
|
else {
|
|
58
65
|
return (async function* () { })();
|
|
@@ -60,7 +67,7 @@ function FromAsyncIterator(schema, references) {
|
|
|
60
67
|
}
|
|
61
68
|
function FromBigInt(schema, references) {
|
|
62
69
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
63
|
-
return schema.default;
|
|
70
|
+
return FromDefault(schema.default);
|
|
64
71
|
}
|
|
65
72
|
else {
|
|
66
73
|
return BigInt(0);
|
|
@@ -68,7 +75,7 @@ function FromBigInt(schema, references) {
|
|
|
68
75
|
}
|
|
69
76
|
function FromBoolean(schema, references) {
|
|
70
77
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
71
|
-
return schema.default;
|
|
78
|
+
return FromDefault(schema.default);
|
|
72
79
|
}
|
|
73
80
|
else {
|
|
74
81
|
return false;
|
|
@@ -76,7 +83,7 @@ function FromBoolean(schema, references) {
|
|
|
76
83
|
}
|
|
77
84
|
function FromConstructor(schema, references) {
|
|
78
85
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
79
|
-
return schema.default;
|
|
86
|
+
return FromDefault(schema.default);
|
|
80
87
|
}
|
|
81
88
|
else {
|
|
82
89
|
const value = Visit(schema.returns, references);
|
|
@@ -98,7 +105,7 @@ function FromConstructor(schema, references) {
|
|
|
98
105
|
}
|
|
99
106
|
function FromDate(schema, references) {
|
|
100
107
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
101
|
-
return schema.default;
|
|
108
|
+
return FromDefault(schema.default);
|
|
102
109
|
}
|
|
103
110
|
else if (schema.minimumTimestamp !== undefined) {
|
|
104
111
|
return new Date(schema.minimumTimestamp);
|
|
@@ -109,7 +116,7 @@ function FromDate(schema, references) {
|
|
|
109
116
|
}
|
|
110
117
|
function FromFunction(schema, references) {
|
|
111
118
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
112
|
-
return schema.default;
|
|
119
|
+
return FromDefault(schema.default);
|
|
113
120
|
}
|
|
114
121
|
else {
|
|
115
122
|
return () => Visit(schema.returns, references);
|
|
@@ -117,7 +124,7 @@ function FromFunction(schema, references) {
|
|
|
117
124
|
}
|
|
118
125
|
function FromInteger(schema, references) {
|
|
119
126
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
120
|
-
return schema.default;
|
|
127
|
+
return FromDefault(schema.default);
|
|
121
128
|
}
|
|
122
129
|
else if (schema.minimum !== undefined) {
|
|
123
130
|
return schema.minimum;
|
|
@@ -128,7 +135,7 @@ function FromInteger(schema, references) {
|
|
|
128
135
|
}
|
|
129
136
|
function FromIntersect(schema, references) {
|
|
130
137
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
131
|
-
return schema.default;
|
|
138
|
+
return FromDefault(schema.default);
|
|
132
139
|
}
|
|
133
140
|
else {
|
|
134
141
|
// --------------------------------------------------------------
|
|
@@ -149,7 +156,7 @@ function FromIntersect(schema, references) {
|
|
|
149
156
|
}
|
|
150
157
|
function FromIterator(schema, references) {
|
|
151
158
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
152
|
-
return schema.default;
|
|
159
|
+
return FromDefault(schema.default);
|
|
153
160
|
}
|
|
154
161
|
else {
|
|
155
162
|
return (function* () { })();
|
|
@@ -157,7 +164,7 @@ function FromIterator(schema, references) {
|
|
|
157
164
|
}
|
|
158
165
|
function FromLiteral(schema, references) {
|
|
159
166
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
160
|
-
return schema.default;
|
|
167
|
+
return FromDefault(schema.default);
|
|
161
168
|
}
|
|
162
169
|
else {
|
|
163
170
|
return schema.const;
|
|
@@ -165,7 +172,7 @@ function FromLiteral(schema, references) {
|
|
|
165
172
|
}
|
|
166
173
|
function FromNever(schema, references) {
|
|
167
174
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
168
|
-
return schema.default;
|
|
175
|
+
return FromDefault(schema.default);
|
|
169
176
|
}
|
|
170
177
|
else {
|
|
171
178
|
throw new ValueCreateError(schema, 'Never types cannot be created. Consider using a default value.');
|
|
@@ -173,7 +180,7 @@ function FromNever(schema, references) {
|
|
|
173
180
|
}
|
|
174
181
|
function FromNot(schema, references) {
|
|
175
182
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
176
|
-
return schema.default;
|
|
183
|
+
return FromDefault(schema.default);
|
|
177
184
|
}
|
|
178
185
|
else {
|
|
179
186
|
throw new ValueCreateError(schema, 'Not types must have a default value');
|
|
@@ -181,7 +188,7 @@ function FromNot(schema, references) {
|
|
|
181
188
|
}
|
|
182
189
|
function FromNull(schema, references) {
|
|
183
190
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
184
|
-
return schema.default;
|
|
191
|
+
return FromDefault(schema.default);
|
|
185
192
|
}
|
|
186
193
|
else {
|
|
187
194
|
return null;
|
|
@@ -189,7 +196,7 @@ function FromNull(schema, references) {
|
|
|
189
196
|
}
|
|
190
197
|
function FromNumber(schema, references) {
|
|
191
198
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
192
|
-
return schema.default;
|
|
199
|
+
return FromDefault(schema.default);
|
|
193
200
|
}
|
|
194
201
|
else if (schema.minimum !== undefined) {
|
|
195
202
|
return schema.minimum;
|
|
@@ -200,11 +207,11 @@ function FromNumber(schema, references) {
|
|
|
200
207
|
}
|
|
201
208
|
function FromObject(schema, references) {
|
|
202
209
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
203
|
-
return schema.default;
|
|
210
|
+
return FromDefault(schema.default);
|
|
204
211
|
}
|
|
205
212
|
else {
|
|
206
213
|
const required = new Set(schema.required);
|
|
207
|
-
return (schema.default ||
|
|
214
|
+
return (FromDefault(schema.default) ||
|
|
208
215
|
Object.entries(schema.properties).reduce((acc, [key, schema]) => {
|
|
209
216
|
return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
|
|
210
217
|
}, {}));
|
|
@@ -212,7 +219,7 @@ function FromObject(schema, references) {
|
|
|
212
219
|
}
|
|
213
220
|
function FromPromise(schema, references) {
|
|
214
221
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
215
|
-
return schema.default;
|
|
222
|
+
return FromDefault(schema.default);
|
|
216
223
|
}
|
|
217
224
|
else {
|
|
218
225
|
return Promise.resolve(Visit(schema.item, references));
|
|
@@ -221,9 +228,9 @@ function FromPromise(schema, references) {
|
|
|
221
228
|
function FromRecord(schema, references) {
|
|
222
229
|
const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
|
|
223
230
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
224
|
-
return schema.default;
|
|
231
|
+
return FromDefault(schema.default);
|
|
225
232
|
}
|
|
226
|
-
else if (!(keyPattern ===
|
|
233
|
+
else if (!(keyPattern === index_6.PatternStringExact || keyPattern === index_6.PatternNumberExact)) {
|
|
227
234
|
const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
|
|
228
235
|
return propertyKeys.reduce((acc, key) => {
|
|
229
236
|
return { ...acc, [key]: Visit(valueSchema, references) };
|
|
@@ -235,15 +242,15 @@ function FromRecord(schema, references) {
|
|
|
235
242
|
}
|
|
236
243
|
function FromRef(schema, references) {
|
|
237
244
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
238
|
-
return schema.default;
|
|
245
|
+
return FromDefault(schema.default);
|
|
239
246
|
}
|
|
240
247
|
else {
|
|
241
|
-
return Visit((0,
|
|
248
|
+
return Visit((0, index_4.Deref)(schema, references), references);
|
|
242
249
|
}
|
|
243
250
|
}
|
|
244
251
|
function FromRegExp(schema, references) {
|
|
245
252
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
246
|
-
return schema.default;
|
|
253
|
+
return FromDefault(schema.default);
|
|
247
254
|
}
|
|
248
255
|
else {
|
|
249
256
|
throw new ValueCreateError(schema, 'RegExp types cannot be created. Consider using a default value.');
|
|
@@ -255,7 +262,7 @@ function FromString(schema, references) {
|
|
|
255
262
|
throw new ValueCreateError(schema, 'String types with patterns must specify a default value');
|
|
256
263
|
}
|
|
257
264
|
else {
|
|
258
|
-
return schema.default;
|
|
265
|
+
return FromDefault(schema.default);
|
|
259
266
|
}
|
|
260
267
|
}
|
|
261
268
|
else if (schema.format !== undefined) {
|
|
@@ -263,12 +270,12 @@ function FromString(schema, references) {
|
|
|
263
270
|
throw new ValueCreateError(schema, 'String types with formats must specify a default value');
|
|
264
271
|
}
|
|
265
272
|
else {
|
|
266
|
-
return schema.default;
|
|
273
|
+
return FromDefault(schema.default);
|
|
267
274
|
}
|
|
268
275
|
}
|
|
269
276
|
else {
|
|
270
277
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
271
|
-
return schema.default;
|
|
278
|
+
return FromDefault(schema.default);
|
|
272
279
|
}
|
|
273
280
|
else if (schema.minLength !== undefined) {
|
|
274
281
|
// prettier-ignore
|
|
@@ -281,7 +288,7 @@ function FromString(schema, references) {
|
|
|
281
288
|
}
|
|
282
289
|
function FromSymbol(schema, references) {
|
|
283
290
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
284
|
-
return schema.default;
|
|
291
|
+
return FromDefault(schema.default);
|
|
285
292
|
}
|
|
286
293
|
else if ('value' in schema) {
|
|
287
294
|
return Symbol.for(schema.value);
|
|
@@ -292,26 +299,26 @@ function FromSymbol(schema, references) {
|
|
|
292
299
|
}
|
|
293
300
|
function FromTemplateLiteral(schema, references) {
|
|
294
301
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
295
|
-
return schema.default;
|
|
302
|
+
return FromDefault(schema.default);
|
|
296
303
|
}
|
|
297
|
-
if (!(0,
|
|
304
|
+
if (!(0, index_5.IsTemplateLiteralFinite)(schema))
|
|
298
305
|
throw new ValueCreateError(schema, 'Can only create template literals that produce a finite variants. Consider using a default value.');
|
|
299
|
-
const generated = (0,
|
|
306
|
+
const generated = (0, index_5.TemplateLiteralGenerate)(schema);
|
|
300
307
|
return generated[0];
|
|
301
308
|
}
|
|
302
309
|
function FromThis(schema, references) {
|
|
303
310
|
if (recursiveDepth++ > recursiveMaxDepth)
|
|
304
311
|
throw new ValueCreateError(schema, 'Cannot create recursive type as it appears possibly infinite. Consider using a default.');
|
|
305
312
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
306
|
-
return schema.default;
|
|
313
|
+
return FromDefault(schema.default);
|
|
307
314
|
}
|
|
308
315
|
else {
|
|
309
|
-
return Visit((0,
|
|
316
|
+
return Visit((0, index_4.Deref)(schema, references), references);
|
|
310
317
|
}
|
|
311
318
|
}
|
|
312
319
|
function FromTuple(schema, references) {
|
|
313
320
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
314
|
-
return schema.default;
|
|
321
|
+
return FromDefault(schema.default);
|
|
315
322
|
}
|
|
316
323
|
if (schema.items === undefined) {
|
|
317
324
|
return [];
|
|
@@ -322,7 +329,7 @@ function FromTuple(schema, references) {
|
|
|
322
329
|
}
|
|
323
330
|
function FromUndefined(schema, references) {
|
|
324
331
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
325
|
-
return schema.default;
|
|
332
|
+
return FromDefault(schema.default);
|
|
326
333
|
}
|
|
327
334
|
else {
|
|
328
335
|
return undefined;
|
|
@@ -330,7 +337,7 @@ function FromUndefined(schema, references) {
|
|
|
330
337
|
}
|
|
331
338
|
function FromUnion(schema, references) {
|
|
332
339
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
333
|
-
return schema.default;
|
|
340
|
+
return FromDefault(schema.default);
|
|
334
341
|
}
|
|
335
342
|
else if (schema.anyOf.length === 0) {
|
|
336
343
|
throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
|
|
@@ -341,7 +348,7 @@ function FromUnion(schema, references) {
|
|
|
341
348
|
}
|
|
342
349
|
function FromUint8Array(schema, references) {
|
|
343
350
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
344
|
-
return schema.default;
|
|
351
|
+
return FromDefault(schema.default);
|
|
345
352
|
}
|
|
346
353
|
else if (schema.minByteLength !== undefined) {
|
|
347
354
|
return new Uint8Array(schema.minByteLength);
|
|
@@ -352,7 +359,7 @@ function FromUint8Array(schema, references) {
|
|
|
352
359
|
}
|
|
353
360
|
function FromUnknown(schema, references) {
|
|
354
361
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
355
|
-
return schema.default;
|
|
362
|
+
return FromDefault(schema.default);
|
|
356
363
|
}
|
|
357
364
|
else {
|
|
358
365
|
return {};
|
|
@@ -360,7 +367,7 @@ function FromUnknown(schema, references) {
|
|
|
360
367
|
}
|
|
361
368
|
function FromVoid(schema, references) {
|
|
362
369
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
363
|
-
return schema.default;
|
|
370
|
+
return FromDefault(schema.default);
|
|
364
371
|
}
|
|
365
372
|
else {
|
|
366
373
|
return void 0;
|
|
@@ -368,7 +375,7 @@ function FromVoid(schema, references) {
|
|
|
368
375
|
}
|
|
369
376
|
function FromKind(schema, references) {
|
|
370
377
|
if ((0, index_1.HasPropertyKey)(schema, 'default')) {
|
|
371
|
-
return schema.default;
|
|
378
|
+
return FromDefault(schema.default);
|
|
372
379
|
}
|
|
373
380
|
else {
|
|
374
381
|
throw new Error('User defined types must specify a default value');
|
|
@@ -377,7 +384,7 @@ function FromKind(schema, references) {
|
|
|
377
384
|
function Visit(schema, references) {
|
|
378
385
|
const references_ = (0, index_1.IsString)(schema.$id) ? [...references, schema] : references;
|
|
379
386
|
const schema_ = schema;
|
|
380
|
-
switch (schema_[
|
|
387
|
+
switch (schema_[index_8.Kind]) {
|
|
381
388
|
case 'Any':
|
|
382
389
|
return FromAny(schema_, references_);
|
|
383
390
|
case 'Array':
|
|
@@ -441,7 +448,7 @@ function Visit(schema, references) {
|
|
|
441
448
|
case 'Void':
|
|
442
449
|
return FromVoid(schema_, references_);
|
|
443
450
|
default:
|
|
444
|
-
if (!
|
|
451
|
+
if (!index_7.TypeRegistry.Has(schema_[index_8.Kind]))
|
|
445
452
|
throw new ValueCreateError(schema_, 'Unknown type');
|
|
446
453
|
return FromKind(schema_, references_);
|
|
447
454
|
}
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Default = void 0;
|
|
5
5
|
const index_1 = require("../check/index");
|
|
6
|
-
const index_2 = require("../
|
|
7
|
-
const index_3 = require("
|
|
6
|
+
const index_2 = require("../clone/index");
|
|
7
|
+
const index_3 = require("../deref/index");
|
|
8
|
+
const index_4 = require("../../type/symbols/index");
|
|
8
9
|
// ------------------------------------------------------------------
|
|
9
10
|
// ValueGuard
|
|
10
11
|
// ------------------------------------------------------------------
|
|
11
|
-
const
|
|
12
|
+
const index_5 = require("../guard/index");
|
|
12
13
|
// ------------------------------------------------------------------
|
|
13
14
|
// TypeGuard
|
|
14
15
|
// ------------------------------------------------------------------
|
|
@@ -17,13 +18,13 @@ const type_1 = require("../../type/guard/type");
|
|
|
17
18
|
// ValueOrDefault
|
|
18
19
|
// ------------------------------------------------------------------
|
|
19
20
|
function ValueOrDefault(schema, value) {
|
|
20
|
-
return
|
|
21
|
+
return value === undefined && 'default' in schema ? (0, index_2.Clone)(schema.default) : value;
|
|
21
22
|
}
|
|
22
23
|
// ------------------------------------------------------------------
|
|
23
24
|
// IsCheckable
|
|
24
25
|
// ------------------------------------------------------------------
|
|
25
26
|
function IsCheckable(schema) {
|
|
26
|
-
return (0, type_1.IsSchema)(schema) && schema[
|
|
27
|
+
return (0, type_1.IsSchema)(schema) && schema[index_4.Kind] !== 'Unsafe';
|
|
27
28
|
}
|
|
28
29
|
// ------------------------------------------------------------------
|
|
29
30
|
// IsDefaultSchema
|
|
@@ -36,7 +37,7 @@ function IsDefaultSchema(value) {
|
|
|
36
37
|
// ------------------------------------------------------------------
|
|
37
38
|
function FromArray(schema, references, value) {
|
|
38
39
|
const defaulted = ValueOrDefault(schema, value);
|
|
39
|
-
if (!(0,
|
|
40
|
+
if (!(0, index_5.IsArray)(defaulted))
|
|
40
41
|
return defaulted;
|
|
41
42
|
for (let i = 0; i < defaulted.length; i++) {
|
|
42
43
|
defaulted[i] = Visit(schema.items, references, defaulted[i]);
|
|
@@ -47,12 +48,12 @@ function FromIntersect(schema, references, value) {
|
|
|
47
48
|
const defaulted = ValueOrDefault(schema, value);
|
|
48
49
|
return schema.allOf.reduce((acc, schema) => {
|
|
49
50
|
const next = Visit(schema, references, defaulted);
|
|
50
|
-
return (0,
|
|
51
|
+
return (0, index_5.IsObject)(next) ? { ...acc, ...next } : next;
|
|
51
52
|
}, {});
|
|
52
53
|
}
|
|
53
54
|
function FromObject(schema, references, value) {
|
|
54
55
|
const defaulted = ValueOrDefault(schema, value);
|
|
55
|
-
if (!(0,
|
|
56
|
+
if (!(0, index_5.IsObject)(defaulted))
|
|
56
57
|
return defaulted;
|
|
57
58
|
const additionalPropertiesSchema = schema.additionalProperties;
|
|
58
59
|
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
|
|
@@ -75,7 +76,7 @@ function FromObject(schema, references, value) {
|
|
|
75
76
|
}
|
|
76
77
|
function FromRecord(schema, references, value) {
|
|
77
78
|
const defaulted = ValueOrDefault(schema, value);
|
|
78
|
-
if (!(0,
|
|
79
|
+
if (!(0, index_5.IsObject)(defaulted))
|
|
79
80
|
return defaulted;
|
|
80
81
|
const additionalPropertiesSchema = schema.additionalProperties;
|
|
81
82
|
const [propertyKeyPattern, propertySchema] = Object.entries(schema.patternProperties)[0];
|
|
@@ -98,14 +99,14 @@ function FromRecord(schema, references, value) {
|
|
|
98
99
|
return defaulted;
|
|
99
100
|
}
|
|
100
101
|
function FromRef(schema, references, value) {
|
|
101
|
-
return Visit((0,
|
|
102
|
+
return Visit((0, index_3.Deref)(schema, references), references, ValueOrDefault(schema, value));
|
|
102
103
|
}
|
|
103
104
|
function FromThis(schema, references, value) {
|
|
104
|
-
return Visit((0,
|
|
105
|
+
return Visit((0, index_3.Deref)(schema, references), references, value);
|
|
105
106
|
}
|
|
106
107
|
function FromTuple(schema, references, value) {
|
|
107
108
|
const defaulted = ValueOrDefault(schema, value);
|
|
108
|
-
if (!(0,
|
|
109
|
+
if (!(0, index_5.IsArray)(defaulted) || (0, index_5.IsUndefined)(schema.items))
|
|
109
110
|
return defaulted;
|
|
110
111
|
const [items, max] = [schema.items, Math.max(schema.items.length, defaulted.length)];
|
|
111
112
|
for (let i = 0; i < max; i++) {
|
|
@@ -125,9 +126,9 @@ function FromUnion(schema, references, value) {
|
|
|
125
126
|
return defaulted;
|
|
126
127
|
}
|
|
127
128
|
function Visit(schema, references, value) {
|
|
128
|
-
const references_ = (0,
|
|
129
|
+
const references_ = (0, index_5.IsString)(schema.$id) ? [...references, schema] : references;
|
|
129
130
|
const schema_ = schema;
|
|
130
|
-
switch (schema_[
|
|
131
|
+
switch (schema_[index_4.Kind]) {
|
|
131
132
|
case 'Array':
|
|
132
133
|
return FromArray(schema_, references_, value);
|
|
133
134
|
case 'Intersect':
|