@sinclair/typebox 0.24.28 → 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.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 +81 -49
- package/value/check.js +7 -0
- package/value/value.d.ts +2 -2
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(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.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.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.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
|
@@ -69,6 +69,7 @@ License MIT
|
|
|
69
69
|
- [Strict](#strict)
|
|
70
70
|
- [Validation](#validation)
|
|
71
71
|
- [Compiler](#compiler)
|
|
72
|
+
- [Formats](#formats)
|
|
72
73
|
- [Benchmark](#benchmark)
|
|
73
74
|
- [Contribute](#contribute)
|
|
74
75
|
|
|
@@ -861,7 +862,37 @@ console.log(C.Code()) // return function check(va
|
|
|
861
862
|
// }
|
|
862
863
|
```
|
|
863
864
|
|
|
865
|
+
## Formats
|
|
864
866
|
|
|
867
|
+
Use the `Format` module to define custom string formats.
|
|
868
|
+
|
|
869
|
+
```typescript
|
|
870
|
+
import { Format } from '@sinclair/typebox/format'
|
|
871
|
+
```
|
|
872
|
+
|
|
873
|
+
Formats are shared between `Value` and the `TypeCompiler` modules.
|
|
874
|
+
|
|
875
|
+
```typescript
|
|
876
|
+
//--------------------------------------------------------------------------------------------
|
|
877
|
+
//
|
|
878
|
+
// Use Format.Set(...) to define a format
|
|
879
|
+
//
|
|
880
|
+
//--------------------------------------------------------------------------------------------
|
|
881
|
+
|
|
882
|
+
Format.Set('ObjectId', value => /^[0-9a-fA-F]{24}$/.test(value))
|
|
883
|
+
|
|
884
|
+
//--------------------------------------------------------------------------------------------
|
|
885
|
+
//
|
|
886
|
+
// The format is now available to TypeCompiler and Value modules
|
|
887
|
+
//
|
|
888
|
+
//--------------------------------------------------------------------------------------------
|
|
889
|
+
|
|
890
|
+
const T = Type.String({ format: 'ObjectId' })
|
|
891
|
+
|
|
892
|
+
const R1 = TypeCompiler.Compile(T).Check('507f1f77bcf86cd799439011')
|
|
893
|
+
|
|
894
|
+
const R2 = Value.Check(T, '507f1f77bcf86cd799439011')
|
|
895
|
+
```
|
|
865
896
|
|
|
866
897
|
## Benchmark
|
|
867
898
|
|
|
@@ -877,29 +908,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
877
908
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
878
909
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
879
910
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
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 │ '
|
|
911
|
+
│ Number │ 2000 │ ' 402 ms' │ ' 8 ms' │ ' 50.25 x' │
|
|
912
|
+
│ String │ 2000 │ ' 316 ms' │ ' 8 ms' │ ' 39.50 x' │
|
|
913
|
+
│ Boolean │ 2000 │ ' 308 ms' │ ' 6 ms' │ ' 51.33 x' │
|
|
914
|
+
│ Null │ 2000 │ ' 259 ms' │ ' 5 ms' │ ' 51.80 x' │
|
|
915
|
+
│ RegEx │ 2000 │ ' 479 ms' │ ' 11 ms' │ ' 43.55 x' │
|
|
916
|
+
│ ObjectA │ 2000 │ ' 2766 ms' │ ' 41 ms' │ ' 67.46 x' │
|
|
917
|
+
│ ObjectB │ 2000 │ ' 3062 ms' │ ' 29 ms' │ ' 105.59 x' │
|
|
918
|
+
│ Tuple │ 2000 │ ' 1258 ms' │ ' 21 ms' │ ' 59.90 x' │
|
|
919
|
+
│ Union │ 2000 │ ' 1449 ms' │ ' 29 ms' │ ' 49.97 x' │
|
|
920
|
+
│ Vector4 │ 2000 │ ' 1616 ms' │ ' 16 ms' │ ' 101.00 x' │
|
|
921
|
+
│ Matrix4 │ 2000 │ ' 975 ms' │ ' 8 ms' │ ' 121.88 x' │
|
|
922
|
+
│ Literal_String │ 2000 │ ' 350 ms' │ ' 6 ms' │ ' 58.33 x' │
|
|
923
|
+
│ Literal_Number │ 2000 │ ' 386 ms' │ ' 5 ms' │ ' 77.20 x' │
|
|
924
|
+
│ Literal_Boolean │ 2000 │ ' 377 ms' │ ' 4 ms' │ ' 94.25 x' │
|
|
925
|
+
│ Array_Number │ 2000 │ ' 737 ms' │ ' 7 ms' │ ' 105.29 x' │
|
|
926
|
+
│ Array_String │ 2000 │ ' 765 ms' │ ' 7 ms' │ ' 109.29 x' │
|
|
927
|
+
│ Array_Boolean │ 2000 │ ' 783 ms' │ ' 8 ms' │ ' 97.88 x' │
|
|
928
|
+
│ Array_ObjectA │ 2000 │ ' 3578 ms' │ ' 31 ms' │ ' 115.42 x' │
|
|
929
|
+
│ Array_ObjectB │ 2000 │ ' 3718 ms' │ ' 33 ms' │ ' 112.67 x' │
|
|
930
|
+
│ Array_Tuple │ 2000 │ ' 2259 ms' │ ' 14 ms' │ ' 161.36 x' │
|
|
931
|
+
│ Array_Union │ 2000 │ ' 1704 ms' │ ' 20 ms' │ ' 85.20 x' │
|
|
932
|
+
│ Array_Vector4 │ 2000 │ ' 2293 ms' │ ' 17 ms' │ ' 134.88 x' │
|
|
933
|
+
│ Array_Matrix4 │ 2000 │ ' 1575 ms' │ ' 12 ms' │ ' 131.25 x' │
|
|
903
934
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
904
935
|
```
|
|
905
936
|
|
|
@@ -911,31 +942,31 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
911
942
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
912
943
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
913
944
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
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 │ '
|
|
945
|
+
│ Number │ 1000000 │ ' 38 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
|
|
946
|
+
│ String │ 1000000 │ ' 26 ms' │ ' 23 ms' │ ' 12 ms' │ ' 1.92 x' │
|
|
947
|
+
│ Boolean │ 1000000 │ ' 26 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
|
|
948
|
+
│ Null │ 1000000 │ ' 29 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
|
|
949
|
+
│ RegEx │ 1000000 │ ' 169 ms' │ ' 47 ms' │ ' 37 ms' │ ' 1.27 x' │
|
|
950
|
+
│ ObjectA │ 1000000 │ ' 551 ms' │ ' 45 ms' │ ' 23 ms' │ ' 1.96 x' │
|
|
951
|
+
│ ObjectB │ 1000000 │ ' 995 ms' │ ' 49 ms' │ ' 39 ms' │ ' 1.26 x' │
|
|
952
|
+
│ Tuple │ 1000000 │ ' 115 ms' │ ' 30 ms' │ ' 14 ms' │ ' 2.14 x' │
|
|
953
|
+
│ Union │ 1000000 │ ' 294 ms' │ ' 30 ms' │ ' 14 ms' │ ' 2.14 x' │
|
|
954
|
+
│ Recursive │ 1000000 │ ' 3308 ms' │ ' 429 ms' │ ' 174 ms' │ ' 2.47 x' │
|
|
955
|
+
│ Vector4 │ 1000000 │ ' 145 ms' │ ' 25 ms' │ ' 13 ms' │ ' 1.92 x' │
|
|
956
|
+
│ Matrix4 │ 1000000 │ ' 663 ms' │ ' 42 ms' │ ' 34 ms' │ ' 1.24 x' │
|
|
957
|
+
│ Literal_String │ 1000000 │ ' 46 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
958
|
+
│ Literal_Number │ 1000000 │ ' 50 ms' │ ' 26 ms' │ ' 11 ms' │ ' 2.36 x' │
|
|
959
|
+
│ Literal_Boolean │ 1000000 │ ' 45 ms' │ ' 24 ms' │ ' 11 ms' │ ' 2.18 x' │
|
|
960
|
+
│ Array_Number │ 1000000 │ ' 411 ms' │ ' 35 ms' │ ' 19 ms' │ ' 1.84 x' │
|
|
961
|
+
│ Array_String │ 1000000 │ ' 438 ms' │ ' 33 ms' │ ' 20 ms' │ ' 1.65 x' │
|
|
962
|
+
│ Array_Boolean │ 1000000 │ ' 444 ms' │ ' 38 ms' │ ' 24 ms' │ ' 1.58 x' │
|
|
963
|
+
│ Array_ObjectA │ 1000000 │ ' 13714 ms' │ ' 2819 ms' │ ' 1791 ms' │ ' 1.57 x' │
|
|
964
|
+
│ Array_ObjectB │ 1000000 │ ' 15855 ms' │ ' 2965 ms' │ ' 2066 ms' │ ' 1.44 x' │
|
|
965
|
+
│ Array_Tuple │ 1000000 │ ' 1682 ms' │ ' 94 ms' │ ' 71 ms' │ ' 1.32 x' │
|
|
966
|
+
│ Array_Union │ 1000000 │ ' 4575 ms' │ ' 239 ms' │ ' 86 ms' │ ' 2.78 x' │
|
|
967
|
+
│ Array_Recursive │ 1000000 │ ' 51970 ms' │ ' 7192 ms' │ ' 2617 ms' │ ' 2.75 x' │
|
|
968
|
+
│ Array_Vector4 │ 1000000 │ ' 2097 ms' │ ' 100 ms' │ ' 54 ms' │ ' 1.85 x' │
|
|
969
|
+
│ Array_Matrix4 │ 1000000 │ ' 11596 ms' │ ' 381 ms' │ ' 327 ms' │ ' 1.17 x' │
|
|
939
970
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
940
971
|
```
|
|
941
972
|
|
|
@@ -947,8 +978,9 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
947
978
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
948
979
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
949
980
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
950
|
-
│ typebox/compiler │ '
|
|
981
|
+
│ typebox/compiler │ ' 48 kb' │ ' 24 kb' │ '2.00 x' │
|
|
951
982
|
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.47 x' │
|
|
983
|
+
│ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.68 x' │
|
|
952
984
|
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.08 x' │
|
|
953
985
|
│ typebox/value │ ' 55 kb' │ ' 25 kb' │ '2.15 x' │
|
|
954
986
|
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.91 x' │
|
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/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>;
|