@sinclair/typebox 0.24.27 → 0.24.28
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 +9 -4
- package/package.json +2 -2
- package/readme.md +75 -65
package/compiler/compiler.js
CHANGED
|
@@ -174,7 +174,7 @@ var TypeCompiler;
|
|
|
174
174
|
yield `(Object.keys(${value}).length <= ${schema.maxProperties})`;
|
|
175
175
|
const propertyKeys = globalThis.Object.keys(schema.properties);
|
|
176
176
|
if (schema.additionalProperties === false) {
|
|
177
|
-
//
|
|
177
|
+
// Optimization: If the property key length matches the required keys length
|
|
178
178
|
// then we only need check that the values property key length matches that
|
|
179
179
|
// of the property key length. This is because exhaustive testing for values
|
|
180
180
|
// will occur in subsequent property tests.
|
|
@@ -210,6 +210,11 @@ var TypeCompiler;
|
|
|
210
210
|
yield `(Object.values(${value}).every(value => ${expression}))`;
|
|
211
211
|
}
|
|
212
212
|
function* Ref(schema, value) {
|
|
213
|
+
// Reference: If we have seen this reference before we can just yield and return
|
|
214
|
+
// the function call. If this isn't the case we defer to visit to generate and
|
|
215
|
+
// set the function for subsequent passes. Consider for refactor.
|
|
216
|
+
if (names.has(schema.$ref))
|
|
217
|
+
return yield `(${CreateFunctionName(schema.$ref)}(${value}))`;
|
|
213
218
|
if (!referenceMap.has(schema.$ref))
|
|
214
219
|
throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`);
|
|
215
220
|
const reference = referenceMap.get(schema.$ref);
|
|
@@ -263,9 +268,9 @@ var TypeCompiler;
|
|
|
263
268
|
yield `(${value} === null)`;
|
|
264
269
|
}
|
|
265
270
|
function* Visit(schema, value) {
|
|
266
|
-
//
|
|
267
|
-
//
|
|
268
|
-
//
|
|
271
|
+
// Reference: Referenced schemas can originate from either additional schemas
|
|
272
|
+
// or inline in the schema itself. Ideally the recursive path should align to
|
|
273
|
+
// reference path. Consider for refactor.
|
|
269
274
|
if (schema.$id && !names.has(schema.$id)) {
|
|
270
275
|
names.add(schema.$id);
|
|
271
276
|
const name = CreateFunctionName(schema.$id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.28",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"publish": "hammer task publish"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@sinclair/hammer": "^0.
|
|
29
|
+
"@sinclair/hammer": "^0.17.0",
|
|
30
30
|
"@types/chai": "^4.3.0",
|
|
31
31
|
"@types/mocha": "^9.1.0",
|
|
32
32
|
"@types/node": "^17.0.12",
|
package/readme.md
CHANGED
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
|
|
20
20
|
## Install
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Node
|
|
23
23
|
|
|
24
24
|
```bash
|
|
25
25
|
$ npm install @sinclair/typebox --save
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
Deno and ESM
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
|
-
import { Static, Type } from 'https://
|
|
31
|
+
import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## Example
|
|
@@ -45,9 +45,9 @@ type T = Static<typeof T> // type T = string
|
|
|
45
45
|
|
|
46
46
|
## Overview
|
|
47
47
|
|
|
48
|
-
TypeBox is a type builder library that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create unified
|
|
48
|
+
TypeBox is a type builder library that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
|
|
49
49
|
|
|
50
|
-
TypeBox can be used as a simple tool to build up complex schemas or integrated into
|
|
50
|
+
TypeBox is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used either as a simple tool to build up complex schemas or integrated into REST and RPC services to help validate data received over the wire.
|
|
51
51
|
|
|
52
52
|
License MIT
|
|
53
53
|
|
|
@@ -554,7 +554,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
554
554
|
type T = Static<typeof T> // type T = string
|
|
555
555
|
```
|
|
556
556
|
|
|
557
|
-
|
|
557
|
+
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.
|
|
558
558
|
|
|
559
559
|
```typescript
|
|
560
560
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -596,7 +596,9 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
|
596
596
|
|
|
597
597
|
## Conditional Types
|
|
598
598
|
|
|
599
|
-
Use the
|
|
599
|
+
Use the conditional module to create [Conditional Types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html). This module implements TypeScript's structural equivalence checks to enable TypeBox types to be conditionally inferred at runtime. This module also provides the [Extract](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union) and [Exclude](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers) utility types which are expressed as conditional types in TypeScript.
|
|
600
|
+
|
|
601
|
+
The conditional module is provided as an optional import.
|
|
600
602
|
|
|
601
603
|
```typescript
|
|
602
604
|
import { Conditional } from '@sinclair/typebox/conditional'
|
|
@@ -645,12 +647,18 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
645
647
|
|
|
646
648
|
## Values
|
|
647
649
|
|
|
648
|
-
Use the
|
|
650
|
+
Use the value module to perform common type operations on values. This module provides functionality to create, check and cast values into a given type. Note that this module internally uses dynamic type checking to perform these operations. For faster type checking performance, consider using either Ajv or the TypeBox [TypeCompiler](#compiler).
|
|
651
|
+
|
|
652
|
+
The value module is provided as an optional import.
|
|
649
653
|
|
|
650
654
|
```typescript
|
|
651
655
|
import { Value } from '@sinclair/typebox/value'
|
|
656
|
+
```
|
|
657
|
+
The following demonstrates its use.
|
|
658
|
+
```typescript
|
|
659
|
+
|
|
652
660
|
|
|
653
|
-
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
661
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
|
|
654
662
|
|
|
655
663
|
//--------------------------------------------------------------------------------------------
|
|
656
664
|
//
|
|
@@ -685,7 +693,7 @@ const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
|
685
693
|
|
|
686
694
|
## Guards
|
|
687
695
|
|
|
688
|
-
Use the
|
|
696
|
+
Use the guard module to test if values are TypeBox types.
|
|
689
697
|
|
|
690
698
|
```typescript
|
|
691
699
|
import { TypeGuard } from '@sinclair/typebox/guard'
|
|
@@ -792,7 +800,9 @@ Please refer to the official Ajv [documentation](https://ajv.js.org/guide/gettin
|
|
|
792
800
|
|
|
793
801
|
## Compiler
|
|
794
802
|
|
|
795
|
-
TypeBox provides an optional high performance
|
|
803
|
+
TypeBox provides an optional high performance just-in-time (JIT) compiler and type checker that can be used in applications that require extremely fast validation. Note that this compiler is optimized for TypeBox types only where the schematics are known in advance. If defining custom types with `Type.Unsafe<T>` please consider Ajv.
|
|
804
|
+
|
|
805
|
+
The compiler module is provided as an optional import.
|
|
796
806
|
|
|
797
807
|
```typescript
|
|
798
808
|
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
@@ -855,7 +865,7 @@ console.log(C.Code()) // return function check(va
|
|
|
855
865
|
|
|
856
866
|
## Benchmark
|
|
857
867
|
|
|
858
|
-
This project maintains a set of benchmarks that measure Ajv and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.11.0.
|
|
868
|
+
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.11.0.
|
|
859
869
|
|
|
860
870
|
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
861
871
|
|
|
@@ -867,29 +877,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
867
877
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
868
878
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
869
879
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
870
|
-
│ Number │ 2000 │ '
|
|
871
|
-
│ String │ 2000 │ '
|
|
872
|
-
│ Boolean │ 2000 │ '
|
|
873
|
-
│ Null │ 2000 │ '
|
|
874
|
-
│ RegEx │ 2000 │ '
|
|
875
|
-
│ ObjectA │ 2000 │ '
|
|
876
|
-
│ ObjectB │ 2000 │ '
|
|
877
|
-
│ Tuple │ 2000 │ '
|
|
878
|
-
│ Union │ 2000 │ '
|
|
879
|
-
│ Vector4 │ 2000 │ '
|
|
880
|
-
│ Matrix4 │ 2000 │ '
|
|
881
|
-
│ Literal_String │ 2000 │ '
|
|
882
|
-
│ Literal_Number │ 2000 │ '
|
|
883
|
-
│ Literal_Boolean │ 2000 │ '
|
|
884
|
-
│ Array_Number │ 2000 │ '
|
|
885
|
-
│ Array_String │ 2000 │ '
|
|
886
|
-
│ Array_Boolean │ 2000 │ '
|
|
887
|
-
│ Array_ObjectA │ 2000 │ '
|
|
888
|
-
│ Array_ObjectB │ 2000 │ '
|
|
889
|
-
│ Array_Tuple │ 2000 │ '
|
|
890
|
-
│ Array_Union │ 2000 │ '
|
|
891
|
-
│ Array_Vector4 │ 2000 │ '
|
|
892
|
-
│ Array_Matrix4 │ 2000 │ '
|
|
880
|
+
│ Number │ 2000 │ ' 399 ms' │ ' 9 ms' │ ' 44.33 x' │
|
|
881
|
+
│ String │ 2000 │ ' 306 ms' │ ' 8 ms' │ ' 38.25 x' │
|
|
882
|
+
│ Boolean │ 2000 │ ' 315 ms' │ ' 5 ms' │ ' 63.00 x' │
|
|
883
|
+
│ Null │ 2000 │ ' 255 ms' │ ' 6 ms' │ ' 42.50 x' │
|
|
884
|
+
│ RegEx │ 2000 │ ' 478 ms' │ ' 10 ms' │ ' 47.80 x' │
|
|
885
|
+
│ ObjectA │ 2000 │ ' 2850 ms' │ ' 39 ms' │ ' 73.08 x' │
|
|
886
|
+
│ ObjectB │ 2000 │ ' 3027 ms' │ ' 34 ms' │ ' 89.03 x' │
|
|
887
|
+
│ Tuple │ 2000 │ ' 1374 ms' │ ' 27 ms' │ ' 50.89 x' │
|
|
888
|
+
│ Union │ 2000 │ ' 1307 ms' │ ' 22 ms' │ ' 59.41 x' │
|
|
889
|
+
│ Vector4 │ 2000 │ ' 1568 ms' │ ' 17 ms' │ ' 92.24 x' │
|
|
890
|
+
│ Matrix4 │ 2000 │ ' 911 ms' │ ' 11 ms' │ ' 82.82 x' │
|
|
891
|
+
│ Literal_String │ 2000 │ ' 332 ms' │ ' 8 ms' │ ' 41.50 x' │
|
|
892
|
+
│ Literal_Number │ 2000 │ ' 363 ms' │ ' 8 ms' │ ' 45.38 x' │
|
|
893
|
+
│ Literal_Boolean │ 2000 │ ' 360 ms' │ ' 5 ms' │ ' 72.00 x' │
|
|
894
|
+
│ Array_Number │ 2000 │ ' 704 ms' │ ' 6 ms' │ ' 117.33 x' │
|
|
895
|
+
│ Array_String │ 2000 │ ' 745 ms' │ ' 11 ms' │ ' 67.73 x' │
|
|
896
|
+
│ Array_Boolean │ 2000 │ ' 781 ms' │ ' 7 ms' │ ' 111.57 x' │
|
|
897
|
+
│ Array_ObjectA │ 2000 │ ' 3552 ms' │ ' 31 ms' │ ' 114.58 x' │
|
|
898
|
+
│ Array_ObjectB │ 2000 │ ' 3738 ms' │ ' 33 ms' │ ' 113.27 x' │
|
|
899
|
+
│ Array_Tuple │ 2000 │ ' 2336 ms' │ ' 16 ms' │ ' 146.00 x' │
|
|
900
|
+
│ Array_Union │ 2000 │ ' 1758 ms' │ ' 20 ms' │ ' 87.90 x' │
|
|
901
|
+
│ Array_Vector4 │ 2000 │ ' 2305 ms' │ ' 16 ms' │ ' 144.06 x' │
|
|
902
|
+
│ Array_Matrix4 │ 2000 │ ' 1635 ms' │ ' 11 ms' │ ' 148.64 x' │
|
|
893
903
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
894
904
|
```
|
|
895
905
|
|
|
@@ -901,47 +911,47 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
901
911
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
902
912
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
903
913
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
904
|
-
│ Number │ 1000000 │ '
|
|
905
|
-
│ String │ 1000000 │ '
|
|
906
|
-
│ Boolean │ 1000000 │ ' 21 ms' │ '
|
|
907
|
-
│ Null │ 1000000 │ '
|
|
908
|
-
│ RegEx │ 1000000 │ '
|
|
909
|
-
│ ObjectA │ 1000000 │ '
|
|
910
|
-
│ ObjectB │ 1000000 │ '
|
|
911
|
-
│ Tuple │ 1000000 │ '
|
|
912
|
-
│ Union │ 1000000 │ '
|
|
913
|
-
│ Recursive │ 1000000 │ '
|
|
914
|
-
│ Vector4 │ 1000000 │ '
|
|
915
|
-
│ Matrix4 │ 1000000 │ '
|
|
916
|
-
│ Literal_String │ 1000000 │ '
|
|
917
|
-
│ Literal_Number │ 1000000 │ '
|
|
918
|
-
│ Literal_Boolean │ 1000000 │ '
|
|
919
|
-
│ Array_Number │ 1000000 │ '
|
|
920
|
-
│ Array_String │ 1000000 │ '
|
|
921
|
-
│ Array_Boolean │ 1000000 │ '
|
|
922
|
-
│ Array_ObjectA │ 1000000 │ '
|
|
923
|
-
│ Array_ObjectB │ 1000000 │ '
|
|
924
|
-
│ Array_Tuple │ 1000000 │ '
|
|
925
|
-
│ Array_Union │ 1000000 │ '
|
|
926
|
-
│ Array_Recursive │ 1000000 │ '
|
|
927
|
-
│ Array_Vector4 │ 1000000 │ '
|
|
928
|
-
│ Array_Matrix4 │ 1000000 │ '
|
|
914
|
+
│ Number │ 1000000 │ ' 31 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
|
|
915
|
+
│ String │ 1000000 │ ' 26 ms' │ ' 22 ms' │ ' 12 ms' │ ' 1.83 x' │
|
|
916
|
+
│ Boolean │ 1000000 │ ' 21 ms' │ ' 22 ms' │ ' 11 ms' │ ' 2.00 x' │
|
|
917
|
+
│ Null │ 1000000 │ ' 25 ms' │ ' 24 ms' │ ' 9 ms' │ ' 2.67 x' │
|
|
918
|
+
│ RegEx │ 1000000 │ ' 166 ms' │ ' 45 ms' │ ' 38 ms' │ ' 1.18 x' │
|
|
919
|
+
│ ObjectA │ 1000000 │ ' 535 ms' │ ' 36 ms' │ ' 22 ms' │ ' 1.64 x' │
|
|
920
|
+
│ ObjectB │ 1000000 │ ' 957 ms' │ ' 49 ms' │ ' 37 ms' │ ' 1.32 x' │
|
|
921
|
+
│ Tuple │ 1000000 │ ' 112 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' │
|
|
922
|
+
│ Union │ 1000000 │ ' 304 ms' │ ' 25 ms' │ ' 14 ms' │ ' 1.79 x' │
|
|
923
|
+
│ Recursive │ 1000000 │ ' 2986 ms' │ ' 391 ms' │ ' 164 ms' │ ' 2.38 x' │
|
|
924
|
+
│ Vector4 │ 1000000 │ ' 145 ms' │ ' 22 ms' │ ' 13 ms' │ ' 1.69 x' │
|
|
925
|
+
│ Matrix4 │ 1000000 │ ' 575 ms' │ ' 39 ms' │ ' 29 ms' │ ' 1.34 x' │
|
|
926
|
+
│ Literal_String │ 1000000 │ ' 44 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
927
|
+
│ Literal_Number │ 1000000 │ ' 46 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
|
|
928
|
+
│ Literal_Boolean │ 1000000 │ ' 47 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
|
|
929
|
+
│ Array_Number │ 1000000 │ ' 398 ms' │ ' 30 ms' │ ' 17 ms' │ ' 1.76 x' │
|
|
930
|
+
│ Array_String │ 1000000 │ ' 438 ms' │ ' 30 ms' │ ' 20 ms' │ ' 1.50 x' │
|
|
931
|
+
│ Array_Boolean │ 1000000 │ ' 443 ms' │ ' 37 ms' │ ' 24 ms' │ ' 1.54 x' │
|
|
932
|
+
│ Array_ObjectA │ 1000000 │ ' 13702 ms' │ ' 2649 ms' │ ' 1673 ms' │ ' 1.58 x' │
|
|
933
|
+
│ Array_ObjectB │ 1000000 │ ' 16091 ms' │ ' 2964 ms' │ ' 2032 ms' │ ' 1.46 x' │
|
|
934
|
+
│ Array_Tuple │ 1000000 │ ' 1665 ms' │ ' 92 ms' │ ' 70 ms' │ ' 1.31 x' │
|
|
935
|
+
│ Array_Union │ 1000000 │ ' 4631 ms' │ ' 220 ms' │ ' 88 ms' │ ' 2.50 x' │
|
|
936
|
+
│ Array_Recursive │ 1000000 │ ' 53745 ms' │ ' 6891 ms' │ ' 2744 ms' │ ' 2.51 x' │
|
|
937
|
+
│ Array_Vector4 │ 1000000 │ ' 2156 ms' │ ' 105 ms' │ ' 55 ms' │ ' 1.91 x' │
|
|
938
|
+
│ Array_Matrix4 │ 1000000 │ ' 11686 ms' │ ' 388 ms' │ ' 330 ms' │ ' 1.18 x' │
|
|
929
939
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
930
940
|
```
|
|
931
941
|
|
|
932
942
|
### Compression
|
|
933
943
|
|
|
934
|
-
The following table lists esbuild compiled and minified sizes for each TypeBox
|
|
944
|
+
The following table lists esbuild compiled and minified sizes for each TypeBox module.
|
|
935
945
|
|
|
936
946
|
```typescript
|
|
937
947
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
938
948
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
939
949
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
940
950
|
│ typebox/compiler │ ' 47 kb' │ ' 23 kb' │ '1.99 x' │
|
|
941
|
-
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.
|
|
942
|
-
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.
|
|
943
|
-
│ typebox/value │ '
|
|
944
|
-
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.
|
|
951
|
+
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.47 x' │
|
|
952
|
+
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.08 x' │
|
|
953
|
+
│ typebox/value │ ' 55 kb' │ ' 25 kb' │ '2.15 x' │
|
|
954
|
+
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.91 x' │
|
|
945
955
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
946
956
|
```
|
|
947
957
|
|