@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/value/check.js
CHANGED
|
@@ -27,15 +27,16 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCheck = exports.
|
|
31
|
-
const
|
|
32
|
-
|
|
30
|
+
exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
const format_1 = require("../format");
|
|
33
|
+
class ValueCheckUnknownTypeError extends Error {
|
|
33
34
|
constructor(schema) {
|
|
34
|
-
super('ValueCheck:
|
|
35
|
+
super('ValueCheck: Unknown type');
|
|
35
36
|
this.schema = schema;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
|
-
exports.
|
|
39
|
+
exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
|
|
39
40
|
var ValueCheck;
|
|
40
41
|
(function (ValueCheck) {
|
|
41
42
|
function Any(schema, references, value) {
|
|
@@ -205,6 +206,12 @@ var ValueCheck;
|
|
|
205
206
|
if (!regex.test(value))
|
|
206
207
|
return false;
|
|
207
208
|
}
|
|
209
|
+
if (schema.format !== undefined) {
|
|
210
|
+
if (!format_1.Format.Has(schema.format))
|
|
211
|
+
return false;
|
|
212
|
+
const func = format_1.Format.Get(schema.format);
|
|
213
|
+
return func(value);
|
|
214
|
+
}
|
|
208
215
|
return true;
|
|
209
216
|
}
|
|
210
217
|
function Tuple(schema, references, value) {
|
|
@@ -251,72 +258,53 @@ var ValueCheck;
|
|
|
251
258
|
return value === null;
|
|
252
259
|
}
|
|
253
260
|
function Visit(schema, references, value) {
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return Tuple(schema, refs, value);
|
|
302
|
-
}
|
|
303
|
-
else if (index_1.TypeGuard.TUndefined(schema)) {
|
|
304
|
-
return Undefined(schema, refs, value);
|
|
305
|
-
}
|
|
306
|
-
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
307
|
-
return Union(schema, refs, value);
|
|
308
|
-
}
|
|
309
|
-
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
310
|
-
return Uint8Array(schema, refs, value);
|
|
311
|
-
}
|
|
312
|
-
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
313
|
-
return Unknown(schema, refs, value);
|
|
314
|
-
}
|
|
315
|
-
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
316
|
-
return Void(schema, refs, value);
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
throw new ValueCheckInvalidTypeError(schema);
|
|
261
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
262
|
+
const anySchema = schema;
|
|
263
|
+
switch (anySchema[Types.Kind]) {
|
|
264
|
+
case 'Any':
|
|
265
|
+
return Any(anySchema, anyReferences, value);
|
|
266
|
+
case 'Array':
|
|
267
|
+
return Array(anySchema, anyReferences, value);
|
|
268
|
+
case 'Boolean':
|
|
269
|
+
return Boolean(anySchema, anyReferences, value);
|
|
270
|
+
case 'Constructor':
|
|
271
|
+
return Constructor(anySchema, anyReferences, value);
|
|
272
|
+
case 'Function':
|
|
273
|
+
return Function(anySchema, anyReferences, value);
|
|
274
|
+
case 'Integer':
|
|
275
|
+
return Integer(anySchema, anyReferences, value);
|
|
276
|
+
case 'Literal':
|
|
277
|
+
return Literal(anySchema, anyReferences, value);
|
|
278
|
+
case 'Null':
|
|
279
|
+
return Null(anySchema, anyReferences, value);
|
|
280
|
+
case 'Number':
|
|
281
|
+
return Number(anySchema, anyReferences, value);
|
|
282
|
+
case 'Object':
|
|
283
|
+
return Object(anySchema, anyReferences, value);
|
|
284
|
+
case 'Promise':
|
|
285
|
+
return Promise(anySchema, anyReferences, value);
|
|
286
|
+
case 'Record':
|
|
287
|
+
return Record(anySchema, anyReferences, value);
|
|
288
|
+
case 'Ref':
|
|
289
|
+
return Ref(anySchema, anyReferences, value);
|
|
290
|
+
case 'Self':
|
|
291
|
+
return Self(anySchema, anyReferences, value);
|
|
292
|
+
case 'String':
|
|
293
|
+
return String(anySchema, anyReferences, value);
|
|
294
|
+
case 'Tuple':
|
|
295
|
+
return Tuple(anySchema, anyReferences, value);
|
|
296
|
+
case 'Undefined':
|
|
297
|
+
return Undefined(anySchema, anyReferences, value);
|
|
298
|
+
case 'Union':
|
|
299
|
+
return Union(anySchema, anyReferences, value);
|
|
300
|
+
case 'Uint8Array':
|
|
301
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
302
|
+
case 'Unknown':
|
|
303
|
+
return Unknown(anySchema, anyReferences, value);
|
|
304
|
+
case 'Void':
|
|
305
|
+
return Void(anySchema, anyReferences, value);
|
|
306
|
+
default:
|
|
307
|
+
throw new ValueCheckUnknownTypeError(anySchema);
|
|
320
308
|
}
|
|
321
309
|
}
|
|
322
310
|
// -------------------------------------------------------------------------
|
package/value/create.d.ts
CHANGED
package/value/create.js
CHANGED
|
@@ -27,15 +27,15 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCreate = exports.
|
|
31
|
-
const
|
|
32
|
-
class
|
|
30
|
+
exports.ValueCreate = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
class ValueCreateUnknownTypeError extends Error {
|
|
33
33
|
constructor(schema) {
|
|
34
|
-
super('ValueCreate:
|
|
34
|
+
super('ValueCreate: Unknown type');
|
|
35
35
|
this.schema = schema;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
exports.
|
|
38
|
+
exports.ValueCreateUnknownTypeError = ValueCreateUnknownTypeError;
|
|
39
39
|
var ValueCreate;
|
|
40
40
|
(function (ValueCreate) {
|
|
41
41
|
function Any(schema, references) {
|
|
@@ -276,72 +276,57 @@ var ValueCreate;
|
|
|
276
276
|
}
|
|
277
277
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
278
278
|
function Visit(schema, references) {
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
332
|
-
return Union(schema, refs);
|
|
333
|
-
}
|
|
334
|
-
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
335
|
-
return Uint8Array(schema, refs);
|
|
336
|
-
}
|
|
337
|
-
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
338
|
-
return Unknown(schema, refs);
|
|
339
|
-
}
|
|
340
|
-
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
341
|
-
return Void(schema, refs);
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
throw new ValueCreateInvalidTypeError(schema);
|
|
279
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
280
|
+
const anySchema = schema;
|
|
281
|
+
switch (anySchema[Types.Kind]) {
|
|
282
|
+
case 'Any':
|
|
283
|
+
return Any(anySchema, anyReferences);
|
|
284
|
+
case 'Array':
|
|
285
|
+
return Array(anySchema, anyReferences);
|
|
286
|
+
case 'Boolean':
|
|
287
|
+
return Boolean(anySchema, anyReferences);
|
|
288
|
+
case 'Constructor':
|
|
289
|
+
return Constructor(anySchema, anyReferences);
|
|
290
|
+
case 'Enum':
|
|
291
|
+
return Enum(anySchema, anyReferences);
|
|
292
|
+
case 'Function':
|
|
293
|
+
return Function(anySchema, anyReferences);
|
|
294
|
+
case 'Integer':
|
|
295
|
+
return Integer(anySchema, anyReferences);
|
|
296
|
+
case 'Literal':
|
|
297
|
+
return Literal(anySchema, anyReferences);
|
|
298
|
+
case 'Null':
|
|
299
|
+
return Null(anySchema, anyReferences);
|
|
300
|
+
case 'Number':
|
|
301
|
+
return Number(anySchema, anyReferences);
|
|
302
|
+
case 'Object':
|
|
303
|
+
return Object(anySchema, anyReferences);
|
|
304
|
+
case 'Promise':
|
|
305
|
+
return Promise(anySchema, anyReferences);
|
|
306
|
+
case 'Record':
|
|
307
|
+
return Record(anySchema, anyReferences);
|
|
308
|
+
case 'Rec':
|
|
309
|
+
return Recursive(anySchema, anyReferences);
|
|
310
|
+
case 'Ref':
|
|
311
|
+
return Ref(anySchema, anyReferences);
|
|
312
|
+
case 'Self':
|
|
313
|
+
return Self(anySchema, anyReferences);
|
|
314
|
+
case 'String':
|
|
315
|
+
return String(anySchema, anyReferences);
|
|
316
|
+
case 'Tuple':
|
|
317
|
+
return Tuple(anySchema, anyReferences);
|
|
318
|
+
case 'Undefined':
|
|
319
|
+
return Undefined(anySchema, anyReferences);
|
|
320
|
+
case 'Union':
|
|
321
|
+
return Union(anySchema, anyReferences);
|
|
322
|
+
case 'Uint8Array':
|
|
323
|
+
return Uint8Array(anySchema, anyReferences);
|
|
324
|
+
case 'Unknown':
|
|
325
|
+
return Unknown(anySchema, anyReferences);
|
|
326
|
+
case 'Void':
|
|
327
|
+
return Void(anySchema, anyReferences);
|
|
328
|
+
default:
|
|
329
|
+
throw new ValueCreateUnknownTypeError(anySchema);
|
|
345
330
|
}
|
|
346
331
|
}
|
|
347
332
|
ValueCreate.Visit = Visit;
|
package/value/value.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ export declare namespace Value {
|
|
|
6
6
|
function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;
|
|
7
7
|
/** Creates a value from the given type */
|
|
8
8
|
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
9
|
-
/**
|
|
9
|
+
/** Returns true if the value matches the given type. */
|
|
10
10
|
function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): value is Types.Static<T>;
|
|
11
|
-
/**
|
|
11
|
+
/** Returns true if the value matches the given type. */
|
|
12
12
|
function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
|
|
13
13
|
/** Casts a value into a structure matching the given type. The result will be a new value that retains as much information of the original value as possible. */
|
|
14
14
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|