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