@sinclair/typebox 0.26.0-dev.1 → 0.26.0-dev.3

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/typebox.js CHANGED
@@ -1,1875 +1,1881 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox
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.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.ReferenceRegistry = exports.TypeRegistry = exports.Modifier = exports.Hint = exports.Kind = void 0;
31
- // -------------------------------------------------------------------------------------
32
- // Symbols
33
- // -------------------------------------------------------------------------------------
34
- exports.Kind = Symbol.for('TypeBox.Kind');
35
- exports.Hint = Symbol.for('TypeBox.Hint');
36
- exports.Modifier = Symbol.for('TypeBox.Modifier');
37
- var TypeRegistry;
38
- (function (TypeRegistry) {
39
- const types = new Map();
40
- /** Clears all user defined types */
41
- function Clear() {
42
- return types.clear();
43
- }
44
- TypeRegistry.Clear = Clear;
45
- /** Returns true if this registry contains this kind */
46
- function Has(kind) {
47
- return types.has(kind);
48
- }
49
- TypeRegistry.Has = Has;
50
- /** Sets a validation function for a user defined type */
51
- function Set(kind, func) {
52
- types.set(kind, func);
53
- }
54
- TypeRegistry.Set = Set;
55
- /** Gets a custom validation function for a user defined type */
56
- function Get(kind) {
57
- return types.get(kind);
58
- }
59
- TypeRegistry.Get = Get;
60
- })(TypeRegistry = exports.TypeRegistry || (exports.TypeRegistry = {}));
61
- // -------------------------------------------------------------------------------------
62
- // ReferenceRegistry
63
- // -------------------------------------------------------------------------------------
64
- var ReferenceRegistry;
65
- (function (ReferenceRegistry) {
66
- const references = new Map();
67
- /** Clears the reference registry */
68
- function Clear() {
69
- return references.clear();
70
- }
71
- ReferenceRegistry.Clear = Clear;
72
- /** Returns true if this registry contains this schema */
73
- function Has(schema) {
74
- return references.has(schema.$id);
75
- }
76
- ReferenceRegistry.Has = Has;
77
- /** Sets this schema on this registry if a $id exists */
78
- function Set(schema) {
79
- const $id = typeof schema === 'object' && schema !== null && typeof schema['$id'] === 'string' ? schema['$id'] : undefined;
80
- if ($id !== undefined)
81
- references.set($id, schema);
82
- }
83
- ReferenceRegistry.Set = Set;
84
- /** Dereferences the schema one level deep */
85
- function DerefOne(schema) {
86
- if (TypeGuard.TRef(schema) || TypeGuard.TSelf(schema)) {
87
- if (!references.has(schema.$ref))
88
- throw Error(`ReferenceRegistry: Cannot deref schema with $id '${schema.$ref}'`);
89
- return references.get(schema.$ref);
90
- }
91
- else {
92
- return schema;
93
- }
94
- }
95
- ReferenceRegistry.DerefOne = DerefOne;
96
- /** Dereferences the schema recursively */
97
- function Deref(schema) {
98
- if (TypeGuard.TRef(schema)) {
99
- return Deref(DerefOne(schema));
100
- }
101
- else {
102
- return schema;
103
- }
104
- }
105
- ReferenceRegistry.Deref = Deref;
106
- })(ReferenceRegistry = exports.ReferenceRegistry || (exports.ReferenceRegistry = {}));
107
- /** Provides functions to create user defined string formats */
108
- var FormatRegistry;
109
- (function (FormatRegistry) {
110
- const formats = new Map();
111
- /** Clears all user defined string formats */
112
- function Clear() {
113
- return formats.clear();
114
- }
115
- FormatRegistry.Clear = Clear;
116
- /** Returns true if the user defined string format exists */
117
- function Has(format) {
118
- return formats.has(format);
119
- }
120
- FormatRegistry.Has = Has;
121
- /** Sets a validation function for a user defined string format */
122
- function Set(format, func) {
123
- formats.set(format, func);
124
- }
125
- FormatRegistry.Set = Set;
126
- /** Gets a validation function for a user defined string format */
127
- function Get(format) {
128
- return formats.get(format);
129
- }
130
- FormatRegistry.Get = Get;
131
- })(FormatRegistry = exports.FormatRegistry || (exports.FormatRegistry = {}));
132
- // -------------------------------------------------------------------------------------
133
- // TypeGuard
134
- // -------------------------------------------------------------------------------------
135
- class TypeGuardUnknownTypeError extends Error {
136
- constructor(schema) {
137
- super('TypeGuard: Unknown type');
138
- this.schema = schema;
139
- }
140
- }
141
- exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
142
- var TypeGuard;
143
- (function (TypeGuard) {
144
- function IsObject(value) {
145
- return typeof value === 'object' && value !== null && !Array.isArray(value);
146
- }
147
- function IsArray(value) {
148
- return typeof value === 'object' && value !== null && Array.isArray(value);
149
- }
150
- function IsPattern(value) {
151
- try {
152
- new RegExp(value);
153
- return true;
154
- }
155
- catch {
156
- return false;
157
- }
158
- }
159
- function IsControlCharacterFree(value) {
160
- if (typeof value !== 'string')
161
- return false;
162
- for (let i = 0; i < value.length; i++) {
163
- const code = value.charCodeAt(i);
164
- if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
165
- return false;
166
- }
167
- }
168
- return true;
169
- }
170
- function IsBigInt(value) {
171
- return typeof value === 'bigint';
172
- }
173
- function IsString(value) {
174
- return typeof value === 'string';
175
- }
176
- function IsNumber(value) {
177
- return typeof value === 'number' && globalThis.Number.isFinite(value);
178
- }
179
- function IsBoolean(value) {
180
- return typeof value === 'boolean';
181
- }
182
- function IsOptionalBigInt(value) {
183
- return value === undefined || (value !== undefined && IsBigInt(value));
184
- }
185
- function IsOptionalNumber(value) {
186
- return value === undefined || (value !== undefined && IsNumber(value));
187
- }
188
- function IsOptionalBoolean(value) {
189
- return value === undefined || (value !== undefined && IsBoolean(value));
190
- }
191
- function IsOptionalString(value) {
192
- return value === undefined || (value !== undefined && IsString(value));
193
- }
194
- function IsOptionalPattern(value) {
195
- return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
196
- }
197
- function IsOptionalFormat(value) {
198
- return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value));
199
- }
200
- function IsOptionalSchema(value) {
201
- return value === undefined || TSchema(value);
202
- }
203
- /** Returns true if the given schema is TAny */
204
- function TAny(schema) {
205
- return TKind(schema) && schema[exports.Kind] === 'Any' && IsOptionalString(schema.$id);
206
- }
207
- TypeGuard.TAny = TAny;
208
- /** Returns true if the given schema is TArray */
209
- function TArray(schema) {
210
- return (TKind(schema) &&
211
- schema[exports.Kind] === 'Array' &&
212
- schema.type === 'array' &&
213
- IsOptionalString(schema.$id) &&
214
- TSchema(schema.items) &&
215
- IsOptionalNumber(schema.minItems) &&
216
- IsOptionalNumber(schema.maxItems) &&
217
- IsOptionalBoolean(schema.uniqueItems));
218
- }
219
- TypeGuard.TArray = TArray;
220
- /** Returns true if the given schema is TSymbol */
221
- function TBigInt(schema) {
222
- // prettier-ignore
223
- return (TKind(schema) &&
224
- schema[exports.Kind] === 'BigInt' &&
225
- schema.type === 'null' &&
226
- schema.typeOf === 'BigInt' &&
227
- IsOptionalString(schema.$id) &&
228
- IsOptionalBigInt(schema.multipleOf) &&
229
- IsOptionalBigInt(schema.minimum) &&
230
- IsOptionalBigInt(schema.maximum) &&
231
- IsOptionalBigInt(schema.exclusiveMinimum) &&
232
- IsOptionalBigInt(schema.exclusiveMaximum));
233
- }
234
- TypeGuard.TBigInt = TBigInt;
235
- /** Returns true if the given schema is TBoolean */
236
- function TBoolean(schema) {
237
- // prettier-ignore
238
- return (TKind(schema) &&
239
- schema[exports.Kind] === 'Boolean' &&
240
- schema.type === 'boolean' &&
241
- IsOptionalString(schema.$id));
242
- }
243
- TypeGuard.TBoolean = TBoolean;
244
- /** Returns true if the given schema is TConstructor */
245
- function TConstructor(schema) {
246
- // prettier-ignore
247
- if (!(TKind(schema) &&
248
- schema[exports.Kind] === 'Constructor' &&
249
- schema.type === 'object' &&
250
- schema.instanceOf === 'Constructor' &&
251
- IsOptionalString(schema.$id) &&
252
- IsArray(schema.parameters) &&
253
- TSchema(schema.returns))) {
254
- return false;
255
- }
256
- for (const parameter of schema.parameters) {
257
- if (!TSchema(parameter))
258
- return false;
259
- }
260
- return true;
261
- }
262
- TypeGuard.TConstructor = TConstructor;
263
- /** Returns true if the given schema is TDate */
264
- function TDate(schema) {
265
- return (TKind(schema) &&
266
- schema[exports.Kind] === 'Date' &&
267
- schema.type === 'object' &&
268
- schema.instanceOf === 'Date' &&
269
- IsOptionalString(schema.$id) &&
270
- IsOptionalNumber(schema.minimumTimestamp) &&
271
- IsOptionalNumber(schema.maximumTimestamp) &&
272
- IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
273
- IsOptionalNumber(schema.exclusiveMaximumTimestamp));
274
- }
275
- TypeGuard.TDate = TDate;
276
- /** Returns true if the given schema is TFunction */
277
- function TFunction(schema) {
278
- // prettier-ignore
279
- if (!(TKind(schema) &&
280
- schema[exports.Kind] === 'Function' &&
281
- schema.type === 'object' &&
282
- schema.instanceOf === 'Function' &&
283
- IsOptionalString(schema.$id) &&
284
- IsArray(schema.parameters) &&
285
- TSchema(schema.returns))) {
286
- return false;
287
- }
288
- for (const parameter of schema.parameters) {
289
- if (!TSchema(parameter))
290
- return false;
291
- }
292
- return true;
293
- }
294
- TypeGuard.TFunction = TFunction;
295
- /** Returns true if the given schema is TInteger */
296
- function TInteger(schema) {
297
- return (TKind(schema) &&
298
- schema[exports.Kind] === 'Integer' &&
299
- schema.type === 'integer' &&
300
- IsOptionalString(schema.$id) &&
301
- IsOptionalNumber(schema.multipleOf) &&
302
- IsOptionalNumber(schema.minimum) &&
303
- IsOptionalNumber(schema.maximum) &&
304
- IsOptionalNumber(schema.exclusiveMinimum) &&
305
- IsOptionalNumber(schema.exclusiveMaximum));
306
- }
307
- TypeGuard.TInteger = TInteger;
308
- /** Returns true if the given schema is TIntersect */
309
- function TIntersect(schema) {
310
- // prettier-ignore
311
- if (!(TKind(schema) &&
312
- schema[exports.Kind] === 'Intersect' &&
313
- IsArray(schema.allOf) &&
314
- IsOptionalString(schema.type) &&
315
- (IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
316
- IsOptionalString(schema.$id))) {
317
- return false;
318
- }
319
- if ('type' in schema && schema.type !== 'object') {
320
- return false;
321
- }
322
- for (const inner of schema.allOf) {
323
- if (!TSchema(inner))
324
- return false;
325
- }
326
- return true;
327
- }
328
- TypeGuard.TIntersect = TIntersect;
329
- /** Returns true if the given schema is TKind */
330
- function TKind(schema) {
331
- return IsObject(schema) && exports.Kind in schema && typeof schema[exports.Kind] === 'string'; // TS 4.1.5: any required for symbol indexer
332
- }
333
- TypeGuard.TKind = TKind;
334
- /** Returns true if the given schema is TLiteral */
335
- function TLiteral(schema) {
336
- // prettier-ignore
337
- return (TKind(schema) &&
338
- schema[exports.Kind] === 'Literal' &&
339
- IsOptionalString(schema.$id) &&
340
- (IsString(schema.const) ||
341
- IsNumber(schema.const) ||
342
- IsBoolean(schema.const) ||
343
- IsBigInt(schema.const)));
344
- }
345
- TypeGuard.TLiteral = TLiteral;
346
- /** Returns true if the given schema is TNever */
347
- function TNever(schema) {
348
- return (TKind(schema) &&
349
- schema[exports.Kind] === 'Never' &&
350
- IsArray(schema.allOf) &&
351
- schema.allOf.length === 2 &&
352
- IsObject(schema.allOf[0]) &&
353
- IsString(schema.allOf[0].type) &&
354
- schema.allOf[0].type === 'boolean' &&
355
- schema.allOf[0].const === false &&
356
- IsObject(schema.allOf[1]) &&
357
- IsString(schema.allOf[1].type) &&
358
- schema.allOf[1].type === 'boolean' &&
359
- schema.allOf[1].const === true);
360
- }
361
- TypeGuard.TNever = TNever;
362
- /** Returns true if the given schema is TNot */
363
- function TNot(schema) {
364
- // prettier-ignore
365
- return (TKind(schema) &&
366
- schema[exports.Kind] === 'Not' &&
367
- IsArray(schema.allOf) &&
368
- schema.allOf.length === 2 &&
369
- IsObject(schema.allOf[0]) &&
370
- TSchema(schema.allOf[0].not) &&
371
- TSchema(schema.allOf[1]));
372
- }
373
- TypeGuard.TNot = TNot;
374
- /** Returns true if the given schema is TNull */
375
- function TNull(schema) {
376
- // prettier-ignore
377
- return (TKind(schema) &&
378
- schema[exports.Kind] === 'Null' &&
379
- schema.type === 'null' &&
380
- IsOptionalString(schema.$id));
381
- }
382
- TypeGuard.TNull = TNull;
383
- /** Returns true if the given schema is TNumber */
384
- function TNumber(schema) {
385
- return (TKind(schema) &&
386
- schema[exports.Kind] === 'Number' &&
387
- schema.type === 'number' &&
388
- IsOptionalString(schema.$id) &&
389
- IsOptionalNumber(schema.multipleOf) &&
390
- IsOptionalNumber(schema.minimum) &&
391
- IsOptionalNumber(schema.maximum) &&
392
- IsOptionalNumber(schema.exclusiveMinimum) &&
393
- IsOptionalNumber(schema.exclusiveMaximum));
394
- }
395
- TypeGuard.TNumber = TNumber;
396
- /** Returns true if the given schema is TObject */
397
- function TObject(schema) {
398
- if (!(TKind(schema) &&
399
- schema[exports.Kind] === 'Object' &&
400
- schema.type === 'object' &&
401
- IsOptionalString(schema.$id) &&
402
- IsObject(schema.properties) &&
403
- (IsOptionalBoolean(schema.additionalProperties) || IsOptionalSchema(schema.additionalProperties)) &&
404
- IsOptionalNumber(schema.minProperties) &&
405
- IsOptionalNumber(schema.maxProperties))) {
406
- return false;
407
- }
408
- for (const [key, value] of Object.entries(schema.properties)) {
409
- if (!IsControlCharacterFree(key))
410
- return false;
411
- if (!TSchema(value))
412
- return false;
413
- }
414
- return true;
415
- }
416
- TypeGuard.TObject = TObject;
417
- /** Returns true if the given schema is TPromise */
418
- function TPromise(schema) {
419
- // prettier-ignore
420
- return (TKind(schema) &&
421
- schema[exports.Kind] === 'Promise' &&
422
- schema.type === 'object' &&
423
- schema.instanceOf === 'Promise' &&
424
- IsOptionalString(schema.$id) &&
425
- TSchema(schema.item));
426
- }
427
- TypeGuard.TPromise = TPromise;
428
- /** Returns true if the given schema is TRecord */
429
- function TRecord(schema) {
430
- // prettier-ignore
431
- if (!(TKind(schema) &&
432
- schema[exports.Kind] === 'Record' &&
433
- schema.type === 'object' &&
434
- IsOptionalString(schema.$id) &&
435
- schema.additionalProperties === false &&
436
- IsObject(schema.patternProperties))) {
437
- return false;
438
- }
439
- const keys = Object.keys(schema.patternProperties);
440
- if (keys.length !== 1) {
441
- return false;
442
- }
443
- if (!IsPattern(keys[0])) {
444
- return false;
445
- }
446
- if (!TSchema(schema.patternProperties[keys[0]])) {
447
- return false;
448
- }
449
- return true;
450
- }
451
- TypeGuard.TRecord = TRecord;
452
- /** Returns true if the given schema is TSelf */
453
- function TSelf(schema) {
454
- // prettier-ignore
455
- return (TKind(schema) &&
456
- schema[exports.Kind] === 'Self' &&
457
- IsOptionalString(schema.$id) &&
458
- IsString(schema.$ref));
459
- }
460
- TypeGuard.TSelf = TSelf;
461
- /** Returns true if the given schema is TRef */
462
- function TRef(schema) {
463
- // prettier-ignore
464
- return (TKind(schema) &&
465
- schema[exports.Kind] === 'Ref' &&
466
- IsOptionalString(schema.$id) &&
467
- IsString(schema.$ref));
468
- }
469
- TypeGuard.TRef = TRef;
470
- /** Returns true if the given schema is TString */
471
- function TString(schema) {
472
- return (TKind(schema) &&
473
- schema[exports.Kind] === 'String' &&
474
- schema.type === 'string' &&
475
- IsOptionalString(schema.$id) &&
476
- IsOptionalNumber(schema.minLength) &&
477
- IsOptionalNumber(schema.maxLength) &&
478
- IsOptionalPattern(schema.pattern) &&
479
- IsOptionalFormat(schema.format));
480
- }
481
- TypeGuard.TString = TString;
482
- /** Returns true if the given schema is TSymbol */
483
- function TSymbol(schema) {
484
- // prettier-ignore
485
- return (TKind(schema) &&
486
- schema[exports.Kind] === 'Symbol' &&
487
- schema.type === 'null' &&
488
- schema.typeOf === 'Symbol' &&
489
- IsOptionalString(schema.$id));
490
- }
491
- TypeGuard.TSymbol = TSymbol;
492
- /** Returns true if the given schema is TTuple */
493
- function TTuple(schema) {
494
- // prettier-ignore
495
- if (!(TKind(schema) &&
496
- schema[exports.Kind] === 'Tuple' &&
497
- schema.type === 'array' &&
498
- IsOptionalString(schema.$id) &&
499
- IsNumber(schema.minItems) &&
500
- IsNumber(schema.maxItems) &&
501
- schema.minItems === schema.maxItems)) {
502
- return false;
503
- }
504
- if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
505
- return true;
506
- }
507
- if (!IsArray(schema.items)) {
508
- return false;
509
- }
510
- for (const inner of schema.items) {
511
- if (!TSchema(inner))
512
- return false;
513
- }
514
- return true;
515
- }
516
- TypeGuard.TTuple = TTuple;
517
- /** Returns true if the given schema is TUndefined */
518
- function TUndefined(schema) {
519
- // prettier-ignore
520
- return (TKind(schema) &&
521
- schema[exports.Kind] === 'Undefined' &&
522
- schema.type === 'null' &&
523
- schema.typeOf === 'Undefined' &&
524
- IsOptionalString(schema.$id));
525
- }
526
- TypeGuard.TUndefined = TUndefined;
527
- /** Returns true if the given schema is TUnion */
528
- function TUnion(schema) {
529
- // prettier-ignore
530
- if (!(TKind(schema) &&
531
- schema[exports.Kind] === 'Union' &&
532
- IsArray(schema.anyOf) &&
533
- IsOptionalString(schema.$id))) {
534
- return false;
535
- }
536
- for (const inner of schema.anyOf) {
537
- if (!TSchema(inner))
538
- return false;
539
- }
540
- return true;
541
- }
542
- TypeGuard.TUnion = TUnion;
543
- /** Returns true if the given schema is TUnion<Literal<string>[]> */
544
- function TUnionLiteral(schema) {
545
- return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema) && typeof schema.const === 'string');
546
- }
547
- TypeGuard.TUnionLiteral = TUnionLiteral;
548
- /** Returns true if the given schema is TUint8Array */
549
- function TUint8Array(schema) {
550
- return TKind(schema) && schema[exports.Kind] === 'Uint8Array' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.instanceOf === 'Uint8Array' && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
551
- }
552
- TypeGuard.TUint8Array = TUint8Array;
553
- /** Returns true if the given schema is TUnknown */
554
- function TUnknown(schema) {
555
- // prettier-ignore
556
- return (TKind(schema) &&
557
- schema[exports.Kind] === 'Unknown' &&
558
- IsOptionalString(schema.$id));
559
- }
560
- TypeGuard.TUnknown = TUnknown;
561
- /** Returns true if the given schema is TVoid */
562
- function TVoid(schema) {
563
- // prettier-ignore
564
- return (TKind(schema) &&
565
- schema[exports.Kind] === 'Void' &&
566
- schema.type === 'null' &&
567
- schema.typeOf === 'Void' &&
568
- IsOptionalString(schema.$id));
569
- }
570
- TypeGuard.TVoid = TVoid;
571
- /** Returns true if this schema has the ReadonlyOptional modifier */
572
- function TReadonlyOptional(schema) {
573
- return IsObject(schema) && schema[exports.Modifier] === 'ReadonlyOptional';
574
- }
575
- TypeGuard.TReadonlyOptional = TReadonlyOptional;
576
- /** Returns true if this schema has the Readonly modifier */
577
- function TReadonly(schema) {
578
- return IsObject(schema) && schema[exports.Modifier] === 'Readonly';
579
- }
580
- TypeGuard.TReadonly = TReadonly;
581
- /** Returns true if this schema has the Optional modifier */
582
- function TOptional(schema) {
583
- return IsObject(schema) && schema[exports.Modifier] === 'Optional';
584
- }
585
- TypeGuard.TOptional = TOptional;
586
- /** Returns true if the given schema is TSchema */
587
- function TSchema(schema) {
588
- return (typeof schema === 'object' &&
589
- (TAny(schema) ||
590
- TArray(schema) ||
591
- TBoolean(schema) ||
592
- TBigInt(schema) ||
593
- TConstructor(schema) ||
594
- TDate(schema) ||
595
- TFunction(schema) ||
596
- TInteger(schema) ||
597
- TIntersect(schema) ||
598
- TLiteral(schema) ||
599
- TNever(schema) ||
600
- TNot(schema) ||
601
- TNull(schema) ||
602
- TNumber(schema) ||
603
- TObject(schema) ||
604
- TPromise(schema) ||
605
- TRecord(schema) ||
606
- TSelf(schema) ||
607
- TRef(schema) ||
608
- TString(schema) ||
609
- TSymbol(schema) ||
610
- TTuple(schema) ||
611
- TUndefined(schema) ||
612
- TUnion(schema) ||
613
- TUint8Array(schema) ||
614
- TUnknown(schema) ||
615
- TVoid(schema) ||
616
- (TKind(schema) && TypeRegistry.Has(schema[exports.Kind]))));
617
- }
618
- TypeGuard.TSchema = TSchema;
619
- })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
620
- // -------------------------------------------------------------------------------------
621
- // ExtendsUndefined
622
- // -------------------------------------------------------------------------------------
623
- var ExtendsUndefined;
624
- (function (ExtendsUndefined) {
625
- /** Fast undefined check for properties of type undefined */
626
- function Check(schema) {
627
- if (schema[exports.Kind] === 'Undefined')
628
- return true;
629
- if (schema[exports.Kind] === 'Union') {
630
- const union = schema;
631
- return union.anyOf.some((schema) => Check(schema));
632
- }
633
- return false;
634
- }
635
- ExtendsUndefined.Check = Check;
636
- })(ExtendsUndefined = exports.ExtendsUndefined || (exports.ExtendsUndefined = {}));
637
- // -------------------------------------------------------------------------------------
638
- // TypeExtends
639
- // -------------------------------------------------------------------------------------
640
- var TypeExtendsResult;
641
- (function (TypeExtendsResult) {
642
- TypeExtendsResult[TypeExtendsResult["Union"] = 0] = "Union";
643
- TypeExtendsResult[TypeExtendsResult["True"] = 1] = "True";
644
- TypeExtendsResult[TypeExtendsResult["False"] = 2] = "False";
645
- })(TypeExtendsResult = exports.TypeExtendsResult || (exports.TypeExtendsResult = {}));
646
- var TypeExtends;
647
- (function (TypeExtends) {
648
- // -----------------------------------------------------------------------------------
649
- // IntoBooleanResult
650
- // -----------------------------------------------------------------------------------
651
- function IntoBooleanResult(result) {
652
- return result === TypeExtendsResult.False ? TypeExtendsResult.False : TypeExtendsResult.True;
653
- }
654
- // -----------------------------------------------------------------------------------
655
- // Any
656
- // -----------------------------------------------------------------------------------
657
- function AnyRight(left, right) {
658
- return TypeExtendsResult.True;
659
- }
660
- function Any(left, right) {
661
- if (TypeGuard.TIntersect(right))
662
- return IntersectRight(left, right);
663
- if (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema)))
664
- return TypeExtendsResult.True;
665
- if (TypeGuard.TUnion(right))
666
- return TypeExtendsResult.Union;
667
- if (TypeGuard.TUnknown(right))
668
- return TypeExtendsResult.True;
669
- if (TypeGuard.TAny(right))
670
- return TypeExtendsResult.True;
671
- return TypeExtendsResult.Union;
672
- }
673
- // -----------------------------------------------------------------------------------
674
- // Array
675
- // -----------------------------------------------------------------------------------
676
- function ArrayRight(left, right) {
677
- if (TypeGuard.TUnknown(left))
678
- return TypeExtendsResult.False;
679
- if (TypeGuard.TAny(left))
680
- return TypeExtendsResult.Union;
681
- if (TypeGuard.TNever(left))
682
- return TypeExtendsResult.True;
683
- return TypeExtendsResult.False;
684
- }
685
- function Array(left, right) {
686
- if (TypeGuard.TIntersect(right))
687
- return IntersectRight(left, right);
688
- if (TypeGuard.TUnion(right))
689
- return UnionRight(left, right);
690
- if (TypeGuard.TUnknown(right))
691
- return UnknownRight(left, right);
692
- if (TypeGuard.TAny(right))
693
- return AnyRight(left, right);
694
- if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
695
- return TypeExtendsResult.True;
696
- if (!TypeGuard.TArray(right))
697
- return TypeExtendsResult.False;
698
- return IntoBooleanResult(Visit(left.items, right.items));
699
- }
700
- // -----------------------------------------------------------------------------------
701
- // BigInt
702
- // -----------------------------------------------------------------------------------
703
- function BigInt(left, right) {
704
- if (TypeGuard.TIntersect(right))
705
- return IntersectRight(left, right);
706
- if (TypeGuard.TUnion(right))
707
- return UnionRight(left, right);
708
- if (TypeGuard.TNever(right))
709
- return NeverRight(left, right);
710
- if (TypeGuard.TUnknown(right))
711
- return UnknownRight(left, right);
712
- if (TypeGuard.TAny(right))
713
- return AnyRight(left, right);
714
- if (TypeGuard.TObject(right))
715
- return ObjectRight(left, right);
716
- if (TypeGuard.TRecord(right))
717
- return RecordRight(left, right);
718
- return TypeGuard.TBigInt(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
719
- }
720
- // -----------------------------------------------------------------------------------
721
- // Boolean
722
- // -----------------------------------------------------------------------------------
723
- function BooleanRight(left, right) {
724
- if (TypeGuard.TLiteral(left) && typeof left.const === 'boolean')
725
- return TypeExtendsResult.True;
726
- return TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
727
- }
728
- function Boolean(left, right) {
729
- if (TypeGuard.TIntersect(right))
730
- return IntersectRight(left, right);
731
- if (TypeGuard.TUnion(right))
732
- return UnionRight(left, right);
733
- if (TypeGuard.TNever(right))
734
- return NeverRight(left, right);
735
- if (TypeGuard.TUnknown(right))
736
- return UnknownRight(left, right);
737
- if (TypeGuard.TAny(right))
738
- return AnyRight(left, right);
739
- if (TypeGuard.TObject(right))
740
- return ObjectRight(left, right);
741
- if (TypeGuard.TRecord(right))
742
- return RecordRight(left, right);
743
- return TypeGuard.TBoolean(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
744
- }
745
- // -----------------------------------------------------------------------------------
746
- // Constructor
747
- // -----------------------------------------------------------------------------------
748
- function Constructor(left, right) {
749
- if (TypeGuard.TIntersect(right))
750
- return IntersectRight(left, right);
751
- if (TypeGuard.TUnion(right))
752
- return UnionRight(left, right);
753
- if (TypeGuard.TUnknown(right))
754
- return UnknownRight(left, right);
755
- if (TypeGuard.TAny(right))
756
- return AnyRight(left, right);
757
- if (TypeGuard.TObject(right))
758
- return ObjectRight(left, right);
759
- if (!TypeGuard.TConstructor(right))
760
- return TypeExtendsResult.False;
761
- if (left.parameters.length > right.parameters.length)
762
- return TypeExtendsResult.False;
763
- if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
764
- return TypeExtendsResult.False;
765
- }
766
- return IntoBooleanResult(Visit(left.returns, right.returns));
767
- }
768
- // -----------------------------------------------------------------------------------
769
- // Date
770
- // -----------------------------------------------------------------------------------
771
- function Date(left, right) {
772
- if (TypeGuard.TIntersect(right))
773
- return IntersectRight(left, right);
774
- if (TypeGuard.TUnion(right))
775
- return UnionRight(left, right);
776
- if (TypeGuard.TUnknown(right))
777
- return UnknownRight(left, right);
778
- if (TypeGuard.TAny(right))
779
- return AnyRight(left, right);
780
- if (TypeGuard.TObject(right))
781
- return ObjectRight(left, right);
782
- if (TypeGuard.TRecord(right))
783
- return RecordRight(left, right);
784
- return TypeGuard.TDate(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
785
- }
786
- // -----------------------------------------------------------------------------------
787
- // Function
788
- // -----------------------------------------------------------------------------------
789
- function Function(left, right) {
790
- if (TypeGuard.TIntersect(right))
791
- return IntersectRight(left, right);
792
- if (TypeGuard.TUnion(right))
793
- return UnionRight(left, right);
794
- if (TypeGuard.TUnknown(right))
795
- return UnknownRight(left, right);
796
- if (TypeGuard.TAny(right))
797
- return AnyRight(left, right);
798
- if (TypeGuard.TObject(right))
799
- return ObjectRight(left, right);
800
- if (!TypeGuard.TFunction(right))
801
- return TypeExtendsResult.False;
802
- if (left.parameters.length > right.parameters.length)
803
- return TypeExtendsResult.False;
804
- if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
805
- return TypeExtendsResult.False;
806
- }
807
- return IntoBooleanResult(Visit(left.returns, right.returns));
808
- }
809
- // -----------------------------------------------------------------------------------
810
- // Integer
811
- // -----------------------------------------------------------------------------------
812
- function IntegerRight(left, right) {
813
- if (TypeGuard.TLiteral(left) && typeof left.const === 'number')
814
- return TypeExtendsResult.True;
815
- return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
816
- }
817
- function Integer(left, right) {
818
- if (TypeGuard.TIntersect(right))
819
- return IntersectRight(left, right);
820
- if (TypeGuard.TUnion(right))
821
- return UnionRight(left, right);
822
- if (TypeGuard.TNever(right))
823
- return NeverRight(left, right);
824
- if (TypeGuard.TUnknown(right))
825
- return UnknownRight(left, right);
826
- if (TypeGuard.TAny(right))
827
- return AnyRight(left, right);
828
- if (TypeGuard.TObject(right))
829
- return ObjectRight(left, right);
830
- if (TypeGuard.TRecord(right))
831
- return RecordRight(left, right);
832
- return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
833
- }
834
- // -----------------------------------------------------------------------------------
835
- // Intersect
836
- // -----------------------------------------------------------------------------------
837
- function IntersectRight(left, right) {
838
- return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
839
- }
840
- function Intersect(left, right) {
841
- return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
842
- }
843
- // -----------------------------------------------------------------------------------
844
- // Literal
845
- // -----------------------------------------------------------------------------------
846
- function IsLiteralString(schema) {
847
- return typeof schema.const === 'string';
848
- }
849
- function IsLiteralNumber(schema) {
850
- return typeof schema.const === 'number';
851
- }
852
- function IsLiteralBoolean(schema) {
853
- return typeof schema.const === 'boolean';
854
- }
855
- function Literal(left, right) {
856
- if (TypeGuard.TIntersect(right))
857
- return IntersectRight(left, right);
858
- if (TypeGuard.TUnion(right))
859
- return UnionRight(left, right);
860
- if (TypeGuard.TNever(right))
861
- return NeverRight(left, right);
862
- if (TypeGuard.TUnknown(right))
863
- return UnknownRight(left, right);
864
- if (TypeGuard.TAny(right))
865
- return AnyRight(left, right);
866
- if (TypeGuard.TObject(right))
867
- return ObjectRight(left, right);
868
- if (TypeGuard.TRecord(right))
869
- return RecordRight(left, right);
870
- if (TypeGuard.TString(right))
871
- return StringRight(left, right);
872
- if (TypeGuard.TNumber(right))
873
- return NumberRight(left, right);
874
- if (TypeGuard.TInteger(right))
875
- return IntegerRight(left, right);
876
- if (TypeGuard.TBoolean(right))
877
- return BooleanRight(left, right);
878
- return TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : TypeExtendsResult.False;
879
- }
880
- // -----------------------------------------------------------------------------------
881
- // Never
882
- // -----------------------------------------------------------------------------------
883
- function NeverRight(left, right) {
884
- return TypeExtendsResult.False;
885
- }
886
- function Never(left, right) {
887
- return TypeExtendsResult.True;
888
- }
889
- // -----------------------------------------------------------------------------------
890
- // Null
891
- // -----------------------------------------------------------------------------------
892
- function Null(left, right) {
893
- if (TypeGuard.TIntersect(right))
894
- return IntersectRight(left, right);
895
- if (TypeGuard.TUnion(right))
896
- return UnionRight(left, right);
897
- if (TypeGuard.TNever(right))
898
- return NeverRight(left, right);
899
- if (TypeGuard.TUnknown(right))
900
- return UnknownRight(left, right);
901
- if (TypeGuard.TAny(right))
902
- return AnyRight(left, right);
903
- if (TypeGuard.TObject(right))
904
- return ObjectRight(left, right);
905
- if (TypeGuard.TRecord(right))
906
- return RecordRight(left, right);
907
- return TypeGuard.TNull(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
908
- }
909
- // -----------------------------------------------------------------------------------
910
- // Number
911
- // -----------------------------------------------------------------------------------
912
- function NumberRight(left, right) {
913
- if (TypeGuard.TLiteral(left) && IsLiteralNumber(left))
914
- return TypeExtendsResult.True;
915
- return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
916
- }
917
- function Number(left, right) {
918
- if (TypeGuard.TIntersect(right))
919
- return IntersectRight(left, right);
920
- if (TypeGuard.TUnion(right))
921
- return UnionRight(left, right);
922
- if (TypeGuard.TNever(right))
923
- return NeverRight(left, right);
924
- if (TypeGuard.TUnknown(right))
925
- return UnknownRight(left, right);
926
- if (TypeGuard.TAny(right))
927
- return AnyRight(left, right);
928
- if (TypeGuard.TObject(right))
929
- return ObjectRight(left, right);
930
- if (TypeGuard.TRecord(right))
931
- return RecordRight(left, right);
932
- return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
933
- }
934
- // -----------------------------------------------------------------------------------
935
- // Object
936
- // -----------------------------------------------------------------------------------
937
- function IsObjectPropertyCount(schema, count) {
938
- return globalThis.Object.keys(schema.properties).length === count;
939
- }
940
- function IsObjectStringLike(schema) {
941
- return IsObjectArrayLike(schema);
942
- }
943
- function IsObjectSymbolLike(schema) {
944
- // prettier-ignore
945
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && ((TypeGuard.TString(schema.properties.description.anyOf[0]) &&
946
- TypeGuard.TUndefined(schema.properties.description.anyOf[1])) || (TypeGuard.TString(schema.properties.description.anyOf[1]) &&
947
- TypeGuard.TUndefined(schema.properties.description.anyOf[0]))));
948
- }
949
- function IsObjectNumberLike(schema) {
950
- return IsObjectPropertyCount(schema, 0);
951
- }
952
- function IsObjectBooleanLike(schema) {
953
- return IsObjectPropertyCount(schema, 0);
954
- }
955
- function IsObjectBigIntLike(schema) {
956
- return IsObjectPropertyCount(schema, 0);
957
- }
958
- function IsObjectDateLike(schema) {
959
- return IsObjectPropertyCount(schema, 0);
960
- }
961
- function IsObjectUint8ArrayLike(schema) {
962
- return IsObjectArrayLike(schema);
963
- }
964
- function IsObjectFunctionLike(schema) {
965
- const length = exports.Type.Number();
966
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
967
- }
968
- function IsObjectConstructorLike(schema) {
969
- return IsObjectPropertyCount(schema, 0);
970
- }
971
- function IsObjectArrayLike(schema) {
972
- const length = exports.Type.Number();
973
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
974
- }
975
- function IsObjectPromiseLike(schema) {
976
- const then = exports.Type.Function([exports.Type.Any()], exports.Type.Any());
977
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'then' in schema.properties && IntoBooleanResult(Visit(schema.properties['then'], then)) === TypeExtendsResult.True);
978
- }
979
- // -----------------------------------------------------------------------------------
980
- // Property
981
- // -----------------------------------------------------------------------------------
982
- function Property(left, right) {
983
- if (Visit(left, right) === TypeExtendsResult.False)
984
- return TypeExtendsResult.False;
985
- if (TypeGuard.TOptional(left) && !TypeGuard.TOptional(right))
986
- return TypeExtendsResult.False;
987
- return TypeExtendsResult.True;
988
- }
989
- function ObjectRight(left, right) {
990
- if (TypeGuard.TUnknown(left))
991
- return TypeExtendsResult.False;
992
- if (TypeGuard.TAny(left))
993
- return TypeExtendsResult.Union;
994
- if (TypeGuard.TNever(left))
995
- return TypeExtendsResult.True;
996
- if (TypeGuard.TLiteral(left) && IsLiteralString(left) && IsObjectStringLike(right))
997
- return TypeExtendsResult.True;
998
- if (TypeGuard.TLiteral(left) && IsLiteralNumber(left) && IsObjectNumberLike(right))
999
- return TypeExtendsResult.True;
1000
- if (TypeGuard.TLiteral(left) && IsLiteralBoolean(left) && IsObjectBooleanLike(right))
1001
- return TypeExtendsResult.True;
1002
- if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1003
- return TypeExtendsResult.True;
1004
- if (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right))
1005
- return TypeExtendsResult.True;
1006
- if (TypeGuard.TString(left) && IsObjectStringLike(right))
1007
- return TypeExtendsResult.True;
1008
- if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1009
- return TypeExtendsResult.True;
1010
- if (TypeGuard.TNumber(left) && IsObjectNumberLike(right))
1011
- return TypeExtendsResult.True;
1012
- if (TypeGuard.TInteger(left) && IsObjectNumberLike(right))
1013
- return TypeExtendsResult.True;
1014
- if (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right))
1015
- return TypeExtendsResult.True;
1016
- if (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right))
1017
- return TypeExtendsResult.True;
1018
- if (TypeGuard.TDate(left) && IsObjectDateLike(right))
1019
- return TypeExtendsResult.True;
1020
- if (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right))
1021
- return TypeExtendsResult.True;
1022
- if (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))
1023
- return TypeExtendsResult.True;
1024
- if (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) {
1025
- // When expressing a Record with literal key values, the Record is converted into a Object with
1026
- // the Hint assigned as `Record`. This is used to invert the extends logic.
1027
- return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
1028
- }
1029
- if (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) {
1030
- return IsObjectPropertyCount(right, 0) ? TypeExtendsResult.True : TypeExtendsResult.False;
1031
- }
1032
- return TypeExtendsResult.False;
1033
- }
1034
- function Object(left, right) {
1035
- if (TypeGuard.TIntersect(right))
1036
- return IntersectRight(left, right);
1037
- if (TypeGuard.TUnion(right))
1038
- return UnionRight(left, right);
1039
- if (TypeGuard.TUnknown(right))
1040
- return UnknownRight(left, right);
1041
- if (TypeGuard.TAny(right))
1042
- return AnyRight(left, right);
1043
- if (TypeGuard.TRecord(right))
1044
- return RecordRight(left, right);
1045
- if (!TypeGuard.TObject(right))
1046
- return TypeExtendsResult.False;
1047
- for (const key of globalThis.Object.keys(right.properties)) {
1048
- if (!(key in left.properties))
1049
- return TypeExtendsResult.False;
1050
- if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
1051
- return TypeExtendsResult.False;
1052
- }
1053
- }
1054
- return TypeExtendsResult.True;
1055
- }
1056
- // -----------------------------------------------------------------------------------
1057
- // Promise
1058
- // -----------------------------------------------------------------------------------
1059
- function Promise(left, right) {
1060
- if (TypeGuard.TIntersect(right))
1061
- return IntersectRight(left, right);
1062
- if (TypeGuard.TUnion(right))
1063
- return UnionRight(left, right);
1064
- if (TypeGuard.TUnknown(right))
1065
- return UnknownRight(left, right);
1066
- if (TypeGuard.TAny(right))
1067
- return AnyRight(left, right);
1068
- if (TypeGuard.TObject(right) && IsObjectPromiseLike(right))
1069
- return TypeExtendsResult.True;
1070
- if (!TypeGuard.TPromise(right))
1071
- return TypeExtendsResult.False;
1072
- return IntoBooleanResult(Visit(left.item, right.item));
1073
- }
1074
- // -----------------------------------------------------------------------------------
1075
- // Record
1076
- // -----------------------------------------------------------------------------------
1077
- function RecordKey(schema) {
1078
- if ('^(0|[1-9][0-9]*)$' in schema.patternProperties)
1079
- return exports.Type.Number();
1080
- if ('^.*$' in schema.patternProperties)
1081
- return exports.Type.String();
1082
- throw Error('TypeExtends: Cannot get record key');
1083
- }
1084
- function RecordValue(schema) {
1085
- if ('^(0|[1-9][0-9]*)$' in schema.patternProperties)
1086
- return schema.patternProperties['^(0|[1-9][0-9]*)$'];
1087
- if ('^.*$' in schema.patternProperties)
1088
- return schema.patternProperties['^.*$'];
1089
- throw Error('TypeExtends: Cannot get record value');
1090
- }
1091
- function RecordRight(left, right) {
1092
- const Key = RecordKey(right);
1093
- const Value = RecordValue(right);
1094
- if (TypeGuard.TLiteral(left) && IsLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True)
1095
- return TypeExtendsResult.True;
1096
- if (TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key))
1097
- return Visit(left, Value);
1098
- if (TypeGuard.TString(left) && TypeGuard.TNumber(Key))
1099
- return Visit(left, Value);
1100
- if (TypeGuard.TArray(left) && TypeGuard.TNumber(Key))
1101
- return Visit(left, Value);
1102
- if (TypeGuard.TObject(left)) {
1103
- for (const key of globalThis.Object.keys(left.properties)) {
1104
- if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
1105
- return TypeExtendsResult.False;
1106
- }
1107
- }
1108
- return TypeExtendsResult.True;
1109
- }
1110
- return TypeExtendsResult.False;
1111
- }
1112
- function Record(left, right) {
1113
- const Value = RecordValue(left);
1114
- if (TypeGuard.TIntersect(right))
1115
- return IntersectRight(left, right);
1116
- if (TypeGuard.TUnion(right))
1117
- return UnionRight(left, right);
1118
- if (TypeGuard.TUnknown(right))
1119
- return UnknownRight(left, right);
1120
- if (TypeGuard.TAny(right))
1121
- return AnyRight(left, right);
1122
- if (TypeGuard.TObject(right))
1123
- return ObjectRight(left, right);
1124
- if (!TypeGuard.TRecord(right))
1125
- return TypeExtendsResult.False;
1126
- return Visit(Value, RecordValue(right));
1127
- }
1128
- // -----------------------------------------------------------------------------------
1129
- // String
1130
- // -----------------------------------------------------------------------------------
1131
- function StringRight(left, right) {
1132
- if (TypeGuard.TLiteral(left) && typeof left.const === 'string')
1133
- return TypeExtendsResult.True;
1134
- return TypeGuard.TString(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1135
- }
1136
- function String(left, right) {
1137
- if (TypeGuard.TIntersect(right))
1138
- return IntersectRight(left, right);
1139
- if (TypeGuard.TUnion(right))
1140
- return UnionRight(left, right);
1141
- if (TypeGuard.TNever(right))
1142
- return NeverRight(left, right);
1143
- if (TypeGuard.TUnknown(right))
1144
- return UnknownRight(left, right);
1145
- if (TypeGuard.TAny(right))
1146
- return AnyRight(left, right);
1147
- if (TypeGuard.TObject(right))
1148
- return ObjectRight(left, right);
1149
- if (TypeGuard.TRecord(right))
1150
- return RecordRight(left, right);
1151
- return TypeGuard.TString(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1152
- }
1153
- // -----------------------------------------------------------------------------------
1154
- // Symbol
1155
- // -----------------------------------------------------------------------------------
1156
- function Symbol(left, right) {
1157
- if (TypeGuard.TIntersect(right))
1158
- return IntersectRight(left, right);
1159
- if (TypeGuard.TUnion(right))
1160
- return UnionRight(left, right);
1161
- if (TypeGuard.TNever(right))
1162
- return NeverRight(left, right);
1163
- if (TypeGuard.TUnknown(right))
1164
- return UnknownRight(left, right);
1165
- if (TypeGuard.TAny(right))
1166
- return AnyRight(left, right);
1167
- if (TypeGuard.TObject(right))
1168
- return ObjectRight(left, right);
1169
- if (TypeGuard.TRecord(right))
1170
- return RecordRight(left, right);
1171
- return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1172
- }
1173
- // -----------------------------------------------------------------------------------
1174
- // Tuple
1175
- // -----------------------------------------------------------------------------------
1176
- function TupleRight(left, right) {
1177
- if (TypeGuard.TUnknown(left))
1178
- return TypeExtendsResult.False;
1179
- if (TypeGuard.TAny(left))
1180
- return TypeExtendsResult.Union;
1181
- if (TypeGuard.TNever(left))
1182
- return TypeExtendsResult.True;
1183
- return TypeExtendsResult.False;
1184
- }
1185
- function IsArrayOfTuple(left, right) {
1186
- return TypeGuard.TArray(right) && left.items !== undefined && left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True);
1187
- }
1188
- function Tuple(left, right) {
1189
- if (TypeGuard.TIntersect(right))
1190
- return IntersectRight(left, right);
1191
- if (TypeGuard.TUnion(right))
1192
- return UnionRight(left, right);
1193
- if (TypeGuard.TUnknown(right))
1194
- return UnknownRight(left, right);
1195
- if (TypeGuard.TAny(right))
1196
- return AnyRight(left, right);
1197
- if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
1198
- return TypeExtendsResult.True;
1199
- if (TypeGuard.TArray(right) && IsArrayOfTuple(left, right))
1200
- return TypeExtendsResult.True;
1201
- if (!TypeGuard.TTuple(right))
1202
- return TypeExtendsResult.False;
1203
- if ((left.items === undefined && right.items !== undefined) || (left.items !== undefined && right.items === undefined))
1204
- return TypeExtendsResult.False;
1205
- if (left.items === undefined && right.items === undefined)
1206
- return TypeExtendsResult.True;
1207
- return left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1208
- }
1209
- // -----------------------------------------------------------------------------------
1210
- // Uint8Array
1211
- // -----------------------------------------------------------------------------------
1212
- function Uint8Array(left, right) {
1213
- if (TypeGuard.TIntersect(right))
1214
- return IntersectRight(left, right);
1215
- if (TypeGuard.TUnion(right))
1216
- return UnionRight(left, right);
1217
- if (TypeGuard.TUnknown(right))
1218
- return UnknownRight(left, right);
1219
- if (TypeGuard.TAny(right))
1220
- return AnyRight(left, right);
1221
- if (TypeGuard.TObject(right))
1222
- return ObjectRight(left, right);
1223
- if (TypeGuard.TRecord(right))
1224
- return RecordRight(left, right);
1225
- return TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1226
- }
1227
- // -----------------------------------------------------------------------------------
1228
- // Undefined
1229
- // -----------------------------------------------------------------------------------
1230
- function Undefined(left, right) {
1231
- if (TypeGuard.TIntersect(right))
1232
- return IntersectRight(left, right);
1233
- if (TypeGuard.TUnion(right))
1234
- return UnionRight(left, right);
1235
- if (TypeGuard.TNever(right))
1236
- return NeverRight(left, right);
1237
- if (TypeGuard.TUnknown(right))
1238
- return UnknownRight(left, right);
1239
- if (TypeGuard.TAny(right))
1240
- return AnyRight(left, right);
1241
- if (TypeGuard.TObject(right))
1242
- return ObjectRight(left, right);
1243
- if (TypeGuard.TRecord(right))
1244
- return RecordRight(left, right);
1245
- if (TypeGuard.TVoid(right))
1246
- return VoidRight(left, right);
1247
- return TypeGuard.TUndefined(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1248
- }
1249
- // -----------------------------------------------------------------------------------
1250
- // Union
1251
- // -----------------------------------------------------------------------------------
1252
- function UnionRight(left, right) {
1253
- return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1254
- }
1255
- function Union(left, right) {
1256
- return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1257
- }
1258
- // -----------------------------------------------------------------------------------
1259
- // Unknown
1260
- // -----------------------------------------------------------------------------------
1261
- function UnknownRight(left, right) {
1262
- return TypeExtendsResult.True;
1263
- }
1264
- function Unknown(left, right) {
1265
- if (TypeGuard.TIntersect(right))
1266
- return IntersectRight(left, right);
1267
- if (TypeGuard.TUnion(right))
1268
- return UnionRight(left, right);
1269
- if (TypeGuard.TAny(right))
1270
- return AnyRight(left, right);
1271
- if (TypeGuard.TString(right))
1272
- return StringRight(left, right);
1273
- if (TypeGuard.TNumber(right))
1274
- return NumberRight(left, right);
1275
- if (TypeGuard.TInteger(right))
1276
- return IntegerRight(left, right);
1277
- if (TypeGuard.TBoolean(right))
1278
- return BooleanRight(left, right);
1279
- if (TypeGuard.TArray(right))
1280
- return ArrayRight(left, right);
1281
- if (TypeGuard.TTuple(right))
1282
- return TupleRight(left, right);
1283
- if (TypeGuard.TObject(right))
1284
- return ObjectRight(left, right);
1285
- return TypeGuard.TUnknown(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1286
- }
1287
- // -----------------------------------------------------------------------------------
1288
- // Void
1289
- // -----------------------------------------------------------------------------------
1290
- function VoidRight(left, right) {
1291
- if (TypeGuard.TUndefined(left))
1292
- return TypeExtendsResult.True;
1293
- return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1294
- }
1295
- function Void(left, right) {
1296
- if (TypeGuard.TIntersect(right))
1297
- return IntersectRight(left, right);
1298
- if (TypeGuard.TUnion(right))
1299
- return UnionRight(left, right);
1300
- if (TypeGuard.TUnknown(right))
1301
- return UnknownRight(left, right);
1302
- if (TypeGuard.TAny(right))
1303
- return AnyRight(left, right);
1304
- if (TypeGuard.TObject(right))
1305
- return ObjectRight(left, right);
1306
- return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1307
- }
1308
- function Visit(left, right) {
1309
- const right_ = ReferenceRegistry.Deref(right);
1310
- const left_ = ReferenceRegistry.Deref(left);
1311
- if (TypeGuard.TAny(left_))
1312
- return Any(left_, right_);
1313
- if (TypeGuard.TArray(left_))
1314
- return Array(left_, right_);
1315
- if (TypeGuard.TBigInt(left_))
1316
- return BigInt(left_, right_);
1317
- if (TypeGuard.TBoolean(left_))
1318
- return Boolean(left_, right_);
1319
- if (TypeGuard.TConstructor(left_))
1320
- return Constructor(left_, right_);
1321
- if (TypeGuard.TDate(left_))
1322
- return Date(left_, right_);
1323
- if (TypeGuard.TFunction(left_))
1324
- return Function(left_, right_);
1325
- if (TypeGuard.TInteger(left_))
1326
- return Integer(left_, right_);
1327
- if (TypeGuard.TIntersect(left_))
1328
- return Intersect(left_, right_);
1329
- if (TypeGuard.TLiteral(left_))
1330
- return Literal(left_, right_);
1331
- if (TypeGuard.TNever(left_))
1332
- return Never(left_, right_);
1333
- if (TypeGuard.TNull(left_))
1334
- return Null(left_, right_);
1335
- if (TypeGuard.TNumber(left_))
1336
- return Number(left_, right_);
1337
- if (TypeGuard.TRecord(left_))
1338
- return Record(left_, right_);
1339
- if (TypeGuard.TString(left_))
1340
- return String(left_, right_);
1341
- if (TypeGuard.TSymbol(left_))
1342
- return Symbol(left_, right_);
1343
- if (TypeGuard.TObject(left_))
1344
- return Object(left_, right_);
1345
- if (TypeGuard.TTuple(left_))
1346
- return Tuple(left_, right_);
1347
- if (TypeGuard.TPromise(left_))
1348
- return Promise(left_, right_);
1349
- if (TypeGuard.TUint8Array(left_))
1350
- return Uint8Array(left_, right_);
1351
- if (TypeGuard.TUndefined(left_))
1352
- return Undefined(left_, right_);
1353
- if (TypeGuard.TUnion(left_))
1354
- return Union(left_, right_);
1355
- if (TypeGuard.TUnknown(left_))
1356
- return Unknown(left_, right_);
1357
- if (TypeGuard.TVoid(left_))
1358
- return Void(left_, right_);
1359
- throw Error(`TypeExtends: Unknown left operand '${left[exports.Kind]}'`);
1360
- }
1361
- function Extends(left, right) {
1362
- return Visit(left, right);
1363
- }
1364
- TypeExtends.Extends = Extends;
1365
- })(TypeExtends = exports.TypeExtends || (exports.TypeExtends = {}));
1366
- // -------------------------------------------------------------------------------------
1367
- // TypeClone
1368
- // -------------------------------------------------------------------------------------
1369
- /** Specialized Clone for Types. */
1370
- var TypeClone;
1371
- (function (TypeClone) {
1372
- function IsObject(value) {
1373
- return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
1374
- }
1375
- function IsArray(value) {
1376
- return globalThis.Array.isArray(value);
1377
- }
1378
- function Array(value) {
1379
- return value.map((value) => Visit(value));
1380
- }
1381
- function Object(value) {
1382
- const clone = globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), globalThis.Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {}));
1383
- return clone;
1384
- }
1385
- function Visit(value) {
1386
- if (IsObject(value))
1387
- delete value['$id'];
1388
- if (IsObject(value))
1389
- return Object({ ...value });
1390
- if (IsArray(value))
1391
- return Array([...value]);
1392
- return value;
1393
- }
1394
- function Clone(schema, options) {
1395
- return { ...Visit({ ...schema }), ...options };
1396
- }
1397
- TypeClone.Clone = Clone;
1398
- })(TypeClone = exports.TypeClone || (exports.TypeClone = {}));
1399
- // -------------------------------------------------------------------------------------
1400
- // ObjectMap
1401
- // -------------------------------------------------------------------------------------
1402
- var ObjectMap;
1403
- (function (ObjectMap) {
1404
- function Intersect(schema, callback) {
1405
- return exports.Type.Intersect(schema.allOf.map((inner) => Visit(ReferenceRegistry.Deref(inner), callback)), { ...schema });
1406
- }
1407
- function Union(schema, callback) {
1408
- return exports.Type.Union(schema.anyOf.map((inner) => Visit(ReferenceRegistry.Deref(inner), callback)), { ...schema });
1409
- }
1410
- function Object(schema, callback) {
1411
- return callback(schema);
1412
- }
1413
- function Visit(schema, callback) {
1414
- if (TypeGuard.TIntersect(schema))
1415
- return Intersect(schema, callback);
1416
- if (TypeGuard.TUnion(schema))
1417
- return Union(schema, callback);
1418
- if (TypeGuard.TObject(schema))
1419
- return Object(schema, callback);
1420
- return schema;
1421
- }
1422
- function Map(schema, callback, options) {
1423
- return { ...Visit(TypeClone.Clone(schema, {}), callback), ...options };
1424
- }
1425
- ObjectMap.Map = Map;
1426
- })(ObjectMap = exports.ObjectMap || (exports.ObjectMap = {}));
1427
- // -------------------------------------------------------------------------------------
1428
- // KeyResolver
1429
- // -------------------------------------------------------------------------------------
1430
- var KeyResolver;
1431
- (function (KeyResolver) {
1432
- function IsKeyable(schema) {
1433
- return TypeGuard.TIntersect(schema) || TypeGuard.TUnion(schema) || TypeGuard.TObject(schema);
1434
- }
1435
- function Intersect(schema) {
1436
- return [...schema.allOf.filter((schema) => IsKeyable(schema)).reduce((set, schema) => Visit(schema).map((key) => set.add(key))[0], new Set())];
1437
- }
1438
- function Union(schema) {
1439
- const sets = schema.anyOf.filter((schema) => IsKeyable(schema)).map((inner) => Visit(inner));
1440
- return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
1441
- }
1442
- function Object(schema) {
1443
- return globalThis.Object.keys(schema.properties);
1444
- }
1445
- function Visit(schema) {
1446
- if (TypeGuard.TIntersect(schema))
1447
- return Intersect(schema);
1448
- if (TypeGuard.TUnion(schema))
1449
- return Union(schema);
1450
- if (TypeGuard.TObject(schema))
1451
- return Object(schema);
1452
- return [];
1453
- }
1454
- function Resolve(schema) {
1455
- return Visit(schema);
1456
- }
1457
- KeyResolver.Resolve = Resolve;
1458
- })(KeyResolver = exports.KeyResolver || (exports.KeyResolver = {}));
1459
- // -------------------------------------------------------------------------------------
1460
- // TypeOrdinal: Used for auto $id generation
1461
- // -------------------------------------------------------------------------------------
1462
- let TypeOrdinal = 0;
1463
- // -------------------------------------------------------------------------------------
1464
- // TypeBuilder
1465
- // -------------------------------------------------------------------------------------
1466
- class TypeBuilder {
1467
- /** `[Utility]` Creates a schema without `static` and `params` types */
1468
- Create(schema) {
1469
- ReferenceRegistry.Set(schema);
1470
- return schema;
1471
- }
1472
- /** `[Utility]` Clones a schema or properties object */
1473
- Clone(schema, options = {}) {
1474
- const clone = TypeClone.Clone(schema, options);
1475
- ReferenceRegistry.Set(clone);
1476
- return clone;
1477
- }
1478
- /** `[Standard]` Omits compositing symbols from this schema */
1479
- Strict(schema) {
1480
- return JSON.parse(JSON.stringify(schema));
1481
- }
1482
- }
1483
- exports.TypeBuilder = TypeBuilder;
1484
- // -------------------------------------------------------------------------------------
1485
- // StandardTypeBuilder
1486
- // -------------------------------------------------------------------------------------
1487
- class StandardTypeBuilder extends TypeBuilder {
1488
- // ----------------------------------------------------------------------
1489
- // Modifiers
1490
- // ----------------------------------------------------------------------
1491
- /** `[Modifier]` Creates a Optional property */
1492
- Optional(schema) {
1493
- return { [exports.Modifier]: 'Optional', ...this.Clone(schema, {}) };
1494
- }
1495
- /** `[Modifier]` Creates a ReadonlyOptional property */
1496
- ReadonlyOptional(schema) {
1497
- return { [exports.Modifier]: 'ReadonlyOptional', ...this.Clone(schema, {}) };
1498
- }
1499
- /** `[Modifier]` Creates a Readonly object or property */
1500
- Readonly(schema) {
1501
- return { [exports.Modifier]: 'Readonly', ...schema };
1502
- }
1503
- // ----------------------------------------------------------------------
1504
- // Types
1505
- // ----------------------------------------------------------------------
1506
- /** `[Standard]` Creates an Any type */
1507
- Any(options = {}) {
1508
- return this.Create({ ...options, [exports.Kind]: 'Any' });
1509
- }
1510
- /** `[Standard]` Creates an Array type */
1511
- Array(items, options = {}) {
1512
- return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
1513
- }
1514
- /** `[Standard]` Creates a Boolean type */
1515
- Boolean(options = {}) {
1516
- return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
1517
- }
1518
- /** `[Standard]` Creates a Composite object type that will Union any overlapping properties of the given Object array */
1519
- Composite(schemas, options = {}) {
1520
- const optional = new Set();
1521
- for (const schema of schemas) {
1522
- for (const [key, property] of globalThis.Object.entries(schema.properties)) {
1523
- if (TypeGuard.TOptional(property) || TypeGuard.TReadonlyOptional(property))
1524
- optional.add(key);
1525
- }
1526
- }
1527
- const properties = {};
1528
- for (const object of schemas) {
1529
- for (const [key, property] of globalThis.Object.entries(object.properties)) {
1530
- const mapped = key in properties ? this.Union([properties[key], property]) : property;
1531
- properties[key] = optional.has(key) ? this.Optional(mapped) : mapped;
1532
- }
1533
- }
1534
- return this.Object(properties, options);
1535
- }
1536
- /** `[Standard]` Dereferences the given TRef to its target type */
1537
- Deref(schema) {
1538
- return ReferenceRegistry.Deref(schema);
1539
- }
1540
- /** `[Standard]` Creates a Enum type */
1541
- Enum(item, options = {}) {
1542
- // prettier-ignore
1543
- const values = globalThis.Object.keys(item).filter((key) => isNaN(key)).map((key) => item[key]);
1544
- const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
1545
- return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
1546
- }
1547
- /** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
1548
- Extends(left, right, trueType, falseType, options = {}) {
1549
- switch (TypeExtends.Extends(ReferenceRegistry.Deref(left), ReferenceRegistry.Deref(right))) {
1550
- case TypeExtendsResult.Union:
1551
- return this.Union([this.Clone(trueType, options), this.Clone(falseType, options)]);
1552
- case TypeExtendsResult.True:
1553
- return this.Clone(trueType, options);
1554
- case TypeExtendsResult.False:
1555
- return this.Clone(falseType, options);
1556
- }
1557
- }
1558
- /** `[Standard]` Excludes from the left type any type that is not assignable to the right */
1559
- Exclude(left, right, options = {}) {
1560
- if (TypeGuard.TUnion(left)) {
1561
- const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) === TypeExtendsResult.False);
1562
- return (narrowed.length === 1 ? this.Clone(narrowed[0], options) : this.Union(narrowed, options));
1563
- }
1564
- else {
1565
- return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) : this.Clone(left, options));
1566
- }
1567
- }
1568
- /** `[Standard]` Extracts from left left any type that is assignable to the right */
1569
- Extract(left, right, options = {}) {
1570
- if (TypeGuard.TUnion(left)) {
1571
- const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) !== TypeExtendsResult.False);
1572
- return (narrowed.length === 1 ? this.Clone(narrowed[0], options) : this.Union(narrowed, options));
1573
- }
1574
- else {
1575
- return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Clone(left, options) : this.Never(options));
1576
- }
1577
- }
1578
- /** `[Standard]` Creates an Integer type */
1579
- Integer(options = {}) {
1580
- return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
1581
- }
1582
- Intersect(allOf, options = {}) {
1583
- if (allOf.length === 0)
1584
- return exports.Type.Never();
1585
- if (allOf.length === 1)
1586
- return this.Clone(allOf[0], options);
1587
- const objects = allOf.every((schema) => TypeGuard.TObject(schema));
1588
- const cloned = allOf.map((schema) => this.Clone(schema, {}));
1589
- if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
1590
- return this.Create({ ...options, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned });
1591
- }
1592
- else {
1593
- return this.Create({ ...options, [exports.Kind]: 'Intersect', allOf: cloned });
1594
- }
1595
- }
1596
- /** `[Standard]` Creates a KeyOf type */
1597
- KeyOf(schema, options = {}) {
1598
- const keys = KeyResolver.Resolve(ReferenceRegistry.Deref(schema));
1599
- // prettier-ignore
1600
- const keyof = keys.length === 0 ? this.Never(options) : this.Union(keys.map((key) => this.Literal(key)), options);
1601
- return keyof;
1602
- }
1603
- /** `[Standard]` Creates a Literal type */
1604
- Literal(value, options = {}) {
1605
- return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
1606
- }
1607
- /** `[Standard]` Creates a Never type */
1608
- Never(options = {}) {
1609
- // prettier-ignore
1610
- return this.Create({ ...options, [exports.Kind]: 'Never', allOf: [
1611
- { type: 'boolean', const: false },
1612
- { type: 'boolean', const: true },
1613
- ] });
1614
- }
1615
- /** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */
1616
- Not(not, schema, options) {
1617
- return this.Create({ ...options, [exports.Kind]: 'Not', allOf: [{ not }, schema] });
1618
- }
1619
- /** `[Standard]` Creates a Null type */
1620
- Null(options = {}) {
1621
- return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
1622
- }
1623
- /** `[Standard]` Creates a Number type */
1624
- Number(options = {}) {
1625
- return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
1626
- }
1627
- /** `[Standard]` Creates an Object type */
1628
- Object(properties, options = {}) {
1629
- const keys = globalThis.Object.keys(properties);
1630
- const optional = keys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
1631
- const required = keys.filter((name) => !optional.includes(name));
1632
- if (required.length > 0) {
1633
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: this.Clone(properties, {}), required });
1634
- }
1635
- else {
1636
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: this.Clone(properties, {}) });
1637
- }
1638
- }
1639
- Omit(schema, unresolved, options = {}) {
1640
- const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
1641
- // prettier-ignore
1642
- return ObjectMap.Map(this.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1643
- if (schema.required) {
1644
- schema.required = schema.required.filter((key) => !keys.includes(key));
1645
- if (schema.required.length === 0)
1646
- delete schema.required;
1647
- }
1648
- for (const key of globalThis.Object.keys(schema.properties)) {
1649
- if (keys.includes(key))
1650
- delete schema.properties[key];
1651
- }
1652
- return this.Create(schema);
1653
- }, options);
1654
- }
1655
- /** `[Standard]` Creates a mapped type where all properties are Optional */
1656
- Partial(schema, options = {}) {
1657
- function Apply(schema) {
1658
- // prettier-ignore
1659
- switch (schema[exports.Modifier]) {
1660
- case 'ReadonlyOptional':
1661
- schema[exports.Modifier] = 'ReadonlyOptional';
1662
- break;
1663
- case 'Readonly':
1664
- schema[exports.Modifier] = 'ReadonlyOptional';
1665
- break;
1666
- case 'Optional':
1667
- schema[exports.Modifier] = 'Optional';
1668
- break;
1669
- default:
1670
- schema[exports.Modifier] = 'Optional';
1671
- break;
1672
- }
1673
- }
1674
- // prettier-ignore
1675
- return ObjectMap.Map(this.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1676
- delete schema.required;
1677
- globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
1678
- return schema;
1679
- }, options);
1680
- }
1681
- Pick(schema, unresolved, options = {}) {
1682
- const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
1683
- // prettier-ignore
1684
- return ObjectMap.Map(this.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1685
- if (schema.required) {
1686
- schema.required = schema.required.filter((key) => keys.includes(key));
1687
- if (schema.required.length === 0)
1688
- delete schema.required;
1689
- }
1690
- for (const key of globalThis.Object.keys(schema.properties)) {
1691
- if (!keys.includes(key))
1692
- delete schema.properties[key];
1693
- }
1694
- return this.Create(schema);
1695
- }, options);
1696
- }
1697
- Record(key, schema, options = {}) {
1698
- if (TypeGuard.TLiteral(key)) {
1699
- if (typeof key.const === 'string' || typeof key.const === 'number') {
1700
- return this.Object({ [key.const]: schema }, options);
1701
- }
1702
- else
1703
- throw Error('TypeBuilder: Record key can only be derived from literals of number or string');
1704
- }
1705
- if (TypeGuard.TUnion(key)) {
1706
- if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
1707
- const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: this.Clone(schema, {}) }), {});
1708
- return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
1709
- }
1710
- else
1711
- throw Error('TypeBuilder: Record key can only be derived from union literal of number or string');
1712
- }
1713
- const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
1714
- return this.Create({
1715
- ...options,
1716
- [exports.Kind]: 'Record',
1717
- type: 'object',
1718
- patternProperties: { [pattern]: schema },
1719
- additionalProperties: false,
1720
- });
1721
- }
1722
- /** `[Standard]` Creates a Recursive type */
1723
- Recursive(callback, options = {}) {
1724
- if (options.$id === undefined)
1725
- options.$id = `T${TypeOrdinal++}`;
1726
- const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
1727
- const reassign = self;
1728
- reassign.$id = options.$id; // required as $id is readonly
1729
- return this.Create({ ...options, ...self });
1730
- }
1731
- /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
1732
- Ref(schema, options = {}) {
1733
- if (schema.$id === undefined)
1734
- throw Error('TypeBuilder.Ref: Referenced schema must specify an $id');
1735
- return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
1736
- }
1737
- /** `[Standard]` Creates a mapped type where all properties are Required */
1738
- Required(schema, options = {}) {
1739
- function Apply(schema) {
1740
- // prettier-ignore
1741
- switch (schema[exports.Modifier]) {
1742
- case 'ReadonlyOptional':
1743
- schema[exports.Modifier] = 'Readonly';
1744
- break;
1745
- case 'Readonly':
1746
- schema[exports.Modifier] = 'Readonly';
1747
- break;
1748
- case 'Optional':
1749
- delete schema[exports.Modifier];
1750
- break;
1751
- default:
1752
- delete schema[exports.Modifier];
1753
- break;
1754
- }
1755
- }
1756
- // prettier-ignore
1757
- return ObjectMap.Map(this.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1758
- schema.required = globalThis.Object.keys(schema.properties);
1759
- globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
1760
- return schema;
1761
- }, options);
1762
- }
1763
- /** `[Standard]` Creates a String type */
1764
- String(options = {}) {
1765
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
1766
- }
1767
- /** `[Standard]` Creates a Tuple type */
1768
- Tuple(items, options = {}) {
1769
- const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
1770
- // prettier-ignore
1771
- const schema = (items.length > 0 ?
1772
- { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } :
1773
- { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
1774
- return this.Create(schema);
1775
- }
1776
- Union(anyOf, options = {}) {
1777
- if (anyOf.length === 0)
1778
- return this.Never(options);
1779
- if (anyOf.length === 1)
1780
- return this.Clone(anyOf[0], options);
1781
- const cloned = anyOf.map((schema) => this.Clone(schema, {}));
1782
- return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: cloned });
1783
- }
1784
- /** `[Standard]` Creates an Unknown type */
1785
- Unknown(options = {}) {
1786
- return this.Create({ ...options, [exports.Kind]: 'Unknown' });
1787
- }
1788
- /** `[Standard]` Creates a Unsafe type that infers for the generic argument */
1789
- Unsafe(options = {}) {
1790
- return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
1791
- }
1792
- }
1793
- exports.StandardTypeBuilder = StandardTypeBuilder;
1794
- // -------------------------------------------------------------------------------------
1795
- // TypeBuilder
1796
- // -------------------------------------------------------------------------------------
1797
- class ExtendedTypeBuilder extends StandardTypeBuilder {
1798
- /** `[Extended]` Creates a BigInt type */
1799
- BigInt(options = {}) {
1800
- return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'null', typeOf: 'BigInt' });
1801
- }
1802
- /** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
1803
- ConstructorParameters(schema, options = {}) {
1804
- return this.Tuple([...schema.parameters], { ...options });
1805
- }
1806
- Constructor(parameters, returns, options = {}) {
1807
- if (parameters[exports.Kind] === 'Tuple') {
1808
- const inner = parameters.items === undefined ? [] : parameters.items;
1809
- return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
1810
- }
1811
- else if (globalThis.Array.isArray(parameters)) {
1812
- return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
1813
- }
1814
- else {
1815
- throw new Error('TypeBuilder.Constructor: Invalid parameters');
1816
- }
1817
- }
1818
- /** `[Extended]` Creates a Date type */
1819
- Date(options = {}) {
1820
- return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
1821
- }
1822
- Function(parameters, returns, options = {}) {
1823
- if (parameters[exports.Kind] === 'Tuple') {
1824
- const inner = parameters.items === undefined ? [] : parameters.items;
1825
- return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: inner, returns });
1826
- }
1827
- else if (globalThis.Array.isArray(parameters)) {
1828
- return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters, returns });
1829
- }
1830
- else {
1831
- throw new Error('TypeBuilder.Function: Invalid parameters');
1832
- }
1833
- }
1834
- /** `[Extended]` Extracts the InstanceType from the given Constructor */
1835
- InstanceType(schema, options = {}) {
1836
- return this.Clone(schema.returns, options);
1837
- }
1838
- /** `[Extended]` Extracts the Parameters from the given Function type */
1839
- Parameters(schema, options = {}) {
1840
- return this.Tuple(schema.parameters, { ...options });
1841
- }
1842
- /** `[Extended]` Creates a Promise type */
1843
- Promise(item, options = {}) {
1844
- return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
1845
- }
1846
- /** `[Extended]` Creates a regular expression type */
1847
- RegEx(regex, options = {}) {
1848
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
1849
- }
1850
- /** `[Extended]` Extracts the ReturnType from the given Function */
1851
- ReturnType(schema, options = {}) {
1852
- return this.Clone(schema.returns, options);
1853
- }
1854
- /** `[Extended]` Creates a Symbol type */
1855
- Symbol(options) {
1856
- return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'null', typeOf: 'Symbol' });
1857
- }
1858
- /** `[Extended]` Creates a Undefined type */
1859
- Undefined(options = {}) {
1860
- return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
1861
- }
1862
- /** `[Extended]` Creates a Uint8Array type */
1863
- Uint8Array(options = {}) {
1864
- return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
1865
- }
1866
- /** `[Extended]` Creates a Void type */
1867
- Void(options = {}) {
1868
- return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
1869
- }
1870
- }
1871
- exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
1872
- /** JSON Schema TypeBuilder with Static Resolution for TypeScript */
1873
- exports.StandardType = new StandardTypeBuilder();
1874
- /** JSON Schema TypeBuilder with Static Resolution for TypeScript */
1875
- exports.Type = new ExtendedTypeBuilder();
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox
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.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.ReferenceRegistry = exports.TypeRegistry = exports.Kind = exports.Hint = exports.Modifier = exports.Recursive = void 0;
31
+ // --------------------------------------------------------------------------
32
+ // Compositing Symbols
33
+ // --------------------------------------------------------------------------
34
+ exports.Recursive = Symbol.for('TypeBox.Recursive');
35
+ exports.Modifier = Symbol.for('TypeBox.Modifier');
36
+ exports.Hint = Symbol.for('TypeBox.Hint');
37
+ exports.Kind = Symbol.for('TypeBox.Kind');
38
+ var TypeRegistry;
39
+ (function (TypeRegistry) {
40
+ const types = new Map();
41
+ /** Clears all user defined types */
42
+ function Clear() {
43
+ return types.clear();
44
+ }
45
+ TypeRegistry.Clear = Clear;
46
+ /** Returns true if this registry contains this kind */
47
+ function Has(kind) {
48
+ return types.has(kind);
49
+ }
50
+ TypeRegistry.Has = Has;
51
+ /** Sets a validation function for a user defined type */
52
+ function Set(kind, func) {
53
+ types.set(kind, func);
54
+ }
55
+ TypeRegistry.Set = Set;
56
+ /** Gets a custom validation function for a user defined type */
57
+ function Get(kind) {
58
+ return types.get(kind);
59
+ }
60
+ TypeRegistry.Get = Get;
61
+ })(TypeRegistry = exports.TypeRegistry || (exports.TypeRegistry = {}));
62
+ // --------------------------------------------------------------------------
63
+ // ReferenceRegistry
64
+ // --------------------------------------------------------------------------
65
+ var ReferenceRegistry;
66
+ (function (ReferenceRegistry) {
67
+ const references = new Map();
68
+ /** Clears the reference registry */
69
+ function Clear() {
70
+ return references.clear();
71
+ }
72
+ ReferenceRegistry.Clear = Clear;
73
+ /** Returns true if this registry contains this schema */
74
+ function Has(schema) {
75
+ return references.has(schema.$id);
76
+ }
77
+ ReferenceRegistry.Has = Has;
78
+ /** Sets this schema on this registry if a $id exists */
79
+ function Set(schema) {
80
+ const $id = typeof schema === 'object' && schema !== null && typeof schema['$id'] === 'string' ? schema['$id'] : undefined;
81
+ if ($id !== undefined)
82
+ references.set($id, schema);
83
+ }
84
+ ReferenceRegistry.Set = Set;
85
+ /** Dereferences the schema one level deep */
86
+ function DerefOne(schema) {
87
+ if (TypeGuard.TRef(schema) || TypeGuard.TSelf(schema)) {
88
+ if (!references.has(schema.$ref))
89
+ throw Error(`ReferenceRegistry: Cannot deref schema with $id '${schema.$ref}'`);
90
+ return references.get(schema.$ref);
91
+ }
92
+ else {
93
+ return schema;
94
+ }
95
+ }
96
+ ReferenceRegistry.DerefOne = DerefOne;
97
+ /** Dereferences the schema recursively */
98
+ function Deref(schema) {
99
+ if (TypeGuard.TRef(schema)) {
100
+ return Deref(DerefOne(schema));
101
+ }
102
+ else {
103
+ return schema;
104
+ }
105
+ }
106
+ ReferenceRegistry.Deref = Deref;
107
+ })(ReferenceRegistry = exports.ReferenceRegistry || (exports.ReferenceRegistry = {}));
108
+ /** Provides functions to create user defined string formats */
109
+ var FormatRegistry;
110
+ (function (FormatRegistry) {
111
+ const formats = new Map();
112
+ /** Clears all user defined string formats */
113
+ function Clear() {
114
+ return formats.clear();
115
+ }
116
+ FormatRegistry.Clear = Clear;
117
+ /** Returns true if the user defined string format exists */
118
+ function Has(format) {
119
+ return formats.has(format);
120
+ }
121
+ FormatRegistry.Has = Has;
122
+ /** Sets a validation function for a user defined string format */
123
+ function Set(format, func) {
124
+ formats.set(format, func);
125
+ }
126
+ FormatRegistry.Set = Set;
127
+ /** Gets a validation function for a user defined string format */
128
+ function Get(format) {
129
+ return formats.get(format);
130
+ }
131
+ FormatRegistry.Get = Get;
132
+ })(FormatRegistry = exports.FormatRegistry || (exports.FormatRegistry = {}));
133
+ // --------------------------------------------------------------------------
134
+ // TypeGuard
135
+ // --------------------------------------------------------------------------
136
+ class TypeGuardUnknownTypeError extends Error {
137
+ constructor(schema) {
138
+ super('TypeGuard: Unknown type');
139
+ this.schema = schema;
140
+ }
141
+ }
142
+ exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
143
+ var TypeGuard;
144
+ (function (TypeGuard) {
145
+ function IsObject(value) {
146
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
147
+ }
148
+ function IsArray(value) {
149
+ return typeof value === 'object' && value !== null && Array.isArray(value);
150
+ }
151
+ function IsPattern(value) {
152
+ try {
153
+ new RegExp(value);
154
+ return true;
155
+ }
156
+ catch {
157
+ return false;
158
+ }
159
+ }
160
+ function IsControlCharacterFree(value) {
161
+ if (typeof value !== 'string')
162
+ return false;
163
+ for (let i = 0; i < value.length; i++) {
164
+ const code = value.charCodeAt(i);
165
+ if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
166
+ return false;
167
+ }
168
+ }
169
+ return true;
170
+ }
171
+ function IsBigInt(value) {
172
+ return typeof value === 'bigint';
173
+ }
174
+ function IsString(value) {
175
+ return typeof value === 'string';
176
+ }
177
+ function IsNumber(value) {
178
+ return typeof value === 'number' && globalThis.Number.isFinite(value);
179
+ }
180
+ function IsBoolean(value) {
181
+ return typeof value === 'boolean';
182
+ }
183
+ function IsOptionalBigInt(value) {
184
+ return value === undefined || (value !== undefined && IsBigInt(value));
185
+ }
186
+ function IsOptionalNumber(value) {
187
+ return value === undefined || (value !== undefined && IsNumber(value));
188
+ }
189
+ function IsOptionalBoolean(value) {
190
+ return value === undefined || (value !== undefined && IsBoolean(value));
191
+ }
192
+ function IsOptionalString(value) {
193
+ return value === undefined || (value !== undefined && IsString(value));
194
+ }
195
+ function IsOptionalPattern(value) {
196
+ return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
197
+ }
198
+ function IsOptionalFormat(value) {
199
+ return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value));
200
+ }
201
+ function IsOptionalSchema(value) {
202
+ return value === undefined || TSchema(value);
203
+ }
204
+ /** Returns true if the given schema is TAny */
205
+ function TAny(schema) {
206
+ return TKind(schema) && schema[exports.Kind] === 'Any' && IsOptionalString(schema.$id);
207
+ }
208
+ TypeGuard.TAny = TAny;
209
+ /** Returns true if the given schema is TArray */
210
+ function TArray(schema) {
211
+ return (TKind(schema) &&
212
+ schema[exports.Kind] === 'Array' &&
213
+ schema.type === 'array' &&
214
+ IsOptionalString(schema.$id) &&
215
+ TSchema(schema.items) &&
216
+ IsOptionalNumber(schema.minItems) &&
217
+ IsOptionalNumber(schema.maxItems) &&
218
+ IsOptionalBoolean(schema.uniqueItems));
219
+ }
220
+ TypeGuard.TArray = TArray;
221
+ /** Returns true if the given schema is TSymbol */
222
+ function TBigInt(schema) {
223
+ // prettier-ignore
224
+ return (TKind(schema) &&
225
+ schema[exports.Kind] === 'BigInt' &&
226
+ schema.type === 'null' &&
227
+ schema.typeOf === 'BigInt' &&
228
+ IsOptionalString(schema.$id) &&
229
+ IsOptionalBigInt(schema.multipleOf) &&
230
+ IsOptionalBigInt(schema.minimum) &&
231
+ IsOptionalBigInt(schema.maximum) &&
232
+ IsOptionalBigInt(schema.exclusiveMinimum) &&
233
+ IsOptionalBigInt(schema.exclusiveMaximum));
234
+ }
235
+ TypeGuard.TBigInt = TBigInt;
236
+ /** Returns true if the given schema is TBoolean */
237
+ function TBoolean(schema) {
238
+ // prettier-ignore
239
+ return (TKind(schema) &&
240
+ schema[exports.Kind] === 'Boolean' &&
241
+ schema.type === 'boolean' &&
242
+ IsOptionalString(schema.$id));
243
+ }
244
+ TypeGuard.TBoolean = TBoolean;
245
+ /** Returns true if the given schema is TConstructor */
246
+ function TConstructor(schema) {
247
+ // prettier-ignore
248
+ if (!(TKind(schema) &&
249
+ schema[exports.Kind] === 'Constructor' &&
250
+ schema.type === 'object' &&
251
+ schema.instanceOf === 'Constructor' &&
252
+ IsOptionalString(schema.$id) &&
253
+ IsArray(schema.parameters) &&
254
+ TSchema(schema.returns))) {
255
+ return false;
256
+ }
257
+ for (const parameter of schema.parameters) {
258
+ if (!TSchema(parameter))
259
+ return false;
260
+ }
261
+ return true;
262
+ }
263
+ TypeGuard.TConstructor = TConstructor;
264
+ /** Returns true if the given schema is TDate */
265
+ function TDate(schema) {
266
+ return (TKind(schema) &&
267
+ schema[exports.Kind] === 'Date' &&
268
+ schema.type === 'object' &&
269
+ schema.instanceOf === 'Date' &&
270
+ IsOptionalString(schema.$id) &&
271
+ IsOptionalNumber(schema.minimumTimestamp) &&
272
+ IsOptionalNumber(schema.maximumTimestamp) &&
273
+ IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
274
+ IsOptionalNumber(schema.exclusiveMaximumTimestamp));
275
+ }
276
+ TypeGuard.TDate = TDate;
277
+ /** Returns true if the given schema is TFunction */
278
+ function TFunction(schema) {
279
+ // prettier-ignore
280
+ if (!(TKind(schema) &&
281
+ schema[exports.Kind] === 'Function' &&
282
+ schema.type === 'object' &&
283
+ schema.instanceOf === 'Function' &&
284
+ IsOptionalString(schema.$id) &&
285
+ IsArray(schema.parameters) &&
286
+ TSchema(schema.returns))) {
287
+ return false;
288
+ }
289
+ for (const parameter of schema.parameters) {
290
+ if (!TSchema(parameter))
291
+ return false;
292
+ }
293
+ return true;
294
+ }
295
+ TypeGuard.TFunction = TFunction;
296
+ /** Returns true if the given schema is TInteger */
297
+ function TInteger(schema) {
298
+ return (TKind(schema) &&
299
+ schema[exports.Kind] === 'Integer' &&
300
+ schema.type === 'integer' &&
301
+ IsOptionalString(schema.$id) &&
302
+ IsOptionalNumber(schema.multipleOf) &&
303
+ IsOptionalNumber(schema.minimum) &&
304
+ IsOptionalNumber(schema.maximum) &&
305
+ IsOptionalNumber(schema.exclusiveMinimum) &&
306
+ IsOptionalNumber(schema.exclusiveMaximum));
307
+ }
308
+ TypeGuard.TInteger = TInteger;
309
+ /** Returns true if the given schema is TIntersect */
310
+ function TIntersect(schema) {
311
+ // prettier-ignore
312
+ if (!(TKind(schema) &&
313
+ schema[exports.Kind] === 'Intersect' &&
314
+ IsArray(schema.allOf) &&
315
+ IsOptionalString(schema.type) &&
316
+ (IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
317
+ IsOptionalString(schema.$id))) {
318
+ return false;
319
+ }
320
+ if ('type' in schema && schema.type !== 'object') {
321
+ return false;
322
+ }
323
+ for (const inner of schema.allOf) {
324
+ if (!TSchema(inner))
325
+ return false;
326
+ }
327
+ return true;
328
+ }
329
+ TypeGuard.TIntersect = TIntersect;
330
+ /** Returns true if the given schema is TKind */
331
+ function TKind(schema) {
332
+ return IsObject(schema) && exports.Kind in schema && typeof schema[exports.Kind] === 'string'; // TS 4.1.5: any required for symbol indexer
333
+ }
334
+ TypeGuard.TKind = TKind;
335
+ /** Returns true if the given schema is TLiteral */
336
+ function TLiteral(schema) {
337
+ // prettier-ignore
338
+ return (TKind(schema) &&
339
+ schema[exports.Kind] === 'Literal' &&
340
+ IsOptionalString(schema.$id) &&
341
+ (IsString(schema.const) ||
342
+ IsNumber(schema.const) ||
343
+ IsBoolean(schema.const) ||
344
+ IsBigInt(schema.const)));
345
+ }
346
+ TypeGuard.TLiteral = TLiteral;
347
+ /** Returns true if the given schema is TNever */
348
+ function TNever(schema) {
349
+ return (TKind(schema) && schema[exports.Kind] === 'Never' && IsObject(schema.not) && globalThis.Object.getOwnPropertyNames(schema.not).length === 0
350
+ // IsArray(schema.allOf) &&
351
+ // schema.allOf.length === 2 &&
352
+ // IsObject(schema.allOf[0]) &&
353
+ // IsString(schema.allOf[0].type) &&
354
+ // schema.allOf[0].type === 'boolean' &&
355
+ // schema.allOf[0].const === false &&
356
+ // IsObject(schema.allOf[1]) &&
357
+ // IsString(schema.allOf[1].type) &&
358
+ // schema.allOf[1].type === 'boolean' &&
359
+ // schema.allOf[1].const === true
360
+ );
361
+ }
362
+ TypeGuard.TNever = TNever;
363
+ /** Returns true if the given schema is TNot */
364
+ function TNot(schema) {
365
+ // prettier-ignore
366
+ return (TKind(schema) &&
367
+ schema[exports.Kind] === 'Not' &&
368
+ IsArray(schema.allOf) &&
369
+ schema.allOf.length === 2 &&
370
+ IsObject(schema.allOf[0]) &&
371
+ TSchema(schema.allOf[0].not) &&
372
+ TSchema(schema.allOf[1]));
373
+ }
374
+ TypeGuard.TNot = TNot;
375
+ /** Returns true if the given schema is TNull */
376
+ function TNull(schema) {
377
+ // prettier-ignore
378
+ return (TKind(schema) &&
379
+ schema[exports.Kind] === 'Null' &&
380
+ schema.type === 'null' &&
381
+ IsOptionalString(schema.$id));
382
+ }
383
+ TypeGuard.TNull = TNull;
384
+ /** Returns true if the given schema is TNumber */
385
+ function TNumber(schema) {
386
+ return (TKind(schema) &&
387
+ schema[exports.Kind] === 'Number' &&
388
+ schema.type === 'number' &&
389
+ IsOptionalString(schema.$id) &&
390
+ IsOptionalNumber(schema.multipleOf) &&
391
+ IsOptionalNumber(schema.minimum) &&
392
+ IsOptionalNumber(schema.maximum) &&
393
+ IsOptionalNumber(schema.exclusiveMinimum) &&
394
+ IsOptionalNumber(schema.exclusiveMaximum));
395
+ }
396
+ TypeGuard.TNumber = TNumber;
397
+ /** Returns true if the given schema is TObject */
398
+ function TObject(schema) {
399
+ if (!(TKind(schema) &&
400
+ schema[exports.Kind] === 'Object' &&
401
+ schema.type === 'object' &&
402
+ IsOptionalString(schema.$id) &&
403
+ IsObject(schema.properties) &&
404
+ (IsOptionalBoolean(schema.additionalProperties) || IsOptionalSchema(schema.additionalProperties)) &&
405
+ IsOptionalNumber(schema.minProperties) &&
406
+ IsOptionalNumber(schema.maxProperties))) {
407
+ return false;
408
+ }
409
+ for (const [key, value] of Object.entries(schema.properties)) {
410
+ if (!IsControlCharacterFree(key))
411
+ return false;
412
+ if (!TSchema(value))
413
+ return false;
414
+ }
415
+ return true;
416
+ }
417
+ TypeGuard.TObject = TObject;
418
+ /** Returns true if the given schema is TPromise */
419
+ function TPromise(schema) {
420
+ // prettier-ignore
421
+ return (TKind(schema) &&
422
+ schema[exports.Kind] === 'Promise' &&
423
+ schema.type === 'object' &&
424
+ schema.instanceOf === 'Promise' &&
425
+ IsOptionalString(schema.$id) &&
426
+ TSchema(schema.item));
427
+ }
428
+ TypeGuard.TPromise = TPromise;
429
+ /** Returns true if the given schema is TRecord */
430
+ function TRecord(schema) {
431
+ // prettier-ignore
432
+ if (!(TKind(schema) &&
433
+ schema[exports.Kind] === 'Record' &&
434
+ schema.type === 'object' &&
435
+ IsOptionalString(schema.$id) &&
436
+ schema.additionalProperties === false &&
437
+ IsObject(schema.patternProperties))) {
438
+ return false;
439
+ }
440
+ const keys = Object.keys(schema.patternProperties);
441
+ if (keys.length !== 1) {
442
+ return false;
443
+ }
444
+ if (!IsPattern(keys[0])) {
445
+ return false;
446
+ }
447
+ if (!TSchema(schema.patternProperties[keys[0]])) {
448
+ return false;
449
+ }
450
+ return true;
451
+ }
452
+ TypeGuard.TRecord = TRecord;
453
+ /** Returns true if the given schema is TSelf */
454
+ function TSelf(schema) {
455
+ // prettier-ignore
456
+ return (TKind(schema) &&
457
+ schema[exports.Kind] === 'Self' &&
458
+ IsOptionalString(schema.$id) &&
459
+ IsString(schema.$ref));
460
+ }
461
+ TypeGuard.TSelf = TSelf;
462
+ /** Returns true if the given schema is TRef */
463
+ function TRef(schema) {
464
+ // prettier-ignore
465
+ return (TKind(schema) &&
466
+ schema[exports.Kind] === 'Ref' &&
467
+ IsOptionalString(schema.$id) &&
468
+ IsString(schema.$ref));
469
+ }
470
+ TypeGuard.TRef = TRef;
471
+ /** Returns true if the given schema is TString */
472
+ function TString(schema) {
473
+ return (TKind(schema) &&
474
+ schema[exports.Kind] === 'String' &&
475
+ schema.type === 'string' &&
476
+ IsOptionalString(schema.$id) &&
477
+ IsOptionalNumber(schema.minLength) &&
478
+ IsOptionalNumber(schema.maxLength) &&
479
+ IsOptionalPattern(schema.pattern) &&
480
+ IsOptionalFormat(schema.format));
481
+ }
482
+ TypeGuard.TString = TString;
483
+ /** Returns true if the given schema is TSymbol */
484
+ function TSymbol(schema) {
485
+ // prettier-ignore
486
+ return (TKind(schema) &&
487
+ schema[exports.Kind] === 'Symbol' &&
488
+ schema.type === 'null' &&
489
+ schema.typeOf === 'Symbol' &&
490
+ IsOptionalString(schema.$id));
491
+ }
492
+ TypeGuard.TSymbol = TSymbol;
493
+ /** Returns true if the given schema is TTuple */
494
+ function TTuple(schema) {
495
+ // prettier-ignore
496
+ if (!(TKind(schema) &&
497
+ schema[exports.Kind] === 'Tuple' &&
498
+ schema.type === 'array' &&
499
+ IsOptionalString(schema.$id) &&
500
+ IsNumber(schema.minItems) &&
501
+ IsNumber(schema.maxItems) &&
502
+ schema.minItems === schema.maxItems)) {
503
+ return false;
504
+ }
505
+ if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
506
+ return true;
507
+ }
508
+ if (!IsArray(schema.items)) {
509
+ return false;
510
+ }
511
+ for (const inner of schema.items) {
512
+ if (!TSchema(inner))
513
+ return false;
514
+ }
515
+ return true;
516
+ }
517
+ TypeGuard.TTuple = TTuple;
518
+ /** Returns true if the given schema is TUndefined */
519
+ function TUndefined(schema) {
520
+ // prettier-ignore
521
+ return (TKind(schema) &&
522
+ schema[exports.Kind] === 'Undefined' &&
523
+ schema.type === 'null' &&
524
+ schema.typeOf === 'Undefined' &&
525
+ IsOptionalString(schema.$id));
526
+ }
527
+ TypeGuard.TUndefined = TUndefined;
528
+ /** Returns true if the given schema is TUnion */
529
+ function TUnion(schema) {
530
+ // prettier-ignore
531
+ if (!(TKind(schema) &&
532
+ schema[exports.Kind] === 'Union' &&
533
+ IsArray(schema.anyOf) &&
534
+ IsOptionalString(schema.$id))) {
535
+ return false;
536
+ }
537
+ for (const inner of schema.anyOf) {
538
+ if (!TSchema(inner))
539
+ return false;
540
+ }
541
+ return true;
542
+ }
543
+ TypeGuard.TUnion = TUnion;
544
+ /** Returns true if the given schema is TUnion<Literal<string>[]> */
545
+ function TUnionLiteral(schema) {
546
+ return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema) && typeof schema.const === 'string');
547
+ }
548
+ TypeGuard.TUnionLiteral = TUnionLiteral;
549
+ /** Returns true if the given schema is TUint8Array */
550
+ function TUint8Array(schema) {
551
+ return TKind(schema) && schema[exports.Kind] === 'Uint8Array' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.instanceOf === 'Uint8Array' && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
552
+ }
553
+ TypeGuard.TUint8Array = TUint8Array;
554
+ /** Returns true if the given schema is TUnknown */
555
+ function TUnknown(schema) {
556
+ // prettier-ignore
557
+ return (TKind(schema) &&
558
+ schema[exports.Kind] === 'Unknown' &&
559
+ IsOptionalString(schema.$id));
560
+ }
561
+ TypeGuard.TUnknown = TUnknown;
562
+ /** Returns true if the given schema is TVoid */
563
+ function TVoid(schema) {
564
+ // prettier-ignore
565
+ return (TKind(schema) &&
566
+ schema[exports.Kind] === 'Void' &&
567
+ schema.type === 'null' &&
568
+ schema.typeOf === 'Void' &&
569
+ IsOptionalString(schema.$id));
570
+ }
571
+ TypeGuard.TVoid = TVoid;
572
+ /** Returns true if this schema has the ReadonlyOptional modifier */
573
+ function TReadonlyOptional(schema) {
574
+ return IsObject(schema) && schema[exports.Modifier] === 'ReadonlyOptional';
575
+ }
576
+ TypeGuard.TReadonlyOptional = TReadonlyOptional;
577
+ /** Returns true if this schema has the Readonly modifier */
578
+ function TReadonly(schema) {
579
+ return IsObject(schema) && schema[exports.Modifier] === 'Readonly';
580
+ }
581
+ TypeGuard.TReadonly = TReadonly;
582
+ /** Returns true if this schema has the Optional modifier */
583
+ function TOptional(schema) {
584
+ return IsObject(schema) && schema[exports.Modifier] === 'Optional';
585
+ }
586
+ TypeGuard.TOptional = TOptional;
587
+ /** Returns true if the given schema is TSchema */
588
+ function TSchema(schema) {
589
+ return (typeof schema === 'object' &&
590
+ (TAny(schema) ||
591
+ TArray(schema) ||
592
+ TBoolean(schema) ||
593
+ TBigInt(schema) ||
594
+ TConstructor(schema) ||
595
+ TDate(schema) ||
596
+ TFunction(schema) ||
597
+ TInteger(schema) ||
598
+ TIntersect(schema) ||
599
+ TLiteral(schema) ||
600
+ TNever(schema) ||
601
+ TNot(schema) ||
602
+ TNull(schema) ||
603
+ TNumber(schema) ||
604
+ TObject(schema) ||
605
+ TPromise(schema) ||
606
+ TRecord(schema) ||
607
+ TSelf(schema) ||
608
+ TRef(schema) ||
609
+ TString(schema) ||
610
+ TSymbol(schema) ||
611
+ TTuple(schema) ||
612
+ TUndefined(schema) ||
613
+ TUnion(schema) ||
614
+ TUint8Array(schema) ||
615
+ TUnknown(schema) ||
616
+ TVoid(schema) ||
617
+ (TKind(schema) && TypeRegistry.Has(schema[exports.Kind]))));
618
+ }
619
+ TypeGuard.TSchema = TSchema;
620
+ })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
621
+ // --------------------------------------------------------------------------
622
+ // ExtendsUndefined
623
+ // --------------------------------------------------------------------------
624
+ var ExtendsUndefined;
625
+ (function (ExtendsUndefined) {
626
+ /** Fast undefined check for properties of type undefined */
627
+ function Check(schema) {
628
+ if (schema[exports.Kind] === 'Undefined')
629
+ return true;
630
+ if (schema[exports.Kind] === 'Union') {
631
+ const union = schema;
632
+ return union.anyOf.some((schema) => Check(schema));
633
+ }
634
+ return false;
635
+ }
636
+ ExtendsUndefined.Check = Check;
637
+ })(ExtendsUndefined = exports.ExtendsUndefined || (exports.ExtendsUndefined = {}));
638
+ // --------------------------------------------------------------------------
639
+ // TypeExtends
640
+ // --------------------------------------------------------------------------
641
+ var TypeExtendsResult;
642
+ (function (TypeExtendsResult) {
643
+ TypeExtendsResult[TypeExtendsResult["Union"] = 0] = "Union";
644
+ TypeExtendsResult[TypeExtendsResult["True"] = 1] = "True";
645
+ TypeExtendsResult[TypeExtendsResult["False"] = 2] = "False";
646
+ })(TypeExtendsResult = exports.TypeExtendsResult || (exports.TypeExtendsResult = {}));
647
+ var TypeExtends;
648
+ (function (TypeExtends) {
649
+ // --------------------------------------------------------------------------
650
+ // IntoBooleanResult
651
+ // --------------------------------------------------------------------------
652
+ function IntoBooleanResult(result) {
653
+ return result === TypeExtendsResult.False ? TypeExtendsResult.False : TypeExtendsResult.True;
654
+ }
655
+ // --------------------------------------------------------------------------
656
+ // Any
657
+ // --------------------------------------------------------------------------
658
+ function AnyRight(left, right) {
659
+ return TypeExtendsResult.True;
660
+ }
661
+ function Any(left, right) {
662
+ if (TypeGuard.TIntersect(right))
663
+ return IntersectRight(left, right);
664
+ if (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema)))
665
+ return TypeExtendsResult.True;
666
+ if (TypeGuard.TUnion(right))
667
+ return TypeExtendsResult.Union;
668
+ if (TypeGuard.TUnknown(right))
669
+ return TypeExtendsResult.True;
670
+ if (TypeGuard.TAny(right))
671
+ return TypeExtendsResult.True;
672
+ return TypeExtendsResult.Union;
673
+ }
674
+ // --------------------------------------------------------------------------
675
+ // Array
676
+ // --------------------------------------------------------------------------
677
+ function ArrayRight(left, right) {
678
+ if (TypeGuard.TUnknown(left))
679
+ return TypeExtendsResult.False;
680
+ if (TypeGuard.TAny(left))
681
+ return TypeExtendsResult.Union;
682
+ if (TypeGuard.TNever(left))
683
+ return TypeExtendsResult.True;
684
+ return TypeExtendsResult.False;
685
+ }
686
+ function Array(left, right) {
687
+ if (TypeGuard.TIntersect(right))
688
+ return IntersectRight(left, right);
689
+ if (TypeGuard.TUnion(right))
690
+ return UnionRight(left, right);
691
+ if (TypeGuard.TUnknown(right))
692
+ return UnknownRight(left, right);
693
+ if (TypeGuard.TAny(right))
694
+ return AnyRight(left, right);
695
+ if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
696
+ return TypeExtendsResult.True;
697
+ if (!TypeGuard.TArray(right))
698
+ return TypeExtendsResult.False;
699
+ return IntoBooleanResult(Visit(left.items, right.items));
700
+ }
701
+ // --------------------------------------------------------------------------
702
+ // BigInt
703
+ // --------------------------------------------------------------------------
704
+ function BigInt(left, right) {
705
+ if (TypeGuard.TIntersect(right))
706
+ return IntersectRight(left, right);
707
+ if (TypeGuard.TUnion(right))
708
+ return UnionRight(left, right);
709
+ if (TypeGuard.TNever(right))
710
+ return NeverRight(left, right);
711
+ if (TypeGuard.TUnknown(right))
712
+ return UnknownRight(left, right);
713
+ if (TypeGuard.TAny(right))
714
+ return AnyRight(left, right);
715
+ if (TypeGuard.TObject(right))
716
+ return ObjectRight(left, right);
717
+ if (TypeGuard.TRecord(right))
718
+ return RecordRight(left, right);
719
+ return TypeGuard.TBigInt(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
720
+ }
721
+ // --------------------------------------------------------------------------
722
+ // Boolean
723
+ // --------------------------------------------------------------------------
724
+ function BooleanRight(left, right) {
725
+ if (TypeGuard.TLiteral(left) && typeof left.const === 'boolean')
726
+ return TypeExtendsResult.True;
727
+ return TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
728
+ }
729
+ function Boolean(left, right) {
730
+ if (TypeGuard.TIntersect(right))
731
+ return IntersectRight(left, right);
732
+ if (TypeGuard.TUnion(right))
733
+ return UnionRight(left, right);
734
+ if (TypeGuard.TNever(right))
735
+ return NeverRight(left, right);
736
+ if (TypeGuard.TUnknown(right))
737
+ return UnknownRight(left, right);
738
+ if (TypeGuard.TAny(right))
739
+ return AnyRight(left, right);
740
+ if (TypeGuard.TObject(right))
741
+ return ObjectRight(left, right);
742
+ if (TypeGuard.TRecord(right))
743
+ return RecordRight(left, right);
744
+ return TypeGuard.TBoolean(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
745
+ }
746
+ // --------------------------------------------------------------------------
747
+ // Constructor
748
+ // --------------------------------------------------------------------------
749
+ function Constructor(left, right) {
750
+ if (TypeGuard.TIntersect(right))
751
+ return IntersectRight(left, right);
752
+ if (TypeGuard.TUnion(right))
753
+ return UnionRight(left, right);
754
+ if (TypeGuard.TUnknown(right))
755
+ return UnknownRight(left, right);
756
+ if (TypeGuard.TAny(right))
757
+ return AnyRight(left, right);
758
+ if (TypeGuard.TObject(right))
759
+ return ObjectRight(left, right);
760
+ if (!TypeGuard.TConstructor(right))
761
+ return TypeExtendsResult.False;
762
+ if (left.parameters.length > right.parameters.length)
763
+ return TypeExtendsResult.False;
764
+ if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
765
+ return TypeExtendsResult.False;
766
+ }
767
+ return IntoBooleanResult(Visit(left.returns, right.returns));
768
+ }
769
+ // --------------------------------------------------------------------------
770
+ // Date
771
+ // --------------------------------------------------------------------------
772
+ function Date(left, right) {
773
+ if (TypeGuard.TIntersect(right))
774
+ return IntersectRight(left, right);
775
+ if (TypeGuard.TUnion(right))
776
+ return UnionRight(left, right);
777
+ if (TypeGuard.TUnknown(right))
778
+ return UnknownRight(left, right);
779
+ if (TypeGuard.TAny(right))
780
+ return AnyRight(left, right);
781
+ if (TypeGuard.TObject(right))
782
+ return ObjectRight(left, right);
783
+ if (TypeGuard.TRecord(right))
784
+ return RecordRight(left, right);
785
+ return TypeGuard.TDate(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
786
+ }
787
+ // --------------------------------------------------------------------------
788
+ // Function
789
+ // --------------------------------------------------------------------------
790
+ function Function(left, right) {
791
+ if (TypeGuard.TIntersect(right))
792
+ return IntersectRight(left, right);
793
+ if (TypeGuard.TUnion(right))
794
+ return UnionRight(left, right);
795
+ if (TypeGuard.TUnknown(right))
796
+ return UnknownRight(left, right);
797
+ if (TypeGuard.TAny(right))
798
+ return AnyRight(left, right);
799
+ if (TypeGuard.TObject(right))
800
+ return ObjectRight(left, right);
801
+ if (!TypeGuard.TFunction(right))
802
+ return TypeExtendsResult.False;
803
+ if (left.parameters.length > right.parameters.length)
804
+ return TypeExtendsResult.False;
805
+ if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
806
+ return TypeExtendsResult.False;
807
+ }
808
+ return IntoBooleanResult(Visit(left.returns, right.returns));
809
+ }
810
+ // --------------------------------------------------------------------------
811
+ // Integer
812
+ // --------------------------------------------------------------------------
813
+ function IntegerRight(left, right) {
814
+ if (TypeGuard.TLiteral(left) && typeof left.const === 'number')
815
+ return TypeExtendsResult.True;
816
+ return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
817
+ }
818
+ function Integer(left, right) {
819
+ if (TypeGuard.TIntersect(right))
820
+ return IntersectRight(left, right);
821
+ if (TypeGuard.TUnion(right))
822
+ return UnionRight(left, right);
823
+ if (TypeGuard.TNever(right))
824
+ return NeverRight(left, right);
825
+ if (TypeGuard.TUnknown(right))
826
+ return UnknownRight(left, right);
827
+ if (TypeGuard.TAny(right))
828
+ return AnyRight(left, right);
829
+ if (TypeGuard.TObject(right))
830
+ return ObjectRight(left, right);
831
+ if (TypeGuard.TRecord(right))
832
+ return RecordRight(left, right);
833
+ return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
834
+ }
835
+ // --------------------------------------------------------------------------
836
+ // Intersect
837
+ // --------------------------------------------------------------------------
838
+ function IntersectRight(left, right) {
839
+ return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
840
+ }
841
+ function Intersect(left, right) {
842
+ return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
843
+ }
844
+ // --------------------------------------------------------------------------
845
+ // Literal
846
+ // --------------------------------------------------------------------------
847
+ function IsLiteralString(schema) {
848
+ return typeof schema.const === 'string';
849
+ }
850
+ function IsLiteralNumber(schema) {
851
+ return typeof schema.const === 'number';
852
+ }
853
+ function IsLiteralBoolean(schema) {
854
+ return typeof schema.const === 'boolean';
855
+ }
856
+ function Literal(left, right) {
857
+ if (TypeGuard.TIntersect(right))
858
+ return IntersectRight(left, right);
859
+ if (TypeGuard.TUnion(right))
860
+ return UnionRight(left, right);
861
+ if (TypeGuard.TNever(right))
862
+ return NeverRight(left, right);
863
+ if (TypeGuard.TUnknown(right))
864
+ return UnknownRight(left, right);
865
+ if (TypeGuard.TAny(right))
866
+ return AnyRight(left, right);
867
+ if (TypeGuard.TObject(right))
868
+ return ObjectRight(left, right);
869
+ if (TypeGuard.TRecord(right))
870
+ return RecordRight(left, right);
871
+ if (TypeGuard.TString(right))
872
+ return StringRight(left, right);
873
+ if (TypeGuard.TNumber(right))
874
+ return NumberRight(left, right);
875
+ if (TypeGuard.TInteger(right))
876
+ return IntegerRight(left, right);
877
+ if (TypeGuard.TBoolean(right))
878
+ return BooleanRight(left, right);
879
+ return TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : TypeExtendsResult.False;
880
+ }
881
+ // --------------------------------------------------------------------------
882
+ // Never
883
+ // --------------------------------------------------------------------------
884
+ function NeverRight(left, right) {
885
+ return TypeExtendsResult.False;
886
+ }
887
+ function Never(left, right) {
888
+ return TypeExtendsResult.True;
889
+ }
890
+ // --------------------------------------------------------------------------
891
+ // Null
892
+ // --------------------------------------------------------------------------
893
+ function Null(left, right) {
894
+ if (TypeGuard.TIntersect(right))
895
+ return IntersectRight(left, right);
896
+ if (TypeGuard.TUnion(right))
897
+ return UnionRight(left, right);
898
+ if (TypeGuard.TNever(right))
899
+ return NeverRight(left, right);
900
+ if (TypeGuard.TUnknown(right))
901
+ return UnknownRight(left, right);
902
+ if (TypeGuard.TAny(right))
903
+ return AnyRight(left, right);
904
+ if (TypeGuard.TObject(right))
905
+ return ObjectRight(left, right);
906
+ if (TypeGuard.TRecord(right))
907
+ return RecordRight(left, right);
908
+ return TypeGuard.TNull(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
909
+ }
910
+ // --------------------------------------------------------------------------
911
+ // Number
912
+ // --------------------------------------------------------------------------
913
+ function NumberRight(left, right) {
914
+ if (TypeGuard.TLiteral(left) && IsLiteralNumber(left))
915
+ return TypeExtendsResult.True;
916
+ return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
917
+ }
918
+ function Number(left, right) {
919
+ if (TypeGuard.TIntersect(right))
920
+ return IntersectRight(left, right);
921
+ if (TypeGuard.TUnion(right))
922
+ return UnionRight(left, right);
923
+ if (TypeGuard.TNever(right))
924
+ return NeverRight(left, right);
925
+ if (TypeGuard.TUnknown(right))
926
+ return UnknownRight(left, right);
927
+ if (TypeGuard.TAny(right))
928
+ return AnyRight(left, right);
929
+ if (TypeGuard.TObject(right))
930
+ return ObjectRight(left, right);
931
+ if (TypeGuard.TRecord(right))
932
+ return RecordRight(left, right);
933
+ return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
934
+ }
935
+ // --------------------------------------------------------------------------
936
+ // Object
937
+ // --------------------------------------------------------------------------
938
+ function IsObjectPropertyCount(schema, count) {
939
+ return globalThis.Object.keys(schema.properties).length === count;
940
+ }
941
+ function IsObjectStringLike(schema) {
942
+ return IsObjectArrayLike(schema);
943
+ }
944
+ function IsObjectSymbolLike(schema) {
945
+ // prettier-ignore
946
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && ((TypeGuard.TString(schema.properties.description.anyOf[0]) &&
947
+ TypeGuard.TUndefined(schema.properties.description.anyOf[1])) || (TypeGuard.TString(schema.properties.description.anyOf[1]) &&
948
+ TypeGuard.TUndefined(schema.properties.description.anyOf[0]))));
949
+ }
950
+ function IsObjectNumberLike(schema) {
951
+ return IsObjectPropertyCount(schema, 0);
952
+ }
953
+ function IsObjectBooleanLike(schema) {
954
+ return IsObjectPropertyCount(schema, 0);
955
+ }
956
+ function IsObjectBigIntLike(schema) {
957
+ return IsObjectPropertyCount(schema, 0);
958
+ }
959
+ function IsObjectDateLike(schema) {
960
+ return IsObjectPropertyCount(schema, 0);
961
+ }
962
+ function IsObjectUint8ArrayLike(schema) {
963
+ return IsObjectArrayLike(schema);
964
+ }
965
+ function IsObjectFunctionLike(schema) {
966
+ const length = exports.Type.Number();
967
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
968
+ }
969
+ function IsObjectConstructorLike(schema) {
970
+ return IsObjectPropertyCount(schema, 0);
971
+ }
972
+ function IsObjectArrayLike(schema) {
973
+ const length = exports.Type.Number();
974
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
975
+ }
976
+ function IsObjectPromiseLike(schema) {
977
+ const then = exports.Type.Function([exports.Type.Any()], exports.Type.Any());
978
+ return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'then' in schema.properties && IntoBooleanResult(Visit(schema.properties['then'], then)) === TypeExtendsResult.True);
979
+ }
980
+ // --------------------------------------------------------------------------
981
+ // Property
982
+ // --------------------------------------------------------------------------
983
+ function Property(left, right) {
984
+ if (Visit(left, right) === TypeExtendsResult.False)
985
+ return TypeExtendsResult.False;
986
+ if (TypeGuard.TOptional(left) && !TypeGuard.TOptional(right))
987
+ return TypeExtendsResult.False;
988
+ return TypeExtendsResult.True;
989
+ }
990
+ function ObjectRight(left, right) {
991
+ if (TypeGuard.TUnknown(left))
992
+ return TypeExtendsResult.False;
993
+ if (TypeGuard.TAny(left))
994
+ return TypeExtendsResult.Union;
995
+ if (TypeGuard.TNever(left))
996
+ return TypeExtendsResult.True;
997
+ if (TypeGuard.TLiteral(left) && IsLiteralString(left) && IsObjectStringLike(right))
998
+ return TypeExtendsResult.True;
999
+ if (TypeGuard.TLiteral(left) && IsLiteralNumber(left) && IsObjectNumberLike(right))
1000
+ return TypeExtendsResult.True;
1001
+ if (TypeGuard.TLiteral(left) && IsLiteralBoolean(left) && IsObjectBooleanLike(right))
1002
+ return TypeExtendsResult.True;
1003
+ if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1004
+ return TypeExtendsResult.True;
1005
+ if (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right))
1006
+ return TypeExtendsResult.True;
1007
+ if (TypeGuard.TString(left) && IsObjectStringLike(right))
1008
+ return TypeExtendsResult.True;
1009
+ if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1010
+ return TypeExtendsResult.True;
1011
+ if (TypeGuard.TNumber(left) && IsObjectNumberLike(right))
1012
+ return TypeExtendsResult.True;
1013
+ if (TypeGuard.TInteger(left) && IsObjectNumberLike(right))
1014
+ return TypeExtendsResult.True;
1015
+ if (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right))
1016
+ return TypeExtendsResult.True;
1017
+ if (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right))
1018
+ return TypeExtendsResult.True;
1019
+ if (TypeGuard.TDate(left) && IsObjectDateLike(right))
1020
+ return TypeExtendsResult.True;
1021
+ if (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right))
1022
+ return TypeExtendsResult.True;
1023
+ if (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))
1024
+ return TypeExtendsResult.True;
1025
+ if (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) {
1026
+ // When expressing a Record with literal key values, the Record is converted into a Object with
1027
+ // the Hint assigned as `Record`. This is used to invert the extends logic.
1028
+ return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
1029
+ }
1030
+ if (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) {
1031
+ return IsObjectPropertyCount(right, 0) ? TypeExtendsResult.True : TypeExtendsResult.False;
1032
+ }
1033
+ return TypeExtendsResult.False;
1034
+ }
1035
+ function Object(left, right) {
1036
+ if (TypeGuard.TIntersect(right))
1037
+ return IntersectRight(left, right);
1038
+ if (TypeGuard.TUnion(right))
1039
+ return UnionRight(left, right);
1040
+ if (TypeGuard.TUnknown(right))
1041
+ return UnknownRight(left, right);
1042
+ if (TypeGuard.TAny(right))
1043
+ return AnyRight(left, right);
1044
+ if (TypeGuard.TRecord(right))
1045
+ return RecordRight(left, right);
1046
+ if (!TypeGuard.TObject(right))
1047
+ return TypeExtendsResult.False;
1048
+ for (const key of globalThis.Object.keys(right.properties)) {
1049
+ if (!(key in left.properties))
1050
+ return TypeExtendsResult.False;
1051
+ if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
1052
+ return TypeExtendsResult.False;
1053
+ }
1054
+ }
1055
+ return TypeExtendsResult.True;
1056
+ }
1057
+ // --------------------------------------------------------------------------
1058
+ // Promise
1059
+ // --------------------------------------------------------------------------
1060
+ function Promise(left, right) {
1061
+ if (TypeGuard.TIntersect(right))
1062
+ return IntersectRight(left, right);
1063
+ if (TypeGuard.TUnion(right))
1064
+ return UnionRight(left, right);
1065
+ if (TypeGuard.TUnknown(right))
1066
+ return UnknownRight(left, right);
1067
+ if (TypeGuard.TAny(right))
1068
+ return AnyRight(left, right);
1069
+ if (TypeGuard.TObject(right) && IsObjectPromiseLike(right))
1070
+ return TypeExtendsResult.True;
1071
+ if (!TypeGuard.TPromise(right))
1072
+ return TypeExtendsResult.False;
1073
+ return IntoBooleanResult(Visit(left.item, right.item));
1074
+ }
1075
+ // --------------------------------------------------------------------------
1076
+ // Record
1077
+ // --------------------------------------------------------------------------
1078
+ function RecordKey(schema) {
1079
+ if ('^(0|[1-9][0-9]*)$' in schema.patternProperties)
1080
+ return exports.Type.Number();
1081
+ if ('^.*$' in schema.patternProperties)
1082
+ return exports.Type.String();
1083
+ throw Error('TypeExtends: Cannot get record key');
1084
+ }
1085
+ function RecordValue(schema) {
1086
+ if ('^(0|[1-9][0-9]*)$' in schema.patternProperties)
1087
+ return schema.patternProperties['^(0|[1-9][0-9]*)$'];
1088
+ if ('^.*$' in schema.patternProperties)
1089
+ return schema.patternProperties['^.*$'];
1090
+ throw Error('TypeExtends: Cannot get record value');
1091
+ }
1092
+ function RecordRight(left, right) {
1093
+ const Key = RecordKey(right);
1094
+ const Value = RecordValue(right);
1095
+ if (TypeGuard.TLiteral(left) && IsLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True)
1096
+ return TypeExtendsResult.True;
1097
+ if (TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key))
1098
+ return Visit(left, Value);
1099
+ if (TypeGuard.TString(left) && TypeGuard.TNumber(Key))
1100
+ return Visit(left, Value);
1101
+ if (TypeGuard.TArray(left) && TypeGuard.TNumber(Key))
1102
+ return Visit(left, Value);
1103
+ if (TypeGuard.TObject(left)) {
1104
+ for (const key of globalThis.Object.keys(left.properties)) {
1105
+ if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
1106
+ return TypeExtendsResult.False;
1107
+ }
1108
+ }
1109
+ return TypeExtendsResult.True;
1110
+ }
1111
+ return TypeExtendsResult.False;
1112
+ }
1113
+ function Record(left, right) {
1114
+ const Value = RecordValue(left);
1115
+ if (TypeGuard.TIntersect(right))
1116
+ return IntersectRight(left, right);
1117
+ if (TypeGuard.TUnion(right))
1118
+ return UnionRight(left, right);
1119
+ if (TypeGuard.TUnknown(right))
1120
+ return UnknownRight(left, right);
1121
+ if (TypeGuard.TAny(right))
1122
+ return AnyRight(left, right);
1123
+ if (TypeGuard.TObject(right))
1124
+ return ObjectRight(left, right);
1125
+ if (!TypeGuard.TRecord(right))
1126
+ return TypeExtendsResult.False;
1127
+ return Visit(Value, RecordValue(right));
1128
+ }
1129
+ // --------------------------------------------------------------------------
1130
+ // String
1131
+ // --------------------------------------------------------------------------
1132
+ function StringRight(left, right) {
1133
+ if (TypeGuard.TLiteral(left) && typeof left.const === 'string')
1134
+ return TypeExtendsResult.True;
1135
+ return TypeGuard.TString(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1136
+ }
1137
+ function String(left, right) {
1138
+ if (TypeGuard.TIntersect(right))
1139
+ return IntersectRight(left, right);
1140
+ if (TypeGuard.TUnion(right))
1141
+ return UnionRight(left, right);
1142
+ if (TypeGuard.TNever(right))
1143
+ return NeverRight(left, right);
1144
+ if (TypeGuard.TUnknown(right))
1145
+ return UnknownRight(left, right);
1146
+ if (TypeGuard.TAny(right))
1147
+ return AnyRight(left, right);
1148
+ if (TypeGuard.TObject(right))
1149
+ return ObjectRight(left, right);
1150
+ if (TypeGuard.TRecord(right))
1151
+ return RecordRight(left, right);
1152
+ return TypeGuard.TString(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1153
+ }
1154
+ // --------------------------------------------------------------------------
1155
+ // Symbol
1156
+ // --------------------------------------------------------------------------
1157
+ function Symbol(left, right) {
1158
+ if (TypeGuard.TIntersect(right))
1159
+ return IntersectRight(left, right);
1160
+ if (TypeGuard.TUnion(right))
1161
+ return UnionRight(left, right);
1162
+ if (TypeGuard.TNever(right))
1163
+ return NeverRight(left, right);
1164
+ if (TypeGuard.TUnknown(right))
1165
+ return UnknownRight(left, right);
1166
+ if (TypeGuard.TAny(right))
1167
+ return AnyRight(left, right);
1168
+ if (TypeGuard.TObject(right))
1169
+ return ObjectRight(left, right);
1170
+ if (TypeGuard.TRecord(right))
1171
+ return RecordRight(left, right);
1172
+ return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1173
+ }
1174
+ // --------------------------------------------------------------------------
1175
+ // Tuple
1176
+ // --------------------------------------------------------------------------
1177
+ function TupleRight(left, right) {
1178
+ if (TypeGuard.TUnknown(left))
1179
+ return TypeExtendsResult.False;
1180
+ if (TypeGuard.TAny(left))
1181
+ return TypeExtendsResult.Union;
1182
+ if (TypeGuard.TNever(left))
1183
+ return TypeExtendsResult.True;
1184
+ return TypeExtendsResult.False;
1185
+ }
1186
+ function IsArrayOfTuple(left, right) {
1187
+ return TypeGuard.TArray(right) && left.items !== undefined && left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True);
1188
+ }
1189
+ function Tuple(left, right) {
1190
+ if (TypeGuard.TIntersect(right))
1191
+ return IntersectRight(left, right);
1192
+ if (TypeGuard.TUnion(right))
1193
+ return UnionRight(left, right);
1194
+ if (TypeGuard.TUnknown(right))
1195
+ return UnknownRight(left, right);
1196
+ if (TypeGuard.TAny(right))
1197
+ return AnyRight(left, right);
1198
+ if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
1199
+ return TypeExtendsResult.True;
1200
+ if (TypeGuard.TArray(right) && IsArrayOfTuple(left, right))
1201
+ return TypeExtendsResult.True;
1202
+ if (!TypeGuard.TTuple(right))
1203
+ return TypeExtendsResult.False;
1204
+ if ((left.items === undefined && right.items !== undefined) || (left.items !== undefined && right.items === undefined))
1205
+ return TypeExtendsResult.False;
1206
+ if (left.items === undefined && right.items === undefined)
1207
+ return TypeExtendsResult.True;
1208
+ return left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1209
+ }
1210
+ // --------------------------------------------------------------------------
1211
+ // Uint8Array
1212
+ // --------------------------------------------------------------------------
1213
+ function Uint8Array(left, right) {
1214
+ if (TypeGuard.TIntersect(right))
1215
+ return IntersectRight(left, right);
1216
+ if (TypeGuard.TUnion(right))
1217
+ return UnionRight(left, right);
1218
+ if (TypeGuard.TUnknown(right))
1219
+ return UnknownRight(left, right);
1220
+ if (TypeGuard.TAny(right))
1221
+ return AnyRight(left, right);
1222
+ if (TypeGuard.TObject(right))
1223
+ return ObjectRight(left, right);
1224
+ if (TypeGuard.TRecord(right))
1225
+ return RecordRight(left, right);
1226
+ return TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1227
+ }
1228
+ // --------------------------------------------------------------------------
1229
+ // Undefined
1230
+ // --------------------------------------------------------------------------
1231
+ function Undefined(left, right) {
1232
+ if (TypeGuard.TIntersect(right))
1233
+ return IntersectRight(left, right);
1234
+ if (TypeGuard.TUnion(right))
1235
+ return UnionRight(left, right);
1236
+ if (TypeGuard.TNever(right))
1237
+ return NeverRight(left, right);
1238
+ if (TypeGuard.TUnknown(right))
1239
+ return UnknownRight(left, right);
1240
+ if (TypeGuard.TAny(right))
1241
+ return AnyRight(left, right);
1242
+ if (TypeGuard.TObject(right))
1243
+ return ObjectRight(left, right);
1244
+ if (TypeGuard.TRecord(right))
1245
+ return RecordRight(left, right);
1246
+ if (TypeGuard.TVoid(right))
1247
+ return VoidRight(left, right);
1248
+ return TypeGuard.TUndefined(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1249
+ }
1250
+ // --------------------------------------------------------------------------
1251
+ // Union
1252
+ // --------------------------------------------------------------------------
1253
+ function UnionRight(left, right) {
1254
+ return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1255
+ }
1256
+ function Union(left, right) {
1257
+ return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1258
+ }
1259
+ // --------------------------------------------------------------------------
1260
+ // Unknown
1261
+ // --------------------------------------------------------------------------
1262
+ function UnknownRight(left, right) {
1263
+ return TypeExtendsResult.True;
1264
+ }
1265
+ function Unknown(left, right) {
1266
+ if (TypeGuard.TIntersect(right))
1267
+ return IntersectRight(left, right);
1268
+ if (TypeGuard.TUnion(right))
1269
+ return UnionRight(left, right);
1270
+ if (TypeGuard.TAny(right))
1271
+ return AnyRight(left, right);
1272
+ if (TypeGuard.TString(right))
1273
+ return StringRight(left, right);
1274
+ if (TypeGuard.TNumber(right))
1275
+ return NumberRight(left, right);
1276
+ if (TypeGuard.TInteger(right))
1277
+ return IntegerRight(left, right);
1278
+ if (TypeGuard.TBoolean(right))
1279
+ return BooleanRight(left, right);
1280
+ if (TypeGuard.TArray(right))
1281
+ return ArrayRight(left, right);
1282
+ if (TypeGuard.TTuple(right))
1283
+ return TupleRight(left, right);
1284
+ if (TypeGuard.TObject(right))
1285
+ return ObjectRight(left, right);
1286
+ return TypeGuard.TUnknown(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1287
+ }
1288
+ // --------------------------------------------------------------------------
1289
+ // Void
1290
+ // --------------------------------------------------------------------------
1291
+ function VoidRight(left, right) {
1292
+ if (TypeGuard.TUndefined(left))
1293
+ return TypeExtendsResult.True;
1294
+ return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1295
+ }
1296
+ function Void(left, right) {
1297
+ if (TypeGuard.TIntersect(right))
1298
+ return IntersectRight(left, right);
1299
+ if (TypeGuard.TUnion(right))
1300
+ return UnionRight(left, right);
1301
+ if (TypeGuard.TUnknown(right))
1302
+ return UnknownRight(left, right);
1303
+ if (TypeGuard.TAny(right))
1304
+ return AnyRight(left, right);
1305
+ if (TypeGuard.TObject(right))
1306
+ return ObjectRight(left, right);
1307
+ return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1308
+ }
1309
+ function Visit(left, right) {
1310
+ const right_ = ReferenceRegistry.Deref(right);
1311
+ const left_ = ReferenceRegistry.Deref(left);
1312
+ if (TypeGuard.TAny(left_))
1313
+ return Any(left_, right_);
1314
+ if (TypeGuard.TArray(left_))
1315
+ return Array(left_, right_);
1316
+ if (TypeGuard.TBigInt(left_))
1317
+ return BigInt(left_, right_);
1318
+ if (TypeGuard.TBoolean(left_))
1319
+ return Boolean(left_, right_);
1320
+ if (TypeGuard.TConstructor(left_))
1321
+ return Constructor(left_, right_);
1322
+ if (TypeGuard.TDate(left_))
1323
+ return Date(left_, right_);
1324
+ if (TypeGuard.TFunction(left_))
1325
+ return Function(left_, right_);
1326
+ if (TypeGuard.TInteger(left_))
1327
+ return Integer(left_, right_);
1328
+ if (TypeGuard.TIntersect(left_))
1329
+ return Intersect(left_, right_);
1330
+ if (TypeGuard.TLiteral(left_))
1331
+ return Literal(left_, right_);
1332
+ if (TypeGuard.TNever(left_))
1333
+ return Never(left_, right_);
1334
+ if (TypeGuard.TNull(left_))
1335
+ return Null(left_, right_);
1336
+ if (TypeGuard.TNumber(left_))
1337
+ return Number(left_, right_);
1338
+ if (TypeGuard.TRecord(left_))
1339
+ return Record(left_, right_);
1340
+ if (TypeGuard.TString(left_))
1341
+ return String(left_, right_);
1342
+ if (TypeGuard.TSymbol(left_))
1343
+ return Symbol(left_, right_);
1344
+ if (TypeGuard.TObject(left_))
1345
+ return Object(left_, right_);
1346
+ if (TypeGuard.TTuple(left_))
1347
+ return Tuple(left_, right_);
1348
+ if (TypeGuard.TPromise(left_))
1349
+ return Promise(left_, right_);
1350
+ if (TypeGuard.TUint8Array(left_))
1351
+ return Uint8Array(left_, right_);
1352
+ if (TypeGuard.TUndefined(left_))
1353
+ return Undefined(left_, right_);
1354
+ if (TypeGuard.TUnion(left_))
1355
+ return Union(left_, right_);
1356
+ if (TypeGuard.TUnknown(left_))
1357
+ return Unknown(left_, right_);
1358
+ if (TypeGuard.TVoid(left_))
1359
+ return Void(left_, right_);
1360
+ throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
1361
+ }
1362
+ function Extends(left, right) {
1363
+ return Visit(left, right);
1364
+ }
1365
+ TypeExtends.Extends = Extends;
1366
+ })(TypeExtends = exports.TypeExtends || (exports.TypeExtends = {}));
1367
+ // --------------------------------------------------------------------------
1368
+ // TypeClone
1369
+ // --------------------------------------------------------------------------
1370
+ /** Specialized Clone for Types. Clones and removes non self-referential identifiers */
1371
+ var TypeClone;
1372
+ (function (TypeClone) {
1373
+ function IsRecursive(value) {
1374
+ return typeof value[exports.Recursive] === 'string';
1375
+ }
1376
+ function IsObject(value) {
1377
+ return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
1378
+ }
1379
+ function IsArray(value) {
1380
+ return globalThis.Array.isArray(value);
1381
+ }
1382
+ function Array(value) {
1383
+ return value.map((value) => Visit(value));
1384
+ }
1385
+ function Object(value) {
1386
+ const clone = globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), globalThis.Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {}));
1387
+ return clone;
1388
+ }
1389
+ function Visit(value) {
1390
+ const clone = IsObject(value) ? { ...value } : IsArray(value) ? [...value] : value;
1391
+ if (IsObject(clone) && !IsRecursive(clone))
1392
+ delete clone['$id'];
1393
+ if (IsObject(clone))
1394
+ return Object(clone);
1395
+ if (IsArray(clone))
1396
+ return Array(clone);
1397
+ return clone;
1398
+ }
1399
+ /** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
1400
+ function Clone(schema, options) {
1401
+ return { ...Visit({ ...schema }), ...options };
1402
+ }
1403
+ TypeClone.Clone = Clone;
1404
+ })(TypeClone = exports.TypeClone || (exports.TypeClone = {}));
1405
+ // --------------------------------------------------------------------------
1406
+ // ObjectMap
1407
+ // --------------------------------------------------------------------------
1408
+ var ObjectMap;
1409
+ (function (ObjectMap) {
1410
+ function Intersect(schema, callback) {
1411
+ return exports.Type.Intersect(schema.allOf.map((inner) => Visit(ReferenceRegistry.Deref(inner), callback)), { ...schema });
1412
+ }
1413
+ function Union(schema, callback) {
1414
+ return exports.Type.Union(schema.anyOf.map((inner) => Visit(ReferenceRegistry.Deref(inner), callback)), { ...schema });
1415
+ }
1416
+ function Object(schema, callback) {
1417
+ return callback(schema);
1418
+ }
1419
+ function Visit(schema, callback) {
1420
+ if (TypeGuard.TIntersect(schema))
1421
+ return Intersect(schema, callback);
1422
+ if (TypeGuard.TUnion(schema))
1423
+ return Union(schema, callback);
1424
+ if (TypeGuard.TObject(schema))
1425
+ return Object(schema, callback);
1426
+ return schema;
1427
+ }
1428
+ function Map(schema, callback, options) {
1429
+ return { ...Visit(TypeClone.Clone(schema, {}), callback), ...options };
1430
+ }
1431
+ ObjectMap.Map = Map;
1432
+ })(ObjectMap = exports.ObjectMap || (exports.ObjectMap = {}));
1433
+ // --------------------------------------------------------------------------
1434
+ // KeyResolver
1435
+ // --------------------------------------------------------------------------
1436
+ var KeyResolver;
1437
+ (function (KeyResolver) {
1438
+ function IsKeyable(schema) {
1439
+ return TypeGuard.TIntersect(schema) || TypeGuard.TUnion(schema) || (TypeGuard.TObject(schema) && globalThis.Object.getOwnPropertyNames(schema.properties).length > 0);
1440
+ }
1441
+ function Intersect(schema) {
1442
+ return [...schema.allOf.filter((schema) => IsKeyable(schema)).reduce((set, schema) => Visit(schema).map((key) => set.add(key))[0], new Set())];
1443
+ }
1444
+ function Union(schema) {
1445
+ const sets = schema.anyOf.filter((schema) => IsKeyable(schema)).map((inner) => Visit(inner));
1446
+ return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
1447
+ }
1448
+ function Object(schema) {
1449
+ return globalThis.Object.keys(schema.properties);
1450
+ }
1451
+ function Visit(schema) {
1452
+ if (TypeGuard.TIntersect(schema))
1453
+ return Intersect(schema);
1454
+ if (TypeGuard.TUnion(schema))
1455
+ return Union(schema);
1456
+ if (TypeGuard.TObject(schema))
1457
+ return Object(schema);
1458
+ return [];
1459
+ }
1460
+ function Resolve(schema) {
1461
+ return Visit(schema);
1462
+ }
1463
+ KeyResolver.Resolve = Resolve;
1464
+ })(KeyResolver = exports.KeyResolver || (exports.KeyResolver = {}));
1465
+ // --------------------------------------------------------------------------
1466
+ // TypeOrdinal: Used for auto $id generation
1467
+ // --------------------------------------------------------------------------
1468
+ let TypeOrdinal = 0;
1469
+ // --------------------------------------------------------------------------
1470
+ // TypeBuilder
1471
+ // --------------------------------------------------------------------------
1472
+ class TypeBuilder {
1473
+ /** `[Utility]` Creates a schema without `static` and `params` types */
1474
+ Create(schema) {
1475
+ ReferenceRegistry.Set(schema);
1476
+ return schema;
1477
+ }
1478
+ /** `[Standard]` Omits compositing symbols from this schema */
1479
+ Strict(schema) {
1480
+ return JSON.parse(JSON.stringify(schema));
1481
+ }
1482
+ }
1483
+ exports.TypeBuilder = TypeBuilder;
1484
+ // --------------------------------------------------------------------------
1485
+ // StandardTypeBuilder
1486
+ // --------------------------------------------------------------------------
1487
+ class StandardTypeBuilder extends TypeBuilder {
1488
+ // ------------------------------------------------------------------------
1489
+ // Modifiers
1490
+ // ------------------------------------------------------------------------
1491
+ /** `[Modifier]` Creates a Optional property */
1492
+ Optional(schema) {
1493
+ return { [exports.Modifier]: 'Optional', ...TypeClone.Clone(schema, {}) };
1494
+ }
1495
+ /** `[Modifier]` Creates a ReadonlyOptional property */
1496
+ ReadonlyOptional(schema) {
1497
+ return { [exports.Modifier]: 'ReadonlyOptional', ...TypeClone.Clone(schema, {}) };
1498
+ }
1499
+ /** `[Modifier]` Creates a Readonly object or property */
1500
+ Readonly(schema) {
1501
+ return { [exports.Modifier]: 'Readonly', ...schema };
1502
+ }
1503
+ // ------------------------------------------------------------------------
1504
+ // Types
1505
+ // ------------------------------------------------------------------------
1506
+ /** `[Standard]` Creates an Any type */
1507
+ Any(options = {}) {
1508
+ return this.Create({ ...options, [exports.Kind]: 'Any' });
1509
+ }
1510
+ /** `[Standard]` Creates an Array type */
1511
+ Array(items, options = {}) {
1512
+ return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Clone(items, {}) });
1513
+ }
1514
+ /** `[Standard]` Creates a Boolean type */
1515
+ Boolean(options = {}) {
1516
+ return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
1517
+ }
1518
+ /** `[Standard]` Creates a Composite object type that will Union any overlapping properties of the given Object array */
1519
+ Composite(schemas, options = {}) {
1520
+ const optional = new Set();
1521
+ for (const schema of schemas) {
1522
+ for (const [key, property] of globalThis.Object.entries(schema.properties)) {
1523
+ if (TypeGuard.TOptional(property) || TypeGuard.TReadonlyOptional(property))
1524
+ optional.add(key);
1525
+ }
1526
+ }
1527
+ const properties = {};
1528
+ for (const object of schemas) {
1529
+ for (const [key, property] of globalThis.Object.entries(object.properties)) {
1530
+ const mapped = key in properties ? this.Union([properties[key], property]) : TypeClone.Clone(property, {});
1531
+ properties[key] = optional.has(key) ? this.Optional(mapped) : mapped;
1532
+ }
1533
+ }
1534
+ return this.Object(properties, options);
1535
+ }
1536
+ /** `[Standard]` Dereferences the given TRef to its target type */
1537
+ Deref(schema) {
1538
+ return ReferenceRegistry.Deref(schema);
1539
+ }
1540
+ /** `[Standard]` Creates a Enum type */
1541
+ Enum(item, options = {}) {
1542
+ // prettier-ignore
1543
+ const values = globalThis.Object.keys(item).filter((key) => isNaN(key)).map((key) => item[key]);
1544
+ const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
1545
+ return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
1546
+ }
1547
+ /** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
1548
+ Extends(left, right, trueType, falseType, options = {}) {
1549
+ switch (TypeExtends.Extends(ReferenceRegistry.Deref(left), ReferenceRegistry.Deref(right))) {
1550
+ case TypeExtendsResult.Union:
1551
+ return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
1552
+ case TypeExtendsResult.True:
1553
+ return TypeClone.Clone(trueType, options);
1554
+ case TypeExtendsResult.False:
1555
+ return TypeClone.Clone(falseType, options);
1556
+ }
1557
+ }
1558
+ /** `[Standard]` Excludes from the left type any type that is not assignable to the right */
1559
+ Exclude(left, right, options = {}) {
1560
+ if (TypeGuard.TUnion(left)) {
1561
+ const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) === TypeExtendsResult.False);
1562
+ return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
1563
+ }
1564
+ else {
1565
+ return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(left, options));
1566
+ }
1567
+ }
1568
+ /** `[Standard]` Extracts from left left any type that is assignable to the right */
1569
+ Extract(left, right, options = {}) {
1570
+ if (TypeGuard.TUnion(left)) {
1571
+ const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) !== TypeExtendsResult.False);
1572
+ return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
1573
+ }
1574
+ else {
1575
+ return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? TypeClone.Clone(left, options) : this.Never(options));
1576
+ }
1577
+ }
1578
+ /** `[Standard]` Creates an Integer type */
1579
+ Integer(options = {}) {
1580
+ return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
1581
+ }
1582
+ Intersect(allOf, options = {}) {
1583
+ if (allOf.length === 0)
1584
+ return exports.Type.Never();
1585
+ if (allOf.length === 1)
1586
+ return TypeClone.Clone(allOf[0], options);
1587
+ const objects = allOf.every((schema) => TypeGuard.TObject(schema));
1588
+ const cloned = allOf.map((schema) => TypeClone.Clone(schema, {}));
1589
+ const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Clone(options.unevaluatedProperties, {}) } : {};
1590
+ if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
1591
+ return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned });
1592
+ }
1593
+ else {
1594
+ return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', allOf: cloned });
1595
+ }
1596
+ }
1597
+ /** `[Standard]` Creates a KeyOf type */
1598
+ KeyOf(schema, options = {}) {
1599
+ const keys = KeyResolver.Resolve(ReferenceRegistry.Deref(schema));
1600
+ // prettier-ignore
1601
+ const keyof = keys.length === 0 ? this.Never(options) : this.Union(keys.map((key) => this.Literal(key)), options);
1602
+ return keyof;
1603
+ }
1604
+ /** `[Standard]` Creates a Literal type */
1605
+ Literal(value, options = {}) {
1606
+ return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
1607
+ }
1608
+ /** `[Standard]` Creates a Never type */
1609
+ Never(options = {}) {
1610
+ return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
1611
+ }
1612
+ /** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */
1613
+ Not(not, schema, options) {
1614
+ return this.Create({ ...options, [exports.Kind]: 'Not', allOf: [{ not: TypeClone.Clone(not, {}) }, TypeClone.Clone(schema, {})] });
1615
+ }
1616
+ /** `[Standard]` Creates a Null type */
1617
+ Null(options = {}) {
1618
+ return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
1619
+ }
1620
+ /** `[Standard]` Creates a Number type */
1621
+ Number(options = {}) {
1622
+ return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
1623
+ }
1624
+ /** `[Standard]` Creates an Object type */
1625
+ Object(properties, options = {}) {
1626
+ const keys = globalThis.Object.keys(properties);
1627
+ const optional = keys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
1628
+ const required = keys.filter((name) => !optional.includes(name));
1629
+ const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties, {}) } : {};
1630
+ const clonedProperties = globalThis.Object.getOwnPropertyNames(properties).reduce((acc, key) => {
1631
+ return { ...acc, [key]: TypeClone.Clone(properties[key], {}) };
1632
+ }, {});
1633
+ if (required.length > 0) {
1634
+ return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required });
1635
+ }
1636
+ else {
1637
+ return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
1638
+ }
1639
+ }
1640
+ Omit(schema, unresolved, options = {}) {
1641
+ const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
1642
+ // prettier-ignore
1643
+ return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1644
+ if (schema.required) {
1645
+ schema.required = schema.required.filter((key) => !keys.includes(key));
1646
+ if (schema.required.length === 0)
1647
+ delete schema.required;
1648
+ }
1649
+ for (const key of globalThis.Object.keys(schema.properties)) {
1650
+ if (keys.includes(key))
1651
+ delete schema.properties[key];
1652
+ }
1653
+ return this.Create(schema);
1654
+ }, options);
1655
+ }
1656
+ /** `[Standard]` Creates a mapped type where all properties are Optional */
1657
+ Partial(schema, options = {}) {
1658
+ function Apply(schema) {
1659
+ // prettier-ignore
1660
+ switch (schema[exports.Modifier]) {
1661
+ case 'ReadonlyOptional':
1662
+ schema[exports.Modifier] = 'ReadonlyOptional';
1663
+ break;
1664
+ case 'Readonly':
1665
+ schema[exports.Modifier] = 'ReadonlyOptional';
1666
+ break;
1667
+ case 'Optional':
1668
+ schema[exports.Modifier] = 'Optional';
1669
+ break;
1670
+ default:
1671
+ schema[exports.Modifier] = 'Optional';
1672
+ break;
1673
+ }
1674
+ }
1675
+ // prettier-ignore
1676
+ return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1677
+ delete schema.required;
1678
+ globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
1679
+ return schema;
1680
+ }, options);
1681
+ }
1682
+ Pick(schema, unresolved, options = {}) {
1683
+ const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
1684
+ // prettier-ignore
1685
+ return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1686
+ if (schema.required) {
1687
+ schema.required = schema.required.filter((key) => keys.includes(key));
1688
+ if (schema.required.length === 0)
1689
+ delete schema.required;
1690
+ }
1691
+ for (const key of globalThis.Object.keys(schema.properties)) {
1692
+ if (!keys.includes(key))
1693
+ delete schema.properties[key];
1694
+ }
1695
+ return this.Create(schema);
1696
+ }, options);
1697
+ }
1698
+ Record(key, schema, options = {}) {
1699
+ if (TypeGuard.TLiteral(key)) {
1700
+ if (typeof key.const === 'string' || typeof key.const === 'number') {
1701
+ return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
1702
+ }
1703
+ else
1704
+ throw Error('TypeBuilder: Record key can only be derived from literals of number or string');
1705
+ }
1706
+ if (TypeGuard.TUnion(key)) {
1707
+ if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
1708
+ const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
1709
+ return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
1710
+ }
1711
+ else
1712
+ throw Error('TypeBuilder: Record key can only be derived from union literal of number or string');
1713
+ }
1714
+ const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
1715
+ return this.Create({
1716
+ ...options,
1717
+ [exports.Kind]: 'Record',
1718
+ type: 'object',
1719
+ patternProperties: { [pattern]: TypeClone.Clone(schema, {}) },
1720
+ additionalProperties: false,
1721
+ });
1722
+ }
1723
+ /** `[Standard]` Creates a Recursive type */
1724
+ Recursive(callback, options = {}) {
1725
+ if (options.$id === undefined)
1726
+ options.$id = `T${TypeOrdinal++}`;
1727
+ const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
1728
+ const reassign = self;
1729
+ reassign.$id = options.$id; // required as $id is readonly
1730
+ return this.Create({ ...options, ...self, [exports.Recursive]: reassign.$id });
1731
+ }
1732
+ /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
1733
+ Ref(schema, options = {}) {
1734
+ if (schema.$id === undefined)
1735
+ throw Error('StandardTypeBuilder.Ref: Target type must specify an $id');
1736
+ return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
1737
+ }
1738
+ /** `[Standard]` Creates a mapped type where all properties are Required */
1739
+ Required(schema, options = {}) {
1740
+ function Apply(schema) {
1741
+ // prettier-ignore
1742
+ switch (schema[exports.Modifier]) {
1743
+ case 'ReadonlyOptional':
1744
+ schema[exports.Modifier] = 'Readonly';
1745
+ break;
1746
+ case 'Readonly':
1747
+ schema[exports.Modifier] = 'Readonly';
1748
+ break;
1749
+ case 'Optional':
1750
+ delete schema[exports.Modifier];
1751
+ break;
1752
+ default:
1753
+ delete schema[exports.Modifier];
1754
+ break;
1755
+ }
1756
+ }
1757
+ // prettier-ignore
1758
+ return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
1759
+ schema.required = globalThis.Object.keys(schema.properties);
1760
+ globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
1761
+ return schema;
1762
+ }, options);
1763
+ }
1764
+ /** `[Standard]` Creates a String type */
1765
+ String(options = {}) {
1766
+ return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
1767
+ }
1768
+ /** `[Standard]` Creates a Tuple type */
1769
+ Tuple(items, options = {}) {
1770
+ const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
1771
+ const clonedItems = items.map((item) => TypeClone.Clone(item, {}));
1772
+ // prettier-ignore
1773
+ const schema = (items.length > 0 ?
1774
+ { ...options, [exports.Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
1775
+ { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
1776
+ return this.Create(schema);
1777
+ }
1778
+ Union(anyOf, options = {}) {
1779
+ if (anyOf.length === 0)
1780
+ return this.Never(options);
1781
+ if (anyOf.length === 1)
1782
+ return TypeClone.Clone(anyOf[0], options);
1783
+ const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema, {}));
1784
+ return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
1785
+ }
1786
+ /** `[Standard]` Creates an Unknown type */
1787
+ Unknown(options = {}) {
1788
+ return this.Create({ ...options, [exports.Kind]: 'Unknown' });
1789
+ }
1790
+ /** `[Standard]` Creates a Unsafe type that infers for the generic argument */
1791
+ Unsafe(options = {}) {
1792
+ return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
1793
+ }
1794
+ }
1795
+ exports.StandardTypeBuilder = StandardTypeBuilder;
1796
+ // --------------------------------------------------------------------------
1797
+ // TypeBuilder
1798
+ // --------------------------------------------------------------------------
1799
+ class ExtendedTypeBuilder extends StandardTypeBuilder {
1800
+ /** `[Extended]` Creates a BigInt type */
1801
+ BigInt(options = {}) {
1802
+ return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'null', typeOf: 'BigInt' });
1803
+ }
1804
+ /** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
1805
+ ConstructorParameters(schema, options = {}) {
1806
+ return this.Tuple([...schema.parameters], { ...options });
1807
+ }
1808
+ Constructor(parameters, returns, options = {}) {
1809
+ const clonedReturns = TypeClone.Clone(returns, {});
1810
+ if (TypeGuard.TTuple(parameters)) {
1811
+ const clonedParameters = parameters.items === undefined ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
1812
+ return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
1813
+ }
1814
+ else if (globalThis.Array.isArray(parameters)) {
1815
+ const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
1816
+ return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
1817
+ }
1818
+ else {
1819
+ throw new Error('ExtendedTypeBuilder.Constructor: Invalid parameters');
1820
+ }
1821
+ }
1822
+ /** `[Extended]` Creates a Date type */
1823
+ Date(options = {}) {
1824
+ return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
1825
+ }
1826
+ Function(parameters, returns, options = {}) {
1827
+ const clonedReturns = TypeClone.Clone(returns, {});
1828
+ if (TypeGuard.TTuple(parameters)) {
1829
+ const clonedParameters = parameters.items === undefined ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
1830
+ return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
1831
+ }
1832
+ else if (globalThis.Array.isArray(parameters)) {
1833
+ const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
1834
+ return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
1835
+ }
1836
+ else {
1837
+ throw new Error('ExtendedTypeBuilder.Function: Invalid parameters');
1838
+ }
1839
+ }
1840
+ /** `[Extended]` Extracts the InstanceType from the given Constructor */
1841
+ InstanceType(schema, options = {}) {
1842
+ return TypeClone.Clone(schema.returns, options);
1843
+ }
1844
+ /** `[Extended]` Extracts the Parameters from the given Function type */
1845
+ Parameters(schema, options = {}) {
1846
+ return this.Tuple(schema.parameters, { ...options });
1847
+ }
1848
+ /** `[Extended]` Creates a Promise type */
1849
+ Promise(item, options = {}) {
1850
+ return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item: TypeClone.Clone(item, {}) });
1851
+ }
1852
+ /** `[Extended]` Creates a regular expression type */
1853
+ RegEx(regex, options = {}) {
1854
+ return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
1855
+ }
1856
+ /** `[Extended]` Extracts the ReturnType from the given Function */
1857
+ ReturnType(schema, options = {}) {
1858
+ return TypeClone.Clone(schema.returns, options);
1859
+ }
1860
+ /** `[Extended]` Creates a Symbol type */
1861
+ Symbol(options) {
1862
+ return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'null', typeOf: 'Symbol' });
1863
+ }
1864
+ /** `[Extended]` Creates a Undefined type */
1865
+ Undefined(options = {}) {
1866
+ return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
1867
+ }
1868
+ /** `[Extended]` Creates a Uint8Array type */
1869
+ Uint8Array(options = {}) {
1870
+ return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
1871
+ }
1872
+ /** `[Extended]` Creates a Void type */
1873
+ Void(options = {}) {
1874
+ return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
1875
+ }
1876
+ }
1877
+ exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
1878
+ /** JSON Schema TypeBuilder with Static Resolution for TypeScript */
1879
+ exports.StandardType = new StandardTypeBuilder();
1880
+ /** JSON Schema TypeBuilder with Static Resolution for TypeScript */
1881
+ exports.Type = new ExtendedTypeBuilder();