@sinclair/typebox 0.24.51 → 0.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.d.ts +1 -1
- package/compiler/compiler.js +15 -2
- package/conditional/structural.js +23 -0
- package/errors/errors.d.ts +43 -38
- package/errors/errors.js +62 -38
- package/guard/guard.d.ts +2 -0
- package/guard/guard.js +96 -14
- package/package.json +1 -1
- package/readme.md +245 -161
- package/typebox.d.ts +66 -47
- package/typebox.js +47 -42
- package/value/cast.js +6 -1
- package/value/check.js +21 -1
- package/value/clone.js +10 -4
- package/value/create.js +13 -0
- package/value/delta.js +8 -0
- package/value/equal.js +6 -0
- package/value/is.d.ts +1 -0
- package/value/is.js +5 -1
package/compiler/compiler.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
11
11
|
Code(): string;
|
|
12
12
|
/** Returns an iterator for each error in this value. */
|
|
13
13
|
Errors(value: unknown): IterableIterator<ValueError>;
|
|
14
|
-
/** Returns true if the value matches the
|
|
14
|
+
/** Returns true if the value matches the compiled type. */
|
|
15
15
|
Check(value: unknown): value is Types.Static<T>;
|
|
16
16
|
}
|
|
17
17
|
export declare namespace Property {
|
package/compiler/compiler.js
CHANGED
|
@@ -50,7 +50,7 @@ class TypeCheck {
|
|
|
50
50
|
Errors(value) {
|
|
51
51
|
return index_1.ValueErrors.Errors(this.schema, this.references, value);
|
|
52
52
|
}
|
|
53
|
-
/** Returns true if the value matches the
|
|
53
|
+
/** Returns true if the value matches the compiled type. */
|
|
54
54
|
Check(value) {
|
|
55
55
|
return this.checkFunc(value);
|
|
56
56
|
}
|
|
@@ -127,6 +127,17 @@ var TypeCompiler;
|
|
|
127
127
|
function* Constructor(schema, value) {
|
|
128
128
|
yield* Visit(schema.returns, `${value}.prototype`);
|
|
129
129
|
}
|
|
130
|
+
function* Date(schema, value) {
|
|
131
|
+
yield `(${value} instanceof Date)`;
|
|
132
|
+
if (schema.exclusiveMinimumTimestamp !== undefined)
|
|
133
|
+
yield `(${value}.getTime() > ${schema.exclusiveMinimumTimestamp})`;
|
|
134
|
+
if (schema.exclusiveMaximumTimestamp !== undefined)
|
|
135
|
+
yield `(${value}.getTime() < ${schema.exclusiveMaximumTimestamp})`;
|
|
136
|
+
if (schema.minimumTimestamp !== undefined)
|
|
137
|
+
yield `(${value}.getTime() >= ${schema.minimumTimestamp})`;
|
|
138
|
+
if (schema.maximumTimestamp !== undefined)
|
|
139
|
+
yield `(${value}.getTime() <= ${schema.maximumTimestamp})`;
|
|
140
|
+
}
|
|
130
141
|
function* Function(schema, value) {
|
|
131
142
|
yield `(typeof ${value} === 'function')`;
|
|
132
143
|
}
|
|
@@ -211,7 +222,7 @@ var TypeCompiler;
|
|
|
211
222
|
yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
|
|
212
223
|
}
|
|
213
224
|
function* Record(schema, value) {
|
|
214
|
-
yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
|
|
225
|
+
yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}) && !(${value} instanceof Date))`;
|
|
215
226
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
216
227
|
const local = PushLocal(`new RegExp(/${keyPattern}/)`);
|
|
217
228
|
yield `(Object.keys(${value}).every(key => ${local}.test(key)))`;
|
|
@@ -301,6 +312,8 @@ var TypeCompiler;
|
|
|
301
312
|
return yield* Boolean(anySchema, value);
|
|
302
313
|
case 'Constructor':
|
|
303
314
|
return yield* Constructor(anySchema, value);
|
|
315
|
+
case 'Date':
|
|
316
|
+
return yield* Date(anySchema, value);
|
|
304
317
|
case 'Function':
|
|
305
318
|
return yield* Function(anySchema, value);
|
|
306
319
|
case 'Integer':
|
|
@@ -204,6 +204,26 @@ var Structural;
|
|
|
204
204
|
return StructuralResult.True;
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
+
function Date(left, right) {
|
|
208
|
+
if (AnyOrUnknownRule(right)) {
|
|
209
|
+
return StructuralResult.True;
|
|
210
|
+
}
|
|
211
|
+
else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
|
|
212
|
+
return StructuralResult.True;
|
|
213
|
+
}
|
|
214
|
+
else if (guard_1.TypeGuard.TRecord(right)) {
|
|
215
|
+
return StructuralResult.False;
|
|
216
|
+
}
|
|
217
|
+
else if (guard_1.TypeGuard.TDate(right)) {
|
|
218
|
+
return StructuralResult.True;
|
|
219
|
+
}
|
|
220
|
+
else if (guard_1.TypeGuard.TUnion(right)) {
|
|
221
|
+
return UnionRightRule(left, right);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
return StructuralResult.False;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
207
227
|
function Function(left, right) {
|
|
208
228
|
if (AnyOrUnknownRule(right)) {
|
|
209
229
|
return StructuralResult.True;
|
|
@@ -592,6 +612,9 @@ var Structural;
|
|
|
592
612
|
else if (guard_1.TypeGuard.TConstructor(left)) {
|
|
593
613
|
return Constructor(left, resolvedRight);
|
|
594
614
|
}
|
|
615
|
+
else if (guard_1.TypeGuard.TDate(left)) {
|
|
616
|
+
return Date(left, resolvedRight);
|
|
617
|
+
}
|
|
595
618
|
else if (guard_1.TypeGuard.TFunction(left)) {
|
|
596
619
|
return Function(left, resolvedRight);
|
|
597
620
|
}
|
package/errors/errors.d.ts
CHANGED
|
@@ -5,44 +5,49 @@ export declare enum ValueErrorType {
|
|
|
5
5
|
ArrayMaxItems = 2,
|
|
6
6
|
ArrayUniqueItems = 3,
|
|
7
7
|
Boolean = 4,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
8
|
+
Date = 5,
|
|
9
|
+
DateExclusiveMinimumTimestamp = 6,
|
|
10
|
+
DateExclusiveMaximumTimestamp = 7,
|
|
11
|
+
DateMinimumTimestamp = 8,
|
|
12
|
+
DateMaximumTimestamp = 9,
|
|
13
|
+
Function = 10,
|
|
14
|
+
Integer = 11,
|
|
15
|
+
IntegerMultipleOf = 12,
|
|
16
|
+
IntegerExclusiveMinimum = 13,
|
|
17
|
+
IntegerExclusiveMaximum = 14,
|
|
18
|
+
IntegerMinimum = 15,
|
|
19
|
+
IntegerMaximum = 16,
|
|
20
|
+
Literal = 17,
|
|
21
|
+
Never = 18,
|
|
22
|
+
Null = 19,
|
|
23
|
+
Number = 20,
|
|
24
|
+
NumberMultipleOf = 21,
|
|
25
|
+
NumberExclusiveMinimum = 22,
|
|
26
|
+
NumberExclusiveMaximum = 23,
|
|
27
|
+
NumberMinumum = 24,
|
|
28
|
+
NumberMaximum = 25,
|
|
29
|
+
Object = 26,
|
|
30
|
+
ObjectMinProperties = 27,
|
|
31
|
+
ObjectMaxProperties = 28,
|
|
32
|
+
ObjectAdditionalProperties = 29,
|
|
33
|
+
ObjectRequiredProperties = 30,
|
|
34
|
+
Promise = 31,
|
|
35
|
+
RecordKeyNumeric = 32,
|
|
36
|
+
RecordKeyString = 33,
|
|
37
|
+
String = 34,
|
|
38
|
+
StringMinLength = 35,
|
|
39
|
+
StringMaxLength = 36,
|
|
40
|
+
StringPattern = 37,
|
|
41
|
+
StringFormatUnknown = 38,
|
|
42
|
+
StringFormat = 39,
|
|
43
|
+
TupleZeroLength = 40,
|
|
44
|
+
TupleLength = 41,
|
|
45
|
+
Undefined = 42,
|
|
46
|
+
Union = 43,
|
|
47
|
+
Uint8Array = 44,
|
|
48
|
+
Uint8ArrayMinByteLength = 45,
|
|
49
|
+
Uint8ArrayMaxByteLength = 46,
|
|
50
|
+
Void = 47
|
|
46
51
|
}
|
|
47
52
|
export interface ValueError {
|
|
48
53
|
type: ValueErrorType;
|
package/errors/errors.js
CHANGED
|
@@ -40,44 +40,49 @@ var ValueErrorType;
|
|
|
40
40
|
ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
|
|
41
41
|
ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
|
|
42
42
|
ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean";
|
|
43
|
-
ValueErrorType[ValueErrorType["
|
|
44
|
-
ValueErrorType[ValueErrorType["
|
|
45
|
-
ValueErrorType[ValueErrorType["
|
|
46
|
-
ValueErrorType[ValueErrorType["
|
|
47
|
-
ValueErrorType[ValueErrorType["
|
|
48
|
-
ValueErrorType[ValueErrorType["
|
|
49
|
-
ValueErrorType[ValueErrorType["
|
|
50
|
-
ValueErrorType[ValueErrorType["
|
|
51
|
-
ValueErrorType[ValueErrorType["
|
|
52
|
-
ValueErrorType[ValueErrorType["
|
|
53
|
-
ValueErrorType[ValueErrorType["
|
|
54
|
-
ValueErrorType[ValueErrorType["
|
|
55
|
-
ValueErrorType[ValueErrorType["
|
|
56
|
-
ValueErrorType[ValueErrorType["
|
|
57
|
-
ValueErrorType[ValueErrorType["
|
|
58
|
-
ValueErrorType[ValueErrorType["
|
|
59
|
-
ValueErrorType[ValueErrorType["
|
|
60
|
-
ValueErrorType[ValueErrorType["
|
|
61
|
-
ValueErrorType[ValueErrorType["
|
|
62
|
-
ValueErrorType[ValueErrorType["
|
|
63
|
-
ValueErrorType[ValueErrorType["
|
|
64
|
-
ValueErrorType[ValueErrorType["
|
|
65
|
-
ValueErrorType[ValueErrorType["
|
|
66
|
-
ValueErrorType[ValueErrorType["
|
|
67
|
-
ValueErrorType[ValueErrorType["
|
|
68
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
76
|
-
ValueErrorType[ValueErrorType["
|
|
77
|
-
ValueErrorType[ValueErrorType["
|
|
78
|
-
ValueErrorType[ValueErrorType["
|
|
79
|
-
ValueErrorType[ValueErrorType["
|
|
80
|
-
ValueErrorType[ValueErrorType["
|
|
43
|
+
ValueErrorType[ValueErrorType["Date"] = 5] = "Date";
|
|
44
|
+
ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 6] = "DateExclusiveMinimumTimestamp";
|
|
45
|
+
ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 7] = "DateExclusiveMaximumTimestamp";
|
|
46
|
+
ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 8] = "DateMinimumTimestamp";
|
|
47
|
+
ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 9] = "DateMaximumTimestamp";
|
|
48
|
+
ValueErrorType[ValueErrorType["Function"] = 10] = "Function";
|
|
49
|
+
ValueErrorType[ValueErrorType["Integer"] = 11] = "Integer";
|
|
50
|
+
ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 12] = "IntegerMultipleOf";
|
|
51
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 13] = "IntegerExclusiveMinimum";
|
|
52
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 14] = "IntegerExclusiveMaximum";
|
|
53
|
+
ValueErrorType[ValueErrorType["IntegerMinimum"] = 15] = "IntegerMinimum";
|
|
54
|
+
ValueErrorType[ValueErrorType["IntegerMaximum"] = 16] = "IntegerMaximum";
|
|
55
|
+
ValueErrorType[ValueErrorType["Literal"] = 17] = "Literal";
|
|
56
|
+
ValueErrorType[ValueErrorType["Never"] = 18] = "Never";
|
|
57
|
+
ValueErrorType[ValueErrorType["Null"] = 19] = "Null";
|
|
58
|
+
ValueErrorType[ValueErrorType["Number"] = 20] = "Number";
|
|
59
|
+
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 21] = "NumberMultipleOf";
|
|
60
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 22] = "NumberExclusiveMinimum";
|
|
61
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 23] = "NumberExclusiveMaximum";
|
|
62
|
+
ValueErrorType[ValueErrorType["NumberMinumum"] = 24] = "NumberMinumum";
|
|
63
|
+
ValueErrorType[ValueErrorType["NumberMaximum"] = 25] = "NumberMaximum";
|
|
64
|
+
ValueErrorType[ValueErrorType["Object"] = 26] = "Object";
|
|
65
|
+
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 27] = "ObjectMinProperties";
|
|
66
|
+
ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 28] = "ObjectMaxProperties";
|
|
67
|
+
ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 29] = "ObjectAdditionalProperties";
|
|
68
|
+
ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 30] = "ObjectRequiredProperties";
|
|
69
|
+
ValueErrorType[ValueErrorType["Promise"] = 31] = "Promise";
|
|
70
|
+
ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 32] = "RecordKeyNumeric";
|
|
71
|
+
ValueErrorType[ValueErrorType["RecordKeyString"] = 33] = "RecordKeyString";
|
|
72
|
+
ValueErrorType[ValueErrorType["String"] = 34] = "String";
|
|
73
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 35] = "StringMinLength";
|
|
74
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 36] = "StringMaxLength";
|
|
75
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 37] = "StringPattern";
|
|
76
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 38] = "StringFormatUnknown";
|
|
77
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 39] = "StringFormat";
|
|
78
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 40] = "TupleZeroLength";
|
|
79
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 41] = "TupleLength";
|
|
80
|
+
ValueErrorType[ValueErrorType["Undefined"] = 42] = "Undefined";
|
|
81
|
+
ValueErrorType[ValueErrorType["Union"] = 43] = "Union";
|
|
82
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 44] = "Uint8Array";
|
|
83
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 45] = "Uint8ArrayMinByteLength";
|
|
84
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 46] = "Uint8ArrayMaxByteLength";
|
|
85
|
+
ValueErrorType[ValueErrorType["Void"] = 47] = "Void";
|
|
81
86
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
82
87
|
// -------------------------------------------------------------------
|
|
83
88
|
// ValueErrors
|
|
@@ -117,6 +122,23 @@ var ValueErrors;
|
|
|
117
122
|
function* Constructor(schema, references, path, value) {
|
|
118
123
|
yield* Visit(schema.returns, references, path, value.prototype);
|
|
119
124
|
}
|
|
125
|
+
function* Date(schema, references, path, value) {
|
|
126
|
+
if (!(value instanceof globalThis.Date)) {
|
|
127
|
+
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
128
|
+
}
|
|
129
|
+
if (schema.exclusiveMinimumTimestamp && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
130
|
+
yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
|
|
131
|
+
}
|
|
132
|
+
if (schema.exclusiveMaximumTimestamp && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
133
|
+
yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
|
|
134
|
+
}
|
|
135
|
+
if (schema.minimumTimestamp && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
136
|
+
yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
|
|
137
|
+
}
|
|
138
|
+
if (schema.maximumTimestamp && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
139
|
+
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
120
142
|
function* Function(schema, references, path, value) {
|
|
121
143
|
if (!(typeof value === 'function')) {
|
|
122
144
|
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
@@ -351,6 +373,8 @@ var ValueErrors;
|
|
|
351
373
|
return yield* Boolean(anySchema, anyReferences, path, value);
|
|
352
374
|
case 'Constructor':
|
|
353
375
|
return yield* Constructor(anySchema, anyReferences, path, value);
|
|
376
|
+
case 'Date':
|
|
377
|
+
return yield* Date(anySchema, anyReferences, path, value);
|
|
354
378
|
case 'Function':
|
|
355
379
|
return yield* Function(anySchema, anyReferences, path, value);
|
|
356
380
|
case 'Integer':
|
package/guard/guard.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export declare namespace TypeGuard {
|
|
|
13
13
|
function TBoolean(schema: unknown): schema is Types.TBoolean;
|
|
14
14
|
/** Returns true if the given schema is TConstructor */
|
|
15
15
|
function TConstructor(schema: unknown): schema is Types.TConstructor;
|
|
16
|
+
/** Returns true if the given schema is TDate */
|
|
17
|
+
function TDate(schema: unknown): schema is Types.TDate;
|
|
16
18
|
/** Returns true if the given schema is TFunction */
|
|
17
19
|
function TFunction(schema: unknown): schema is Types.TFunction;
|
|
18
20
|
/** Returns true if the given schema is TInteger */
|
package/guard/guard.js
CHANGED
|
@@ -111,12 +111,23 @@ var TypeGuard;
|
|
|
111
111
|
TypeGuard.TArray = TArray;
|
|
112
112
|
/** Returns true if the given schema is TBoolean */
|
|
113
113
|
function TBoolean(schema) {
|
|
114
|
-
|
|
114
|
+
// prettier-ignore
|
|
115
|
+
return (IsObject(schema) &&
|
|
116
|
+
schema[Types.Kind] === 'Boolean' &&
|
|
117
|
+
schema.type === 'boolean' &&
|
|
118
|
+
IsOptionalString(schema.$id));
|
|
115
119
|
}
|
|
116
120
|
TypeGuard.TBoolean = TBoolean;
|
|
117
121
|
/** Returns true if the given schema is TConstructor */
|
|
118
122
|
function TConstructor(schema) {
|
|
119
|
-
|
|
123
|
+
// prettier-ignore
|
|
124
|
+
if (!(IsObject(schema) &&
|
|
125
|
+
schema[Types.Kind] === 'Constructor' &&
|
|
126
|
+
schema.type === 'object' &&
|
|
127
|
+
schema.instanceOf === 'Constructor' &&
|
|
128
|
+
IsOptionalString(schema.$id) &&
|
|
129
|
+
IsArray(schema.parameters) &&
|
|
130
|
+
TSchema(schema.returns))) {
|
|
120
131
|
return false;
|
|
121
132
|
}
|
|
122
133
|
for (const parameter of schema.parameters) {
|
|
@@ -126,9 +137,29 @@ var TypeGuard;
|
|
|
126
137
|
return true;
|
|
127
138
|
}
|
|
128
139
|
TypeGuard.TConstructor = TConstructor;
|
|
140
|
+
/** Returns true if the given schema is TDate */
|
|
141
|
+
function TDate(schema) {
|
|
142
|
+
return (IsObject(schema) &&
|
|
143
|
+
schema[Types.Kind] === 'Date' &&
|
|
144
|
+
schema.type === 'object' &&
|
|
145
|
+
schema.instanceOf === 'Date' &&
|
|
146
|
+
IsOptionalString(schema.$id) &&
|
|
147
|
+
IsOptionalNumber(schema.minimumTimestamp) &&
|
|
148
|
+
IsOptionalNumber(schema.maximumTimestamp) &&
|
|
149
|
+
IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
|
|
150
|
+
IsOptionalNumber(schema.exclusiveMaximumTimestamp));
|
|
151
|
+
}
|
|
152
|
+
TypeGuard.TDate = TDate;
|
|
129
153
|
/** Returns true if the given schema is TFunction */
|
|
130
154
|
function TFunction(schema) {
|
|
131
|
-
|
|
155
|
+
// prettier-ignore
|
|
156
|
+
if (!(IsObject(schema) &&
|
|
157
|
+
schema[Types.Kind] === 'Function' &&
|
|
158
|
+
schema.type === 'object' &&
|
|
159
|
+
schema.instanceOf === 'Function' &&
|
|
160
|
+
IsOptionalString(schema.$id) &&
|
|
161
|
+
IsArray(schema.parameters) &&
|
|
162
|
+
TSchema(schema.returns))) {
|
|
132
163
|
return false;
|
|
133
164
|
}
|
|
134
165
|
for (const parameter of schema.parameters) {
|
|
@@ -153,7 +184,13 @@ var TypeGuard;
|
|
|
153
184
|
TypeGuard.TInteger = TInteger;
|
|
154
185
|
/** Returns true if the given schema is TLiteral */
|
|
155
186
|
function TLiteral(schema) {
|
|
156
|
-
|
|
187
|
+
// prettier-ignore
|
|
188
|
+
return (IsObject(schema) &&
|
|
189
|
+
schema[Types.Kind] === 'Literal' &&
|
|
190
|
+
IsOptionalString(schema.$id) &&
|
|
191
|
+
(IsString(schema.const) ||
|
|
192
|
+
IsNumber(schema.const) ||
|
|
193
|
+
IsBoolean(schema.const)));
|
|
157
194
|
}
|
|
158
195
|
TypeGuard.TLiteral = TLiteral;
|
|
159
196
|
/** Returns true if the given schema is TNever */
|
|
@@ -213,12 +250,24 @@ var TypeGuard;
|
|
|
213
250
|
TypeGuard.TObject = TObject;
|
|
214
251
|
/** Returns true if the given schema is TPromise */
|
|
215
252
|
function TPromise(schema) {
|
|
216
|
-
|
|
253
|
+
// prettier-ignore
|
|
254
|
+
return (IsObject(schema) &&
|
|
255
|
+
schema[Types.Kind] === 'Promise' &&
|
|
256
|
+
schema.type === 'object' &&
|
|
257
|
+
schema.instanceOf === 'Promise' &&
|
|
258
|
+
IsOptionalString(schema.$id) &&
|
|
259
|
+
TSchema(schema.item));
|
|
217
260
|
}
|
|
218
261
|
TypeGuard.TPromise = TPromise;
|
|
219
262
|
/** Returns true if the given schema is TRecord */
|
|
220
263
|
function TRecord(schema) {
|
|
221
|
-
|
|
264
|
+
// prettier-ignore
|
|
265
|
+
if (!(IsObject(schema) &&
|
|
266
|
+
schema[Types.Kind] === 'Record' &&
|
|
267
|
+
schema.type === 'object' &&
|
|
268
|
+
IsOptionalString(schema.$id) &&
|
|
269
|
+
schema.additionalProperties === false &&
|
|
270
|
+
IsObject(schema.patternProperties))) {
|
|
222
271
|
return false;
|
|
223
272
|
}
|
|
224
273
|
const keys = Object.keys(schema.patternProperties);
|
|
@@ -236,12 +285,20 @@ var TypeGuard;
|
|
|
236
285
|
TypeGuard.TRecord = TRecord;
|
|
237
286
|
/** Returns true if the given schema is TSelf */
|
|
238
287
|
function TSelf(schema) {
|
|
239
|
-
|
|
288
|
+
// prettier-ignore
|
|
289
|
+
return (IsObject(schema) &&
|
|
290
|
+
schema[Types.Kind] === 'Self' &&
|
|
291
|
+
IsOptionalString(schema.$id) &&
|
|
292
|
+
IsString(schema.$ref));
|
|
240
293
|
}
|
|
241
294
|
TypeGuard.TSelf = TSelf;
|
|
242
295
|
/** Returns true if the given schema is TRef */
|
|
243
296
|
function TRef(schema) {
|
|
244
|
-
|
|
297
|
+
// prettier-ignore
|
|
298
|
+
return (IsObject(schema) &&
|
|
299
|
+
schema[Types.Kind] === 'Ref' &&
|
|
300
|
+
IsOptionalString(schema.$id) &&
|
|
301
|
+
IsString(schema.$ref));
|
|
245
302
|
}
|
|
246
303
|
TypeGuard.TRef = TRef;
|
|
247
304
|
/** Returns true if the given schema is TString */
|
|
@@ -258,7 +315,14 @@ var TypeGuard;
|
|
|
258
315
|
TypeGuard.TString = TString;
|
|
259
316
|
/** Returns true if the given schema is TTuple */
|
|
260
317
|
function TTuple(schema) {
|
|
261
|
-
|
|
318
|
+
// prettier-ignore
|
|
319
|
+
if (!(IsObject(schema) &&
|
|
320
|
+
schema[Types.Kind] === 'Tuple' &&
|
|
321
|
+
schema.type === 'array' &&
|
|
322
|
+
IsOptionalString(schema.$id) &&
|
|
323
|
+
IsNumber(schema.minItems) &&
|
|
324
|
+
IsNumber(schema.maxItems) &&
|
|
325
|
+
schema.minItems === schema.maxItems)) {
|
|
262
326
|
return false;
|
|
263
327
|
}
|
|
264
328
|
if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
|
|
@@ -276,12 +340,21 @@ var TypeGuard;
|
|
|
276
340
|
TypeGuard.TTuple = TTuple;
|
|
277
341
|
/** Returns true if the given schema is TUndefined */
|
|
278
342
|
function TUndefined(schema) {
|
|
279
|
-
|
|
343
|
+
// prettier-ignore
|
|
344
|
+
return (IsObject(schema) &&
|
|
345
|
+
schema[Types.Kind] === 'Undefined' &&
|
|
346
|
+
schema.type === 'null' &&
|
|
347
|
+
schema.typeOf === 'Undefined' &&
|
|
348
|
+
IsOptionalString(schema.$id));
|
|
280
349
|
}
|
|
281
350
|
TypeGuard.TUndefined = TUndefined;
|
|
282
351
|
/** Returns true if the given schema is TUnion */
|
|
283
352
|
function TUnion(schema) {
|
|
284
|
-
|
|
353
|
+
// prettier-ignore
|
|
354
|
+
if (!(IsObject(schema) &&
|
|
355
|
+
schema[Types.Kind] === 'Union' &&
|
|
356
|
+
IsArray(schema.anyOf) &&
|
|
357
|
+
IsOptionalString(schema.$id))) {
|
|
285
358
|
return false;
|
|
286
359
|
}
|
|
287
360
|
for (const inner of schema.anyOf) {
|
|
@@ -297,19 +370,27 @@ var TypeGuard;
|
|
|
297
370
|
schema[Types.Kind] === 'Uint8Array' &&
|
|
298
371
|
schema.type === 'object' &&
|
|
299
372
|
IsOptionalString(schema.$id) &&
|
|
300
|
-
schema.
|
|
373
|
+
schema.instanceOf === 'Uint8Array' &&
|
|
301
374
|
IsOptionalNumber(schema.minByteLength) &&
|
|
302
375
|
IsOptionalNumber(schema.maxByteLength));
|
|
303
376
|
}
|
|
304
377
|
TypeGuard.TUint8Array = TUint8Array;
|
|
305
378
|
/** Returns true if the given schema is TUnknown */
|
|
306
379
|
function TUnknown(schema) {
|
|
307
|
-
|
|
380
|
+
// prettier-ignore
|
|
381
|
+
return (IsObject(schema) &&
|
|
382
|
+
schema[Types.Kind] === 'Unknown' &&
|
|
383
|
+
IsOptionalString(schema.$id));
|
|
308
384
|
}
|
|
309
385
|
TypeGuard.TUnknown = TUnknown;
|
|
310
386
|
/** Returns true if the given schema is TVoid */
|
|
311
387
|
function TVoid(schema) {
|
|
312
|
-
|
|
388
|
+
// prettier-ignore
|
|
389
|
+
return (IsObject(schema) &&
|
|
390
|
+
schema[Types.Kind] === 'Void' &&
|
|
391
|
+
schema.type === 'null' &&
|
|
392
|
+
schema.typeOf === 'Void' &&
|
|
393
|
+
IsOptionalString(schema.$id));
|
|
313
394
|
}
|
|
314
395
|
TypeGuard.TVoid = TVoid;
|
|
315
396
|
/** Returns true if the given schema is TSchema */
|
|
@@ -318,6 +399,7 @@ var TypeGuard;
|
|
|
318
399
|
TArray(schema) ||
|
|
319
400
|
TBoolean(schema) ||
|
|
320
401
|
TConstructor(schema) ||
|
|
402
|
+
TDate(schema) ||
|
|
321
403
|
TFunction(schema) ||
|
|
322
404
|
TInteger(schema) ||
|
|
323
405
|
TLiteral(schema) ||
|