@sinclair/typebox 0.24.28 → 0.24.31
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.js +12 -2
- 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.js +7 -4
- package/package.json +8 -8
- package/readme.md +121 -49
- package/value/cast.d.ts +3 -0
- package/value/cast.js +47 -4
- package/value/check.js +7 -0
- package/value/create.js +8 -0
- package/value/value.d.ts +4 -4
package/compiler/compiler.js
CHANGED
|
@@ -30,6 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
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");
|
|
33
34
|
const Types = require("../typebox");
|
|
34
35
|
// -------------------------------------------------------------------
|
|
35
36
|
// TypeCheck
|
|
@@ -236,6 +237,9 @@ var TypeCompiler;
|
|
|
236
237
|
const local = PushLocal(`new RegExp(/${schema.pattern}/);`);
|
|
237
238
|
yield `(${local}.test(${value}))`;
|
|
238
239
|
}
|
|
240
|
+
if (schema.format !== undefined) {
|
|
241
|
+
yield `(format('${schema.format}', ${value}))`;
|
|
242
|
+
}
|
|
239
243
|
}
|
|
240
244
|
function* Tuple(schema, value) {
|
|
241
245
|
yield `(Array.isArray(${value}))`;
|
|
@@ -382,8 +386,14 @@ var TypeCompiler;
|
|
|
382
386
|
function Compile(schema, references = []) {
|
|
383
387
|
index_2.TypeGuard.Assert(schema, references);
|
|
384
388
|
const code = Build(schema, references);
|
|
385
|
-
const
|
|
386
|
-
|
|
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);
|
|
387
397
|
}
|
|
388
398
|
TypeCompiler.Compile = Compile;
|
|
389
399
|
})(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
|
|
@@ -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(): 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() {
|
|
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.js
CHANGED
|
@@ -54,7 +54,7 @@ var TypeGuard;
|
|
|
54
54
|
return false;
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
function
|
|
57
|
+
function IsControlCharacterFree(value) {
|
|
58
58
|
if (typeof value !== 'string')
|
|
59
59
|
return false;
|
|
60
60
|
for (let i = 0; i < value.length; i++) {
|
|
@@ -84,7 +84,10 @@ var TypeGuard;
|
|
|
84
84
|
return value === undefined || (value !== undefined && IsString(value));
|
|
85
85
|
}
|
|
86
86
|
function IsOptionalPattern(value) {
|
|
87
|
-
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));
|
|
88
91
|
}
|
|
89
92
|
/** Returns true if the given schema is TAny */
|
|
90
93
|
function TAny(schema) {
|
|
@@ -181,7 +184,7 @@ var TypeGuard;
|
|
|
181
184
|
return false;
|
|
182
185
|
}
|
|
183
186
|
for (const [key, value] of Object.entries(schema.properties)) {
|
|
184
|
-
if (!
|
|
187
|
+
if (!IsControlCharacterFree(key))
|
|
185
188
|
return false;
|
|
186
189
|
if (!TSchema(value))
|
|
187
190
|
return false;
|
|
@@ -231,7 +234,7 @@ var TypeGuard;
|
|
|
231
234
|
IsOptionalNumber(schema.minLength) &&
|
|
232
235
|
IsOptionalNumber(schema.maxLength) &&
|
|
233
236
|
IsOptionalPattern(schema.pattern) &&
|
|
234
|
-
|
|
237
|
+
IsOptionalFormat(schema.format));
|
|
235
238
|
}
|
|
236
239
|
TypeGuard.TString = TString;
|
|
237
240
|
/** Returns true if the given schema is TTuple */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.31",
|
|
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.17.
|
|
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
|
}
|
package/readme.md
CHANGED
|
@@ -39,6 +39,8 @@ import { Static, Type } from '@sinclair/typebox'
|
|
|
39
39
|
const T = Type.String() // const T = { type: 'string' }
|
|
40
40
|
|
|
41
41
|
type T = Static<typeof T> // type T = string
|
|
42
|
+
|
|
43
|
+
|
|
42
44
|
```
|
|
43
45
|
|
|
44
46
|
<a name="Overview"></a>
|
|
@@ -69,6 +71,7 @@ License MIT
|
|
|
69
71
|
- [Strict](#strict)
|
|
70
72
|
- [Validation](#validation)
|
|
71
73
|
- [Compiler](#compiler)
|
|
74
|
+
- [Formats](#formats)
|
|
72
75
|
- [Benchmark](#benchmark)
|
|
73
76
|
- [Contribute](#contribute)
|
|
74
77
|
|
|
@@ -145,6 +148,8 @@ function receive(value: T) { // ... as a Type
|
|
|
145
148
|
// ok...
|
|
146
149
|
}
|
|
147
150
|
}
|
|
151
|
+
|
|
152
|
+
|
|
148
153
|
```
|
|
149
154
|
|
|
150
155
|
## Types
|
|
@@ -339,6 +344,8 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
|
|
|
339
344
|
│ │ │ } │
|
|
340
345
|
│ │ │ │
|
|
341
346
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
347
|
+
|
|
348
|
+
|
|
342
349
|
```
|
|
343
350
|
|
|
344
351
|
## Modifiers
|
|
@@ -381,6 +388,8 @@ TypeBox provides modifiers that can be applied to an objects properties. This al
|
|
|
381
388
|
│ │ │ } │
|
|
382
389
|
│ │ │ │
|
|
383
390
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
391
|
+
|
|
392
|
+
|
|
384
393
|
```
|
|
385
394
|
|
|
386
395
|
## Options
|
|
@@ -396,6 +405,8 @@ const T = Type.Number({ multipleOf: 2 })
|
|
|
396
405
|
|
|
397
406
|
// array must have at least 5 integer values
|
|
398
407
|
const T = Type.Array(Type.Integer(), { minItems: 5 })
|
|
408
|
+
|
|
409
|
+
|
|
399
410
|
```
|
|
400
411
|
|
|
401
412
|
## Extended Types
|
|
@@ -458,6 +469,8 @@ In addition to JSON schema types, TypeBox provides several extended types that a
|
|
|
458
469
|
│ │ │ } │
|
|
459
470
|
│ │ │ │
|
|
460
471
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
472
|
+
|
|
473
|
+
|
|
461
474
|
```
|
|
462
475
|
|
|
463
476
|
## Reference Types
|
|
@@ -473,6 +486,8 @@ const T = Type.String({ $id: 'T' }) // const T = {
|
|
|
473
486
|
const R = Type.Ref(T) // const R = {
|
|
474
487
|
// $ref: 'T'
|
|
475
488
|
// }
|
|
489
|
+
|
|
490
|
+
|
|
476
491
|
```
|
|
477
492
|
|
|
478
493
|
## Recursive Types
|
|
@@ -510,6 +525,8 @@ function test(node: Node) {
|
|
|
510
525
|
.nodes[0].nodes[0]
|
|
511
526
|
.id
|
|
512
527
|
}
|
|
528
|
+
|
|
529
|
+
|
|
513
530
|
```
|
|
514
531
|
|
|
515
532
|
## Generic Types
|
|
@@ -540,6 +557,8 @@ const U = Nullable(Type.Number()) // const U = {
|
|
|
540
557
|
// }
|
|
541
558
|
|
|
542
559
|
type U = Static<typeof U> // type U = number | null
|
|
560
|
+
|
|
561
|
+
|
|
543
562
|
```
|
|
544
563
|
|
|
545
564
|
## Unsafe Types
|
|
@@ -552,6 +571,8 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
552
571
|
// }
|
|
553
572
|
|
|
554
573
|
type T = Static<typeof T> // type T = string
|
|
574
|
+
|
|
575
|
+
|
|
555
576
|
```
|
|
556
577
|
|
|
557
578
|
This function can be used to create custom schemas for validators that require specific schema representations. An example of this might be OpenAPI's `nullable` and `enum` schemas which are not provided by TypeBox. The following demonstrates using `Type.Unsafe(...)` to create these types.
|
|
@@ -592,6 +613,8 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
|
592
613
|
// }
|
|
593
614
|
|
|
594
615
|
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
616
|
+
|
|
617
|
+
|
|
595
618
|
```
|
|
596
619
|
|
|
597
620
|
## Conditional Types
|
|
@@ -643,6 +666,8 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
643
666
|
│ ) │ │ │
|
|
644
667
|
│ │ │ │
|
|
645
668
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
669
|
+
|
|
670
|
+
|
|
646
671
|
```
|
|
647
672
|
|
|
648
673
|
## Values
|
|
@@ -704,6 +729,8 @@ if(TypeGuard.TString(T)) {
|
|
|
704
729
|
|
|
705
730
|
// T is TString
|
|
706
731
|
}
|
|
732
|
+
|
|
733
|
+
|
|
707
734
|
```
|
|
708
735
|
|
|
709
736
|
## Strict
|
|
@@ -731,6 +758,8 @@ const U = Type.Strict(T) // const U = {
|
|
|
731
758
|
// }
|
|
732
759
|
// }
|
|
733
760
|
// }
|
|
761
|
+
|
|
762
|
+
|
|
734
763
|
```
|
|
735
764
|
|
|
736
765
|
## Validation
|
|
@@ -794,6 +823,8 @@ const T = Type.Object({
|
|
|
794
823
|
//--------------------------------------------------------------------------------------------
|
|
795
824
|
|
|
796
825
|
const R = ajv.validate(T, { x: 1, y: 2, z: 3 }) // const R = true
|
|
826
|
+
|
|
827
|
+
|
|
797
828
|
```
|
|
798
829
|
|
|
799
830
|
Please refer to the official Ajv [documentation](https://ajv.js.org/guide/getting-started.html) for additional information on using Ajv.
|
|
@@ -818,6 +849,8 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
|
|
|
818
849
|
})) // }>>
|
|
819
850
|
|
|
820
851
|
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
852
|
+
|
|
853
|
+
|
|
821
854
|
```
|
|
822
855
|
|
|
823
856
|
Validation errors can be read with the `Errors(...)` function.
|
|
@@ -859,9 +892,47 @@ console.log(C.Code()) // return function check(va
|
|
|
859
892
|
// (typeof value === 'string')
|
|
860
893
|
// )
|
|
861
894
|
// }
|
|
895
|
+
|
|
896
|
+
|
|
862
897
|
```
|
|
863
898
|
|
|
899
|
+
## Formats
|
|
900
|
+
|
|
901
|
+
Use the `Format` module to create custom string formats. Formats provide greater flexibility over [string patterns](https://json-schema.org/understanding-json-schema/reference/regular_expressions.html), but may come at a cost of schema portability. Formats are internally shared between the TypeCompiler and Value modules. TypeBox does not provide any built in formats by default.
|
|
864
902
|
|
|
903
|
+
The format module is an optional import.
|
|
904
|
+
|
|
905
|
+
```typescript
|
|
906
|
+
import { Format } from '@sinclair/typebox/format'
|
|
907
|
+
```
|
|
908
|
+
|
|
909
|
+
The following creates a custom format to validate Mongo `ObjectId` strings
|
|
910
|
+
|
|
911
|
+
```typescript
|
|
912
|
+
//--------------------------------------------------------------------------------------------
|
|
913
|
+
//
|
|
914
|
+
// Format.Set(...) to create the format
|
|
915
|
+
//
|
|
916
|
+
//--------------------------------------------------------------------------------------------
|
|
917
|
+
|
|
918
|
+
Format.Set('ObjectId', value => /^[0-9a-fA-F]{24}$/.test(value))
|
|
919
|
+
|
|
920
|
+
//--------------------------------------------------------------------------------------------
|
|
921
|
+
//
|
|
922
|
+
// The format can now be used by TypeCompiler and Value modules
|
|
923
|
+
//
|
|
924
|
+
//--------------------------------------------------------------------------------------------
|
|
925
|
+
|
|
926
|
+
const T = Type.String({ format: 'ObjectId' })
|
|
927
|
+
|
|
928
|
+
const V = '507f1f77bcf86cd799439011'
|
|
929
|
+
|
|
930
|
+
const R1 = TypeCompiler.Compile(T).Check(V) // R1 = true
|
|
931
|
+
|
|
932
|
+
const R2 = Value.Check(T, V) // R2 = true
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
```
|
|
865
936
|
|
|
866
937
|
## Benchmark
|
|
867
938
|
|
|
@@ -877,29 +948,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
877
948
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
878
949
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
879
950
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
880
|
-
│ Number │ 2000 │ '
|
|
881
|
-
│ String │ 2000 │ '
|
|
882
|
-
│ Boolean │ 2000 │ '
|
|
883
|
-
│ Null │ 2000 │ '
|
|
884
|
-
│ RegEx │ 2000 │ '
|
|
885
|
-
│ ObjectA │ 2000 │ '
|
|
886
|
-
│ ObjectB │ 2000 │ '
|
|
887
|
-
│ Tuple │ 2000 │ '
|
|
888
|
-
│ Union │ 2000 │ '
|
|
889
|
-
│ Vector4 │ 2000 │ '
|
|
890
|
-
│ Matrix4 │ 2000 │ '
|
|
891
|
-
│ Literal_String │ 2000 │ '
|
|
892
|
-
│ Literal_Number │ 2000 │ '
|
|
893
|
-
│ Literal_Boolean │ 2000 │ '
|
|
894
|
-
│ Array_Number │ 2000 │ '
|
|
895
|
-
│ Array_String │ 2000 │ '
|
|
896
|
-
│ Array_Boolean │ 2000 │ '
|
|
897
|
-
│ Array_ObjectA │ 2000 │ '
|
|
898
|
-
│ Array_ObjectB │ 2000 │ '
|
|
899
|
-
│ Array_Tuple │ 2000 │ '
|
|
900
|
-
│ Array_Union │ 2000 │ '
|
|
901
|
-
│ Array_Vector4 │ 2000 │ '
|
|
902
|
-
│ Array_Matrix4 │ 2000 │ '
|
|
951
|
+
│ Number │ 2000 │ ' 402 ms' │ ' 8 ms' │ ' 50.25 x' │
|
|
952
|
+
│ String │ 2000 │ ' 316 ms' │ ' 8 ms' │ ' 39.50 x' │
|
|
953
|
+
│ Boolean │ 2000 │ ' 308 ms' │ ' 6 ms' │ ' 51.33 x' │
|
|
954
|
+
│ Null │ 2000 │ ' 259 ms' │ ' 5 ms' │ ' 51.80 x' │
|
|
955
|
+
│ RegEx │ 2000 │ ' 479 ms' │ ' 11 ms' │ ' 43.55 x' │
|
|
956
|
+
│ ObjectA │ 2000 │ ' 2766 ms' │ ' 41 ms' │ ' 67.46 x' │
|
|
957
|
+
│ ObjectB │ 2000 │ ' 3062 ms' │ ' 29 ms' │ ' 105.59 x' │
|
|
958
|
+
│ Tuple │ 2000 │ ' 1258 ms' │ ' 21 ms' │ ' 59.90 x' │
|
|
959
|
+
│ Union │ 2000 │ ' 1449 ms' │ ' 29 ms' │ ' 49.97 x' │
|
|
960
|
+
│ Vector4 │ 2000 │ ' 1616 ms' │ ' 16 ms' │ ' 101.00 x' │
|
|
961
|
+
│ Matrix4 │ 2000 │ ' 975 ms' │ ' 8 ms' │ ' 121.88 x' │
|
|
962
|
+
│ Literal_String │ 2000 │ ' 350 ms' │ ' 6 ms' │ ' 58.33 x' │
|
|
963
|
+
│ Literal_Number │ 2000 │ ' 386 ms' │ ' 5 ms' │ ' 77.20 x' │
|
|
964
|
+
│ Literal_Boolean │ 2000 │ ' 377 ms' │ ' 4 ms' │ ' 94.25 x' │
|
|
965
|
+
│ Array_Number │ 2000 │ ' 737 ms' │ ' 7 ms' │ ' 105.29 x' │
|
|
966
|
+
│ Array_String │ 2000 │ ' 765 ms' │ ' 7 ms' │ ' 109.29 x' │
|
|
967
|
+
│ Array_Boolean │ 2000 │ ' 783 ms' │ ' 8 ms' │ ' 97.88 x' │
|
|
968
|
+
│ Array_ObjectA │ 2000 │ ' 3578 ms' │ ' 31 ms' │ ' 115.42 x' │
|
|
969
|
+
│ Array_ObjectB │ 2000 │ ' 3718 ms' │ ' 33 ms' │ ' 112.67 x' │
|
|
970
|
+
│ Array_Tuple │ 2000 │ ' 2259 ms' │ ' 14 ms' │ ' 161.36 x' │
|
|
971
|
+
│ Array_Union │ 2000 │ ' 1704 ms' │ ' 20 ms' │ ' 85.20 x' │
|
|
972
|
+
│ Array_Vector4 │ 2000 │ ' 2293 ms' │ ' 17 ms' │ ' 134.88 x' │
|
|
973
|
+
│ Array_Matrix4 │ 2000 │ ' 1575 ms' │ ' 12 ms' │ ' 131.25 x' │
|
|
903
974
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
904
975
|
```
|
|
905
976
|
|
|
@@ -911,31 +982,31 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
911
982
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
912
983
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
913
984
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
914
|
-
│ Number │ 1000000 │ '
|
|
915
|
-
│ String │ 1000000 │ ' 26 ms' │ '
|
|
916
|
-
│ Boolean │ 1000000 │ '
|
|
917
|
-
│ Null │ 1000000 │ '
|
|
918
|
-
│ RegEx │ 1000000 │ '
|
|
919
|
-
│ ObjectA │ 1000000 │ '
|
|
920
|
-
│ ObjectB │ 1000000 │ '
|
|
921
|
-
│ Tuple │ 1000000 │ '
|
|
922
|
-
│ Union │ 1000000 │ '
|
|
923
|
-
│ Recursive │ 1000000 │ '
|
|
924
|
-
│ Vector4 │ 1000000 │ ' 145 ms' │ '
|
|
925
|
-
│ Matrix4 │ 1000000 │ '
|
|
926
|
-
│ Literal_String │ 1000000 │ '
|
|
927
|
-
│ Literal_Number │ 1000000 │ '
|
|
928
|
-
│ Literal_Boolean │ 1000000 │ '
|
|
929
|
-
│ Array_Number │ 1000000 │ '
|
|
930
|
-
│ Array_String │ 1000000 │ ' 438 ms' │ '
|
|
931
|
-
│ Array_Boolean │ 1000000 │ '
|
|
932
|
-
│ Array_ObjectA │ 1000000 │ '
|
|
933
|
-
│ Array_ObjectB │ 1000000 │ '
|
|
934
|
-
│ Array_Tuple │ 1000000 │ '
|
|
935
|
-
│ Array_Union │ 1000000 │ '
|
|
936
|
-
│ Array_Recursive │ 1000000 │ '
|
|
937
|
-
│ Array_Vector4 │ 1000000 │ '
|
|
938
|
-
│ Array_Matrix4 │ 1000000 │ '
|
|
985
|
+
│ Number │ 1000000 │ ' 38 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
|
|
986
|
+
│ String │ 1000000 │ ' 26 ms' │ ' 23 ms' │ ' 12 ms' │ ' 1.92 x' │
|
|
987
|
+
│ Boolean │ 1000000 │ ' 26 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
|
|
988
|
+
│ Null │ 1000000 │ ' 29 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
|
|
989
|
+
│ RegEx │ 1000000 │ ' 169 ms' │ ' 47 ms' │ ' 37 ms' │ ' 1.27 x' │
|
|
990
|
+
│ ObjectA │ 1000000 │ ' 551 ms' │ ' 45 ms' │ ' 23 ms' │ ' 1.96 x' │
|
|
991
|
+
│ ObjectB │ 1000000 │ ' 995 ms' │ ' 49 ms' │ ' 39 ms' │ ' 1.26 x' │
|
|
992
|
+
│ Tuple │ 1000000 │ ' 115 ms' │ ' 30 ms' │ ' 14 ms' │ ' 2.14 x' │
|
|
993
|
+
│ Union │ 1000000 │ ' 294 ms' │ ' 30 ms' │ ' 14 ms' │ ' 2.14 x' │
|
|
994
|
+
│ Recursive │ 1000000 │ ' 3308 ms' │ ' 429 ms' │ ' 174 ms' │ ' 2.47 x' │
|
|
995
|
+
│ Vector4 │ 1000000 │ ' 145 ms' │ ' 25 ms' │ ' 13 ms' │ ' 1.92 x' │
|
|
996
|
+
│ Matrix4 │ 1000000 │ ' 663 ms' │ ' 42 ms' │ ' 34 ms' │ ' 1.24 x' │
|
|
997
|
+
│ Literal_String │ 1000000 │ ' 46 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
998
|
+
│ Literal_Number │ 1000000 │ ' 50 ms' │ ' 26 ms' │ ' 11 ms' │ ' 2.36 x' │
|
|
999
|
+
│ Literal_Boolean │ 1000000 │ ' 45 ms' │ ' 24 ms' │ ' 11 ms' │ ' 2.18 x' │
|
|
1000
|
+
│ Array_Number │ 1000000 │ ' 411 ms' │ ' 35 ms' │ ' 19 ms' │ ' 1.84 x' │
|
|
1001
|
+
│ Array_String │ 1000000 │ ' 438 ms' │ ' 33 ms' │ ' 20 ms' │ ' 1.65 x' │
|
|
1002
|
+
│ Array_Boolean │ 1000000 │ ' 444 ms' │ ' 38 ms' │ ' 24 ms' │ ' 1.58 x' │
|
|
1003
|
+
│ Array_ObjectA │ 1000000 │ ' 13714 ms' │ ' 2819 ms' │ ' 1791 ms' │ ' 1.57 x' │
|
|
1004
|
+
│ Array_ObjectB │ 1000000 │ ' 15855 ms' │ ' 2965 ms' │ ' 2066 ms' │ ' 1.44 x' │
|
|
1005
|
+
│ Array_Tuple │ 1000000 │ ' 1682 ms' │ ' 94 ms' │ ' 71 ms' │ ' 1.32 x' │
|
|
1006
|
+
│ Array_Union │ 1000000 │ ' 4575 ms' │ ' 239 ms' │ ' 86 ms' │ ' 2.78 x' │
|
|
1007
|
+
│ Array_Recursive │ 1000000 │ ' 51970 ms' │ ' 7192 ms' │ ' 2617 ms' │ ' 2.75 x' │
|
|
1008
|
+
│ Array_Vector4 │ 1000000 │ ' 2097 ms' │ ' 100 ms' │ ' 54 ms' │ ' 1.85 x' │
|
|
1009
|
+
│ Array_Matrix4 │ 1000000 │ ' 11596 ms' │ ' 381 ms' │ ' 327 ms' │ ' 1.17 x' │
|
|
939
1010
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
940
1011
|
```
|
|
941
1012
|
|
|
@@ -947,8 +1018,9 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
947
1018
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
948
1019
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
949
1020
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
950
|
-
│ typebox/compiler │ '
|
|
1021
|
+
│ typebox/compiler │ ' 48 kb' │ ' 24 kb' │ '2.00 x' │
|
|
951
1022
|
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.47 x' │
|
|
1023
|
+
│ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.68 x' │
|
|
952
1024
|
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.08 x' │
|
|
953
1025
|
│ typebox/value │ ' 55 kb' │ ' 25 kb' │ '2.15 x' │
|
|
954
1026
|
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.91 x' │
|
package/value/cast.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ export declare class ValueCastUnknownTypeError extends Error {
|
|
|
3
3
|
readonly schema: Types.TSchema;
|
|
4
4
|
constructor(schema: Types.TSchema);
|
|
5
5
|
}
|
|
6
|
+
export interface ValueCastOptions {
|
|
7
|
+
convert: boolean;
|
|
8
|
+
}
|
|
6
9
|
export declare namespace ValueCast {
|
|
7
10
|
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
|
|
8
11
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
|
package/value/cast.js
CHANGED
|
@@ -80,6 +80,45 @@ class ValueCastUnknownTypeError extends Error {
|
|
|
80
80
|
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
81
81
|
var ValueCast;
|
|
82
82
|
(function (ValueCast) {
|
|
83
|
+
// -----------------------------------------------------------
|
|
84
|
+
// Conversion
|
|
85
|
+
// -----------------------------------------------------------
|
|
86
|
+
function IsString(value) {
|
|
87
|
+
return typeof value === 'string';
|
|
88
|
+
}
|
|
89
|
+
function IsBoolean(value) {
|
|
90
|
+
return typeof value === 'boolean';
|
|
91
|
+
}
|
|
92
|
+
function IsNumber(value) {
|
|
93
|
+
return typeof value === 'number';
|
|
94
|
+
}
|
|
95
|
+
function IsBigInt(value) {
|
|
96
|
+
return typeof value === 'bigint';
|
|
97
|
+
}
|
|
98
|
+
function IsStringNumeric(value) {
|
|
99
|
+
return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value));
|
|
100
|
+
}
|
|
101
|
+
function IsValueToString(value) {
|
|
102
|
+
return IsBigInt(value) || IsBoolean(value) || IsNumber(value);
|
|
103
|
+
}
|
|
104
|
+
function IsValueTrue(value) {
|
|
105
|
+
return (IsNumber(value) && value === 1) || (IsString(value) && ['true', 'TRUE', 'True'].includes(value));
|
|
106
|
+
}
|
|
107
|
+
function TryConvertString(value) {
|
|
108
|
+
return IsValueToString(value) ? value.toString() : value;
|
|
109
|
+
}
|
|
110
|
+
function TryConvertNumber(value) {
|
|
111
|
+
return IsStringNumeric(value) ? parseFloat(value) : value;
|
|
112
|
+
}
|
|
113
|
+
function TryConvertInteger(value) {
|
|
114
|
+
return IsStringNumeric(value) ? parseInt(value) : value;
|
|
115
|
+
}
|
|
116
|
+
function TryConvertBoolean(value) {
|
|
117
|
+
return IsValueTrue(value) ? true : value;
|
|
118
|
+
}
|
|
119
|
+
// -----------------------------------------------------------
|
|
120
|
+
// Casts
|
|
121
|
+
// -----------------------------------------------------------
|
|
83
122
|
function Any(schema, references, value) {
|
|
84
123
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
85
124
|
}
|
|
@@ -91,7 +130,8 @@ var ValueCast;
|
|
|
91
130
|
return value.map((val) => Visit(schema.items, references, val));
|
|
92
131
|
}
|
|
93
132
|
function Boolean(schema, references, value) {
|
|
94
|
-
|
|
133
|
+
const conversion = TryConvertBoolean(value);
|
|
134
|
+
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
95
135
|
}
|
|
96
136
|
function Constructor(schema, references, value) {
|
|
97
137
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
@@ -112,7 +152,8 @@ var ValueCast;
|
|
|
112
152
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
113
153
|
}
|
|
114
154
|
function Integer(schema, references, value) {
|
|
115
|
-
|
|
155
|
+
const conversion = TryConvertInteger(value);
|
|
156
|
+
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
116
157
|
}
|
|
117
158
|
function Literal(schema, references, value) {
|
|
118
159
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
@@ -121,7 +162,8 @@ var ValueCast;
|
|
|
121
162
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
122
163
|
}
|
|
123
164
|
function Number(schema, references, value) {
|
|
124
|
-
|
|
165
|
+
const conversion = TryConvertNumber(value);
|
|
166
|
+
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
125
167
|
}
|
|
126
168
|
function Object(schema, references, value) {
|
|
127
169
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
@@ -169,7 +211,8 @@ var ValueCast;
|
|
|
169
211
|
return Visit(reference, references, value);
|
|
170
212
|
}
|
|
171
213
|
function String(schema, references, value) {
|
|
172
|
-
|
|
214
|
+
const conversion = TryConvertString(value);
|
|
215
|
+
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
173
216
|
}
|
|
174
217
|
function Tuple(schema, references, value) {
|
|
175
218
|
if (check_1.ValueCheck.Check(schema, references, value))
|
package/value/check.js
CHANGED
|
@@ -29,6 +29,7 @@ THE SOFTWARE.
|
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
+
const format_1 = require("../format");
|
|
32
33
|
class ValueCheckUnknownTypeError extends Error {
|
|
33
34
|
constructor(schema) {
|
|
34
35
|
super('ValueCheck: Unknown type');
|
|
@@ -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) {
|
package/value/create.js
CHANGED
|
@@ -213,6 +213,14 @@ var ValueCreate;
|
|
|
213
213
|
return schema.default;
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
|
+
else if (schema.format !== undefined) {
|
|
217
|
+
if (schema.default === undefined) {
|
|
218
|
+
throw new Error('ValueCreate.String: String types with formats must specify a default value');
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
return schema.default;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
216
224
|
else {
|
|
217
225
|
if (schema.default !== undefined) {
|
|
218
226
|
return schema.default;
|
package/value/value.d.ts
CHANGED
|
@@ -6,13 +6,13 @@ 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
|
-
/** Casts a value into a
|
|
13
|
+
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
|
|
14
14
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
|
15
|
-
/** Casts a value into a
|
|
15
|
+
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
|
|
16
16
|
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
17
17
|
/** Returns an iterator for each error in this value. */
|
|
18
18
|
function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): IterableIterator<ValueError>;
|