@sinclair/typebox 0.24.26 → 0.24.29
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 +74 -79
- package/conditional/conditional.js +2 -2
- package/errors/errors.d.ts +1 -1
- package/errors/errors.js +52 -71
- package/format/format.d.ts +12 -0
- package/format/format.js +55 -0
- package/format/index.d.ts +1 -0
- package/format/index.js +44 -0
- package/guard/guard.d.ts +28 -22
- package/guard/guard.js +65 -22
- package/package.json +8 -8
- package/readme.md +154 -97
- package/value/cast.d.ts +1 -1
- package/value/cast.js +55 -71
- package/value/check.d.ts +1 -1
- package/value/check.js +59 -71
- package/value/create.d.ts +1 -1
- package/value/create.js +56 -71
- package/value/value.d.ts +2 -2
package/guard/guard.js
CHANGED
|
@@ -27,8 +27,15 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeGuard = void 0;
|
|
30
|
+
exports.TypeGuard = exports.TypeGuardInvalidTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
+
class TypeGuardInvalidTypeError extends Error {
|
|
33
|
+
constructor(schema) {
|
|
34
|
+
super('TypeGuard: Invalid type');
|
|
35
|
+
this.schema = schema;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.TypeGuardInvalidTypeError = TypeGuardInvalidTypeError;
|
|
32
39
|
/** TypeGuard tests that values conform to a known TypeBox type specification */
|
|
33
40
|
var TypeGuard;
|
|
34
41
|
(function (TypeGuard) {
|
|
@@ -47,7 +54,7 @@ var TypeGuard;
|
|
|
47
54
|
return false;
|
|
48
55
|
}
|
|
49
56
|
}
|
|
50
|
-
function
|
|
57
|
+
function IsControlCharacterFree(value) {
|
|
51
58
|
if (typeof value !== 'string')
|
|
52
59
|
return false;
|
|
53
60
|
for (let i = 0; i < value.length; i++) {
|
|
@@ -77,26 +84,36 @@ var TypeGuard;
|
|
|
77
84
|
return value === undefined || (value !== undefined && IsString(value));
|
|
78
85
|
}
|
|
79
86
|
function IsOptionalPattern(value) {
|
|
80
|
-
return value === undefined || (value !== undefined && IsString(value) && IsPattern(value));
|
|
87
|
+
return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
|
|
88
|
+
}
|
|
89
|
+
function IsOptionalFormat(value) {
|
|
90
|
+
return value === undefined || (value !== undefined && IsString(value) && IsControlCharacterFree(value));
|
|
81
91
|
}
|
|
82
92
|
/** Returns true if the given schema is TAny */
|
|
83
93
|
function TAny(schema) {
|
|
84
|
-
return IsObject(schema) && schema[Types.Kind] === 'Any';
|
|
94
|
+
return IsObject(schema) && schema[Types.Kind] === 'Any' && IsOptionalString(schema.$id);
|
|
85
95
|
}
|
|
86
96
|
TypeGuard.TAny = TAny;
|
|
87
97
|
/** Returns true if the given schema is TArray */
|
|
88
98
|
function TArray(schema) {
|
|
89
|
-
return IsObject(schema) &&
|
|
99
|
+
return (IsObject(schema) &&
|
|
100
|
+
schema[Types.Kind] === 'Array' &&
|
|
101
|
+
schema.type === 'array' &&
|
|
102
|
+
IsOptionalString(schema.$id) &&
|
|
103
|
+
TSchema(schema.items) &&
|
|
104
|
+
IsOptionalNumber(schema.minItems) &&
|
|
105
|
+
IsOptionalNumber(schema.maxItems) &&
|
|
106
|
+
IsOptionalBoolean(schema.uniqueItems));
|
|
90
107
|
}
|
|
91
108
|
TypeGuard.TArray = TArray;
|
|
92
109
|
/** Returns true if the given schema is TBoolean */
|
|
93
110
|
function TBoolean(schema) {
|
|
94
|
-
return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean';
|
|
111
|
+
return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean' && IsOptionalString(schema.$id);
|
|
95
112
|
}
|
|
96
113
|
TypeGuard.TBoolean = TBoolean;
|
|
97
114
|
/** Returns true if the given schema is TConstructor */
|
|
98
115
|
function TConstructor(schema) {
|
|
99
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
116
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
100
117
|
return false;
|
|
101
118
|
}
|
|
102
119
|
for (const parameter of schema.parameters) {
|
|
@@ -108,7 +125,7 @@ var TypeGuard;
|
|
|
108
125
|
TypeGuard.TConstructor = TConstructor;
|
|
109
126
|
/** Returns true if the given schema is TFunction */
|
|
110
127
|
function TFunction(schema) {
|
|
111
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
128
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
|
|
112
129
|
return false;
|
|
113
130
|
}
|
|
114
131
|
for (const parameter of schema.parameters) {
|
|
@@ -123,6 +140,7 @@ var TypeGuard;
|
|
|
123
140
|
return (IsObject(schema) &&
|
|
124
141
|
schema[Types.Kind] === 'Integer' &&
|
|
125
142
|
schema.type === 'integer' &&
|
|
143
|
+
IsOptionalString(schema.$id) &&
|
|
126
144
|
IsOptionalNumber(schema.multipleOf) &&
|
|
127
145
|
IsOptionalNumber(schema.minimum) &&
|
|
128
146
|
IsOptionalNumber(schema.maximum) &&
|
|
@@ -132,12 +150,12 @@ var TypeGuard;
|
|
|
132
150
|
TypeGuard.TInteger = TInteger;
|
|
133
151
|
/** Returns true if the given schema is TLiteral */
|
|
134
152
|
function TLiteral(schema) {
|
|
135
|
-
return IsObject(schema) && schema[Types.Kind] === 'Literal' && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
|
|
153
|
+
return IsObject(schema) && schema[Types.Kind] === 'Literal' && IsOptionalString(schema.$id) && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
|
|
136
154
|
}
|
|
137
155
|
TypeGuard.TLiteral = TLiteral;
|
|
138
156
|
/** Returns true if the given schema is TNull */
|
|
139
157
|
function TNull(schema) {
|
|
140
|
-
return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null';
|
|
158
|
+
return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null' && IsOptionalString(schema.$id);
|
|
141
159
|
}
|
|
142
160
|
TypeGuard.TNull = TNull;
|
|
143
161
|
/** Returns true if the given schema is TNumber */
|
|
@@ -145,6 +163,7 @@ var TypeGuard;
|
|
|
145
163
|
return (IsObject(schema) &&
|
|
146
164
|
schema[Types.Kind] === 'Number' &&
|
|
147
165
|
schema.type === 'number' &&
|
|
166
|
+
IsOptionalString(schema.$id) &&
|
|
148
167
|
IsOptionalNumber(schema.multipleOf) &&
|
|
149
168
|
IsOptionalNumber(schema.minimum) &&
|
|
150
169
|
IsOptionalNumber(schema.maximum) &&
|
|
@@ -157,6 +176,7 @@ var TypeGuard;
|
|
|
157
176
|
if (!(IsObject(schema) &&
|
|
158
177
|
schema[Types.Kind] === 'Object' &&
|
|
159
178
|
schema.type === 'object' &&
|
|
179
|
+
IsOptionalString(schema.$id) &&
|
|
160
180
|
IsObject(schema.properties) &&
|
|
161
181
|
IsOptionalBoolean(schema.additionalProperties) &&
|
|
162
182
|
IsOptionalNumber(schema.minProperties) &&
|
|
@@ -164,7 +184,7 @@ var TypeGuard;
|
|
|
164
184
|
return false;
|
|
165
185
|
}
|
|
166
186
|
for (const [key, value] of Object.entries(schema.properties)) {
|
|
167
|
-
if (!
|
|
187
|
+
if (!IsControlCharacterFree(key))
|
|
168
188
|
return false;
|
|
169
189
|
if (!TSchema(value))
|
|
170
190
|
return false;
|
|
@@ -174,12 +194,12 @@ var TypeGuard;
|
|
|
174
194
|
TypeGuard.TObject = TObject;
|
|
175
195
|
/** Returns true if the given schema is TPromise */
|
|
176
196
|
function TPromise(schema) {
|
|
177
|
-
return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && TSchema(schema.item);
|
|
197
|
+
return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && IsOptionalString(schema.$id) && TSchema(schema.item);
|
|
178
198
|
}
|
|
179
199
|
TypeGuard.TPromise = TPromise;
|
|
180
200
|
/** Returns true if the given schema is TRecord */
|
|
181
201
|
function TRecord(schema) {
|
|
182
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
|
|
202
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
|
|
183
203
|
return false;
|
|
184
204
|
}
|
|
185
205
|
const keys = Object.keys(schema.patternProperties);
|
|
@@ -197,22 +217,29 @@ var TypeGuard;
|
|
|
197
217
|
TypeGuard.TRecord = TRecord;
|
|
198
218
|
/** Returns true if the given schema is TSelf */
|
|
199
219
|
function TSelf(schema) {
|
|
200
|
-
return IsObject(schema) && schema[Types.Kind] === 'Self' && IsString(schema.$ref);
|
|
220
|
+
return IsObject(schema) && schema[Types.Kind] === 'Self' && IsOptionalString(schema.$id) && IsString(schema.$ref);
|
|
201
221
|
}
|
|
202
222
|
TypeGuard.TSelf = TSelf;
|
|
203
223
|
/** Returns true if the given schema is TRef */
|
|
204
224
|
function TRef(schema) {
|
|
205
|
-
return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsString(schema.$ref);
|
|
225
|
+
return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsOptionalString(schema.$id) && IsString(schema.$ref);
|
|
206
226
|
}
|
|
207
227
|
TypeGuard.TRef = TRef;
|
|
208
228
|
/** Returns true if the given schema is TString */
|
|
209
229
|
function TString(schema) {
|
|
210
|
-
return IsObject(schema) &&
|
|
230
|
+
return (IsObject(schema) &&
|
|
231
|
+
schema[Types.Kind] === 'String' &&
|
|
232
|
+
schema.type === 'string' &&
|
|
233
|
+
IsOptionalString(schema.$id) &&
|
|
234
|
+
IsOptionalNumber(schema.minLength) &&
|
|
235
|
+
IsOptionalNumber(schema.maxLength) &&
|
|
236
|
+
IsOptionalPattern(schema.pattern) &&
|
|
237
|
+
IsOptionalFormat(schema.format));
|
|
211
238
|
}
|
|
212
239
|
TypeGuard.TString = TString;
|
|
213
240
|
/** Returns true if the given schema is TTuple */
|
|
214
241
|
function TTuple(schema) {
|
|
215
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
|
|
242
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsOptionalString(schema.$id) && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
|
|
216
243
|
return false;
|
|
217
244
|
}
|
|
218
245
|
if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
|
|
@@ -230,12 +257,12 @@ var TypeGuard;
|
|
|
230
257
|
TypeGuard.TTuple = TTuple;
|
|
231
258
|
/** Returns true if the given schema is TUndefined */
|
|
232
259
|
function TUndefined(schema) {
|
|
233
|
-
return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && schema.specialized === 'Undefined';
|
|
260
|
+
return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.specialized === 'Undefined';
|
|
234
261
|
}
|
|
235
262
|
TypeGuard.TUndefined = TUndefined;
|
|
236
263
|
/** Returns true if the given schema is TUnion */
|
|
237
264
|
function TUnion(schema) {
|
|
238
|
-
if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf))) {
|
|
265
|
+
if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf) && IsOptionalString(schema.$id))) {
|
|
239
266
|
return false;
|
|
240
267
|
}
|
|
241
268
|
for (const inner of schema.anyOf) {
|
|
@@ -247,17 +274,23 @@ var TypeGuard;
|
|
|
247
274
|
TypeGuard.TUnion = TUnion;
|
|
248
275
|
/** Returns true if the given schema is TUint8Array */
|
|
249
276
|
function TUint8Array(schema) {
|
|
250
|
-
return IsObject(schema) &&
|
|
277
|
+
return (IsObject(schema) &&
|
|
278
|
+
schema[Types.Kind] === 'Uint8Array' &&
|
|
279
|
+
schema.type === 'object' &&
|
|
280
|
+
IsOptionalString(schema.$id) &&
|
|
281
|
+
schema.specialized === 'Uint8Array' &&
|
|
282
|
+
IsOptionalNumber(schema.minByteLength) &&
|
|
283
|
+
IsOptionalNumber(schema.maxByteLength));
|
|
251
284
|
}
|
|
252
285
|
TypeGuard.TUint8Array = TUint8Array;
|
|
253
286
|
/** Returns true if the given schema is TUnknown */
|
|
254
287
|
function TUnknown(schema) {
|
|
255
|
-
return IsObject(schema) && schema[Types.Kind] === 'Unknown';
|
|
288
|
+
return IsObject(schema) && schema[Types.Kind] === 'Unknown' && IsOptionalString(schema.$id);
|
|
256
289
|
}
|
|
257
290
|
TypeGuard.TUnknown = TUnknown;
|
|
258
291
|
/** Returns true if the given schema is TVoid */
|
|
259
292
|
function TVoid(schema) {
|
|
260
|
-
return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null';
|
|
293
|
+
return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null' && IsOptionalString(schema.$id);
|
|
261
294
|
}
|
|
262
295
|
TypeGuard.TVoid = TVoid;
|
|
263
296
|
/** Returns true if the given schema is TSchema */
|
|
@@ -285,4 +318,14 @@ var TypeGuard;
|
|
|
285
318
|
TVoid(schema));
|
|
286
319
|
}
|
|
287
320
|
TypeGuard.TSchema = TSchema;
|
|
321
|
+
/** Asserts if this schema and associated references are valid. */
|
|
322
|
+
function Assert(schema, references = []) {
|
|
323
|
+
if (!TSchema(schema))
|
|
324
|
+
throw new TypeGuardInvalidTypeError(schema);
|
|
325
|
+
for (const schema of references) {
|
|
326
|
+
if (!TSchema(schema))
|
|
327
|
+
throw new TypeGuardInvalidTypeError(schema);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
TypeGuard.Assert = Assert;
|
|
288
331
|
})(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.29",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,15 +26,15 @@
|
|
|
26
26
|
"publish": "hammer task publish"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@sinclair/hammer": "^0.
|
|
30
|
-
"@types/chai": "^4.3.
|
|
31
|
-
"@types/mocha": "^9.1.
|
|
32
|
-
"@types/node": "^
|
|
29
|
+
"@sinclair/hammer": "^0.17.1",
|
|
30
|
+
"@types/chai": "^4.3.3",
|
|
31
|
+
"@types/mocha": "^9.1.1",
|
|
32
|
+
"@types/node": "^18.7.13",
|
|
33
33
|
"ajv": "^8.11.0",
|
|
34
34
|
"ajv-formats": "^2.1.1",
|
|
35
|
-
"chai": "^4.3.
|
|
36
|
-
"mocha": "^9.2.
|
|
35
|
+
"chai": "^4.3.6",
|
|
36
|
+
"mocha": "^9.2.2",
|
|
37
37
|
"prettier": "^2.7.1",
|
|
38
|
-
"typescript": "^4.
|
|
38
|
+
"typescript": "^4.8.2"
|
|
39
39
|
}
|
|
40
40
|
}
|