@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/compiler/compiler.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
17
17
|
export declare namespace Property {
|
|
18
18
|
function Check(propertyName: string): boolean;
|
|
19
19
|
}
|
|
20
|
-
export declare class
|
|
20
|
+
export declare class TypeCompilerUnknownTypeError extends Error {
|
|
21
21
|
readonly schema: Types.TSchema;
|
|
22
22
|
constructor(schema: Types.TSchema);
|
|
23
23
|
}
|
package/compiler/compiler.js
CHANGED
|
@@ -27,9 +27,11 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeCompiler = exports.
|
|
30
|
+
exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.Property = exports.TypeCheck = void 0;
|
|
31
31
|
const index_1 = require("../errors/index");
|
|
32
32
|
const index_2 = require("../guard/index");
|
|
33
|
+
const index_3 = require("../format/index");
|
|
34
|
+
const Types = require("../typebox");
|
|
33
35
|
// -------------------------------------------------------------------
|
|
34
36
|
// TypeCheck
|
|
35
37
|
// -------------------------------------------------------------------
|
|
@@ -93,13 +95,13 @@ var Property;
|
|
|
93
95
|
// -------------------------------------------------------------------
|
|
94
96
|
// TypeCompiler
|
|
95
97
|
// -------------------------------------------------------------------
|
|
96
|
-
class
|
|
98
|
+
class TypeCompilerUnknownTypeError extends Error {
|
|
97
99
|
constructor(schema) {
|
|
98
|
-
super('TypeCompiler:
|
|
100
|
+
super('TypeCompiler: Unknown type');
|
|
99
101
|
this.schema = schema;
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
|
-
exports.
|
|
104
|
+
exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
103
105
|
/** Compiles Types for Runtime Type Checking */
|
|
104
106
|
var TypeCompiler;
|
|
105
107
|
(function (TypeCompiler) {
|
|
@@ -145,11 +147,8 @@ var TypeCompiler;
|
|
|
145
147
|
if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
|
|
146
148
|
yield `(${value} === ${schema.const})`;
|
|
147
149
|
}
|
|
148
|
-
else if (typeof schema.const === 'string') {
|
|
149
|
-
yield `(${value} === '${schema.const}')`;
|
|
150
|
-
}
|
|
151
150
|
else {
|
|
152
|
-
|
|
151
|
+
yield `(${value} === '${schema.const}')`;
|
|
153
152
|
}
|
|
154
153
|
}
|
|
155
154
|
function* Null(schema, value) {
|
|
@@ -176,7 +175,7 @@ var TypeCompiler;
|
|
|
176
175
|
yield `(Object.keys(${value}).length <= ${schema.maxProperties})`;
|
|
177
176
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
178
177
|
if (schema.additionalProperties === false) {
|
|
179
|
-
//
|
|
178
|
+
// Optimization: If the property key length matches the required keys length
|
|
180
179
|
// then we only need check that the values property key length matches that
|
|
181
180
|
// of the property key length. This is because exhaustive testing for values
|
|
182
181
|
// will occur in subsequent property tests.
|
|
@@ -212,6 +211,11 @@ var TypeCompiler;
|
|
|
212
211
|
yield `(Object.values(${value}).every(value => ${expression}))`;
|
|
213
212
|
}
|
|
214
213
|
function* Ref(schema, value) {
|
|
214
|
+
// Reference: If we have seen this reference before we can just yield and return
|
|
215
|
+
// the function call. If this isn't the case we defer to visit to generate and
|
|
216
|
+
// set the function for subsequent passes. Consider for refactor.
|
|
217
|
+
if (names.has(schema.$ref))
|
|
218
|
+
return yield `(${CreateFunctionName(schema.$ref)}(${value}))`;
|
|
215
219
|
if (!referenceMap.has(schema.$ref))
|
|
216
220
|
throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`);
|
|
217
221
|
const reference = referenceMap.get(schema.$ref);
|
|
@@ -233,6 +237,9 @@ var TypeCompiler;
|
|
|
233
237
|
const local = PushLocal(`new RegExp(/${schema.pattern}/);`);
|
|
234
238
|
yield `(${local}.test(${value}))`;
|
|
235
239
|
}
|
|
240
|
+
if (schema.format !== undefined) {
|
|
241
|
+
yield `(format('${schema.format}', ${value}))`;
|
|
242
|
+
}
|
|
236
243
|
}
|
|
237
244
|
function* Tuple(schema, value) {
|
|
238
245
|
yield `(Array.isArray(${value}))`;
|
|
@@ -265,9 +272,9 @@ var TypeCompiler;
|
|
|
265
272
|
yield `(${value} === null)`;
|
|
266
273
|
}
|
|
267
274
|
function* Visit(schema, value) {
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
//
|
|
275
|
+
// Reference: Referenced schemas can originate from either additional schemas
|
|
276
|
+
// or inline in the schema itself. Ideally the recursive path should align to
|
|
277
|
+
// reference path. Consider for refactor.
|
|
271
278
|
if (schema.$id && !names.has(schema.$id)) {
|
|
272
279
|
names.add(schema.$id);
|
|
273
280
|
const name = CreateFunctionName(schema.$id);
|
|
@@ -276,71 +283,52 @@ var TypeCompiler;
|
|
|
276
283
|
yield `(${name}(${value}))`;
|
|
277
284
|
return;
|
|
278
285
|
}
|
|
279
|
-
|
|
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
|
-
return yield* Tuple(schema, value);
|
|
326
|
-
}
|
|
327
|
-
else if (index_2.TypeGuard.TUndefined(schema)) {
|
|
328
|
-
return yield* Undefined(schema, value);
|
|
329
|
-
}
|
|
330
|
-
else if (index_2.TypeGuard.TUnion(schema)) {
|
|
331
|
-
return yield* Union(schema, value);
|
|
332
|
-
}
|
|
333
|
-
else if (index_2.TypeGuard.TUint8Array(schema)) {
|
|
334
|
-
return yield* Uint8Array(schema, value);
|
|
335
|
-
}
|
|
336
|
-
else if (index_2.TypeGuard.TUnknown(schema)) {
|
|
337
|
-
return yield* Unknown(schema, value);
|
|
338
|
-
}
|
|
339
|
-
else if (index_2.TypeGuard.TVoid(schema)) {
|
|
340
|
-
return yield* Void(schema, value);
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
throw new TypeCompilerInvalidTypeError(schema);
|
|
286
|
+
const anySchema = schema;
|
|
287
|
+
switch (anySchema[Types.Kind]) {
|
|
288
|
+
case 'Any':
|
|
289
|
+
return yield* Any(anySchema, value);
|
|
290
|
+
case 'Array':
|
|
291
|
+
return yield* Array(anySchema, value);
|
|
292
|
+
case 'Boolean':
|
|
293
|
+
return yield* Boolean(anySchema, value);
|
|
294
|
+
case 'Constructor':
|
|
295
|
+
return yield* Constructor(anySchema, value);
|
|
296
|
+
case 'Function':
|
|
297
|
+
return yield* Function(anySchema, value);
|
|
298
|
+
case 'Integer':
|
|
299
|
+
return yield* Integer(anySchema, value);
|
|
300
|
+
case 'Literal':
|
|
301
|
+
return yield* Literal(anySchema, value);
|
|
302
|
+
case 'Null':
|
|
303
|
+
return yield* Null(anySchema, value);
|
|
304
|
+
case 'Number':
|
|
305
|
+
return yield* Number(anySchema, value);
|
|
306
|
+
case 'Object':
|
|
307
|
+
return yield* Object(anySchema, value);
|
|
308
|
+
case 'Promise':
|
|
309
|
+
return yield* Promise(anySchema, value);
|
|
310
|
+
case 'Record':
|
|
311
|
+
return yield* Record(anySchema, value);
|
|
312
|
+
case 'Ref':
|
|
313
|
+
return yield* Ref(anySchema, value);
|
|
314
|
+
case 'Self':
|
|
315
|
+
return yield* Self(anySchema, value);
|
|
316
|
+
case 'String':
|
|
317
|
+
return yield* String(anySchema, value);
|
|
318
|
+
case 'Tuple':
|
|
319
|
+
return yield* Tuple(anySchema, value);
|
|
320
|
+
case 'Undefined':
|
|
321
|
+
return yield* Undefined(anySchema, value);
|
|
322
|
+
case 'Union':
|
|
323
|
+
return yield* Union(anySchema, value);
|
|
324
|
+
case 'Uint8Array':
|
|
325
|
+
return yield* Uint8Array(anySchema, value);
|
|
326
|
+
case 'Unknown':
|
|
327
|
+
return yield* Unknown(anySchema, value);
|
|
328
|
+
case 'Void':
|
|
329
|
+
return yield* Void(anySchema, value);
|
|
330
|
+
default:
|
|
331
|
+
throw new TypeCompilerUnknownTypeError(schema);
|
|
344
332
|
}
|
|
345
333
|
}
|
|
346
334
|
// -------------------------------------------------------------------
|
|
@@ -396,9 +384,16 @@ var TypeCompiler;
|
|
|
396
384
|
}
|
|
397
385
|
/** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
|
|
398
386
|
function Compile(schema, references = []) {
|
|
387
|
+
index_2.TypeGuard.Assert(schema, references);
|
|
399
388
|
const code = Build(schema, references);
|
|
400
|
-
const
|
|
401
|
-
|
|
389
|
+
const func1 = globalThis.Function('format', code);
|
|
390
|
+
const func2 = func1((format, value) => {
|
|
391
|
+
if (!index_3.Format.Has(format))
|
|
392
|
+
return false;
|
|
393
|
+
const func = index_3.Format.Get(format);
|
|
394
|
+
return func(value);
|
|
395
|
+
});
|
|
396
|
+
return new TypeCheck(schema, references, func2, code);
|
|
402
397
|
}
|
|
403
398
|
TypeCompiler.Compile = Compile;
|
|
404
399
|
})(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
|
|
@@ -30,7 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
30
|
exports.Conditional = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const structural_1 = require("./structural");
|
|
33
|
-
const
|
|
33
|
+
const index_1 = require("../guard/index");
|
|
34
34
|
/** Conditional Types */
|
|
35
35
|
var Conditional;
|
|
36
36
|
(function (Conditional) {
|
|
@@ -59,7 +59,7 @@ var Conditional;
|
|
|
59
59
|
Conditional.Exclude = Exclude;
|
|
60
60
|
/** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */
|
|
61
61
|
function Extract(type, union, options = {}) {
|
|
62
|
-
if (
|
|
62
|
+
if (index_1.TypeGuard.TUnion(type)) {
|
|
63
63
|
const anyOf = type.anyOf.filter((schema) => structural_1.Structural.Check(schema, union) === structural_1.StructuralResult.True).map((schema) => Clone(schema));
|
|
64
64
|
return { ...options, [Types.Kind]: 'Union', anyOf };
|
|
65
65
|
}
|
package/errors/errors.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface ValueError {
|
|
|
47
47
|
value: unknown;
|
|
48
48
|
message: string;
|
|
49
49
|
}
|
|
50
|
-
export declare class
|
|
50
|
+
export declare class ValueErrorsUnknownTypeError extends Error {
|
|
51
51
|
readonly schema: Types.TSchema;
|
|
52
52
|
constructor(schema: Types.TSchema);
|
|
53
53
|
}
|
package/errors/errors.js
CHANGED
|
@@ -27,8 +27,8 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueErrors = exports.
|
|
31
|
-
const
|
|
30
|
+
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
32
|
// -------------------------------------------------------------------
|
|
33
33
|
// ValueErrorType
|
|
34
34
|
// -------------------------------------------------------------------
|
|
@@ -77,13 +77,13 @@ var ValueErrorType;
|
|
|
77
77
|
// -------------------------------------------------------------------
|
|
78
78
|
// ValueErrors
|
|
79
79
|
// -------------------------------------------------------------------
|
|
80
|
-
class
|
|
80
|
+
class ValueErrorsUnknownTypeError extends Error {
|
|
81
81
|
constructor(schema) {
|
|
82
|
-
super('ValueErrors:
|
|
82
|
+
super('ValueErrors: Unknown type');
|
|
83
83
|
this.schema = schema;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
exports.
|
|
86
|
+
exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
87
87
|
var ValueErrors;
|
|
88
88
|
(function (ValueErrors) {
|
|
89
89
|
function* Any(schema, references, path, value) { }
|
|
@@ -306,72 +306,53 @@ var ValueErrors;
|
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
function* Visit(schema, references, path, value) {
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return yield* Tuple(schema, refs, path, value);
|
|
357
|
-
}
|
|
358
|
-
else if (index_1.TypeGuard.TUndefined(schema)) {
|
|
359
|
-
return yield* Undefined(schema, refs, path, value);
|
|
360
|
-
}
|
|
361
|
-
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
362
|
-
return yield* Union(schema, refs, path, value);
|
|
363
|
-
}
|
|
364
|
-
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
365
|
-
return yield* Uint8Array(schema, refs, path, value);
|
|
366
|
-
}
|
|
367
|
-
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
368
|
-
return yield* Unknown(schema, refs, path, value);
|
|
369
|
-
}
|
|
370
|
-
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
371
|
-
return yield* Void(schema, refs, path, value);
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
throw new ValueErrorsInvalidTypeError(schema);
|
|
309
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
310
|
+
const anySchema = schema;
|
|
311
|
+
switch (anySchema[Types.Kind]) {
|
|
312
|
+
case 'Any':
|
|
313
|
+
return yield* Any(anySchema, anyReferences, path, value);
|
|
314
|
+
case 'Array':
|
|
315
|
+
return yield* Array(anySchema, anyReferences, path, value);
|
|
316
|
+
case 'Boolean':
|
|
317
|
+
return yield* Boolean(anySchema, anyReferences, path, value);
|
|
318
|
+
case 'Constructor':
|
|
319
|
+
return yield* Constructor(anySchema, anyReferences, path, value);
|
|
320
|
+
case 'Function':
|
|
321
|
+
return yield* Function(anySchema, anyReferences, path, value);
|
|
322
|
+
case 'Integer':
|
|
323
|
+
return yield* Integer(anySchema, anyReferences, path, value);
|
|
324
|
+
case 'Literal':
|
|
325
|
+
return yield* Literal(anySchema, anyReferences, path, value);
|
|
326
|
+
case 'Null':
|
|
327
|
+
return yield* Null(anySchema, anyReferences, path, value);
|
|
328
|
+
case 'Number':
|
|
329
|
+
return yield* Number(anySchema, anyReferences, path, value);
|
|
330
|
+
case 'Object':
|
|
331
|
+
return yield* Object(anySchema, anyReferences, path, value);
|
|
332
|
+
case 'Promise':
|
|
333
|
+
return yield* Promise(anySchema, anyReferences, path, value);
|
|
334
|
+
case 'Record':
|
|
335
|
+
return yield* Record(anySchema, anyReferences, path, value);
|
|
336
|
+
case 'Ref':
|
|
337
|
+
return yield* Ref(anySchema, anyReferences, path, value);
|
|
338
|
+
case 'Self':
|
|
339
|
+
return yield* Self(anySchema, anyReferences, path, value);
|
|
340
|
+
case 'String':
|
|
341
|
+
return yield* String(anySchema, anyReferences, path, value);
|
|
342
|
+
case 'Tuple':
|
|
343
|
+
return yield* Tuple(anySchema, anyReferences, path, value);
|
|
344
|
+
case 'Undefined':
|
|
345
|
+
return yield* Undefined(anySchema, anyReferences, path, value);
|
|
346
|
+
case 'Union':
|
|
347
|
+
return yield* Union(anySchema, anyReferences, path, value);
|
|
348
|
+
case 'Uint8Array':
|
|
349
|
+
return yield* Uint8Array(anySchema, anyReferences, path, value);
|
|
350
|
+
case 'Unknown':
|
|
351
|
+
return yield* Unknown(anySchema, anyReferences, path, value);
|
|
352
|
+
case 'Void':
|
|
353
|
+
return yield* Void(anySchema, anyReferences, path, value);
|
|
354
|
+
default:
|
|
355
|
+
throw new ValueErrorsUnknownTypeError(schema);
|
|
375
356
|
}
|
|
376
357
|
}
|
|
377
358
|
function* Errors(schema, references, value) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare type FormatValidationFunction = (value: string) => boolean;
|
|
2
|
+
/** Shared string formats used by the TypeCompiler and Value modules */
|
|
3
|
+
export declare namespace Format {
|
|
4
|
+
/** Clears all formats */
|
|
5
|
+
function Clear(format: string): void;
|
|
6
|
+
/** Returns true if the string format exists */
|
|
7
|
+
function Has(format: string): boolean;
|
|
8
|
+
/** Sets a string format validation function */
|
|
9
|
+
function Set(format: string, func: FormatValidationFunction): void;
|
|
10
|
+
/** Gets a string format validation function */
|
|
11
|
+
function Get(format: string): FormatValidationFunction | undefined;
|
|
12
|
+
}
|
package/format/format.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/format
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Format = void 0;
|
|
31
|
+
/** Shared string formats used by the TypeCompiler and Value modules */
|
|
32
|
+
var Format;
|
|
33
|
+
(function (Format) {
|
|
34
|
+
const formats = new Map();
|
|
35
|
+
/** Clears all formats */
|
|
36
|
+
function Clear(format) {
|
|
37
|
+
return formats.clear();
|
|
38
|
+
}
|
|
39
|
+
Format.Clear = Clear;
|
|
40
|
+
/** Returns true if the string format exists */
|
|
41
|
+
function Has(format) {
|
|
42
|
+
return formats.has(format);
|
|
43
|
+
}
|
|
44
|
+
Format.Has = Has;
|
|
45
|
+
/** Sets a string format validation function */
|
|
46
|
+
function Set(format, func) {
|
|
47
|
+
formats.set(format, func);
|
|
48
|
+
}
|
|
49
|
+
Format.Set = Set;
|
|
50
|
+
/** Gets a string format validation function */
|
|
51
|
+
function Get(format) {
|
|
52
|
+
return formats.get(format);
|
|
53
|
+
}
|
|
54
|
+
Format.Get = Get;
|
|
55
|
+
})(Format = exports.Format || (exports.Format = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './format';
|
package/format/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/format
|
|
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
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
32
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
33
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
34
|
+
}
|
|
35
|
+
Object.defineProperty(o, k2, desc);
|
|
36
|
+
}) : (function(o, m, k, k2) {
|
|
37
|
+
if (k2 === undefined) k2 = k;
|
|
38
|
+
o[k2] = m[k];
|
|
39
|
+
}));
|
|
40
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
41
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
__exportStar(require("./format"), exports);
|
package/guard/guard.d.ts
CHANGED
|
@@ -1,48 +1,54 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
+
export declare class TypeGuardInvalidTypeError extends Error {
|
|
3
|
+
readonly schema: unknown;
|
|
4
|
+
constructor(schema: unknown);
|
|
5
|
+
}
|
|
2
6
|
/** TypeGuard tests that values conform to a known TypeBox type specification */
|
|
3
7
|
export declare namespace TypeGuard {
|
|
4
8
|
/** Returns true if the given schema is TAny */
|
|
5
|
-
function TAny(schema:
|
|
9
|
+
function TAny(schema: unknown): schema is Types.TAny;
|
|
6
10
|
/** Returns true if the given schema is TArray */
|
|
7
|
-
function TArray(schema:
|
|
11
|
+
function TArray(schema: unknown): schema is Types.TArray;
|
|
8
12
|
/** Returns true if the given schema is TBoolean */
|
|
9
|
-
function TBoolean(schema:
|
|
13
|
+
function TBoolean(schema: unknown): schema is Types.TBoolean;
|
|
10
14
|
/** Returns true if the given schema is TConstructor */
|
|
11
|
-
function TConstructor(schema:
|
|
15
|
+
function TConstructor(schema: unknown): schema is Types.TConstructor;
|
|
12
16
|
/** Returns true if the given schema is TFunction */
|
|
13
|
-
function TFunction(schema:
|
|
17
|
+
function TFunction(schema: unknown): schema is Types.TFunction;
|
|
14
18
|
/** Returns true if the given schema is TInteger */
|
|
15
|
-
function TInteger(schema:
|
|
19
|
+
function TInteger(schema: unknown): schema is Types.TInteger;
|
|
16
20
|
/** Returns true if the given schema is TLiteral */
|
|
17
|
-
function TLiteral(schema:
|
|
21
|
+
function TLiteral(schema: unknown): schema is Types.TLiteral;
|
|
18
22
|
/** Returns true if the given schema is TNull */
|
|
19
|
-
function TNull(schema:
|
|
23
|
+
function TNull(schema: unknown): schema is Types.TNull;
|
|
20
24
|
/** Returns true if the given schema is TNumber */
|
|
21
|
-
function TNumber(schema:
|
|
25
|
+
function TNumber(schema: unknown): schema is Types.TNumber;
|
|
22
26
|
/** Returns true if the given schema is TObject */
|
|
23
|
-
function TObject(schema:
|
|
27
|
+
function TObject(schema: unknown): schema is Types.TObject;
|
|
24
28
|
/** Returns true if the given schema is TPromise */
|
|
25
|
-
function TPromise(schema:
|
|
29
|
+
function TPromise(schema: unknown): schema is Types.TPromise;
|
|
26
30
|
/** Returns true if the given schema is TRecord */
|
|
27
|
-
function TRecord(schema:
|
|
31
|
+
function TRecord(schema: unknown): schema is Types.TRecord;
|
|
28
32
|
/** Returns true if the given schema is TSelf */
|
|
29
|
-
function TSelf(schema:
|
|
33
|
+
function TSelf(schema: unknown): schema is Types.TSelf;
|
|
30
34
|
/** Returns true if the given schema is TRef */
|
|
31
|
-
function TRef(schema:
|
|
35
|
+
function TRef(schema: unknown): schema is Types.TRef;
|
|
32
36
|
/** Returns true if the given schema is TString */
|
|
33
|
-
function TString(schema:
|
|
37
|
+
function TString(schema: unknown): schema is Types.TString;
|
|
34
38
|
/** Returns true if the given schema is TTuple */
|
|
35
|
-
function TTuple(schema:
|
|
39
|
+
function TTuple(schema: unknown): schema is Types.TTuple;
|
|
36
40
|
/** Returns true if the given schema is TUndefined */
|
|
37
|
-
function TUndefined(schema:
|
|
41
|
+
function TUndefined(schema: unknown): schema is Types.TUndefined;
|
|
38
42
|
/** Returns true if the given schema is TUnion */
|
|
39
|
-
function TUnion(schema:
|
|
43
|
+
function TUnion(schema: unknown): schema is Types.TUnion;
|
|
40
44
|
/** Returns true if the given schema is TUint8Array */
|
|
41
|
-
function TUint8Array(schema:
|
|
45
|
+
function TUint8Array(schema: unknown): schema is Types.TUint8Array;
|
|
42
46
|
/** Returns true if the given schema is TUnknown */
|
|
43
|
-
function TUnknown(schema:
|
|
47
|
+
function TUnknown(schema: unknown): schema is Types.TUnknown;
|
|
44
48
|
/** Returns true if the given schema is TVoid */
|
|
45
|
-
function TVoid(schema:
|
|
49
|
+
function TVoid(schema: unknown): schema is Types.TVoid;
|
|
46
50
|
/** Returns true if the given schema is TSchema */
|
|
47
|
-
function TSchema(schema:
|
|
51
|
+
function TSchema(schema: unknown): schema is Types.TSchema;
|
|
52
|
+
/** Asserts if this schema and associated references are valid. */
|
|
53
|
+
function Assert<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): void;
|
|
48
54
|
}
|