@sinclair/typebox 0.29.6 → 0.30.0-dev-2
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 +7 -4
- package/compiler/compiler.js +208 -194
- package/errors/errors.d.ts +65 -60
- package/errors/errors.js +515 -492
- package/package.json +14 -2
- package/readme.md +161 -169
- package/system/system.js +0 -6
- package/typebox.d.ts +99 -52
- package/typebox.js +496 -573
- package/value/cast.d.ts +4 -4
- package/value/cast.js +254 -260
- package/value/check.d.ts +4 -3
- package/value/check.js +412 -397
- package/value/clone.d.ts +2 -3
- package/value/clone.js +62 -42
- package/value/convert.d.ts +4 -4
- package/value/convert.js +304 -318
- package/value/create.d.ts +4 -6
- package/value/create.js +386 -376
- package/value/delta.d.ts +2 -4
- package/value/delta.js +121 -130
- package/value/equal.d.ts +2 -3
- package/value/equal.js +48 -51
- package/value/guard.d.ts +44 -0
- package/value/guard.js +146 -0
- package/value/hash.d.ts +14 -3
- package/value/hash.js +124 -166
- package/value/index.d.ts +3 -4
- package/value/index.js +6 -20
- package/value/mutate.d.ts +2 -4
- package/value/mutate.js +75 -67
- package/value/pointer.js +8 -2
- package/value/value.d.ts +20 -20
- package/value/value.js +27 -27
- package/value/is.d.ts +0 -11
- package/value/is.js +0 -53
package/typebox.js
CHANGED
|
@@ -27,11 +27,12 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.
|
|
30
|
+
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = void 0;
|
|
31
31
|
// --------------------------------------------------------------------------
|
|
32
32
|
// Symbols
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
34
|
-
exports.
|
|
34
|
+
exports.Readonly = Symbol.for('TypeBox.Readonly');
|
|
35
|
+
exports.Optional = Symbol.for('TypeBox.Optional');
|
|
35
36
|
exports.Hint = Symbol.for('TypeBox.Hint');
|
|
36
37
|
exports.Kind = Symbol.for('TypeBox.Kind');
|
|
37
38
|
// --------------------------------------------------------------------------
|
|
@@ -57,6 +58,11 @@ var TypeRegistry;
|
|
|
57
58
|
return map.clear();
|
|
58
59
|
}
|
|
59
60
|
TypeRegistry.Clear = Clear;
|
|
61
|
+
/** Deletes a registered type */
|
|
62
|
+
function Delete(kind) {
|
|
63
|
+
return map.delete(kind);
|
|
64
|
+
}
|
|
65
|
+
TypeRegistry.Delete = Delete;
|
|
60
66
|
/** Returns true if this registry contains this kind */
|
|
61
67
|
function Has(kind) {
|
|
62
68
|
return map.has(kind);
|
|
@@ -87,6 +93,11 @@ var FormatRegistry;
|
|
|
87
93
|
return map.clear();
|
|
88
94
|
}
|
|
89
95
|
FormatRegistry.Clear = Clear;
|
|
96
|
+
/** Deletes a registered format */
|
|
97
|
+
function Delete(format) {
|
|
98
|
+
return map.delete(format);
|
|
99
|
+
}
|
|
100
|
+
FormatRegistry.Delete = Delete;
|
|
90
101
|
/** Returns true if the user defined string format exists */
|
|
91
102
|
function Has(format) {
|
|
92
103
|
return map.has(format);
|
|
@@ -104,6 +115,44 @@ var FormatRegistry;
|
|
|
104
115
|
FormatRegistry.Get = Get;
|
|
105
116
|
})(FormatRegistry || (exports.FormatRegistry = FormatRegistry = {}));
|
|
106
117
|
// --------------------------------------------------------------------------
|
|
118
|
+
// ValueGuard
|
|
119
|
+
// --------------------------------------------------------------------------
|
|
120
|
+
var ValueGuard;
|
|
121
|
+
(function (ValueGuard) {
|
|
122
|
+
function IsObject(value) {
|
|
123
|
+
return typeof value === 'object' && value !== null;
|
|
124
|
+
}
|
|
125
|
+
ValueGuard.IsObject = IsObject;
|
|
126
|
+
function IsArray(value) {
|
|
127
|
+
return Array.isArray(value);
|
|
128
|
+
}
|
|
129
|
+
ValueGuard.IsArray = IsArray;
|
|
130
|
+
function IsBoolean(value) {
|
|
131
|
+
return typeof value === 'boolean';
|
|
132
|
+
}
|
|
133
|
+
ValueGuard.IsBoolean = IsBoolean;
|
|
134
|
+
function IsNull(value) {
|
|
135
|
+
return value === null;
|
|
136
|
+
}
|
|
137
|
+
ValueGuard.IsNull = IsNull;
|
|
138
|
+
function IsUndefined(value) {
|
|
139
|
+
return value === undefined;
|
|
140
|
+
}
|
|
141
|
+
ValueGuard.IsUndefined = IsUndefined;
|
|
142
|
+
function IsBigInt(value) {
|
|
143
|
+
return typeof value === 'bigint';
|
|
144
|
+
}
|
|
145
|
+
ValueGuard.IsBigInt = IsBigInt;
|
|
146
|
+
function IsNumber(value) {
|
|
147
|
+
return typeof value === 'number';
|
|
148
|
+
}
|
|
149
|
+
ValueGuard.IsNumber = IsNumber;
|
|
150
|
+
function IsString(value) {
|
|
151
|
+
return typeof value === 'string';
|
|
152
|
+
}
|
|
153
|
+
ValueGuard.IsString = IsString;
|
|
154
|
+
})(ValueGuard || (exports.ValueGuard = ValueGuard = {}));
|
|
155
|
+
// --------------------------------------------------------------------------
|
|
107
156
|
// TypeGuard
|
|
108
157
|
// --------------------------------------------------------------------------
|
|
109
158
|
class TypeGuardUnknownTypeError extends Error {
|
|
@@ -116,12 +165,6 @@ exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
|
|
|
116
165
|
/** Provides functions to test if JavaScript values are TypeBox types */
|
|
117
166
|
var TypeGuard;
|
|
118
167
|
(function (TypeGuard) {
|
|
119
|
-
function IsObject(value) {
|
|
120
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
121
|
-
}
|
|
122
|
-
function IsArray(value) {
|
|
123
|
-
return typeof value === 'object' && value !== null && Array.isArray(value);
|
|
124
|
-
}
|
|
125
168
|
function IsPattern(value) {
|
|
126
169
|
try {
|
|
127
170
|
new RegExp(value);
|
|
@@ -132,7 +175,7 @@ var TypeGuard;
|
|
|
132
175
|
}
|
|
133
176
|
}
|
|
134
177
|
function IsControlCharacterFree(value) {
|
|
135
|
-
if (
|
|
178
|
+
if (!ValueGuard.IsString(value))
|
|
136
179
|
return false;
|
|
137
180
|
for (let i = 0; i < value.length; i++) {
|
|
138
181
|
const code = value.charCodeAt(i);
|
|
@@ -145,63 +188,62 @@ var TypeGuard;
|
|
|
145
188
|
function IsAdditionalProperties(value) {
|
|
146
189
|
return IsOptionalBoolean(value) || TSchema(value);
|
|
147
190
|
}
|
|
148
|
-
function IsBigInt(value) {
|
|
149
|
-
return typeof value === 'bigint';
|
|
150
|
-
}
|
|
151
|
-
function IsString(value) {
|
|
152
|
-
return typeof value === 'string';
|
|
153
|
-
}
|
|
154
|
-
function IsNumber(value) {
|
|
155
|
-
return typeof value === 'number' && globalThis.Number.isFinite(value);
|
|
156
|
-
}
|
|
157
|
-
function IsBoolean(value) {
|
|
158
|
-
return typeof value === 'boolean';
|
|
159
|
-
}
|
|
160
191
|
function IsOptionalBigInt(value) {
|
|
161
|
-
return value
|
|
192
|
+
return ValueGuard.IsUndefined(value) || ValueGuard.IsBigInt(value);
|
|
162
193
|
}
|
|
163
194
|
function IsOptionalNumber(value) {
|
|
164
|
-
return value
|
|
195
|
+
return ValueGuard.IsUndefined(value) || ValueGuard.IsNumber(value);
|
|
165
196
|
}
|
|
166
197
|
function IsOptionalBoolean(value) {
|
|
167
|
-
return value
|
|
198
|
+
return ValueGuard.IsUndefined(value) || ValueGuard.IsBoolean(value);
|
|
168
199
|
}
|
|
169
200
|
function IsOptionalString(value) {
|
|
170
|
-
return value
|
|
201
|
+
return ValueGuard.IsUndefined(value) || ValueGuard.IsString(value);
|
|
171
202
|
}
|
|
172
203
|
function IsOptionalPattern(value) {
|
|
173
|
-
return value
|
|
204
|
+
return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
|
|
174
205
|
}
|
|
175
206
|
function IsOptionalFormat(value) {
|
|
176
|
-
return value
|
|
207
|
+
return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value));
|
|
177
208
|
}
|
|
178
209
|
function IsOptionalSchema(value) {
|
|
179
|
-
return value
|
|
210
|
+
return ValueGuard.IsUndefined(value) || TSchema(value);
|
|
180
211
|
}
|
|
181
212
|
/** Returns true if the given schema is TAny */
|
|
182
213
|
function TAny(schema) {
|
|
183
|
-
|
|
214
|
+
// prettier-ignore
|
|
215
|
+
return (TKindOf(schema, 'Any') &&
|
|
216
|
+
IsOptionalString(schema.$id));
|
|
184
217
|
}
|
|
185
218
|
TypeGuard.TAny = TAny;
|
|
186
219
|
/** Returns true if the given schema is TArray */
|
|
187
220
|
function TArray(schema) {
|
|
188
|
-
return (
|
|
189
|
-
schema[exports.Kind] === 'Array' &&
|
|
221
|
+
return (TKindOf(schema, 'Array') &&
|
|
190
222
|
schema.type === 'array' &&
|
|
191
223
|
IsOptionalString(schema.$id) &&
|
|
192
224
|
TSchema(schema.items) &&
|
|
225
|
+
IsOptionalSchema(schema.contains) &&
|
|
226
|
+
IsOptionalNumber(schema.minContains) &&
|
|
227
|
+
IsOptionalNumber(schema.maxContains) &&
|
|
193
228
|
IsOptionalNumber(schema.minItems) &&
|
|
194
229
|
IsOptionalNumber(schema.maxItems) &&
|
|
195
230
|
IsOptionalBoolean(schema.uniqueItems));
|
|
196
231
|
}
|
|
197
232
|
TypeGuard.TArray = TArray;
|
|
233
|
+
/** Returns true if the given schema is TAsyncIterator */
|
|
234
|
+
function TAsyncIterator(schema) {
|
|
235
|
+
// prettier-ignore
|
|
236
|
+
return (TKindOf(schema, 'AsyncIterator') &&
|
|
237
|
+
schema.type === 'AsyncIterator' &&
|
|
238
|
+
IsOptionalString(schema.$id) &&
|
|
239
|
+
TSchema(schema.items));
|
|
240
|
+
}
|
|
241
|
+
TypeGuard.TAsyncIterator = TAsyncIterator;
|
|
198
242
|
/** Returns true if the given schema is TBigInt */
|
|
199
243
|
function TBigInt(schema) {
|
|
200
244
|
// prettier-ignore
|
|
201
|
-
return (
|
|
202
|
-
schema
|
|
203
|
-
schema.type === 'null' &&
|
|
204
|
-
schema.typeOf === 'BigInt' &&
|
|
245
|
+
return (TKindOf(schema, 'BigInt') &&
|
|
246
|
+
schema.type === 'bigint' &&
|
|
205
247
|
IsOptionalString(schema.$id) &&
|
|
206
248
|
IsOptionalBigInt(schema.multipleOf) &&
|
|
207
249
|
IsOptionalBigInt(schema.minimum) &&
|
|
@@ -213,8 +255,7 @@ var TypeGuard;
|
|
|
213
255
|
/** Returns true if the given schema is TBoolean */
|
|
214
256
|
function TBoolean(schema) {
|
|
215
257
|
// prettier-ignore
|
|
216
|
-
return (
|
|
217
|
-
schema[exports.Kind] === 'Boolean' &&
|
|
258
|
+
return (TKindOf(schema, 'Boolean') &&
|
|
218
259
|
schema.type === 'boolean' &&
|
|
219
260
|
IsOptionalString(schema.$id));
|
|
220
261
|
}
|
|
@@ -222,12 +263,10 @@ var TypeGuard;
|
|
|
222
263
|
/** Returns true if the given schema is TConstructor */
|
|
223
264
|
function TConstructor(schema) {
|
|
224
265
|
// prettier-ignore
|
|
225
|
-
if (!(
|
|
226
|
-
schema
|
|
227
|
-
schema.type === 'object' &&
|
|
228
|
-
schema.instanceOf === 'Constructor' &&
|
|
266
|
+
if (!(TKindOf(schema, 'Constructor') &&
|
|
267
|
+
schema.type === 'constructor' &&
|
|
229
268
|
IsOptionalString(schema.$id) &&
|
|
230
|
-
IsArray(schema.parameters) &&
|
|
269
|
+
ValueGuard.IsArray(schema.parameters) &&
|
|
231
270
|
TSchema(schema.returns))) {
|
|
232
271
|
return false;
|
|
233
272
|
}
|
|
@@ -240,10 +279,8 @@ var TypeGuard;
|
|
|
240
279
|
TypeGuard.TConstructor = TConstructor;
|
|
241
280
|
/** Returns true if the given schema is TDate */
|
|
242
281
|
function TDate(schema) {
|
|
243
|
-
return (
|
|
244
|
-
schema
|
|
245
|
-
schema.type === 'object' &&
|
|
246
|
-
schema.instanceOf === 'Date' &&
|
|
282
|
+
return (TKindOf(schema, 'Date') &&
|
|
283
|
+
schema.type === 'Date' &&
|
|
247
284
|
IsOptionalString(schema.$id) &&
|
|
248
285
|
IsOptionalNumber(schema.minimumTimestamp) &&
|
|
249
286
|
IsOptionalNumber(schema.maximumTimestamp) &&
|
|
@@ -254,12 +291,10 @@ var TypeGuard;
|
|
|
254
291
|
/** Returns true if the given schema is TFunction */
|
|
255
292
|
function TFunction(schema) {
|
|
256
293
|
// prettier-ignore
|
|
257
|
-
if (!(
|
|
258
|
-
schema
|
|
259
|
-
schema.type === 'object' &&
|
|
260
|
-
schema.instanceOf === 'Function' &&
|
|
294
|
+
if (!(TKindOf(schema, 'Function') &&
|
|
295
|
+
schema.type === 'function' &&
|
|
261
296
|
IsOptionalString(schema.$id) &&
|
|
262
|
-
IsArray(schema.parameters) &&
|
|
297
|
+
ValueGuard.IsArray(schema.parameters) &&
|
|
263
298
|
TSchema(schema.returns))) {
|
|
264
299
|
return false;
|
|
265
300
|
}
|
|
@@ -272,8 +307,7 @@ var TypeGuard;
|
|
|
272
307
|
TypeGuard.TFunction = TFunction;
|
|
273
308
|
/** Returns true if the given schema is TInteger */
|
|
274
309
|
function TInteger(schema) {
|
|
275
|
-
return (
|
|
276
|
-
schema[exports.Kind] === 'Integer' &&
|
|
310
|
+
return (TKindOf(schema, 'Integer') &&
|
|
277
311
|
schema.type === 'integer' &&
|
|
278
312
|
IsOptionalString(schema.$id) &&
|
|
279
313
|
IsOptionalNumber(schema.multipleOf) &&
|
|
@@ -286,9 +320,8 @@ var TypeGuard;
|
|
|
286
320
|
/** Returns true if the given schema is TIntersect */
|
|
287
321
|
function TIntersect(schema) {
|
|
288
322
|
// prettier-ignore
|
|
289
|
-
if (!(
|
|
290
|
-
schema
|
|
291
|
-
IsArray(schema.allOf) &&
|
|
323
|
+
if (!(TKindOf(schema, 'Intersect') &&
|
|
324
|
+
ValueGuard.IsArray(schema.allOf) &&
|
|
292
325
|
IsOptionalString(schema.type) &&
|
|
293
326
|
(IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
|
|
294
327
|
IsOptionalString(schema.$id))) {
|
|
@@ -304,57 +337,75 @@ var TypeGuard;
|
|
|
304
337
|
return true;
|
|
305
338
|
}
|
|
306
339
|
TypeGuard.TIntersect = TIntersect;
|
|
340
|
+
/** Returns true if the given schema is TIterator */
|
|
341
|
+
function TIterator(schema) {
|
|
342
|
+
// prettier-ignore
|
|
343
|
+
return (TKindOf(schema, 'Iterator') &&
|
|
344
|
+
schema.type === 'Iterator' &&
|
|
345
|
+
IsOptionalString(schema.$id) &&
|
|
346
|
+
TSchema(schema.items));
|
|
347
|
+
}
|
|
348
|
+
TypeGuard.TIterator = TIterator;
|
|
349
|
+
/** Returns true if the given schema is a TKind with the given name. */
|
|
350
|
+
function TKindOf(schema, kind) {
|
|
351
|
+
return TKind(schema) && schema[exports.Kind] === kind;
|
|
352
|
+
}
|
|
353
|
+
TypeGuard.TKindOf = TKindOf;
|
|
307
354
|
/** Returns true if the given schema is TKind */
|
|
308
355
|
function TKind(schema) {
|
|
309
|
-
return IsObject(schema) && exports.Kind in schema &&
|
|
356
|
+
return ValueGuard.IsObject(schema) && exports.Kind in schema && ValueGuard.IsString(schema[exports.Kind]);
|
|
310
357
|
}
|
|
311
358
|
TypeGuard.TKind = TKind;
|
|
312
359
|
/** Returns true if the given schema is TLiteral<string> */
|
|
313
360
|
function TLiteralString(schema) {
|
|
314
|
-
return
|
|
361
|
+
return TLiteral(schema) && ValueGuard.IsString(schema.const);
|
|
315
362
|
}
|
|
316
363
|
TypeGuard.TLiteralString = TLiteralString;
|
|
317
364
|
/** Returns true if the given schema is TLiteral<number> */
|
|
318
365
|
function TLiteralNumber(schema) {
|
|
319
|
-
return
|
|
366
|
+
return TLiteral(schema) && ValueGuard.IsNumber(schema.const);
|
|
320
367
|
}
|
|
321
368
|
TypeGuard.TLiteralNumber = TLiteralNumber;
|
|
322
369
|
/** Returns true if the given schema is TLiteral<boolean> */
|
|
323
370
|
function TLiteralBoolean(schema) {
|
|
324
|
-
return
|
|
371
|
+
return TLiteral(schema) && ValueGuard.IsBoolean(schema.const);
|
|
325
372
|
}
|
|
326
373
|
TypeGuard.TLiteralBoolean = TLiteralBoolean;
|
|
327
374
|
/** Returns true if the given schema is TLiteral */
|
|
328
375
|
function TLiteral(schema) {
|
|
329
|
-
|
|
376
|
+
// prettier-ignore
|
|
377
|
+
return (TKindOf(schema, 'Literal') &&
|
|
378
|
+
IsOptionalString(schema.$id) && (ValueGuard.IsBoolean(schema.const) ||
|
|
379
|
+
ValueGuard.IsNumber(schema.const) ||
|
|
380
|
+
ValueGuard.IsString(schema.const)));
|
|
330
381
|
}
|
|
331
382
|
TypeGuard.TLiteral = TLiteral;
|
|
332
383
|
/** Returns true if the given schema is TNever */
|
|
333
384
|
function TNever(schema) {
|
|
334
|
-
|
|
385
|
+
// prettier-ignore
|
|
386
|
+
return (TKindOf(schema, 'Never') &&
|
|
387
|
+
ValueGuard.IsObject(schema.not) &&
|
|
388
|
+
Object.getOwnPropertyNames(schema.not).length === 0);
|
|
335
389
|
}
|
|
336
390
|
TypeGuard.TNever = TNever;
|
|
337
391
|
/** Returns true if the given schema is TNot */
|
|
338
392
|
function TNot(schema) {
|
|
339
393
|
// prettier-ignore
|
|
340
|
-
return (
|
|
341
|
-
schema[exports.Kind] === 'Not' &&
|
|
394
|
+
return (TKindOf(schema, 'Not') &&
|
|
342
395
|
TSchema(schema.not));
|
|
343
396
|
}
|
|
344
397
|
TypeGuard.TNot = TNot;
|
|
345
398
|
/** Returns true if the given schema is TNull */
|
|
346
399
|
function TNull(schema) {
|
|
347
400
|
// prettier-ignore
|
|
348
|
-
return (
|
|
349
|
-
schema[exports.Kind] === 'Null' &&
|
|
401
|
+
return (TKindOf(schema, 'Null') &&
|
|
350
402
|
schema.type === 'null' &&
|
|
351
403
|
IsOptionalString(schema.$id));
|
|
352
404
|
}
|
|
353
405
|
TypeGuard.TNull = TNull;
|
|
354
406
|
/** Returns true if the given schema is TNumber */
|
|
355
407
|
function TNumber(schema) {
|
|
356
|
-
return (
|
|
357
|
-
schema[exports.Kind] === 'Number' &&
|
|
408
|
+
return (TKindOf(schema, 'Number') &&
|
|
358
409
|
schema.type === 'number' &&
|
|
359
410
|
IsOptionalString(schema.$id) &&
|
|
360
411
|
IsOptionalNumber(schema.multipleOf) &&
|
|
@@ -366,11 +417,10 @@ var TypeGuard;
|
|
|
366
417
|
TypeGuard.TNumber = TNumber;
|
|
367
418
|
/** Returns true if the given schema is TObject */
|
|
368
419
|
function TObject(schema) {
|
|
369
|
-
if (!(
|
|
370
|
-
schema[exports.Kind] === 'Object' &&
|
|
420
|
+
if (!(TKindOf(schema, 'Object') &&
|
|
371
421
|
schema.type === 'object' &&
|
|
372
422
|
IsOptionalString(schema.$id) &&
|
|
373
|
-
IsObject(schema.properties) &&
|
|
423
|
+
ValueGuard.IsObject(schema.properties) &&
|
|
374
424
|
IsAdditionalProperties(schema.additionalProperties) &&
|
|
375
425
|
IsOptionalNumber(schema.minProperties) &&
|
|
376
426
|
IsOptionalNumber(schema.maxProperties))) {
|
|
@@ -388,10 +438,8 @@ var TypeGuard;
|
|
|
388
438
|
/** Returns true if the given schema is TPromise */
|
|
389
439
|
function TPromise(schema) {
|
|
390
440
|
// prettier-ignore
|
|
391
|
-
return (
|
|
392
|
-
schema
|
|
393
|
-
schema.type === 'object' &&
|
|
394
|
-
schema.instanceOf === 'Promise' &&
|
|
441
|
+
return (TKindOf(schema, 'Promise') &&
|
|
442
|
+
schema.type === 'Promise' &&
|
|
395
443
|
IsOptionalString(schema.$id) &&
|
|
396
444
|
TSchema(schema.item));
|
|
397
445
|
}
|
|
@@ -399,15 +447,14 @@ var TypeGuard;
|
|
|
399
447
|
/** Returns true if the given schema is TRecord */
|
|
400
448
|
function TRecord(schema) {
|
|
401
449
|
// prettier-ignore
|
|
402
|
-
if (!(
|
|
403
|
-
schema[exports.Kind] === 'Record' &&
|
|
450
|
+
if (!(TKindOf(schema, 'Record') &&
|
|
404
451
|
schema.type === 'object' &&
|
|
405
452
|
IsOptionalString(schema.$id) &&
|
|
406
453
|
IsAdditionalProperties(schema.additionalProperties) &&
|
|
407
|
-
IsObject(schema.patternProperties))) {
|
|
454
|
+
ValueGuard.IsObject(schema.patternProperties))) {
|
|
408
455
|
return false;
|
|
409
456
|
}
|
|
410
|
-
const keys = Object.
|
|
457
|
+
const keys = Object.getOwnPropertyNames(schema.patternProperties);
|
|
411
458
|
if (keys.length !== 1) {
|
|
412
459
|
return false;
|
|
413
460
|
}
|
|
@@ -423,41 +470,30 @@ var TypeGuard;
|
|
|
423
470
|
/** Returns true if the given schema is TRef */
|
|
424
471
|
function TRef(schema) {
|
|
425
472
|
// prettier-ignore
|
|
426
|
-
return (
|
|
427
|
-
schema[exports.Kind] === 'Ref' &&
|
|
473
|
+
return (TKindOf(schema, 'Ref') &&
|
|
428
474
|
IsOptionalString(schema.$id) &&
|
|
429
|
-
IsString(schema.$ref));
|
|
475
|
+
ValueGuard.IsString(schema.$ref));
|
|
430
476
|
}
|
|
431
477
|
TypeGuard.TRef = TRef;
|
|
432
478
|
/** Returns true if the given schema is TString */
|
|
433
479
|
function TString(schema) {
|
|
434
|
-
return (
|
|
435
|
-
schema[exports.Kind] === 'String' &&
|
|
436
|
-
schema.type === 'string' &&
|
|
437
|
-
IsOptionalString(schema.$id) &&
|
|
438
|
-
IsOptionalNumber(schema.minLength) &&
|
|
439
|
-
IsOptionalNumber(schema.maxLength) &&
|
|
440
|
-
IsOptionalPattern(schema.pattern) &&
|
|
441
|
-
IsOptionalFormat(schema.format));
|
|
480
|
+
return (TKindOf(schema, 'String') && schema.type === 'string' && IsOptionalString(schema.$id) && IsOptionalNumber(schema.minLength) && IsOptionalNumber(schema.maxLength) && IsOptionalPattern(schema.pattern) && IsOptionalFormat(schema.format));
|
|
442
481
|
}
|
|
443
482
|
TypeGuard.TString = TString;
|
|
444
483
|
/** Returns true if the given schema is TSymbol */
|
|
445
484
|
function TSymbol(schema) {
|
|
446
485
|
// prettier-ignore
|
|
447
|
-
return (
|
|
448
|
-
schema
|
|
449
|
-
schema.type === 'null' &&
|
|
450
|
-
schema.typeOf === 'Symbol' &&
|
|
486
|
+
return (TKindOf(schema, 'Symbol') &&
|
|
487
|
+
schema.type === 'symbol' &&
|
|
451
488
|
IsOptionalString(schema.$id));
|
|
452
489
|
}
|
|
453
490
|
TypeGuard.TSymbol = TSymbol;
|
|
454
491
|
/** Returns true if the given schema is TTemplateLiteral */
|
|
455
492
|
function TTemplateLiteral(schema) {
|
|
456
493
|
// prettier-ignore
|
|
457
|
-
return (
|
|
458
|
-
schema[exports.Kind] === 'TemplateLiteral' &&
|
|
494
|
+
return (TKindOf(schema, 'TemplateLiteral') &&
|
|
459
495
|
schema.type === 'string' &&
|
|
460
|
-
IsString(schema.pattern) &&
|
|
496
|
+
ValueGuard.IsString(schema.pattern) &&
|
|
461
497
|
schema.pattern[0] === '^' &&
|
|
462
498
|
schema.pattern[schema.pattern.length - 1] === '$');
|
|
463
499
|
}
|
|
@@ -465,28 +501,26 @@ var TypeGuard;
|
|
|
465
501
|
/** Returns true if the given schema is TThis */
|
|
466
502
|
function TThis(schema) {
|
|
467
503
|
// prettier-ignore
|
|
468
|
-
return (
|
|
469
|
-
schema[exports.Kind] === 'This' &&
|
|
504
|
+
return (TKindOf(schema, 'This') &&
|
|
470
505
|
IsOptionalString(schema.$id) &&
|
|
471
|
-
IsString(schema.$ref));
|
|
506
|
+
ValueGuard.IsString(schema.$ref));
|
|
472
507
|
}
|
|
473
508
|
TypeGuard.TThis = TThis;
|
|
474
509
|
/** Returns true if the given schema is TTuple */
|
|
475
510
|
function TTuple(schema) {
|
|
476
511
|
// prettier-ignore
|
|
477
|
-
if (!(
|
|
478
|
-
schema[exports.Kind] === 'Tuple' &&
|
|
512
|
+
if (!(TKindOf(schema, 'Tuple') &&
|
|
479
513
|
schema.type === 'array' &&
|
|
480
514
|
IsOptionalString(schema.$id) &&
|
|
481
|
-
IsNumber(schema.minItems) &&
|
|
482
|
-
IsNumber(schema.maxItems) &&
|
|
515
|
+
ValueGuard.IsNumber(schema.minItems) &&
|
|
516
|
+
ValueGuard.IsNumber(schema.maxItems) &&
|
|
483
517
|
schema.minItems === schema.maxItems)) {
|
|
484
518
|
return false;
|
|
485
519
|
}
|
|
486
|
-
if (schema.items
|
|
520
|
+
if (ValueGuard.IsUndefined(schema.items) && ValueGuard.IsUndefined(schema.additionalItems) && schema.minItems === 0) {
|
|
487
521
|
return true;
|
|
488
522
|
}
|
|
489
|
-
if (!IsArray(schema.items)) {
|
|
523
|
+
if (!ValueGuard.IsArray(schema.items)) {
|
|
490
524
|
return false;
|
|
491
525
|
}
|
|
492
526
|
for (const inner of schema.items) {
|
|
@@ -499,10 +533,8 @@ var TypeGuard;
|
|
|
499
533
|
/** Returns true if the given schema is TUndefined */
|
|
500
534
|
function TUndefined(schema) {
|
|
501
535
|
// prettier-ignore
|
|
502
|
-
return (
|
|
503
|
-
schema
|
|
504
|
-
schema.type === 'null' &&
|
|
505
|
-
schema.typeOf === 'Undefined' &&
|
|
536
|
+
return (TKindOf(schema, 'Undefined') &&
|
|
537
|
+
schema.type === 'undefined' &&
|
|
506
538
|
IsOptionalString(schema.$id));
|
|
507
539
|
}
|
|
508
540
|
TypeGuard.TUndefined = TUndefined;
|
|
@@ -514,9 +546,8 @@ var TypeGuard;
|
|
|
514
546
|
/** Returns true if the given schema is TUnion */
|
|
515
547
|
function TUnion(schema) {
|
|
516
548
|
// prettier-ignore
|
|
517
|
-
if (!(
|
|
518
|
-
schema
|
|
519
|
-
IsArray(schema.anyOf) &&
|
|
549
|
+
if (!(TKindOf(schema, 'Union') &&
|
|
550
|
+
ValueGuard.IsArray(schema.anyOf) &&
|
|
520
551
|
IsOptionalString(schema.$id))) {
|
|
521
552
|
return false;
|
|
522
553
|
}
|
|
@@ -529,61 +560,58 @@ var TypeGuard;
|
|
|
529
560
|
TypeGuard.TUnion = TUnion;
|
|
530
561
|
/** Returns true if the given schema is TUint8Array */
|
|
531
562
|
function TUint8Array(schema) {
|
|
532
|
-
|
|
563
|
+
// prettier-ignore
|
|
564
|
+
return (TKindOf(schema, 'Uint8Array') &&
|
|
565
|
+
schema.type === 'Uint8Array' &&
|
|
566
|
+
IsOptionalString(schema.$id) &&
|
|
567
|
+
IsOptionalNumber(schema.minByteLength) &&
|
|
568
|
+
IsOptionalNumber(schema.maxByteLength));
|
|
533
569
|
}
|
|
534
570
|
TypeGuard.TUint8Array = TUint8Array;
|
|
535
571
|
/** Returns true if the given schema is TUnknown */
|
|
536
572
|
function TUnknown(schema) {
|
|
537
573
|
// prettier-ignore
|
|
538
|
-
return (
|
|
539
|
-
schema[exports.Kind] === 'Unknown' &&
|
|
574
|
+
return (TKindOf(schema, 'Unknown') &&
|
|
540
575
|
IsOptionalString(schema.$id));
|
|
541
576
|
}
|
|
542
577
|
TypeGuard.TUnknown = TUnknown;
|
|
543
578
|
/** Returns true if the given schema is a raw TUnsafe */
|
|
544
579
|
function TUnsafe(schema) {
|
|
545
|
-
|
|
546
|
-
return (TKind(schema) &&
|
|
547
|
-
schema[exports.Kind] === 'Unsafe');
|
|
580
|
+
return TKindOf(schema, 'Unsafe');
|
|
548
581
|
}
|
|
549
582
|
TypeGuard.TUnsafe = TUnsafe;
|
|
550
583
|
/** Returns true if the given schema is TVoid */
|
|
551
584
|
function TVoid(schema) {
|
|
552
585
|
// prettier-ignore
|
|
553
|
-
return (
|
|
554
|
-
schema
|
|
555
|
-
schema.type === 'null' &&
|
|
556
|
-
schema.typeOf === 'Void' &&
|
|
586
|
+
return (TKindOf(schema, 'Void') &&
|
|
587
|
+
schema.type === 'void' &&
|
|
557
588
|
IsOptionalString(schema.$id));
|
|
558
589
|
}
|
|
559
590
|
TypeGuard.TVoid = TVoid;
|
|
560
|
-
/** Returns true if this schema has the ReadonlyOptional modifier */
|
|
561
|
-
function TReadonlyOptional(schema) {
|
|
562
|
-
return IsObject(schema) && schema[exports.Modifier] === 'ReadonlyOptional';
|
|
563
|
-
}
|
|
564
|
-
TypeGuard.TReadonlyOptional = TReadonlyOptional;
|
|
565
591
|
/** Returns true if this schema has the Readonly modifier */
|
|
566
592
|
function TReadonly(schema) {
|
|
567
|
-
return IsObject(schema) && schema[exports.
|
|
593
|
+
return ValueGuard.IsObject(schema) && schema[exports.Readonly] === 'Readonly';
|
|
568
594
|
}
|
|
569
595
|
TypeGuard.TReadonly = TReadonly;
|
|
570
596
|
/** Returns true if this schema has the Optional modifier */
|
|
571
597
|
function TOptional(schema) {
|
|
572
|
-
return IsObject(schema) && schema[exports.
|
|
598
|
+
return ValueGuard.IsObject(schema) && schema[exports.Optional] === 'Optional';
|
|
573
599
|
}
|
|
574
600
|
TypeGuard.TOptional = TOptional;
|
|
575
601
|
/** Returns true if the given schema is TSchema */
|
|
576
602
|
function TSchema(schema) {
|
|
577
|
-
return (
|
|
603
|
+
return (ValueGuard.IsObject(schema) &&
|
|
578
604
|
(TAny(schema) ||
|
|
579
605
|
TArray(schema) ||
|
|
580
606
|
TBoolean(schema) ||
|
|
581
607
|
TBigInt(schema) ||
|
|
608
|
+
TAsyncIterator(schema) ||
|
|
582
609
|
TConstructor(schema) ||
|
|
583
610
|
TDate(schema) ||
|
|
584
611
|
TFunction(schema) ||
|
|
585
612
|
TInteger(schema) ||
|
|
586
613
|
TIntersect(schema) ||
|
|
614
|
+
TIterator(schema) ||
|
|
587
615
|
TLiteral(schema) ||
|
|
588
616
|
TNever(schema) ||
|
|
589
617
|
TNot(schema) ||
|
|
@@ -650,14 +678,38 @@ var TypeExtends;
|
|
|
650
678
|
return result === TypeExtendsResult.False ? TypeExtendsResult.False : TypeExtendsResult.True;
|
|
651
679
|
}
|
|
652
680
|
// --------------------------------------------------------------------------
|
|
681
|
+
// StructuralRight
|
|
682
|
+
// --------------------------------------------------------------------------
|
|
683
|
+
function IsStructuralRight(right) {
|
|
684
|
+
// prettier-ignore
|
|
685
|
+
return (TypeGuard.TNever(right) ||
|
|
686
|
+
TypeGuard.TIntersect(right) ||
|
|
687
|
+
TypeGuard.TUnion(right) ||
|
|
688
|
+
TypeGuard.TUnknown(right) ||
|
|
689
|
+
TypeGuard.TAny(right));
|
|
690
|
+
}
|
|
691
|
+
function StructuralRight(left, right) {
|
|
692
|
+
if (TypeGuard.TNever(right))
|
|
693
|
+
return TNeverRight(left, right);
|
|
694
|
+
if (TypeGuard.TIntersect(right))
|
|
695
|
+
return TIntersectRight(left, right);
|
|
696
|
+
if (TypeGuard.TUnion(right))
|
|
697
|
+
return TUnionRight(left, right);
|
|
698
|
+
if (TypeGuard.TUnknown(right))
|
|
699
|
+
return TUnknownRight(left, right);
|
|
700
|
+
if (TypeGuard.TAny(right))
|
|
701
|
+
return TAnyRight(left, right);
|
|
702
|
+
throw Error('TypeExtends: StructuralRight');
|
|
703
|
+
}
|
|
704
|
+
// --------------------------------------------------------------------------
|
|
653
705
|
// Any
|
|
654
706
|
// --------------------------------------------------------------------------
|
|
655
|
-
function
|
|
707
|
+
function TAnyRight(left, right) {
|
|
656
708
|
return TypeExtendsResult.True;
|
|
657
709
|
}
|
|
658
|
-
function
|
|
710
|
+
function TAny(left, right) {
|
|
659
711
|
if (TypeGuard.TIntersect(right))
|
|
660
|
-
return
|
|
712
|
+
return TIntersectRight(left, right);
|
|
661
713
|
if (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema)))
|
|
662
714
|
return TypeExtendsResult.True;
|
|
663
715
|
if (TypeGuard.TUnion(right))
|
|
@@ -671,7 +723,7 @@ var TypeExtends;
|
|
|
671
723
|
// --------------------------------------------------------------------------
|
|
672
724
|
// Array
|
|
673
725
|
// --------------------------------------------------------------------------
|
|
674
|
-
function
|
|
726
|
+
function TArrayRight(left, right) {
|
|
675
727
|
if (TypeGuard.TUnknown(left))
|
|
676
728
|
return TypeExtendsResult.False;
|
|
677
729
|
if (TypeGuard.TAny(left))
|
|
@@ -680,15 +732,9 @@ var TypeExtends;
|
|
|
680
732
|
return TypeExtendsResult.True;
|
|
681
733
|
return TypeExtendsResult.False;
|
|
682
734
|
}
|
|
683
|
-
function
|
|
684
|
-
if (
|
|
685
|
-
return
|
|
686
|
-
if (TypeGuard.TUnion(right))
|
|
687
|
-
return UnionRight(left, right);
|
|
688
|
-
if (TypeGuard.TUnknown(right))
|
|
689
|
-
return UnknownRight(left, right);
|
|
690
|
-
if (TypeGuard.TAny(right))
|
|
691
|
-
return AnyRight(left, right);
|
|
735
|
+
function TArray(left, right) {
|
|
736
|
+
if (IsStructuralRight(right))
|
|
737
|
+
return StructuralRight(left, right);
|
|
692
738
|
if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
|
|
693
739
|
return TypeExtendsResult.True;
|
|
694
740
|
if (!TypeGuard.TArray(right))
|
|
@@ -696,64 +742,52 @@ var TypeExtends;
|
|
|
696
742
|
return IntoBooleanResult(Visit(left.items, right.items));
|
|
697
743
|
}
|
|
698
744
|
// --------------------------------------------------------------------------
|
|
745
|
+
// AsyncIterator
|
|
746
|
+
// --------------------------------------------------------------------------
|
|
747
|
+
function TAsyncIterator(left, right) {
|
|
748
|
+
if (IsStructuralRight(right))
|
|
749
|
+
return StructuralRight(left, right);
|
|
750
|
+
if (!TypeGuard.TAsyncIterator(right))
|
|
751
|
+
return TypeExtendsResult.False;
|
|
752
|
+
return IntoBooleanResult(Visit(left.items, right.items));
|
|
753
|
+
}
|
|
754
|
+
// --------------------------------------------------------------------------
|
|
699
755
|
// BigInt
|
|
700
756
|
// --------------------------------------------------------------------------
|
|
701
|
-
function
|
|
702
|
-
if (
|
|
703
|
-
return
|
|
704
|
-
if (TypeGuard.TUnion(right))
|
|
705
|
-
return UnionRight(left, right);
|
|
706
|
-
if (TypeGuard.TNever(right))
|
|
707
|
-
return NeverRight(left, right);
|
|
708
|
-
if (TypeGuard.TUnknown(right))
|
|
709
|
-
return UnknownRight(left, right);
|
|
710
|
-
if (TypeGuard.TAny(right))
|
|
711
|
-
return AnyRight(left, right);
|
|
757
|
+
function TBigInt(left, right) {
|
|
758
|
+
if (IsStructuralRight(right))
|
|
759
|
+
return StructuralRight(left, right);
|
|
712
760
|
if (TypeGuard.TObject(right))
|
|
713
|
-
return
|
|
761
|
+
return TObjectRight(left, right);
|
|
714
762
|
if (TypeGuard.TRecord(right))
|
|
715
|
-
return
|
|
763
|
+
return TRecordRight(left, right);
|
|
716
764
|
return TypeGuard.TBigInt(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
717
765
|
}
|
|
718
766
|
// --------------------------------------------------------------------------
|
|
719
767
|
// Boolean
|
|
720
768
|
// --------------------------------------------------------------------------
|
|
721
|
-
function
|
|
722
|
-
if (TypeGuard.TLiteral(left) &&
|
|
769
|
+
function TBooleanRight(left, right) {
|
|
770
|
+
if (TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const))
|
|
723
771
|
return TypeExtendsResult.True;
|
|
724
772
|
return TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
725
773
|
}
|
|
726
|
-
function
|
|
727
|
-
if (
|
|
728
|
-
return
|
|
729
|
-
if (TypeGuard.TUnion(right))
|
|
730
|
-
return UnionRight(left, right);
|
|
731
|
-
if (TypeGuard.TNever(right))
|
|
732
|
-
return NeverRight(left, right);
|
|
733
|
-
if (TypeGuard.TUnknown(right))
|
|
734
|
-
return UnknownRight(left, right);
|
|
735
|
-
if (TypeGuard.TAny(right))
|
|
736
|
-
return AnyRight(left, right);
|
|
774
|
+
function TBoolean(left, right) {
|
|
775
|
+
if (IsStructuralRight(right))
|
|
776
|
+
return StructuralRight(left, right);
|
|
737
777
|
if (TypeGuard.TObject(right))
|
|
738
|
-
return
|
|
778
|
+
return TObjectRight(left, right);
|
|
739
779
|
if (TypeGuard.TRecord(right))
|
|
740
|
-
return
|
|
780
|
+
return TRecordRight(left, right);
|
|
741
781
|
return TypeGuard.TBoolean(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
742
782
|
}
|
|
743
783
|
// --------------------------------------------------------------------------
|
|
744
784
|
// Constructor
|
|
745
785
|
// --------------------------------------------------------------------------
|
|
746
|
-
function
|
|
747
|
-
if (
|
|
748
|
-
return
|
|
749
|
-
if (TypeGuard.TUnion(right))
|
|
750
|
-
return UnionRight(left, right);
|
|
751
|
-
if (TypeGuard.TUnknown(right))
|
|
752
|
-
return UnknownRight(left, right);
|
|
753
|
-
if (TypeGuard.TAny(right))
|
|
754
|
-
return AnyRight(left, right);
|
|
786
|
+
function TConstructor(left, right) {
|
|
787
|
+
if (IsStructuralRight(right))
|
|
788
|
+
return StructuralRight(left, right);
|
|
755
789
|
if (TypeGuard.TObject(right))
|
|
756
|
-
return
|
|
790
|
+
return TObjectRight(left, right);
|
|
757
791
|
if (!TypeGuard.TConstructor(right))
|
|
758
792
|
return TypeExtendsResult.False;
|
|
759
793
|
if (left.parameters.length > right.parameters.length)
|
|
@@ -766,35 +800,23 @@ var TypeExtends;
|
|
|
766
800
|
// --------------------------------------------------------------------------
|
|
767
801
|
// Date
|
|
768
802
|
// --------------------------------------------------------------------------
|
|
769
|
-
function
|
|
770
|
-
if (
|
|
771
|
-
return
|
|
772
|
-
if (TypeGuard.TUnion(right))
|
|
773
|
-
return UnionRight(left, right);
|
|
774
|
-
if (TypeGuard.TUnknown(right))
|
|
775
|
-
return UnknownRight(left, right);
|
|
776
|
-
if (TypeGuard.TAny(right))
|
|
777
|
-
return AnyRight(left, right);
|
|
803
|
+
function TDate(left, right) {
|
|
804
|
+
if (IsStructuralRight(right))
|
|
805
|
+
return StructuralRight(left, right);
|
|
778
806
|
if (TypeGuard.TObject(right))
|
|
779
|
-
return
|
|
807
|
+
return TObjectRight(left, right);
|
|
780
808
|
if (TypeGuard.TRecord(right))
|
|
781
|
-
return
|
|
809
|
+
return TRecordRight(left, right);
|
|
782
810
|
return TypeGuard.TDate(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
783
811
|
}
|
|
784
812
|
// --------------------------------------------------------------------------
|
|
785
813
|
// Function
|
|
786
814
|
// --------------------------------------------------------------------------
|
|
787
|
-
function
|
|
788
|
-
if (
|
|
789
|
-
return
|
|
790
|
-
if (TypeGuard.TUnion(right))
|
|
791
|
-
return UnionRight(left, right);
|
|
792
|
-
if (TypeGuard.TUnknown(right))
|
|
793
|
-
return UnknownRight(left, right);
|
|
794
|
-
if (TypeGuard.TAny(right))
|
|
795
|
-
return AnyRight(left, right);
|
|
815
|
+
function TFunction(left, right) {
|
|
816
|
+
if (IsStructuralRight(right))
|
|
817
|
+
return StructuralRight(left, right);
|
|
796
818
|
if (TypeGuard.TObject(right))
|
|
797
|
-
return
|
|
819
|
+
return TObjectRight(left, right);
|
|
798
820
|
if (!TypeGuard.TFunction(right))
|
|
799
821
|
return TypeExtendsResult.False;
|
|
800
822
|
if (left.parameters.length > right.parameters.length)
|
|
@@ -807,87 +829,72 @@ var TypeExtends;
|
|
|
807
829
|
// --------------------------------------------------------------------------
|
|
808
830
|
// Integer
|
|
809
831
|
// --------------------------------------------------------------------------
|
|
810
|
-
function
|
|
811
|
-
if (TypeGuard.TLiteral(left) &&
|
|
832
|
+
function TIntegerRight(left, right) {
|
|
833
|
+
if (TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const))
|
|
812
834
|
return TypeExtendsResult.True;
|
|
813
835
|
return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
814
836
|
}
|
|
815
|
-
function
|
|
816
|
-
if (
|
|
817
|
-
return
|
|
818
|
-
if (TypeGuard.TUnion(right))
|
|
819
|
-
return UnionRight(left, right);
|
|
820
|
-
if (TypeGuard.TNever(right))
|
|
821
|
-
return NeverRight(left, right);
|
|
822
|
-
if (TypeGuard.TUnknown(right))
|
|
823
|
-
return UnknownRight(left, right);
|
|
824
|
-
if (TypeGuard.TAny(right))
|
|
825
|
-
return AnyRight(left, right);
|
|
837
|
+
function TInteger(left, right) {
|
|
838
|
+
if (IsStructuralRight(right))
|
|
839
|
+
return StructuralRight(left, right);
|
|
826
840
|
if (TypeGuard.TObject(right))
|
|
827
|
-
return
|
|
841
|
+
return TObjectRight(left, right);
|
|
828
842
|
if (TypeGuard.TRecord(right))
|
|
829
|
-
return
|
|
843
|
+
return TRecordRight(left, right);
|
|
830
844
|
return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
831
845
|
}
|
|
832
846
|
// --------------------------------------------------------------------------
|
|
833
847
|
// Intersect
|
|
834
848
|
// --------------------------------------------------------------------------
|
|
835
|
-
function
|
|
849
|
+
function TIntersectRight(left, right) {
|
|
836
850
|
return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
837
851
|
}
|
|
838
|
-
function
|
|
852
|
+
function TIntersect(left, right) {
|
|
839
853
|
return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
840
854
|
}
|
|
841
855
|
// --------------------------------------------------------------------------
|
|
842
|
-
//
|
|
856
|
+
// Iterator
|
|
843
857
|
// --------------------------------------------------------------------------
|
|
844
|
-
function
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
function IsLiteralBoolean(schema) {
|
|
851
|
-
return typeof schema.const === 'boolean';
|
|
858
|
+
function TIterator(left, right) {
|
|
859
|
+
if (IsStructuralRight(right))
|
|
860
|
+
return StructuralRight(left, right);
|
|
861
|
+
if (!TypeGuard.TIterator(right))
|
|
862
|
+
return TypeExtendsResult.False;
|
|
863
|
+
return IntoBooleanResult(Visit(left.items, right.items));
|
|
852
864
|
}
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
return NeverRight(left, right);
|
|
860
|
-
if (TypeGuard.TUnknown(right))
|
|
861
|
-
return UnknownRight(left, right);
|
|
862
|
-
if (TypeGuard.TAny(right))
|
|
863
|
-
return AnyRight(left, right);
|
|
865
|
+
// --------------------------------------------------------------------------
|
|
866
|
+
// Literal
|
|
867
|
+
// --------------------------------------------------------------------------
|
|
868
|
+
function TLiteral(left, right) {
|
|
869
|
+
if (IsStructuralRight(right))
|
|
870
|
+
return StructuralRight(left, right);
|
|
864
871
|
if (TypeGuard.TObject(right))
|
|
865
|
-
return
|
|
872
|
+
return TObjectRight(left, right);
|
|
866
873
|
if (TypeGuard.TRecord(right))
|
|
867
|
-
return
|
|
874
|
+
return TRecordRight(left, right);
|
|
868
875
|
if (TypeGuard.TString(right))
|
|
869
|
-
return
|
|
876
|
+
return TStringRight(left, right);
|
|
870
877
|
if (TypeGuard.TNumber(right))
|
|
871
|
-
return
|
|
878
|
+
return TNumberRight(left, right);
|
|
872
879
|
if (TypeGuard.TInteger(right))
|
|
873
|
-
return
|
|
880
|
+
return TIntegerRight(left, right);
|
|
874
881
|
if (TypeGuard.TBoolean(right))
|
|
875
|
-
return
|
|
882
|
+
return TBooleanRight(left, right);
|
|
876
883
|
return TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
877
884
|
}
|
|
878
885
|
// --------------------------------------------------------------------------
|
|
879
886
|
// Never
|
|
880
887
|
// --------------------------------------------------------------------------
|
|
881
|
-
function
|
|
888
|
+
function TNeverRight(left, right) {
|
|
882
889
|
return TypeExtendsResult.False;
|
|
883
890
|
}
|
|
884
|
-
function
|
|
891
|
+
function TNever(left, right) {
|
|
885
892
|
return TypeExtendsResult.True;
|
|
886
893
|
}
|
|
887
894
|
// --------------------------------------------------------------------------
|
|
888
895
|
// Not
|
|
889
896
|
// --------------------------------------------------------------------------
|
|
890
|
-
function
|
|
897
|
+
function UnwrapTNot(schema) {
|
|
891
898
|
let [current, depth] = [schema, 0];
|
|
892
899
|
while (true) {
|
|
893
900
|
if (!TypeGuard.TNot(current))
|
|
@@ -897,66 +904,50 @@ var TypeExtends;
|
|
|
897
904
|
}
|
|
898
905
|
return depth % 2 === 0 ? current : exports.Type.Unknown();
|
|
899
906
|
}
|
|
900
|
-
function
|
|
907
|
+
function TNot(left, right) {
|
|
901
908
|
// TypeScript has no concept of negated types, and attempts to correctly check the negated
|
|
902
909
|
// type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
|
|
903
910
|
// the type. Instead we unwrap to either unknown or T and continue evaluating.
|
|
904
911
|
if (TypeGuard.TNot(left))
|
|
905
|
-
return Visit(
|
|
912
|
+
return Visit(UnwrapTNot(left), right);
|
|
906
913
|
if (TypeGuard.TNot(right))
|
|
907
|
-
return Visit(left,
|
|
914
|
+
return Visit(left, UnwrapTNot(right));
|
|
908
915
|
throw new Error(`TypeExtends: Invalid fallthrough for Not`);
|
|
909
916
|
}
|
|
910
917
|
// --------------------------------------------------------------------------
|
|
911
918
|
// Null
|
|
912
919
|
// --------------------------------------------------------------------------
|
|
913
|
-
function
|
|
914
|
-
if (
|
|
915
|
-
return
|
|
916
|
-
if (TypeGuard.TUnion(right))
|
|
917
|
-
return UnionRight(left, right);
|
|
918
|
-
if (TypeGuard.TNever(right))
|
|
919
|
-
return NeverRight(left, right);
|
|
920
|
-
if (TypeGuard.TUnknown(right))
|
|
921
|
-
return UnknownRight(left, right);
|
|
922
|
-
if (TypeGuard.TAny(right))
|
|
923
|
-
return AnyRight(left, right);
|
|
920
|
+
function TNull(left, right) {
|
|
921
|
+
if (IsStructuralRight(right))
|
|
922
|
+
return StructuralRight(left, right);
|
|
924
923
|
if (TypeGuard.TObject(right))
|
|
925
|
-
return
|
|
924
|
+
return TObjectRight(left, right);
|
|
926
925
|
if (TypeGuard.TRecord(right))
|
|
927
|
-
return
|
|
926
|
+
return TRecordRight(left, right);
|
|
928
927
|
return TypeGuard.TNull(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
929
928
|
}
|
|
930
929
|
// --------------------------------------------------------------------------
|
|
931
930
|
// Number
|
|
932
931
|
// --------------------------------------------------------------------------
|
|
933
|
-
function
|
|
934
|
-
if (TypeGuard.
|
|
932
|
+
function TNumberRight(left, right) {
|
|
933
|
+
if (TypeGuard.TLiteralNumber(left))
|
|
935
934
|
return TypeExtendsResult.True;
|
|
936
935
|
return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
937
936
|
}
|
|
938
|
-
function
|
|
939
|
-
if (
|
|
940
|
-
return
|
|
941
|
-
if (TypeGuard.TUnion(right))
|
|
942
|
-
return UnionRight(left, right);
|
|
943
|
-
if (TypeGuard.TNever(right))
|
|
944
|
-
return NeverRight(left, right);
|
|
945
|
-
if (TypeGuard.TUnknown(right))
|
|
946
|
-
return UnknownRight(left, right);
|
|
947
|
-
if (TypeGuard.TAny(right))
|
|
948
|
-
return AnyRight(left, right);
|
|
937
|
+
function TNumber(left, right) {
|
|
938
|
+
if (IsStructuralRight(right))
|
|
939
|
+
return StructuralRight(left, right);
|
|
949
940
|
if (TypeGuard.TObject(right))
|
|
950
|
-
return
|
|
941
|
+
return TObjectRight(left, right);
|
|
951
942
|
if (TypeGuard.TRecord(right))
|
|
952
|
-
return
|
|
943
|
+
return TRecordRight(left, right);
|
|
953
944
|
return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
954
945
|
}
|
|
955
946
|
// --------------------------------------------------------------------------
|
|
956
947
|
// Object
|
|
957
948
|
// --------------------------------------------------------------------------
|
|
958
949
|
function IsObjectPropertyCount(schema, count) {
|
|
959
|
-
return
|
|
950
|
+
return Object.getOwnPropertyNames(schema.properties).length === count;
|
|
960
951
|
}
|
|
961
952
|
function IsObjectStringLike(schema) {
|
|
962
953
|
return IsObjectArrayLike(schema);
|
|
@@ -1007,18 +998,18 @@ var TypeExtends;
|
|
|
1007
998
|
return TypeExtendsResult.False;
|
|
1008
999
|
return TypeExtendsResult.True;
|
|
1009
1000
|
}
|
|
1010
|
-
function
|
|
1001
|
+
function TObjectRight(left, right) {
|
|
1011
1002
|
if (TypeGuard.TUnknown(left))
|
|
1012
1003
|
return TypeExtendsResult.False;
|
|
1013
1004
|
if (TypeGuard.TAny(left))
|
|
1014
1005
|
return TypeExtendsResult.Union;
|
|
1015
1006
|
if (TypeGuard.TNever(left))
|
|
1016
1007
|
return TypeExtendsResult.True;
|
|
1017
|
-
if (TypeGuard.
|
|
1008
|
+
if (TypeGuard.TLiteralString(left) && IsObjectStringLike(right))
|
|
1018
1009
|
return TypeExtendsResult.True;
|
|
1019
|
-
if (TypeGuard.
|
|
1010
|
+
if (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right))
|
|
1020
1011
|
return TypeExtendsResult.True;
|
|
1021
|
-
if (TypeGuard.
|
|
1012
|
+
if (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right))
|
|
1022
1013
|
return TypeExtendsResult.True;
|
|
1023
1014
|
if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
|
|
1024
1015
|
return TypeExtendsResult.True;
|
|
@@ -1052,20 +1043,14 @@ var TypeExtends;
|
|
|
1052
1043
|
}
|
|
1053
1044
|
return TypeExtendsResult.False;
|
|
1054
1045
|
}
|
|
1055
|
-
function
|
|
1056
|
-
if (
|
|
1057
|
-
return
|
|
1058
|
-
if (TypeGuard.TUnion(right))
|
|
1059
|
-
return UnionRight(left, right);
|
|
1060
|
-
if (TypeGuard.TUnknown(right))
|
|
1061
|
-
return UnknownRight(left, right);
|
|
1062
|
-
if (TypeGuard.TAny(right))
|
|
1063
|
-
return AnyRight(left, right);
|
|
1046
|
+
function TObject(left, right) {
|
|
1047
|
+
if (IsStructuralRight(right))
|
|
1048
|
+
return StructuralRight(left, right);
|
|
1064
1049
|
if (TypeGuard.TRecord(right))
|
|
1065
|
-
return
|
|
1050
|
+
return TRecordRight(left, right);
|
|
1066
1051
|
if (!TypeGuard.TObject(right))
|
|
1067
1052
|
return TypeExtendsResult.False;
|
|
1068
|
-
for (const key of
|
|
1053
|
+
for (const key of Object.getOwnPropertyNames(right.properties)) {
|
|
1069
1054
|
if (!(key in left.properties))
|
|
1070
1055
|
return TypeExtendsResult.False;
|
|
1071
1056
|
if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
|
|
@@ -1077,15 +1062,9 @@ var TypeExtends;
|
|
|
1077
1062
|
// --------------------------------------------------------------------------
|
|
1078
1063
|
// Promise
|
|
1079
1064
|
// --------------------------------------------------------------------------
|
|
1080
|
-
function
|
|
1081
|
-
if (
|
|
1082
|
-
return
|
|
1083
|
-
if (TypeGuard.TUnion(right))
|
|
1084
|
-
return UnionRight(left, right);
|
|
1085
|
-
if (TypeGuard.TUnknown(right))
|
|
1086
|
-
return UnknownRight(left, right);
|
|
1087
|
-
if (TypeGuard.TAny(right))
|
|
1088
|
-
return AnyRight(left, right);
|
|
1065
|
+
function TPromise(left, right) {
|
|
1066
|
+
if (IsStructuralRight(right))
|
|
1067
|
+
return StructuralRight(left, right);
|
|
1089
1068
|
if (TypeGuard.TObject(right) && IsObjectPromiseLike(right))
|
|
1090
1069
|
return TypeExtendsResult.True;
|
|
1091
1070
|
if (!TypeGuard.TPromise(right))
|
|
@@ -1109,10 +1088,10 @@ var TypeExtends;
|
|
|
1109
1088
|
return schema.patternProperties[exports.PatternStringExact];
|
|
1110
1089
|
throw Error('TypeExtends: Cannot get record value');
|
|
1111
1090
|
}
|
|
1112
|
-
function
|
|
1091
|
+
function TRecordRight(left, right) {
|
|
1113
1092
|
const Key = RecordKey(right);
|
|
1114
1093
|
const Value = RecordValue(right);
|
|
1115
|
-
if (TypeGuard.
|
|
1094
|
+
if (TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True)
|
|
1116
1095
|
return TypeExtendsResult.True;
|
|
1117
1096
|
if (TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key))
|
|
1118
1097
|
return Visit(left, Value);
|
|
@@ -1121,7 +1100,7 @@ var TypeExtends;
|
|
|
1121
1100
|
if (TypeGuard.TArray(left) && TypeGuard.TNumber(Key))
|
|
1122
1101
|
return Visit(left, Value);
|
|
1123
1102
|
if (TypeGuard.TObject(left)) {
|
|
1124
|
-
for (const key of
|
|
1103
|
+
for (const key of Object.getOwnPropertyNames(left.properties)) {
|
|
1125
1104
|
if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
|
|
1126
1105
|
return TypeExtendsResult.False;
|
|
1127
1106
|
}
|
|
@@ -1130,18 +1109,12 @@ var TypeExtends;
|
|
|
1130
1109
|
}
|
|
1131
1110
|
return TypeExtendsResult.False;
|
|
1132
1111
|
}
|
|
1133
|
-
function
|
|
1112
|
+
function TRecord(left, right) {
|
|
1134
1113
|
const Value = RecordValue(left);
|
|
1135
|
-
if (
|
|
1136
|
-
return
|
|
1137
|
-
if (TypeGuard.TUnion(right))
|
|
1138
|
-
return UnionRight(left, right);
|
|
1139
|
-
if (TypeGuard.TUnknown(right))
|
|
1140
|
-
return UnknownRight(left, right);
|
|
1141
|
-
if (TypeGuard.TAny(right))
|
|
1142
|
-
return AnyRight(left, right);
|
|
1114
|
+
if (IsStructuralRight(right))
|
|
1115
|
+
return StructuralRight(left, right);
|
|
1143
1116
|
if (TypeGuard.TObject(right))
|
|
1144
|
-
return
|
|
1117
|
+
return TObjectRight(left, right);
|
|
1145
1118
|
if (!TypeGuard.TRecord(right))
|
|
1146
1119
|
return TypeExtendsResult.False;
|
|
1147
1120
|
return Visit(Value, RecordValue(right));
|
|
@@ -1149,52 +1122,36 @@ var TypeExtends;
|
|
|
1149
1122
|
// --------------------------------------------------------------------------
|
|
1150
1123
|
// String
|
|
1151
1124
|
// --------------------------------------------------------------------------
|
|
1152
|
-
function
|
|
1153
|
-
if (TypeGuard.TLiteral(left) &&
|
|
1125
|
+
function TStringRight(left, right) {
|
|
1126
|
+
if (TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const))
|
|
1154
1127
|
return TypeExtendsResult.True;
|
|
1155
1128
|
return TypeGuard.TString(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1156
1129
|
}
|
|
1157
|
-
function
|
|
1158
|
-
if (
|
|
1159
|
-
return
|
|
1160
|
-
if (TypeGuard.TUnion(right))
|
|
1161
|
-
return UnionRight(left, right);
|
|
1162
|
-
if (TypeGuard.TNever(right))
|
|
1163
|
-
return NeverRight(left, right);
|
|
1164
|
-
if (TypeGuard.TUnknown(right))
|
|
1165
|
-
return UnknownRight(left, right);
|
|
1166
|
-
if (TypeGuard.TAny(right))
|
|
1167
|
-
return AnyRight(left, right);
|
|
1130
|
+
function TString(left, right) {
|
|
1131
|
+
if (IsStructuralRight(right))
|
|
1132
|
+
return StructuralRight(left, right);
|
|
1168
1133
|
if (TypeGuard.TObject(right))
|
|
1169
|
-
return
|
|
1134
|
+
return TObjectRight(left, right);
|
|
1170
1135
|
if (TypeGuard.TRecord(right))
|
|
1171
|
-
return
|
|
1136
|
+
return TRecordRight(left, right);
|
|
1172
1137
|
return TypeGuard.TString(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1173
1138
|
}
|
|
1174
1139
|
// --------------------------------------------------------------------------
|
|
1175
1140
|
// Symbol
|
|
1176
1141
|
// --------------------------------------------------------------------------
|
|
1177
|
-
function
|
|
1178
|
-
if (
|
|
1179
|
-
return
|
|
1180
|
-
if (TypeGuard.TUnion(right))
|
|
1181
|
-
return UnionRight(left, right);
|
|
1182
|
-
if (TypeGuard.TNever(right))
|
|
1183
|
-
return NeverRight(left, right);
|
|
1184
|
-
if (TypeGuard.TUnknown(right))
|
|
1185
|
-
return UnknownRight(left, right);
|
|
1186
|
-
if (TypeGuard.TAny(right))
|
|
1187
|
-
return AnyRight(left, right);
|
|
1142
|
+
function TSymbol(left, right) {
|
|
1143
|
+
if (IsStructuralRight(right))
|
|
1144
|
+
return StructuralRight(left, right);
|
|
1188
1145
|
if (TypeGuard.TObject(right))
|
|
1189
|
-
return
|
|
1146
|
+
return TObjectRight(left, right);
|
|
1190
1147
|
if (TypeGuard.TRecord(right))
|
|
1191
|
-
return
|
|
1148
|
+
return TRecordRight(left, right);
|
|
1192
1149
|
return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1193
1150
|
}
|
|
1194
1151
|
// --------------------------------------------------------------------------
|
|
1195
1152
|
// TemplateLiteral
|
|
1196
1153
|
// --------------------------------------------------------------------------
|
|
1197
|
-
function
|
|
1154
|
+
function TTemplateLiteral(left, right) {
|
|
1198
1155
|
// TemplateLiteral types are resolved to either unions for finite expressions or string
|
|
1199
1156
|
// for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
|
|
1200
1157
|
// either type and continue evaluating.
|
|
@@ -1207,75 +1164,58 @@ var TypeExtends;
|
|
|
1207
1164
|
// --------------------------------------------------------------------------
|
|
1208
1165
|
// Tuple
|
|
1209
1166
|
// --------------------------------------------------------------------------
|
|
1210
|
-
function
|
|
1167
|
+
function IsArrayOfTuple(left, right) {
|
|
1168
|
+
// prettier-ignore
|
|
1169
|
+
return (TypeGuard.TArray(right) &&
|
|
1170
|
+
left.items !== undefined &&
|
|
1171
|
+
left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True));
|
|
1172
|
+
}
|
|
1173
|
+
function TTupleRight(left, right) {
|
|
1174
|
+
if (TypeGuard.TNever(left))
|
|
1175
|
+
return TypeExtendsResult.True;
|
|
1211
1176
|
if (TypeGuard.TUnknown(left))
|
|
1212
1177
|
return TypeExtendsResult.False;
|
|
1213
1178
|
if (TypeGuard.TAny(left))
|
|
1214
1179
|
return TypeExtendsResult.Union;
|
|
1215
|
-
if (TypeGuard.TNever(left))
|
|
1216
|
-
return TypeExtendsResult.True;
|
|
1217
1180
|
return TypeExtendsResult.False;
|
|
1218
1181
|
}
|
|
1219
|
-
function
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
function Tuple(left, right) {
|
|
1223
|
-
if (TypeGuard.TIntersect(right))
|
|
1224
|
-
return IntersectRight(left, right);
|
|
1225
|
-
if (TypeGuard.TUnion(right))
|
|
1226
|
-
return UnionRight(left, right);
|
|
1227
|
-
if (TypeGuard.TUnknown(right))
|
|
1228
|
-
return UnknownRight(left, right);
|
|
1229
|
-
if (TypeGuard.TAny(right))
|
|
1230
|
-
return AnyRight(left, right);
|
|
1182
|
+
function TTuple(left, right) {
|
|
1183
|
+
if (IsStructuralRight(right))
|
|
1184
|
+
return StructuralRight(left, right);
|
|
1231
1185
|
if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
|
|
1232
1186
|
return TypeExtendsResult.True;
|
|
1233
1187
|
if (TypeGuard.TArray(right) && IsArrayOfTuple(left, right))
|
|
1234
1188
|
return TypeExtendsResult.True;
|
|
1235
1189
|
if (!TypeGuard.TTuple(right))
|
|
1236
1190
|
return TypeExtendsResult.False;
|
|
1237
|
-
if ((left.items
|
|
1191
|
+
if ((ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)))
|
|
1238
1192
|
return TypeExtendsResult.False;
|
|
1239
|
-
if (left.items
|
|
1193
|
+
if (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items))
|
|
1240
1194
|
return TypeExtendsResult.True;
|
|
1241
1195
|
return left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1242
1196
|
}
|
|
1243
1197
|
// --------------------------------------------------------------------------
|
|
1244
1198
|
// Uint8Array
|
|
1245
1199
|
// --------------------------------------------------------------------------
|
|
1246
|
-
function
|
|
1247
|
-
if (
|
|
1248
|
-
return
|
|
1249
|
-
if (TypeGuard.TUnion(right))
|
|
1250
|
-
return UnionRight(left, right);
|
|
1251
|
-
if (TypeGuard.TUnknown(right))
|
|
1252
|
-
return UnknownRight(left, right);
|
|
1253
|
-
if (TypeGuard.TAny(right))
|
|
1254
|
-
return AnyRight(left, right);
|
|
1200
|
+
function TUint8Array(left, right) {
|
|
1201
|
+
if (IsStructuralRight(right))
|
|
1202
|
+
return StructuralRight(left, right);
|
|
1255
1203
|
if (TypeGuard.TObject(right))
|
|
1256
|
-
return
|
|
1204
|
+
return TObjectRight(left, right);
|
|
1257
1205
|
if (TypeGuard.TRecord(right))
|
|
1258
|
-
return
|
|
1206
|
+
return TRecordRight(left, right);
|
|
1259
1207
|
return TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1260
1208
|
}
|
|
1261
1209
|
// --------------------------------------------------------------------------
|
|
1262
1210
|
// Undefined
|
|
1263
1211
|
// --------------------------------------------------------------------------
|
|
1264
|
-
function
|
|
1265
|
-
if (
|
|
1266
|
-
return
|
|
1267
|
-
if (TypeGuard.TUnion(right))
|
|
1268
|
-
return UnionRight(left, right);
|
|
1269
|
-
if (TypeGuard.TNever(right))
|
|
1270
|
-
return NeverRight(left, right);
|
|
1271
|
-
if (TypeGuard.TUnknown(right))
|
|
1272
|
-
return UnknownRight(left, right);
|
|
1273
|
-
if (TypeGuard.TAny(right))
|
|
1274
|
-
return AnyRight(left, right);
|
|
1212
|
+
function TUndefined(left, right) {
|
|
1213
|
+
if (IsStructuralRight(right))
|
|
1214
|
+
return StructuralRight(left, right);
|
|
1275
1215
|
if (TypeGuard.TObject(right))
|
|
1276
|
-
return
|
|
1216
|
+
return TObjectRight(left, right);
|
|
1277
1217
|
if (TypeGuard.TRecord(right))
|
|
1278
|
-
return
|
|
1218
|
+
return TRecordRight(left, right);
|
|
1279
1219
|
if (TypeGuard.TVoid(right))
|
|
1280
1220
|
return VoidRight(left, right);
|
|
1281
1221
|
return TypeGuard.TUndefined(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
@@ -1283,39 +1223,41 @@ var TypeExtends;
|
|
|
1283
1223
|
// --------------------------------------------------------------------------
|
|
1284
1224
|
// Union
|
|
1285
1225
|
// --------------------------------------------------------------------------
|
|
1286
|
-
function
|
|
1226
|
+
function TUnionRight(left, right) {
|
|
1287
1227
|
return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1288
1228
|
}
|
|
1289
|
-
function
|
|
1229
|
+
function TUnion(left, right) {
|
|
1290
1230
|
return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1291
1231
|
}
|
|
1292
1232
|
// --------------------------------------------------------------------------
|
|
1293
1233
|
// Unknown
|
|
1294
1234
|
// --------------------------------------------------------------------------
|
|
1295
|
-
function
|
|
1235
|
+
function TUnknownRight(left, right) {
|
|
1296
1236
|
return TypeExtendsResult.True;
|
|
1297
1237
|
}
|
|
1298
|
-
function
|
|
1238
|
+
function TUnknown(left, right) {
|
|
1239
|
+
if (TypeGuard.TNever(right))
|
|
1240
|
+
return TNeverRight(left, right);
|
|
1299
1241
|
if (TypeGuard.TIntersect(right))
|
|
1300
|
-
return
|
|
1242
|
+
return TIntersectRight(left, right);
|
|
1301
1243
|
if (TypeGuard.TUnion(right))
|
|
1302
|
-
return
|
|
1244
|
+
return TUnionRight(left, right);
|
|
1303
1245
|
if (TypeGuard.TAny(right))
|
|
1304
|
-
return
|
|
1246
|
+
return TAnyRight(left, right);
|
|
1305
1247
|
if (TypeGuard.TString(right))
|
|
1306
|
-
return
|
|
1248
|
+
return TStringRight(left, right);
|
|
1307
1249
|
if (TypeGuard.TNumber(right))
|
|
1308
|
-
return
|
|
1250
|
+
return TNumberRight(left, right);
|
|
1309
1251
|
if (TypeGuard.TInteger(right))
|
|
1310
|
-
return
|
|
1252
|
+
return TIntegerRight(left, right);
|
|
1311
1253
|
if (TypeGuard.TBoolean(right))
|
|
1312
|
-
return
|
|
1254
|
+
return TBooleanRight(left, right);
|
|
1313
1255
|
if (TypeGuard.TArray(right))
|
|
1314
|
-
return
|
|
1256
|
+
return TArrayRight(left, right);
|
|
1315
1257
|
if (TypeGuard.TTuple(right))
|
|
1316
|
-
return
|
|
1258
|
+
return TTupleRight(left, right);
|
|
1317
1259
|
if (TypeGuard.TObject(right))
|
|
1318
|
-
return
|
|
1260
|
+
return TObjectRight(left, right);
|
|
1319
1261
|
return TypeGuard.TUnknown(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1320
1262
|
}
|
|
1321
1263
|
// --------------------------------------------------------------------------
|
|
@@ -1326,74 +1268,78 @@ var TypeExtends;
|
|
|
1326
1268
|
return TypeExtendsResult.True;
|
|
1327
1269
|
return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1328
1270
|
}
|
|
1329
|
-
function
|
|
1271
|
+
function TVoid(left, right) {
|
|
1330
1272
|
if (TypeGuard.TIntersect(right))
|
|
1331
|
-
return
|
|
1273
|
+
return TIntersectRight(left, right);
|
|
1332
1274
|
if (TypeGuard.TUnion(right))
|
|
1333
|
-
return
|
|
1275
|
+
return TUnionRight(left, right);
|
|
1334
1276
|
if (TypeGuard.TUnknown(right))
|
|
1335
|
-
return
|
|
1277
|
+
return TUnknownRight(left, right);
|
|
1336
1278
|
if (TypeGuard.TAny(right))
|
|
1337
|
-
return
|
|
1279
|
+
return TAnyRight(left, right);
|
|
1338
1280
|
if (TypeGuard.TObject(right))
|
|
1339
|
-
return
|
|
1281
|
+
return TObjectRight(left, right);
|
|
1340
1282
|
return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1341
1283
|
}
|
|
1342
1284
|
function Visit(left, right) {
|
|
1343
1285
|
// Resolvable Types
|
|
1344
1286
|
if (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right))
|
|
1345
|
-
return
|
|
1287
|
+
return TTemplateLiteral(left, right);
|
|
1346
1288
|
if (TypeGuard.TNot(left) || TypeGuard.TNot(right))
|
|
1347
|
-
return
|
|
1289
|
+
return TNot(left, right);
|
|
1348
1290
|
// Standard Types
|
|
1349
1291
|
if (TypeGuard.TAny(left))
|
|
1350
|
-
return
|
|
1292
|
+
return TAny(left, right);
|
|
1351
1293
|
if (TypeGuard.TArray(left))
|
|
1352
|
-
return
|
|
1294
|
+
return TArray(left, right);
|
|
1353
1295
|
if (TypeGuard.TBigInt(left))
|
|
1354
|
-
return
|
|
1296
|
+
return TBigInt(left, right);
|
|
1355
1297
|
if (TypeGuard.TBoolean(left))
|
|
1356
|
-
return
|
|
1298
|
+
return TBoolean(left, right);
|
|
1299
|
+
if (TypeGuard.TAsyncIterator(left))
|
|
1300
|
+
return TAsyncIterator(left, right);
|
|
1357
1301
|
if (TypeGuard.TConstructor(left))
|
|
1358
|
-
return
|
|
1302
|
+
return TConstructor(left, right);
|
|
1359
1303
|
if (TypeGuard.TDate(left))
|
|
1360
|
-
return
|
|
1304
|
+
return TDate(left, right);
|
|
1361
1305
|
if (TypeGuard.TFunction(left))
|
|
1362
|
-
return
|
|
1306
|
+
return TFunction(left, right);
|
|
1363
1307
|
if (TypeGuard.TInteger(left))
|
|
1364
|
-
return
|
|
1308
|
+
return TInteger(left, right);
|
|
1365
1309
|
if (TypeGuard.TIntersect(left))
|
|
1366
|
-
return
|
|
1310
|
+
return TIntersect(left, right);
|
|
1311
|
+
if (TypeGuard.TIterator(left))
|
|
1312
|
+
return TIterator(left, right);
|
|
1367
1313
|
if (TypeGuard.TLiteral(left))
|
|
1368
|
-
return
|
|
1314
|
+
return TLiteral(left, right);
|
|
1369
1315
|
if (TypeGuard.TNever(left))
|
|
1370
|
-
return
|
|
1316
|
+
return TNever(left, right);
|
|
1371
1317
|
if (TypeGuard.TNull(left))
|
|
1372
|
-
return
|
|
1318
|
+
return TNull(left, right);
|
|
1373
1319
|
if (TypeGuard.TNumber(left))
|
|
1374
|
-
return
|
|
1320
|
+
return TNumber(left, right);
|
|
1375
1321
|
if (TypeGuard.TObject(left))
|
|
1376
|
-
return
|
|
1322
|
+
return TObject(left, right);
|
|
1377
1323
|
if (TypeGuard.TRecord(left))
|
|
1378
|
-
return
|
|
1324
|
+
return TRecord(left, right);
|
|
1379
1325
|
if (TypeGuard.TString(left))
|
|
1380
|
-
return
|
|
1326
|
+
return TString(left, right);
|
|
1381
1327
|
if (TypeGuard.TSymbol(left))
|
|
1382
|
-
return
|
|
1328
|
+
return TSymbol(left, right);
|
|
1383
1329
|
if (TypeGuard.TTuple(left))
|
|
1384
|
-
return
|
|
1330
|
+
return TTuple(left, right);
|
|
1385
1331
|
if (TypeGuard.TPromise(left))
|
|
1386
|
-
return
|
|
1332
|
+
return TPromise(left, right);
|
|
1387
1333
|
if (TypeGuard.TUint8Array(left))
|
|
1388
|
-
return
|
|
1334
|
+
return TUint8Array(left, right);
|
|
1389
1335
|
if (TypeGuard.TUndefined(left))
|
|
1390
|
-
return
|
|
1336
|
+
return TUndefined(left, right);
|
|
1391
1337
|
if (TypeGuard.TUnion(left))
|
|
1392
|
-
return
|
|
1338
|
+
return TUnion(left, right);
|
|
1393
1339
|
if (TypeGuard.TUnknown(left))
|
|
1394
|
-
return
|
|
1340
|
+
return TUnknown(left, right);
|
|
1395
1341
|
if (TypeGuard.TVoid(left))
|
|
1396
|
-
return
|
|
1342
|
+
return TVoid(left, right);
|
|
1397
1343
|
throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
|
|
1398
1344
|
}
|
|
1399
1345
|
function Extends(left, right) {
|
|
@@ -1407,29 +1353,19 @@ var TypeExtends;
|
|
|
1407
1353
|
/** Specialized Clone for Types */
|
|
1408
1354
|
var TypeClone;
|
|
1409
1355
|
(function (TypeClone) {
|
|
1410
|
-
function
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
return globalThis.Array.isArray(value);
|
|
1356
|
+
function ObjectType(value) {
|
|
1357
|
+
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1358
|
+
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1359
|
+
return { ...clonedProperties, ...clonedSymbols };
|
|
1415
1360
|
}
|
|
1416
|
-
function
|
|
1361
|
+
function ArrayType(value) {
|
|
1417
1362
|
return value.map((value) => Visit(value));
|
|
1418
1363
|
}
|
|
1419
|
-
function Object(value) {
|
|
1420
|
-
const clonedProperties = globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => {
|
|
1421
|
-
return { ...acc, [key]: Visit(value[key]) };
|
|
1422
|
-
}, {});
|
|
1423
|
-
const clonedSymbols = globalThis.Object.getOwnPropertySymbols(value).reduce((acc, key) => {
|
|
1424
|
-
return { ...acc, [key]: Visit(value[key]) };
|
|
1425
|
-
}, {});
|
|
1426
|
-
return { ...clonedProperties, ...clonedSymbols };
|
|
1427
|
-
}
|
|
1428
1364
|
function Visit(value) {
|
|
1429
|
-
if (IsArray(value))
|
|
1430
|
-
return
|
|
1431
|
-
if (IsObject(value))
|
|
1432
|
-
return
|
|
1365
|
+
if (ValueGuard.IsArray(value))
|
|
1366
|
+
return ArrayType(value);
|
|
1367
|
+
if (ValueGuard.IsObject(value))
|
|
1368
|
+
return ObjectType(value);
|
|
1433
1369
|
return value;
|
|
1434
1370
|
}
|
|
1435
1371
|
/** Clones a type. */
|
|
@@ -1445,7 +1381,7 @@ var IndexedAccessor;
|
|
|
1445
1381
|
(function (IndexedAccessor) {
|
|
1446
1382
|
function OptionalUnwrap(schema) {
|
|
1447
1383
|
return schema.map((schema) => {
|
|
1448
|
-
const { [exports.
|
|
1384
|
+
const { [exports.Optional]: _, ...clone } = TypeClone.Clone(schema, {});
|
|
1449
1385
|
return clone;
|
|
1450
1386
|
});
|
|
1451
1387
|
}
|
|
@@ -1470,39 +1406,39 @@ var IndexedAccessor;
|
|
|
1470
1406
|
return ResolveUnion(schema);
|
|
1471
1407
|
return schema;
|
|
1472
1408
|
}
|
|
1473
|
-
function
|
|
1409
|
+
function TIntersect(schema, key) {
|
|
1474
1410
|
const resolved = schema.allOf.reduce((acc, schema) => {
|
|
1475
1411
|
const indexed = Visit(schema, key);
|
|
1476
1412
|
return indexed[exports.Kind] === 'Never' ? acc : [...acc, indexed];
|
|
1477
1413
|
}, []);
|
|
1478
1414
|
return ResolveOptional(exports.Type.Intersect(resolved));
|
|
1479
1415
|
}
|
|
1480
|
-
function
|
|
1416
|
+
function TUnion(schema, key) {
|
|
1481
1417
|
const resolved = schema.anyOf.map((schema) => Visit(schema, key));
|
|
1482
1418
|
return ResolveOptional(exports.Type.Union(resolved));
|
|
1483
1419
|
}
|
|
1484
|
-
function
|
|
1420
|
+
function TObject(schema, key) {
|
|
1485
1421
|
const property = schema.properties[key];
|
|
1486
|
-
return property
|
|
1422
|
+
return ValueGuard.IsUndefined(property) ? exports.Type.Never() : exports.Type.Union([property]);
|
|
1487
1423
|
}
|
|
1488
|
-
function
|
|
1424
|
+
function TTuple(schema, key) {
|
|
1489
1425
|
const items = schema.items;
|
|
1490
|
-
if (items
|
|
1426
|
+
if (ValueGuard.IsUndefined(items))
|
|
1491
1427
|
return exports.Type.Never();
|
|
1492
1428
|
const element = items[key]; //
|
|
1493
|
-
if (element
|
|
1429
|
+
if (ValueGuard.IsUndefined(element))
|
|
1494
1430
|
return exports.Type.Never();
|
|
1495
1431
|
return element;
|
|
1496
1432
|
}
|
|
1497
1433
|
function Visit(schema, key) {
|
|
1498
1434
|
if (schema[exports.Kind] === 'Intersect')
|
|
1499
|
-
return
|
|
1435
|
+
return TIntersect(schema, key);
|
|
1500
1436
|
if (schema[exports.Kind] === 'Union')
|
|
1501
|
-
return
|
|
1437
|
+
return TUnion(schema, key);
|
|
1502
1438
|
if (schema[exports.Kind] === 'Object')
|
|
1503
|
-
return
|
|
1439
|
+
return TObject(schema, key);
|
|
1504
1440
|
if (schema[exports.Kind] === 'Tuple')
|
|
1505
|
-
return
|
|
1441
|
+
return TTuple(schema, key);
|
|
1506
1442
|
return exports.Type.Never();
|
|
1507
1443
|
}
|
|
1508
1444
|
function Resolve(schema, keys, options = {}) {
|
|
@@ -1516,15 +1452,15 @@ var IndexedAccessor;
|
|
|
1516
1452
|
// --------------------------------------------------------------------------
|
|
1517
1453
|
var ObjectMap;
|
|
1518
1454
|
(function (ObjectMap) {
|
|
1519
|
-
function
|
|
1455
|
+
function TIntersect(schema, callback) {
|
|
1520
1456
|
// prettier-ignore
|
|
1521
1457
|
return exports.Type.Intersect(schema.allOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1522
1458
|
}
|
|
1523
|
-
function
|
|
1459
|
+
function TUnion(schema, callback) {
|
|
1524
1460
|
// prettier-ignore
|
|
1525
1461
|
return exports.Type.Union(schema.anyOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1526
1462
|
}
|
|
1527
|
-
function
|
|
1463
|
+
function TObject(schema, callback) {
|
|
1528
1464
|
return callback(schema);
|
|
1529
1465
|
}
|
|
1530
1466
|
function Visit(schema, callback) {
|
|
@@ -1533,11 +1469,11 @@ var ObjectMap;
|
|
|
1533
1469
|
// case of TObject where unregistered property kinds cause the TObject check to fail. As mapping is only
|
|
1534
1470
|
// used for composition, we use explicit checks instead.
|
|
1535
1471
|
if (schema[exports.Kind] === 'Intersect')
|
|
1536
|
-
return
|
|
1472
|
+
return TIntersect(schema, callback);
|
|
1537
1473
|
if (schema[exports.Kind] === 'Union')
|
|
1538
|
-
return
|
|
1474
|
+
return TUnion(schema, callback);
|
|
1539
1475
|
if (schema[exports.Kind] === 'Object')
|
|
1540
|
-
return
|
|
1476
|
+
return TObject(schema, callback);
|
|
1541
1477
|
return schema;
|
|
1542
1478
|
}
|
|
1543
1479
|
function Map(schema, callback, options) {
|
|
@@ -1550,28 +1486,28 @@ var KeyResolver;
|
|
|
1550
1486
|
function UnwrapPattern(key) {
|
|
1551
1487
|
return key[0] === '^' && key[key.length - 1] === '$' ? key.slice(1, key.length - 1) : key;
|
|
1552
1488
|
}
|
|
1553
|
-
function
|
|
1489
|
+
function TIntersect(schema, options) {
|
|
1554
1490
|
return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, options)], []);
|
|
1555
1491
|
}
|
|
1556
|
-
function
|
|
1492
|
+
function TUnion(schema, options) {
|
|
1557
1493
|
const sets = schema.anyOf.map((inner) => Visit(inner, options));
|
|
1558
1494
|
return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
|
|
1559
1495
|
}
|
|
1560
|
-
function
|
|
1561
|
-
return
|
|
1496
|
+
function TObject(schema, options) {
|
|
1497
|
+
return Object.getOwnPropertyNames(schema.properties);
|
|
1562
1498
|
}
|
|
1563
|
-
function
|
|
1564
|
-
return options.includePatterns ?
|
|
1499
|
+
function TRecord(schema, options) {
|
|
1500
|
+
return options.includePatterns ? Object.getOwnPropertyNames(schema.patternProperties) : [];
|
|
1565
1501
|
}
|
|
1566
1502
|
function Visit(schema, options) {
|
|
1567
1503
|
if (TypeGuard.TIntersect(schema))
|
|
1568
|
-
return
|
|
1504
|
+
return TIntersect(schema, options);
|
|
1569
1505
|
if (TypeGuard.TUnion(schema))
|
|
1570
|
-
return
|
|
1506
|
+
return TUnion(schema, options);
|
|
1571
1507
|
if (TypeGuard.TObject(schema))
|
|
1572
|
-
return
|
|
1508
|
+
return TObject(schema, options);
|
|
1573
1509
|
if (TypeGuard.TRecord(schema))
|
|
1574
|
-
return
|
|
1510
|
+
return TRecord(schema, options);
|
|
1575
1511
|
return [];
|
|
1576
1512
|
}
|
|
1577
1513
|
/** Resolves an array of keys in this schema */
|
|
@@ -1594,7 +1530,7 @@ var KeyArrayResolver;
|
|
|
1594
1530
|
(function (KeyArrayResolver) {
|
|
1595
1531
|
/** Resolves an array of string[] keys from the given schema or array type. */
|
|
1596
1532
|
function Resolve(schema) {
|
|
1597
|
-
if (
|
|
1533
|
+
if (Array.isArray(schema))
|
|
1598
1534
|
return schema;
|
|
1599
1535
|
if (TypeGuard.TUnionLiteral(schema))
|
|
1600
1536
|
return schema.anyOf.map((schema) => schema.const.toString());
|
|
@@ -1615,10 +1551,10 @@ var KeyArrayResolver;
|
|
|
1615
1551
|
// --------------------------------------------------------------------------
|
|
1616
1552
|
var UnionResolver;
|
|
1617
1553
|
(function (UnionResolver) {
|
|
1618
|
-
function*
|
|
1554
|
+
function* TUnion(union) {
|
|
1619
1555
|
for (const schema of union.anyOf) {
|
|
1620
1556
|
if (schema[exports.Kind] === 'Union') {
|
|
1621
|
-
yield*
|
|
1557
|
+
yield* TUnion(schema);
|
|
1622
1558
|
}
|
|
1623
1559
|
else {
|
|
1624
1560
|
yield schema;
|
|
@@ -1627,7 +1563,7 @@ var UnionResolver;
|
|
|
1627
1563
|
}
|
|
1628
1564
|
/** Returns a resolved union with interior unions flattened */
|
|
1629
1565
|
function Resolve(union) {
|
|
1630
|
-
return exports.Type.Union([...
|
|
1566
|
+
return exports.Type.Union([...TUnion(union)], { ...union });
|
|
1631
1567
|
}
|
|
1632
1568
|
UnionResolver.Resolve = Resolve;
|
|
1633
1569
|
})(UnionResolver || (exports.UnionResolver = UnionResolver = {}));
|
|
@@ -1641,12 +1577,10 @@ var TemplateLiteralPattern;
|
|
|
1641
1577
|
}
|
|
1642
1578
|
function Visit(schema, acc) {
|
|
1643
1579
|
if (TypeGuard.TTemplateLiteral(schema)) {
|
|
1644
|
-
|
|
1645
|
-
return pattern;
|
|
1580
|
+
return schema.pattern.slice(1, schema.pattern.length - 1);
|
|
1646
1581
|
}
|
|
1647
1582
|
else if (TypeGuard.TUnion(schema)) {
|
|
1648
|
-
|
|
1649
|
-
return `(${tokens})`;
|
|
1583
|
+
return `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})`;
|
|
1650
1584
|
}
|
|
1651
1585
|
else if (TypeGuard.TNumber(schema)) {
|
|
1652
1586
|
return `${acc}${exports.PatternNumber}`;
|
|
@@ -1973,6 +1907,11 @@ class TypeBuilder {
|
|
|
1973
1907
|
Create(schema) {
|
|
1974
1908
|
return schema;
|
|
1975
1909
|
}
|
|
1910
|
+
/** `[Utility]` Discards a property key from the given schema */
|
|
1911
|
+
Discard(schema, key) {
|
|
1912
|
+
const { [key]: _, ...rest } = schema;
|
|
1913
|
+
return rest;
|
|
1914
|
+
}
|
|
1976
1915
|
/** `[Standard]` Omits compositing symbols from this schema */
|
|
1977
1916
|
Strict(schema) {
|
|
1978
1917
|
return JSON.parse(JSON.stringify(schema));
|
|
@@ -1986,17 +1925,17 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1986
1925
|
// ------------------------------------------------------------------------
|
|
1987
1926
|
// Modifiers
|
|
1988
1927
|
// ------------------------------------------------------------------------
|
|
1989
|
-
/** `[
|
|
1990
|
-
Optional(schema) {
|
|
1991
|
-
return { [exports.Modifier]: 'Optional', ...TypeClone.Clone(schema, {}) };
|
|
1992
|
-
}
|
|
1993
|
-
/** `[Modifier]` Creates a ReadonlyOptional property */
|
|
1928
|
+
/** `[Standard]` Creates a Readonly and Optional property */
|
|
1994
1929
|
ReadonlyOptional(schema) {
|
|
1995
|
-
return
|
|
1930
|
+
return this.Readonly(this.Optional(schema));
|
|
1996
1931
|
}
|
|
1997
|
-
/** `[
|
|
1932
|
+
/** `[Standard]` Creates a Readonly property */
|
|
1998
1933
|
Readonly(schema) {
|
|
1999
|
-
return { [exports.
|
|
1934
|
+
return { ...TypeClone.Clone(schema, {}), [exports.Readonly]: 'Readonly' };
|
|
1935
|
+
}
|
|
1936
|
+
/** `[Standard]` Creates an Optional property */
|
|
1937
|
+
Optional(schema) {
|
|
1938
|
+
return { ...TypeClone.Clone(schema, {}), [exports.Optional]: 'Optional' };
|
|
2000
1939
|
}
|
|
2001
1940
|
// ------------------------------------------------------------------------
|
|
2002
1941
|
// Types
|
|
@@ -2023,8 +1962,8 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2023
1962
|
/** `[Standard]` Creates a Enum type */
|
|
2024
1963
|
Enum(item, options = {}) {
|
|
2025
1964
|
// prettier-ignore
|
|
2026
|
-
const values =
|
|
2027
|
-
const anyOf = values.map((value) => (
|
|
1965
|
+
const values = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
|
|
1966
|
+
const anyOf = values.map((value) => (ValueGuard.IsString(value) ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
|
|
2028
1967
|
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
|
|
2029
1968
|
}
|
|
2030
1969
|
/** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
|
|
@@ -2072,7 +2011,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2072
2011
|
return TypeClone.Clone(schema.items, options);
|
|
2073
2012
|
}
|
|
2074
2013
|
else if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
|
|
2075
|
-
const items = schema.items
|
|
2014
|
+
const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
|
|
2076
2015
|
const cloned = items.map((schema) => TypeClone.Clone(schema, {}));
|
|
2077
2016
|
return this.Union(cloned, options);
|
|
2078
2017
|
}
|
|
@@ -2112,7 +2051,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2112
2051
|
throw Error('StandardTypeBuilder: Unable to resolve key type from Record key pattern');
|
|
2113
2052
|
}
|
|
2114
2053
|
else if (TypeGuard.TTuple(schema)) {
|
|
2115
|
-
const items = schema.items
|
|
2054
|
+
const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
|
|
2116
2055
|
const literals = items.map((_, index) => exports.Type.Literal(index));
|
|
2117
2056
|
return this.Union(literals, options);
|
|
2118
2057
|
}
|
|
@@ -2149,8 +2088,8 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2149
2088
|
}
|
|
2150
2089
|
/** `[Standard]` Creates an Object type */
|
|
2151
2090
|
Object(properties, options = {}) {
|
|
2152
|
-
const propertyKeys =
|
|
2153
|
-
const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key])
|
|
2091
|
+
const propertyKeys = Object.getOwnPropertyNames(properties);
|
|
2092
|
+
const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]));
|
|
2154
2093
|
const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
|
|
2155
2094
|
const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties, {}) } : {};
|
|
2156
2095
|
const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key], {}) }), {});
|
|
@@ -2170,7 +2109,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2170
2109
|
if (schema.required.length === 0)
|
|
2171
2110
|
delete schema.required;
|
|
2172
2111
|
}
|
|
2173
|
-
for (const key of
|
|
2112
|
+
for (const key of Object.getOwnPropertyNames(schema.properties)) {
|
|
2174
2113
|
if (keys.includes(key))
|
|
2175
2114
|
delete schema.properties[key];
|
|
2176
2115
|
}
|
|
@@ -2179,28 +2118,12 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2179
2118
|
}
|
|
2180
2119
|
/** `[Standard]` Creates a mapped type where all properties are Optional */
|
|
2181
2120
|
Partial(schema, options = {}) {
|
|
2182
|
-
function Apply(schema) {
|
|
2183
|
-
// prettier-ignore
|
|
2184
|
-
switch (schema[exports.Modifier]) {
|
|
2185
|
-
case 'ReadonlyOptional':
|
|
2186
|
-
schema[exports.Modifier] = 'ReadonlyOptional';
|
|
2187
|
-
break;
|
|
2188
|
-
case 'Readonly':
|
|
2189
|
-
schema[exports.Modifier] = 'ReadonlyOptional';
|
|
2190
|
-
break;
|
|
2191
|
-
case 'Optional':
|
|
2192
|
-
schema[exports.Modifier] = 'Optional';
|
|
2193
|
-
break;
|
|
2194
|
-
default:
|
|
2195
|
-
schema[exports.Modifier] = 'Optional';
|
|
2196
|
-
break;
|
|
2197
|
-
}
|
|
2198
|
-
}
|
|
2199
2121
|
// prettier-ignore
|
|
2200
|
-
return ObjectMap.Map(
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2122
|
+
return ObjectMap.Map(schema, (object) => {
|
|
2123
|
+
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2124
|
+
return { ...acc, [key]: this.Optional(object.properties[key]) };
|
|
2125
|
+
}, {});
|
|
2126
|
+
return this.Object(properties, this.Discard(object, 'required') /* object used as options to retain other constraints */);
|
|
2204
2127
|
}, options);
|
|
2205
2128
|
}
|
|
2206
2129
|
Pick(schema, unresolved, options = {}) {
|
|
@@ -2212,7 +2135,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2212
2135
|
if (schema.required.length === 0)
|
|
2213
2136
|
delete schema.required;
|
|
2214
2137
|
}
|
|
2215
|
-
for (const key of
|
|
2138
|
+
for (const key of Object.getOwnPropertyNames(schema.properties)) {
|
|
2216
2139
|
if (!keys.includes(key))
|
|
2217
2140
|
delete schema.properties[key];
|
|
2218
2141
|
}
|
|
@@ -2235,21 +2158,20 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2235
2158
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2236
2159
|
}
|
|
2237
2160
|
else
|
|
2238
|
-
throw Error('
|
|
2161
|
+
throw Error('StandardTypeBuilder: Record key of type union contains non-literal types');
|
|
2239
2162
|
}
|
|
2240
2163
|
else if (TypeGuard.TLiteral(key)) {
|
|
2241
|
-
if (
|
|
2164
|
+
if (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const)) {
|
|
2242
2165
|
return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
|
|
2243
2166
|
}
|
|
2244
2167
|
else
|
|
2245
|
-
throw Error('
|
|
2168
|
+
throw Error('StandardTypeBuilder: Record key of type literal is not of type string or number');
|
|
2246
2169
|
}
|
|
2247
2170
|
else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
|
|
2248
|
-
|
|
2249
|
-
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2171
|
+
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [exports.PatternNumberExact]: TypeClone.Clone(schema, {}) } });
|
|
2250
2172
|
}
|
|
2251
2173
|
else if (TypeGuard.TString(key)) {
|
|
2252
|
-
const pattern = key.pattern
|
|
2174
|
+
const pattern = ValueGuard.IsUndefined(key.pattern) ? exports.PatternStringExact : key.pattern;
|
|
2253
2175
|
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2254
2176
|
}
|
|
2255
2177
|
else {
|
|
@@ -2258,48 +2180,34 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2258
2180
|
}
|
|
2259
2181
|
/** `[Standard]` Creates a Recursive type */
|
|
2260
2182
|
Recursive(callback, options = {}) {
|
|
2261
|
-
if (options.$id
|
|
2183
|
+
if (ValueGuard.IsUndefined(options.$id))
|
|
2262
2184
|
options.$id = `T${TypeOrdinal++}`;
|
|
2263
2185
|
const thisType = callback({ [exports.Kind]: 'This', $ref: `${options.$id}` });
|
|
2264
2186
|
thisType.$id = options.$id;
|
|
2265
2187
|
return this.Create({ ...options, [exports.Hint]: 'Recursive', ...thisType });
|
|
2266
2188
|
}
|
|
2267
|
-
/** `[Standard]` Creates a Ref type.
|
|
2268
|
-
Ref(
|
|
2269
|
-
if (
|
|
2189
|
+
/** `[Standard]` Creates a Ref type. */
|
|
2190
|
+
Ref(unresolved, options = {}) {
|
|
2191
|
+
if (ValueGuard.IsString(unresolved))
|
|
2192
|
+
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved });
|
|
2193
|
+
if (ValueGuard.IsUndefined(unresolved.$id))
|
|
2270
2194
|
throw Error('StandardTypeBuilder.Ref: Target type must specify an $id');
|
|
2271
|
-
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref:
|
|
2195
|
+
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved.$id });
|
|
2272
2196
|
}
|
|
2273
2197
|
/** `[Standard]` Creates a mapped type where all properties are Required */
|
|
2274
2198
|
Required(schema, options = {}) {
|
|
2275
|
-
function Apply(schema) {
|
|
2276
|
-
// prettier-ignore
|
|
2277
|
-
switch (schema[exports.Modifier]) {
|
|
2278
|
-
case 'ReadonlyOptional':
|
|
2279
|
-
schema[exports.Modifier] = 'Readonly';
|
|
2280
|
-
break;
|
|
2281
|
-
case 'Readonly':
|
|
2282
|
-
schema[exports.Modifier] = 'Readonly';
|
|
2283
|
-
break;
|
|
2284
|
-
case 'Optional':
|
|
2285
|
-
delete schema[exports.Modifier];
|
|
2286
|
-
break;
|
|
2287
|
-
default:
|
|
2288
|
-
delete schema[exports.Modifier];
|
|
2289
|
-
break;
|
|
2290
|
-
}
|
|
2291
|
-
}
|
|
2292
2199
|
// prettier-ignore
|
|
2293
|
-
return ObjectMap.Map(
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2200
|
+
return ObjectMap.Map(schema, (object) => {
|
|
2201
|
+
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2202
|
+
return { ...acc, [key]: this.Discard(object.properties[key], exports.Optional) };
|
|
2203
|
+
}, {});
|
|
2204
|
+
return this.Object(properties, object /* object used as options to retain other constraints */);
|
|
2297
2205
|
}, options);
|
|
2298
2206
|
}
|
|
2299
2207
|
/** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */
|
|
2300
2208
|
Rest(schema) {
|
|
2301
2209
|
if (TypeGuard.TTuple(schema)) {
|
|
2302
|
-
if (schema.items
|
|
2210
|
+
if (ValueGuard.IsUndefined(schema.items))
|
|
2303
2211
|
return [];
|
|
2304
2212
|
return schema.items.map((schema) => TypeClone.Clone(schema, {}));
|
|
2305
2213
|
}
|
|
@@ -2314,7 +2222,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2314
2222
|
/** `[Standard]` Creates a template literal type */
|
|
2315
2223
|
TemplateLiteral(unresolved, options = {}) {
|
|
2316
2224
|
// prettier-ignore
|
|
2317
|
-
const pattern = (
|
|
2225
|
+
const pattern = (ValueGuard.IsString(unresolved))
|
|
2318
2226
|
? TemplateLiteralPattern.Create(TemplateLiteralDslParser.Parse(unresolved))
|
|
2319
2227
|
: TemplateLiteralPattern.Create(unresolved);
|
|
2320
2228
|
return this.Create({ ...options, [exports.Kind]: 'TemplateLiteral', type: 'string', pattern });
|
|
@@ -2357,9 +2265,13 @@ exports.StandardTypeBuilder = StandardTypeBuilder;
|
|
|
2357
2265
|
// ExtendedTypeBuilder
|
|
2358
2266
|
// --------------------------------------------------------------------------
|
|
2359
2267
|
class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
2268
|
+
/** `[Extended]` Creates a AsyncIterator type */
|
|
2269
|
+
AsyncIterator(items, options = {}) {
|
|
2270
|
+
return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items, {}) });
|
|
2271
|
+
}
|
|
2360
2272
|
/** `[Extended]` Creates a BigInt type */
|
|
2361
2273
|
BigInt(options = {}) {
|
|
2362
|
-
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: '
|
|
2274
|
+
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
|
|
2363
2275
|
}
|
|
2364
2276
|
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
|
2365
2277
|
ConstructorParameters(schema, options = {}) {
|
|
@@ -2369,33 +2281,44 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2369
2281
|
Constructor(parameters, returns, options) {
|
|
2370
2282
|
const clonedReturns = TypeClone.Clone(returns, {});
|
|
2371
2283
|
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2372
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: '
|
|
2284
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2373
2285
|
}
|
|
2374
2286
|
/** `[Extended]` Creates a Date type */
|
|
2375
2287
|
Date(options = {}) {
|
|
2376
|
-
return this.Create({ ...options, [exports.Kind]: 'Date', type: '
|
|
2288
|
+
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'Date' });
|
|
2377
2289
|
}
|
|
2378
2290
|
/** `[Extended]` Creates a Function type */
|
|
2379
2291
|
Function(parameters, returns, options) {
|
|
2380
2292
|
const clonedReturns = TypeClone.Clone(returns, {});
|
|
2381
2293
|
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2382
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: '
|
|
2294
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'function', parameters: clonedParameters, returns: clonedReturns });
|
|
2383
2295
|
}
|
|
2384
2296
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
2385
2297
|
InstanceType(schema, options = {}) {
|
|
2386
2298
|
return TypeClone.Clone(schema.returns, options);
|
|
2387
2299
|
}
|
|
2300
|
+
/** `[Extended]` Creates an Iterator type */
|
|
2301
|
+
Iterator(items, options = {}) {
|
|
2302
|
+
return this.Create({ ...options, [exports.Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Clone(items, {}) });
|
|
2303
|
+
}
|
|
2388
2304
|
/** `[Extended]` Extracts the Parameters from the given Function type */
|
|
2389
2305
|
Parameters(schema, options = {}) {
|
|
2390
2306
|
return this.Tuple(schema.parameters, { ...options });
|
|
2391
2307
|
}
|
|
2392
2308
|
/** `[Extended]` Creates a Promise type */
|
|
2393
2309
|
Promise(item, options = {}) {
|
|
2394
|
-
return this.Create({ ...options, [exports.Kind]: 'Promise', type: '
|
|
2310
|
+
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Clone(item, {}) });
|
|
2311
|
+
}
|
|
2312
|
+
/** `[Extended]` Creates a String pattern type from Regular Expression */
|
|
2313
|
+
RegExp(unresolved, options = {}) {
|
|
2314
|
+
const pattern = ValueGuard.IsString(unresolved) ? unresolved : unresolved.source;
|
|
2315
|
+
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern });
|
|
2395
2316
|
}
|
|
2396
|
-
/**
|
|
2317
|
+
/**
|
|
2318
|
+
* @deprecated Use `Type.RegExp`
|
|
2319
|
+
*/
|
|
2397
2320
|
RegEx(regex, options = {}) {
|
|
2398
|
-
return this.
|
|
2321
|
+
return this.RegExp(regex, options);
|
|
2399
2322
|
}
|
|
2400
2323
|
/** `[Extended]` Extracts the ReturnType from the given Function */
|
|
2401
2324
|
ReturnType(schema, options = {}) {
|
|
@@ -2403,23 +2326,23 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2403
2326
|
}
|
|
2404
2327
|
/** `[Extended]` Creates a Symbol type */
|
|
2405
2328
|
Symbol(options) {
|
|
2406
|
-
return this.Create({ ...options, [exports.Kind]: 'Symbol', type: '
|
|
2329
|
+
return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'symbol' });
|
|
2407
2330
|
}
|
|
2408
2331
|
/** `[Extended]` Creates a Undefined type */
|
|
2409
2332
|
Undefined(options = {}) {
|
|
2410
|
-
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: '
|
|
2333
|
+
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'undefined' });
|
|
2411
2334
|
}
|
|
2412
2335
|
/** `[Extended]` Creates a Uint8Array type */
|
|
2413
2336
|
Uint8Array(options = {}) {
|
|
2414
|
-
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: '
|
|
2337
|
+
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'Uint8Array' });
|
|
2415
2338
|
}
|
|
2416
2339
|
/** `[Extended]` Creates a Void type */
|
|
2417
2340
|
Void(options = {}) {
|
|
2418
|
-
return this.Create({ ...options, [exports.Kind]: 'Void', type: '
|
|
2341
|
+
return this.Create({ ...options, [exports.Kind]: 'Void', type: 'void' });
|
|
2419
2342
|
}
|
|
2420
2343
|
}
|
|
2421
2344
|
exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
|
|
2422
|
-
/** JSON Schema
|
|
2345
|
+
/** JSON Schema Type Builder with Static Resolution for TypeScript */
|
|
2423
2346
|
exports.StandardType = new StandardTypeBuilder();
|
|
2424
|
-
/** JSON Schema
|
|
2347
|
+
/** JSON Schema Type Builder with Static Resolution for TypeScript */
|
|
2425
2348
|
exports.Type = new ExtendedTypeBuilder();
|