@sinclair/typebox 0.24.5 → 0.24.6
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/guard/guard.d.ts +48 -0
- package/guard/guard.js +222 -0
- package/guard/index.d.ts +1 -0
- package/guard/index.js +40 -0
- package/package.json +1 -1
- package/readme.md +26 -1
package/guard/guard.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as Types from '../typebox';
|
|
2
|
+
/** Structural checks for TypeBox types */
|
|
3
|
+
export declare namespace TypeGuard {
|
|
4
|
+
/** Returns true if the given schema is TAny */
|
|
5
|
+
function TAny(schema: any): schema is Types.TAny;
|
|
6
|
+
/** Returns true if the given schema is TArray */
|
|
7
|
+
function TArray(schema: any): schema is Types.TArray;
|
|
8
|
+
/** Returns true if the given schema is TBoolean */
|
|
9
|
+
function TBoolean(schema: any): schema is Types.TBoolean;
|
|
10
|
+
/** Returns true if the given schema is TConstructor */
|
|
11
|
+
function TConstructor(schema: any): schema is Types.TConstructor;
|
|
12
|
+
/** Returns true if the given schema is TFunction */
|
|
13
|
+
function TFunction(schema: any): schema is Types.TFunction;
|
|
14
|
+
/** Returns true if the given schema is TInteger */
|
|
15
|
+
function TInteger(schema: any): schema is Types.TInteger;
|
|
16
|
+
/** Returns true if the given schema is TLiteral */
|
|
17
|
+
function TLiteral(schema: any): schema is Types.TLiteral;
|
|
18
|
+
/** Returns true if the given schema is TNull */
|
|
19
|
+
function TNull(schema: any): schema is Types.TNull;
|
|
20
|
+
/** Returns true if the given schema is TNumber */
|
|
21
|
+
function TNumber(schema: any): schema is Types.TNumber;
|
|
22
|
+
/** Returns true if the given schema is TObject */
|
|
23
|
+
function TObject(schema: any): schema is Types.TObject;
|
|
24
|
+
/** Returns true if the given schema is TPromise */
|
|
25
|
+
function TPromise(schema: any): schema is Types.TPromise;
|
|
26
|
+
/** Returns true if the given schema is TRecord */
|
|
27
|
+
function TRecord(schema: any): schema is Types.TRecord;
|
|
28
|
+
/** Returns true if the given schema is TSelf */
|
|
29
|
+
function TSelf(schema: any): schema is Types.TSelf;
|
|
30
|
+
/** Returns true if the given schema is TRef */
|
|
31
|
+
function TRef(schema: any): schema is Types.TSelf;
|
|
32
|
+
/** Returns true if the given schema is TString */
|
|
33
|
+
function TString(schema: any): schema is Types.TString;
|
|
34
|
+
/** Returns true if the given schema is TTuple */
|
|
35
|
+
function TTuple(schema: any): schema is Types.TTuple;
|
|
36
|
+
/** Returns true if the given schema is TUndefined */
|
|
37
|
+
function TUndefined(schema: any): schema is Types.TUndefined;
|
|
38
|
+
/** Returns true if the given schema is TUnion */
|
|
39
|
+
function TUnion(schema: any): schema is Types.TUnion;
|
|
40
|
+
/** Returns true if the given schema is TUint8Array */
|
|
41
|
+
function TUint8Array(schema: any): schema is Types.TUint8Array;
|
|
42
|
+
/** Returns true if the given schema is TUnknown */
|
|
43
|
+
function TUnknown(schema: any): schema is Types.TUnknown;
|
|
44
|
+
/** Returns true if the given schema is TVoid */
|
|
45
|
+
function TVoid(schema: any): schema is Types.TVoid;
|
|
46
|
+
/** Returns true if the given schema is TSchema */
|
|
47
|
+
function TSchema(schema: any): schema is Types.TSchema;
|
|
48
|
+
}
|
package/guard/guard.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/guard
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, dTribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.TypeGuard = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
/** Structural checks for TypeBox types */
|
|
33
|
+
var TypeGuard;
|
|
34
|
+
(function (TypeGuard) {
|
|
35
|
+
function IsObject(schema) {
|
|
36
|
+
return typeof schema === 'object' && schema !== null && !Array.isArray(schema);
|
|
37
|
+
}
|
|
38
|
+
function IsArray(schema) {
|
|
39
|
+
return typeof schema === 'object' && schema !== null && Array.isArray(schema);
|
|
40
|
+
}
|
|
41
|
+
/** Returns true if the given schema is TAny */
|
|
42
|
+
function TAny(schema) {
|
|
43
|
+
return IsObject(schema) && schema[Types.Kind] === 'Any';
|
|
44
|
+
}
|
|
45
|
+
TypeGuard.TAny = TAny;
|
|
46
|
+
/** Returns true if the given schema is TArray */
|
|
47
|
+
function TArray(schema) {
|
|
48
|
+
return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items);
|
|
49
|
+
}
|
|
50
|
+
TypeGuard.TArray = TArray;
|
|
51
|
+
/** Returns true if the given schema is TBoolean */
|
|
52
|
+
function TBoolean(schema) {
|
|
53
|
+
return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean';
|
|
54
|
+
}
|
|
55
|
+
TypeGuard.TBoolean = TBoolean;
|
|
56
|
+
/** Returns true if the given schema is TConstructor */
|
|
57
|
+
function TConstructor(schema) {
|
|
58
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
for (const parameter of schema.parameters) {
|
|
62
|
+
if (!TSchema(parameter))
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
TypeGuard.TConstructor = TConstructor;
|
|
68
|
+
/** Returns true if the given schema is TFunction */
|
|
69
|
+
function TFunction(schema) {
|
|
70
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
for (const parameter of schema.parameters) {
|
|
74
|
+
if (!TSchema(parameter))
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
TypeGuard.TFunction = TFunction;
|
|
80
|
+
/** Returns true if the given schema is TInteger */
|
|
81
|
+
function TInteger(schema) {
|
|
82
|
+
return IsObject(schema) && schema[Types.Kind] === 'Integer' && schema.type === 'integer';
|
|
83
|
+
}
|
|
84
|
+
TypeGuard.TInteger = TInteger;
|
|
85
|
+
/** Returns true if the given schema is TLiteral */
|
|
86
|
+
function TLiteral(schema) {
|
|
87
|
+
return IsObject(schema) && schema[Types.Kind] === 'Literal' && (typeof schema.const === 'string' || typeof schema.const === 'number' || typeof schema.const === 'boolean');
|
|
88
|
+
}
|
|
89
|
+
TypeGuard.TLiteral = TLiteral;
|
|
90
|
+
/** Returns true if the given schema is TNull */
|
|
91
|
+
function TNull(schema) {
|
|
92
|
+
return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null';
|
|
93
|
+
}
|
|
94
|
+
TypeGuard.TNull = TNull;
|
|
95
|
+
/** Returns true if the given schema is TNumber */
|
|
96
|
+
function TNumber(schema) {
|
|
97
|
+
return IsObject(schema) && schema[Types.Kind] === 'Number' && schema.type === 'number';
|
|
98
|
+
}
|
|
99
|
+
TypeGuard.TNumber = TNumber;
|
|
100
|
+
/** Returns true if the given schema is TObject */
|
|
101
|
+
function TObject(schema) {
|
|
102
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties))) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
for (const property of Object.values(schema.properties)) {
|
|
106
|
+
if (!TSchema(property))
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
TypeGuard.TObject = TObject;
|
|
112
|
+
/** Returns true if the given schema is TPromise */
|
|
113
|
+
function TPromise(schema) {
|
|
114
|
+
return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && TSchema(schema.item);
|
|
115
|
+
}
|
|
116
|
+
TypeGuard.TPromise = TPromise;
|
|
117
|
+
/** Returns true if the given schema is TRecord */
|
|
118
|
+
function TRecord(schema) {
|
|
119
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsObject(schema.patternProperties))) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const keys = Object.keys(schema.patternProperties);
|
|
123
|
+
if (keys.length !== 1) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (!TSchema(schema.patternProperties[keys[0]])) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
TypeGuard.TRecord = TRecord;
|
|
132
|
+
/** Returns true if the given schema is TSelf */
|
|
133
|
+
function TSelf(schema) {
|
|
134
|
+
return IsObject(schema) && schema[Types.Kind] === 'Self' && typeof schema.$ref === 'string';
|
|
135
|
+
}
|
|
136
|
+
TypeGuard.TSelf = TSelf;
|
|
137
|
+
/** Returns true if the given schema is TRef */
|
|
138
|
+
function TRef(schema) {
|
|
139
|
+
return IsObject(schema) && schema[Types.Kind] === 'Ref' && typeof schema.$ref === 'string';
|
|
140
|
+
}
|
|
141
|
+
TypeGuard.TRef = TRef;
|
|
142
|
+
/** Returns true if the given schema is TString */
|
|
143
|
+
function TString(schema) {
|
|
144
|
+
return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string';
|
|
145
|
+
}
|
|
146
|
+
TypeGuard.TString = TString;
|
|
147
|
+
/** Returns true if the given schema is TTuple */
|
|
148
|
+
function TTuple(schema) {
|
|
149
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && schema.additionalItems === false && typeof schema.minItems === 'number' && typeof schema.maxItems === 'number' && schema.minItems === schema.maxItems)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
if (schema.items === undefined && schema.minItems === 0) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
if (!IsArray(schema.items)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
for (const inner of schema.items) {
|
|
159
|
+
if (!TSchema(inner))
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
TypeGuard.TTuple = TTuple;
|
|
165
|
+
/** Returns true if the given schema is TUndefined */
|
|
166
|
+
function TUndefined(schema) {
|
|
167
|
+
return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && schema.specialized === 'Undefined';
|
|
168
|
+
}
|
|
169
|
+
TypeGuard.TUndefined = TUndefined;
|
|
170
|
+
/** Returns true if the given schema is TUnion */
|
|
171
|
+
function TUnion(schema) {
|
|
172
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf))) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
for (const inner of schema.anyOf) {
|
|
176
|
+
if (!TSchema(inner))
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
TypeGuard.TUnion = TUnion;
|
|
182
|
+
/** Returns true if the given schema is TUint8Array */
|
|
183
|
+
function TUint8Array(schema) {
|
|
184
|
+
return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array';
|
|
185
|
+
}
|
|
186
|
+
TypeGuard.TUint8Array = TUint8Array;
|
|
187
|
+
/** Returns true if the given schema is TUnknown */
|
|
188
|
+
function TUnknown(schema) {
|
|
189
|
+
return IsObject(schema) && schema[Types.Kind] === 'Unknown';
|
|
190
|
+
}
|
|
191
|
+
TypeGuard.TUnknown = TUnknown;
|
|
192
|
+
/** Returns true if the given schema is TVoid */
|
|
193
|
+
function TVoid(schema) {
|
|
194
|
+
return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null';
|
|
195
|
+
}
|
|
196
|
+
TypeGuard.TVoid = TVoid;
|
|
197
|
+
/** Returns true if the given schema is TSchema */
|
|
198
|
+
function TSchema(schema) {
|
|
199
|
+
return (TAny(schema) ||
|
|
200
|
+
TArray(schema) ||
|
|
201
|
+
TBoolean(schema) ||
|
|
202
|
+
TConstructor(schema) ||
|
|
203
|
+
TFunction(schema) ||
|
|
204
|
+
TInteger(schema) ||
|
|
205
|
+
TLiteral(schema) ||
|
|
206
|
+
TNull(schema) ||
|
|
207
|
+
TNumber(schema) ||
|
|
208
|
+
TObject(schema) ||
|
|
209
|
+
TPromise(schema) ||
|
|
210
|
+
TRecord(schema) ||
|
|
211
|
+
TSelf(schema) ||
|
|
212
|
+
TRef(schema) ||
|
|
213
|
+
TString(schema) ||
|
|
214
|
+
TTuple(schema) ||
|
|
215
|
+
TUndefined(schema) ||
|
|
216
|
+
TUnion(schema) ||
|
|
217
|
+
TUint8Array(schema) ||
|
|
218
|
+
TUnknown(schema) ||
|
|
219
|
+
TVoid(schema));
|
|
220
|
+
}
|
|
221
|
+
TypeGuard.TSchema = TSchema;
|
|
222
|
+
})(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
|
package/guard/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './guard';
|
package/guard/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/guards
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
30
|
+
if (k2 === undefined) k2 = k;
|
|
31
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
32
|
+
}) : (function(o, m, k, k2) {
|
|
33
|
+
if (k2 === undefined) k2 = k;
|
|
34
|
+
o[k2] = m[k];
|
|
35
|
+
}));
|
|
36
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
37
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
__exportStar(require("./guard"), exports);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -62,6 +62,7 @@ License MIT
|
|
|
62
62
|
- [Generic Types](#Generic-Types)
|
|
63
63
|
- [Unsafe Types](#Unsafe-Types)
|
|
64
64
|
- [Values](#Values)
|
|
65
|
+
- [Guards](#Guards)
|
|
65
66
|
- [Strict](#Strict)
|
|
66
67
|
- [Validation](#Validation)
|
|
67
68
|
- [Compiler](#Compiler)
|
|
@@ -435,7 +436,7 @@ In addition to JSON schema types, TypeBox provides several extended types that a
|
|
|
435
436
|
│ │ │ │
|
|
436
437
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
437
438
|
│ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { │
|
|
438
|
-
│ │ │ type: '
|
|
439
|
+
│ │ │ type: 'object', │
|
|
439
440
|
│ │ │ specialized: 'Uint8Array' │
|
|
440
441
|
│ │ │ } │
|
|
441
442
|
│ │ │ │
|
|
@@ -600,6 +601,7 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
|
600
601
|
|
|
601
602
|
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
602
603
|
```
|
|
604
|
+
|
|
603
605
|
<a name="Values"></a>
|
|
604
606
|
|
|
605
607
|
### Values
|
|
@@ -623,6 +625,29 @@ const V = Value.Create(T) // const V = {
|
|
|
623
625
|
// }
|
|
624
626
|
```
|
|
625
627
|
|
|
628
|
+
<a name="Guards"></a>
|
|
629
|
+
|
|
630
|
+
### Guards
|
|
631
|
+
|
|
632
|
+
In some scenarios it may be helpful to test if an object is a valid TypeBox type. You can use the TypeGuard module to check an object conforms to a valid TypeBox schema representation. Consider the following.
|
|
633
|
+
|
|
634
|
+
```typescript
|
|
635
|
+
import { TypeGuard } from '@sinclair/typebox/guard'
|
|
636
|
+
import { Type } from '@sinclair/typebox'
|
|
637
|
+
|
|
638
|
+
const T: any = Type.String() // T is any
|
|
639
|
+
|
|
640
|
+
const { type } = T // unsafe: type is any
|
|
641
|
+
|
|
642
|
+
if(TypeGuard.TString(T)) {
|
|
643
|
+
|
|
644
|
+
const { type } = T // safe: type is 'string'
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
626
651
|
<a name="Strict"></a>
|
|
627
652
|
|
|
628
653
|
### Strict
|