@sinclair/typebox 0.25.23 → 0.26.0-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.d.ts +9 -4
- package/compiler/compiler.js +160 -119
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -149
- package/package.json +1 -6
- package/readme.md +395 -396
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +386 -162
- package/typebox.js +1710 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +120 -192
- package/value/check.d.ts +1 -1
- package/value/check.js +162 -107
- package/value/convert.d.ts +13 -0
- package/value/convert.js +345 -0
- package/value/create.d.ts +6 -2
- package/value/create.js +149 -107
- package/{hash → value}/hash.js +39 -14
- package/value/index.d.ts +1 -0
- package/value/index.js +3 -1
- package/value/value.d.ts +2 -8
- package/value/value.js +20 -14
- package/conditional/conditional.d.ts +0 -17
- package/conditional/conditional.js +0 -91
- package/conditional/index.d.ts +0 -2
- package/conditional/index.js +0 -45
- package/conditional/structural.d.ts +0 -11
- package/conditional/structural.js +0 -685
- package/custom/custom.d.ts +0 -12
- package/custom/custom.js +0 -55
- package/custom/index.d.ts +0 -1
- package/custom/index.js +0 -44
- package/format/format.d.ts +0 -12
- package/format/format.js +0 -55
- package/format/index.d.ts +0 -1
- package/format/index.js +0 -44
- package/guard/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -1
- package/guard/index.js +0 -44
- package/hash/index.d.ts +0 -1
- package/hash/index.js +0 -44
- /package/{hash → value}/hash.d.ts +0 -0
package/typebox.js
CHANGED
|
@@ -27,362 +27,1843 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Type = exports.TypeBuilder = exports.Modifier = exports.Hint = exports.Kind = void 0;
|
|
31
|
-
//
|
|
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
32
|
// Symbols
|
|
33
|
-
//
|
|
33
|
+
// -------------------------------------------------------------------------------------
|
|
34
34
|
exports.Kind = Symbol.for('TypeBox.Kind');
|
|
35
35
|
exports.Hint = Symbol.for('TypeBox.Hint');
|
|
36
36
|
exports.Modifier = Symbol.for('TypeBox.Modifier');
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
+
// -------------------------------------------------------------------------------------
|
|
40
1462
|
let TypeOrdinal = 0;
|
|
1463
|
+
// -------------------------------------------------------------------------------------
|
|
1464
|
+
// TypeBuilder
|
|
1465
|
+
// -------------------------------------------------------------------------------------
|
|
41
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
|
+
/** `[Standard]` Omits compositing symbols from this schema */
|
|
1473
|
+
Strict(schema) {
|
|
1474
|
+
return JSON.parse(JSON.stringify(schema));
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
exports.TypeBuilder = TypeBuilder;
|
|
1478
|
+
// -------------------------------------------------------------------------------------
|
|
1479
|
+
// StandardTypeBuilder
|
|
1480
|
+
// -------------------------------------------------------------------------------------
|
|
1481
|
+
class StandardTypeBuilder extends TypeBuilder {
|
|
42
1482
|
// ----------------------------------------------------------------------
|
|
43
1483
|
// Modifiers
|
|
44
1484
|
// ----------------------------------------------------------------------
|
|
45
|
-
/** Creates a
|
|
46
|
-
|
|
47
|
-
return { [exports.Modifier]: '
|
|
1485
|
+
/** `[Modifier]` Creates a Optional property */
|
|
1486
|
+
Optional(schema) {
|
|
1487
|
+
return { [exports.Modifier]: 'Optional', ...TypeClone.Clone(schema, {}) };
|
|
48
1488
|
}
|
|
49
|
-
/** Creates a
|
|
50
|
-
|
|
51
|
-
return { [exports.Modifier]: '
|
|
1489
|
+
/** `[Modifier]` Creates a ReadonlyOptional property */
|
|
1490
|
+
ReadonlyOptional(schema) {
|
|
1491
|
+
return { [exports.Modifier]: 'ReadonlyOptional', ...TypeClone.Clone(schema, {}) };
|
|
52
1492
|
}
|
|
53
|
-
/** Creates a
|
|
54
|
-
|
|
55
|
-
return { [exports.Modifier]: '
|
|
1493
|
+
/** `[Modifier]` Creates a Readonly object or property */
|
|
1494
|
+
Readonly(schema) {
|
|
1495
|
+
return { [exports.Modifier]: 'Readonly', ...schema };
|
|
56
1496
|
}
|
|
57
1497
|
// ----------------------------------------------------------------------
|
|
58
1498
|
// Types
|
|
59
1499
|
// ----------------------------------------------------------------------
|
|
60
|
-
/** `Standard` Creates
|
|
1500
|
+
/** `[Standard]` Creates an Any type */
|
|
61
1501
|
Any(options = {}) {
|
|
62
1502
|
return this.Create({ ...options, [exports.Kind]: 'Any' });
|
|
63
1503
|
}
|
|
64
|
-
/** `Standard` Creates
|
|
1504
|
+
/** `[Standard]` Creates an Array type */
|
|
65
1505
|
Array(items, options = {}) {
|
|
66
1506
|
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
|
|
67
1507
|
}
|
|
68
|
-
/** `Standard` Creates a
|
|
1508
|
+
/** `[Standard]` Creates a Boolean type */
|
|
69
1509
|
Boolean(options = {}) {
|
|
70
1510
|
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
71
1511
|
}
|
|
72
|
-
/** `
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
|
|
81
|
-
}
|
|
82
|
-
else if (globalThis.Array.isArray(parameters)) {
|
|
83
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
|
|
1512
|
+
/** `[Standard]` Creates a Composite object type that will Union any overlapping properties of the given Object array */
|
|
1513
|
+
Composite(schemas, options = {}) {
|
|
1514
|
+
const optional = new Set();
|
|
1515
|
+
for (const schema of schemas) {
|
|
1516
|
+
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
1517
|
+
if (TypeGuard.TOptional(property) || TypeGuard.TReadonlyOptional(property))
|
|
1518
|
+
optional.add(key);
|
|
1519
|
+
}
|
|
84
1520
|
}
|
|
85
|
-
|
|
86
|
-
|
|
1521
|
+
const properties = {};
|
|
1522
|
+
for (const object of schemas) {
|
|
1523
|
+
for (const [key, property] of globalThis.Object.entries(object.properties)) {
|
|
1524
|
+
const mapped = key in properties ? this.Union([properties[key], property]) : property;
|
|
1525
|
+
properties[key] = optional.has(key) ? this.Optional(mapped) : mapped;
|
|
1526
|
+
}
|
|
87
1527
|
}
|
|
1528
|
+
return this.Object(properties, options);
|
|
88
1529
|
}
|
|
89
|
-
/** `
|
|
90
|
-
|
|
91
|
-
return
|
|
1530
|
+
/** `[Standard]` Dereferences the given TRef to its target type */
|
|
1531
|
+
Deref(schema) {
|
|
1532
|
+
return ReferenceRegistry.Deref(schema);
|
|
92
1533
|
}
|
|
93
|
-
/** `Standard` Creates a
|
|
1534
|
+
/** `[Standard]` Creates a Enum type */
|
|
94
1535
|
Enum(item, options = {}) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
.map((key) => item[key]);
|
|
1536
|
+
// prettier-ignore
|
|
1537
|
+
const values = globalThis.Object.keys(item).filter((key) => isNaN(key)).map((key) => item[key]);
|
|
98
1538
|
const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
|
|
99
|
-
return this.Create({ ...options, [exports.Kind]: 'Union',
|
|
1539
|
+
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
|
|
100
1540
|
}
|
|
101
|
-
/** `
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
1541
|
+
/** `[Standard]` A conditional type expression that will return the true type if the left extends the right */
|
|
1542
|
+
Extends(left, right, trueType, falseType, options = {}) {
|
|
1543
|
+
switch (TypeExtends.Extends(ReferenceRegistry.Deref(left), ReferenceRegistry.Deref(right))) {
|
|
1544
|
+
case TypeExtendsResult.Union:
|
|
1545
|
+
return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
|
|
1546
|
+
case TypeExtendsResult.True:
|
|
1547
|
+
return TypeClone.Clone(trueType, options);
|
|
1548
|
+
case TypeExtendsResult.False:
|
|
1549
|
+
return TypeClone.Clone(falseType, options);
|
|
106
1550
|
}
|
|
107
|
-
|
|
108
|
-
|
|
1551
|
+
}
|
|
1552
|
+
/** `[Standard]` Excludes from left any type that is not assignable to the right. */
|
|
1553
|
+
Exclude(left, right, options = {}) {
|
|
1554
|
+
if (TypeGuard.TUnion(left)) {
|
|
1555
|
+
const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) === TypeExtendsResult.False);
|
|
1556
|
+
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
109
1557
|
}
|
|
110
1558
|
else {
|
|
111
|
-
|
|
1559
|
+
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(left, options));
|
|
112
1560
|
}
|
|
113
1561
|
}
|
|
114
|
-
/** `
|
|
115
|
-
|
|
116
|
-
|
|
1562
|
+
/** `[Standard]` Extracts from left any type that is assignable to the right. */
|
|
1563
|
+
Extract(left, right, options = {}) {
|
|
1564
|
+
if (TypeGuard.TUnion(left)) {
|
|
1565
|
+
const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) !== TypeExtendsResult.False);
|
|
1566
|
+
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
1567
|
+
}
|
|
1568
|
+
else {
|
|
1569
|
+
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? TypeClone.Clone(left, options) : this.Never(options));
|
|
1570
|
+
}
|
|
117
1571
|
}
|
|
118
|
-
/** `Standard` Creates
|
|
1572
|
+
/** `[Standard]` Creates an Integer type */
|
|
119
1573
|
Integer(options = {}) {
|
|
120
1574
|
return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
|
|
121
1575
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
for (const object of objects) {
|
|
133
|
-
for (const key of Object.keys(object.properties)) {
|
|
134
|
-
if (!optional.has(key))
|
|
135
|
-
required.add(key);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
const properties = {};
|
|
139
|
-
for (const object of objects) {
|
|
140
|
-
for (const [key, schema] of Object.entries(object.properties)) {
|
|
141
|
-
properties[key] = properties[key] === undefined ? schema : { [exports.Kind]: 'Union', anyOf: [properties[key], { ...schema }] };
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (required.size > 0) {
|
|
145
|
-
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required: [...required] });
|
|
1576
|
+
Intersect(allOf, options = {}) {
|
|
1577
|
+
if (allOf.length === 0)
|
|
1578
|
+
return exports.Type.Never();
|
|
1579
|
+
if (allOf.length === 1)
|
|
1580
|
+
return TypeClone.Clone(allOf[0], options);
|
|
1581
|
+
const objects = allOf.every((schema) => TypeGuard.TObject(schema));
|
|
1582
|
+
const cloned = allOf.map((schema) => TypeClone.Clone(schema, {}));
|
|
1583
|
+
if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
|
|
1584
|
+
return this.Create({ ...options, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned });
|
|
146
1585
|
}
|
|
147
1586
|
else {
|
|
148
|
-
return this.Create({ ...options, [exports.Kind]: '
|
|
1587
|
+
return this.Create({ ...options, [exports.Kind]: 'Intersect', allOf: cloned });
|
|
149
1588
|
}
|
|
150
1589
|
}
|
|
151
|
-
/** `Standard` Creates a
|
|
152
|
-
KeyOf(
|
|
153
|
-
const
|
|
154
|
-
|
|
1590
|
+
/** `[Standard]` Creates a KeyOf type */
|
|
1591
|
+
KeyOf(schema, options = {}) {
|
|
1592
|
+
const keys = KeyResolver.Resolve(ReferenceRegistry.Deref(schema));
|
|
1593
|
+
// prettier-ignore
|
|
1594
|
+
const keyof = keys.length === 0 ? this.Never(options) : this.Union(keys.map((key) => this.Literal(key)), options);
|
|
1595
|
+
return keyof;
|
|
155
1596
|
}
|
|
156
|
-
/** `Standard` Creates a
|
|
1597
|
+
/** `[Standard]` Creates a Literal type */
|
|
157
1598
|
Literal(value, options = {}) {
|
|
158
1599
|
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
159
1600
|
}
|
|
160
|
-
/** `Standard` Creates a
|
|
1601
|
+
/** `[Standard]` Creates a Never type */
|
|
161
1602
|
Never(options = {}) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
[exports.Kind]: 'Never',
|
|
165
|
-
allOf: [
|
|
1603
|
+
// prettier-ignore
|
|
1604
|
+
return this.Create({ ...options, [exports.Kind]: 'Never', allOf: [
|
|
166
1605
|
{ type: 'boolean', const: false },
|
|
167
1606
|
{ type: 'boolean', const: true },
|
|
168
|
-
]
|
|
169
|
-
|
|
1607
|
+
] });
|
|
1608
|
+
}
|
|
1609
|
+
/** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */
|
|
1610
|
+
Not(not, schema, options) {
|
|
1611
|
+
return this.Create({ ...options, [exports.Kind]: 'Not', allOf: [{ not }, schema] });
|
|
170
1612
|
}
|
|
171
|
-
/** `Standard` Creates a
|
|
1613
|
+
/** `[Standard]` Creates a Null type */
|
|
172
1614
|
Null(options = {}) {
|
|
173
1615
|
return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
|
|
174
1616
|
}
|
|
175
|
-
/** `Standard` Creates a
|
|
1617
|
+
/** `[Standard]` Creates a Number type */
|
|
176
1618
|
Number(options = {}) {
|
|
177
1619
|
return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
|
|
178
1620
|
}
|
|
179
|
-
/** `Standard` Creates
|
|
1621
|
+
/** `[Standard]` Creates a Object type */
|
|
180
1622
|
Object(properties, options = {}) {
|
|
181
|
-
const
|
|
182
|
-
const optional =
|
|
183
|
-
|
|
184
|
-
const modifier = property[exports.Modifier];
|
|
185
|
-
return modifier && (modifier === 'Optional' || modifier === 'ReadonlyOptional');
|
|
186
|
-
});
|
|
187
|
-
const required = property_names.filter((name) => !optional.includes(name));
|
|
1623
|
+
const keys = globalThis.Object.keys(properties);
|
|
1624
|
+
const optional = keys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
|
|
1625
|
+
const required = keys.filter((name) => !optional.includes(name));
|
|
188
1626
|
if (required.length > 0) {
|
|
189
|
-
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required });
|
|
1627
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: TypeClone.Clone(properties, {}), required });
|
|
190
1628
|
}
|
|
191
1629
|
else {
|
|
192
|
-
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
|
|
1630
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: TypeClone.Clone(properties, {}) });
|
|
193
1631
|
}
|
|
194
1632
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
/** `Extended` Creates a tuple type from this functions parameters */
|
|
211
|
-
Parameters(schema, options = {}) {
|
|
212
|
-
return exports.Type.Tuple(schema.parameters, { ...options });
|
|
1633
|
+
Omit(schema, unresolved, options = {}) {
|
|
1634
|
+
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
|
|
1635
|
+
// prettier-ignore
|
|
1636
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1637
|
+
if (schema.required) {
|
|
1638
|
+
schema.required = schema.required.filter((key) => !keys.includes(key));
|
|
1639
|
+
if (schema.required.length === 0)
|
|
1640
|
+
delete schema.required;
|
|
1641
|
+
}
|
|
1642
|
+
for (const key of globalThis.Object.keys(schema.properties)) {
|
|
1643
|
+
if (keys.includes(key))
|
|
1644
|
+
delete schema.properties[key];
|
|
1645
|
+
}
|
|
1646
|
+
return this.Create(schema);
|
|
1647
|
+
}, options);
|
|
213
1648
|
}
|
|
214
|
-
/** `Standard` Creates
|
|
1649
|
+
/** `[Standard]` Creates a mapped type where all properties are Optional */
|
|
215
1650
|
Partial(schema, options = {}) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const property = next.properties[key];
|
|
220
|
-
const modifer = property[exports.Modifier];
|
|
221
|
-
switch (modifer) {
|
|
1651
|
+
function Apply(schema) {
|
|
1652
|
+
// prettier-ignore
|
|
1653
|
+
switch (schema[exports.Modifier]) {
|
|
222
1654
|
case 'ReadonlyOptional':
|
|
223
|
-
|
|
1655
|
+
schema[exports.Modifier] = 'ReadonlyOptional';
|
|
224
1656
|
break;
|
|
225
1657
|
case 'Readonly':
|
|
226
|
-
|
|
1658
|
+
schema[exports.Modifier] = 'ReadonlyOptional';
|
|
227
1659
|
break;
|
|
228
1660
|
case 'Optional':
|
|
229
|
-
|
|
1661
|
+
schema[exports.Modifier] = 'Optional';
|
|
230
1662
|
break;
|
|
231
1663
|
default:
|
|
232
|
-
|
|
1664
|
+
schema[exports.Modifier] = 'Optional';
|
|
233
1665
|
break;
|
|
234
1666
|
}
|
|
235
1667
|
}
|
|
236
|
-
|
|
1668
|
+
// prettier-ignore
|
|
1669
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1670
|
+
delete schema.required;
|
|
1671
|
+
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1672
|
+
return schema;
|
|
1673
|
+
}, options);
|
|
237
1674
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
/** `Extended` Creates a Promise type */
|
|
254
|
-
Promise(item, options = {}) {
|
|
255
|
-
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
|
|
1675
|
+
Pick(schema, unresolved, options = {}) {
|
|
1676
|
+
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
|
|
1677
|
+
// prettier-ignore
|
|
1678
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1679
|
+
if (schema.required) {
|
|
1680
|
+
schema.required = schema.required.filter((key) => keys.includes(key));
|
|
1681
|
+
if (schema.required.length === 0)
|
|
1682
|
+
delete schema.required;
|
|
1683
|
+
}
|
|
1684
|
+
for (const key of globalThis.Object.keys(schema.properties)) {
|
|
1685
|
+
if (!keys.includes(key))
|
|
1686
|
+
delete schema.properties[key];
|
|
1687
|
+
}
|
|
1688
|
+
return this.Create(schema);
|
|
1689
|
+
}, options);
|
|
256
1690
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
1691
|
+
Record(key, schema, options = {}) {
|
|
1692
|
+
if (TypeGuard.TLiteral(key)) {
|
|
1693
|
+
if (typeof key.const === 'string' || typeof key.const === 'number') {
|
|
1694
|
+
return this.Object({ [key.const]: schema }, options);
|
|
1695
|
+
}
|
|
1696
|
+
else
|
|
1697
|
+
throw Error('TypeBuilder: Record key can only be derived from literals of number or string');
|
|
1698
|
+
}
|
|
1699
|
+
if (TypeGuard.TUnion(key)) {
|
|
1700
|
+
if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
|
|
1701
|
+
const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
1702
|
+
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
1703
|
+
}
|
|
1704
|
+
else
|
|
1705
|
+
throw Error('TypeBuilder: Record key can only be derived from union literal of number or string');
|
|
264
1706
|
}
|
|
265
|
-
// otherwise return TRecord with patternProperties
|
|
266
1707
|
const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
|
|
267
1708
|
return this.Create({
|
|
268
1709
|
...options,
|
|
269
1710
|
[exports.Kind]: 'Record',
|
|
270
1711
|
type: 'object',
|
|
271
|
-
patternProperties: { [pattern]:
|
|
1712
|
+
patternProperties: { [pattern]: schema },
|
|
272
1713
|
additionalProperties: false,
|
|
273
1714
|
});
|
|
274
1715
|
}
|
|
275
|
-
/** `Standard` Creates
|
|
1716
|
+
/** `[Standard]` Creates a Recursive type */
|
|
276
1717
|
Recursive(callback, options = {}) {
|
|
277
1718
|
if (options.$id === undefined)
|
|
278
1719
|
options.$id = `T${TypeOrdinal++}`;
|
|
279
1720
|
const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
|
|
280
|
-
|
|
1721
|
+
const reassign = self;
|
|
1722
|
+
reassign.$id = options.$id; // required as $id is readonly
|
|
281
1723
|
return this.Create({ ...options, ...self });
|
|
282
1724
|
}
|
|
283
|
-
/** `Standard` Creates a
|
|
1725
|
+
/** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
|
|
284
1726
|
Ref(schema, options = {}) {
|
|
285
1727
|
if (schema.$id === undefined)
|
|
286
1728
|
throw Error('TypeBuilder.Ref: Referenced schema must specify an $id');
|
|
287
1729
|
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
|
|
288
1730
|
}
|
|
289
|
-
/** `Standard` Creates a
|
|
290
|
-
RegEx(regex, options = {}) {
|
|
291
|
-
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
|
|
292
|
-
}
|
|
293
|
-
/** `Standard` Creates an object type whose properties are all required */
|
|
1731
|
+
/** `[Standard]` Creates a mapped type where all properties are Required */
|
|
294
1732
|
Required(schema, options = {}) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const property = next.properties[key];
|
|
299
|
-
const modifier = property[exports.Modifier];
|
|
300
|
-
switch (modifier) {
|
|
1733
|
+
function Apply(schema) {
|
|
1734
|
+
// prettier-ignore
|
|
1735
|
+
switch (schema[exports.Modifier]) {
|
|
301
1736
|
case 'ReadonlyOptional':
|
|
302
|
-
|
|
1737
|
+
schema[exports.Modifier] = 'Readonly';
|
|
303
1738
|
break;
|
|
304
1739
|
case 'Readonly':
|
|
305
|
-
|
|
1740
|
+
schema[exports.Modifier] = 'Readonly';
|
|
306
1741
|
break;
|
|
307
1742
|
case 'Optional':
|
|
308
|
-
delete
|
|
1743
|
+
delete schema[exports.Modifier];
|
|
309
1744
|
break;
|
|
310
1745
|
default:
|
|
311
|
-
delete
|
|
1746
|
+
delete schema[exports.Modifier];
|
|
312
1747
|
break;
|
|
313
1748
|
}
|
|
314
1749
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
1750
|
+
// prettier-ignore
|
|
1751
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1752
|
+
schema.required = globalThis.Object.keys(schema.properties);
|
|
1753
|
+
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1754
|
+
return schema;
|
|
1755
|
+
}, options);
|
|
320
1756
|
}
|
|
321
|
-
/**
|
|
322
|
-
Strict(schema) {
|
|
323
|
-
return JSON.parse(JSON.stringify(schema));
|
|
324
|
-
}
|
|
325
|
-
/** `Standard` Creates a string type */
|
|
1757
|
+
/** `[Standard]` Creates a String type */
|
|
326
1758
|
String(options = {}) {
|
|
327
1759
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
328
1760
|
}
|
|
329
|
-
/** `Standard` Creates a
|
|
1761
|
+
/** `[Standard]` Creates a Tuple type */
|
|
330
1762
|
Tuple(items, options = {}) {
|
|
331
|
-
const additionalItems = false;
|
|
332
|
-
|
|
333
|
-
const
|
|
334
|
-
|
|
1763
|
+
const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
|
|
1764
|
+
// prettier-ignore
|
|
1765
|
+
const schema = (items.length > 0 ?
|
|
1766
|
+
{ ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } :
|
|
1767
|
+
{ ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
335
1768
|
return this.Create(schema);
|
|
336
1769
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
return
|
|
344
|
-
}
|
|
345
|
-
/** `Extended` Creates a Uint8Array type */
|
|
346
|
-
Uint8Array(options = {}) {
|
|
347
|
-
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
|
|
1770
|
+
Union(anyOf, options = {}) {
|
|
1771
|
+
if (anyOf.length === 0)
|
|
1772
|
+
return this.Never(options);
|
|
1773
|
+
if (anyOf.length === 1)
|
|
1774
|
+
return TypeClone.Clone(anyOf[0], options);
|
|
1775
|
+
const cloned = anyOf.map((schema) => TypeClone.Clone(schema, {}));
|
|
1776
|
+
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: cloned });
|
|
348
1777
|
}
|
|
349
|
-
/** `Standard` Creates an
|
|
1778
|
+
/** `[Standard]` Creates an Unknown type */
|
|
350
1779
|
Unknown(options = {}) {
|
|
351
1780
|
return this.Create({ ...options, [exports.Kind]: 'Unknown' });
|
|
352
1781
|
}
|
|
353
|
-
/** `Standard` Creates a
|
|
1782
|
+
/** `[Standard]` Creates a Unsafe type that infers for the generic argument */
|
|
354
1783
|
Unsafe(options = {}) {
|
|
355
1784
|
return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
|
|
356
1785
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
1786
|
+
}
|
|
1787
|
+
exports.StandardTypeBuilder = StandardTypeBuilder;
|
|
1788
|
+
// -------------------------------------------------------------------------------------
|
|
1789
|
+
// TypeBuilder
|
|
1790
|
+
// -------------------------------------------------------------------------------------
|
|
1791
|
+
class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
1792
|
+
/** `[Extended]` Creates a BigInt */
|
|
1793
|
+
BigInt(options = {}) {
|
|
1794
|
+
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'null', typeOf: 'BigInt' });
|
|
360
1795
|
}
|
|
361
|
-
/**
|
|
362
|
-
|
|
363
|
-
return schema;
|
|
1796
|
+
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
|
1797
|
+
ConstructorParameters(schema, options = {}) {
|
|
1798
|
+
return this.Tuple([...schema.parameters], { ...options });
|
|
364
1799
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
[key]: this.Clone(value[key]),
|
|
373
|
-
}), Object.getOwnPropertySymbols(value).reduce((acc, key) => ({
|
|
374
|
-
...acc,
|
|
375
|
-
[key]: this.Clone(value[key]),
|
|
376
|
-
}), {}));
|
|
377
|
-
}
|
|
378
|
-
else if (isArray(value)) {
|
|
379
|
-
return value.map((item) => this.Clone(item));
|
|
1800
|
+
Constructor(parameters, returns, options = {}) {
|
|
1801
|
+
if (parameters[exports.Kind] === 'Tuple') {
|
|
1802
|
+
const inner = parameters.items === undefined ? [] : parameters.items;
|
|
1803
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
|
|
1804
|
+
}
|
|
1805
|
+
else if (globalThis.Array.isArray(parameters)) {
|
|
1806
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
|
|
380
1807
|
}
|
|
381
1808
|
else {
|
|
382
|
-
|
|
1809
|
+
throw new Error('TypeBuilder.Constructor: Invalid parameters');
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
/** `[Extended]` Creates a Date type */
|
|
1813
|
+
Date(options = {}) {
|
|
1814
|
+
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
|
|
1815
|
+
}
|
|
1816
|
+
Function(parameters, returns, options = {}) {
|
|
1817
|
+
if (parameters[exports.Kind] === 'Tuple') {
|
|
1818
|
+
const inner = parameters.items === undefined ? [] : parameters.items;
|
|
1819
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: inner, returns });
|
|
383
1820
|
}
|
|
1821
|
+
else if (globalThis.Array.isArray(parameters)) {
|
|
1822
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters, returns });
|
|
1823
|
+
}
|
|
1824
|
+
else {
|
|
1825
|
+
throw new Error('TypeBuilder.Function: Invalid parameters');
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
1829
|
+
InstanceType(schema, options = {}) {
|
|
1830
|
+
return TypeClone.Clone(schema.returns, options);
|
|
1831
|
+
}
|
|
1832
|
+
/** `[Extended]` Extracts the Parameters from the given Function type */
|
|
1833
|
+
Parameters(schema, options = {}) {
|
|
1834
|
+
return this.Tuple(schema.parameters, { ...options });
|
|
1835
|
+
}
|
|
1836
|
+
/** `[Extended]` Creates a Promise type */
|
|
1837
|
+
Promise(item, options = {}) {
|
|
1838
|
+
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
|
|
1839
|
+
}
|
|
1840
|
+
/** `[Extended]` Creates a regular expression type */
|
|
1841
|
+
RegEx(regex, options = {}) {
|
|
1842
|
+
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
|
|
1843
|
+
}
|
|
1844
|
+
/** `[Extended]` Extracts the ReturnType from the given Function */
|
|
1845
|
+
ReturnType(schema, options = {}) {
|
|
1846
|
+
return TypeClone.Clone(schema.returns, options);
|
|
1847
|
+
}
|
|
1848
|
+
/** `[Extended]` Creates a Symbol type */
|
|
1849
|
+
Symbol(options) {
|
|
1850
|
+
return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'null', typeOf: 'Symbol' });
|
|
1851
|
+
}
|
|
1852
|
+
/** `[Extended]` Creates a Undefined type */
|
|
1853
|
+
Undefined(options = {}) {
|
|
1854
|
+
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
|
|
1855
|
+
}
|
|
1856
|
+
/** `[Extended]` Creates a Uint8Array type */
|
|
1857
|
+
Uint8Array(options = {}) {
|
|
1858
|
+
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
|
|
1859
|
+
}
|
|
1860
|
+
/** `[Extended]` Creates a Void type */
|
|
1861
|
+
Void(options = {}) {
|
|
1862
|
+
return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
|
|
384
1863
|
}
|
|
385
1864
|
}
|
|
386
|
-
exports.
|
|
387
|
-
/** JSON Schema
|
|
388
|
-
exports.
|
|
1865
|
+
exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
|
|
1866
|
+
/** JSON Schema TypeBuilder with Static Resolution for TypeScript */
|
|
1867
|
+
exports.StandardType = new StandardTypeBuilder();
|
|
1868
|
+
/** JSON Schema TypeBuilder with Static Resolution for TypeScript */
|
|
1869
|
+
exports.Type = new ExtendedTypeBuilder();
|