@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/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
|
|
|
@@ -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
|
|
|
@@ -194,7 +195,7 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
|
|
|
194
195
|
│ │ │ │
|
|
195
196
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
196
197
|
│ const T = Type.Literal(42) │ type T = 42 │ const T = { │
|
|
197
|
-
│ │ │ const: 42
|
|
198
|
+
│ │ │ const: 42, │
|
|
198
199
|
│ │ │ type: 'number' │
|
|
199
200
|
│ │ │ } │
|
|
200
201
|
│ │ │ │
|
|
@@ -506,9 +507,8 @@ type Node = Static<typeof Node> // type Node = {
|
|
|
506
507
|
// }
|
|
507
508
|
|
|
508
509
|
function test(node: Node) {
|
|
509
|
-
const id = node.nodes[0].nodes[0]
|
|
510
|
-
.nodes[0].nodes[0]
|
|
511
|
-
.nodes[0].nodes[0].nodes[0]
|
|
510
|
+
const id = node.nodes[0].nodes[0] // id is string
|
|
511
|
+
.nodes[0].nodes[0]
|
|
512
512
|
.id
|
|
513
513
|
}
|
|
514
514
|
```
|
|
@@ -555,7 +555,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
555
555
|
type T = Static<typeof T> // type T = string
|
|
556
556
|
```
|
|
557
557
|
|
|
558
|
-
|
|
558
|
+
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.
|
|
559
559
|
|
|
560
560
|
```typescript
|
|
561
561
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -577,6 +577,7 @@ const T = Nullable(Type.String()) // const T = {
|
|
|
577
577
|
|
|
578
578
|
type T = Static<typeof T> // type T = string | null
|
|
579
579
|
|
|
580
|
+
|
|
580
581
|
//--------------------------------------------------------------------------------------------
|
|
581
582
|
//
|
|
582
583
|
// StringEnum<string[]>
|
|
@@ -596,7 +597,9 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
|
596
597
|
|
|
597
598
|
## Conditional Types
|
|
598
599
|
|
|
599
|
-
Use
|
|
600
|
+
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.
|
|
601
|
+
|
|
602
|
+
The conditional module is provided as an optional import.
|
|
600
603
|
|
|
601
604
|
```typescript
|
|
602
605
|
import { Conditional } from '@sinclair/typebox/conditional'
|
|
@@ -645,46 +648,56 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
645
648
|
|
|
646
649
|
## Values
|
|
647
650
|
|
|
648
|
-
Use
|
|
651
|
+
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).
|
|
652
|
+
|
|
653
|
+
The value module is provided as an optional import.
|
|
649
654
|
|
|
650
655
|
```typescript
|
|
651
656
|
import { Value } from '@sinclair/typebox/value'
|
|
652
|
-
import { Type } from '@sinclair/typebox'
|
|
653
|
-
|
|
654
|
-
const T = Type.Object({
|
|
655
|
-
x: Type.Number({ default: 1 }),
|
|
656
|
-
y: Type.Number()
|
|
657
|
-
})
|
|
658
|
-
|
|
659
|
-
const V = Value.Create(T) // const V = {
|
|
660
|
-
// x: 1,
|
|
661
|
-
// y: 0
|
|
662
|
-
// }
|
|
663
657
|
```
|
|
664
|
-
|
|
658
|
+
The following demonstrates its use.
|
|
665
659
|
```typescript
|
|
666
|
-
import { Value } from '@sinclair/typebox/value'
|
|
667
|
-
import { Type } from '@sinclair/typebox'
|
|
668
660
|
|
|
669
|
-
const T = Type.Object({
|
|
670
|
-
x: Type.Number(),
|
|
671
|
-
y: Type.Number()
|
|
672
|
-
})
|
|
673
661
|
|
|
674
|
-
const
|
|
662
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
|
|
663
|
+
|
|
664
|
+
//--------------------------------------------------------------------------------------------
|
|
665
|
+
//
|
|
666
|
+
// Use Value.Create(T) to create a value from T.
|
|
667
|
+
//
|
|
668
|
+
//--------------------------------------------------------------------------------------------
|
|
669
|
+
|
|
670
|
+
const V = Value.Create(T) // const V = { x: 0, y: 0 }
|
|
671
|
+
|
|
672
|
+
//--------------------------------------------------------------------------------------------
|
|
673
|
+
//
|
|
674
|
+
// Use Value.Check(T, ...) to check if a value is of type T.
|
|
675
|
+
//
|
|
676
|
+
//--------------------------------------------------------------------------------------------
|
|
677
|
+
|
|
678
|
+
const R = Value.Check(T, { x: 1 }) // const R = false
|
|
679
|
+
|
|
680
|
+
//--------------------------------------------------------------------------------------------
|
|
681
|
+
//
|
|
682
|
+
// Use Value.Cast(T, ...) to immutably cast a value into T.
|
|
683
|
+
//
|
|
684
|
+
//--------------------------------------------------------------------------------------------
|
|
685
|
+
|
|
686
|
+
const A = Value.Cast(T, null) // const A = { x: 0, y: 0 }
|
|
687
|
+
|
|
688
|
+
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
|
|
689
|
+
|
|
690
|
+
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
675
691
|
|
|
676
|
-
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
|
|
677
692
|
|
|
678
|
-
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
679
693
|
```
|
|
680
694
|
|
|
681
695
|
## Guards
|
|
682
696
|
|
|
683
|
-
Use
|
|
697
|
+
Use the guard module to test if values are TypeBox types.
|
|
684
698
|
|
|
685
699
|
```typescript
|
|
686
700
|
import { TypeGuard } from '@sinclair/typebox/guard'
|
|
687
|
-
import { Type } from '@sinclair/typebox'
|
|
688
701
|
|
|
689
702
|
const T = Type.String()
|
|
690
703
|
|
|
@@ -692,7 +705,6 @@ if(TypeGuard.TString(T)) {
|
|
|
692
705
|
|
|
693
706
|
// T is TString
|
|
694
707
|
}
|
|
695
|
-
|
|
696
708
|
```
|
|
697
709
|
|
|
698
710
|
## Strict
|
|
@@ -789,14 +801,17 @@ Please refer to the official Ajv [documentation](https://ajv.js.org/guide/gettin
|
|
|
789
801
|
|
|
790
802
|
## Compiler
|
|
791
803
|
|
|
792
|
-
TypeBox provides an optional high performance
|
|
804
|
+
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.
|
|
793
805
|
|
|
794
|
-
The
|
|
806
|
+
The compiler module is provided as an optional import.
|
|
795
807
|
|
|
796
808
|
```typescript
|
|
797
809
|
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
798
|
-
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
Use the `Compile(...)` function to compile a type.
|
|
799
813
|
|
|
814
|
+
```typescript
|
|
800
815
|
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
801
816
|
x: Type.Number(), // x: TNumber;
|
|
802
817
|
y: Type.Number(), // y: TNumber;
|
|
@@ -818,19 +833,16 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
|
|
|
818
833
|
const value = { }
|
|
819
834
|
|
|
820
835
|
const errors = [...C.Errors(value)] // const errors = [{
|
|
821
|
-
// type: 14,
|
|
822
836
|
// schema: { type: 'number' },
|
|
823
837
|
// path: '/x',
|
|
824
838
|
// value: undefined,
|
|
825
839
|
// message: 'Expected number'
|
|
826
840
|
// }, {
|
|
827
|
-
// type: 14,
|
|
828
841
|
// schema: { type: 'number' },
|
|
829
842
|
// path: '/y',
|
|
830
843
|
// value: undefined,
|
|
831
844
|
// message: 'Expected number'
|
|
832
845
|
// }, {
|
|
833
|
-
// type: 14,
|
|
834
846
|
// schema: { type: 'number' },
|
|
835
847
|
// path: '/z',
|
|
836
848
|
// value: undefined,
|
|
@@ -850,85 +862,130 @@ console.log(C.Code()) // return function check(va
|
|
|
850
862
|
// }
|
|
851
863
|
```
|
|
852
864
|
|
|
865
|
+
## Formats
|
|
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')
|
|
853
893
|
|
|
894
|
+
const R2 = Value.Check(T, '507f1f77bcf86cd799439011')
|
|
895
|
+
```
|
|
854
896
|
|
|
855
897
|
## Benchmark
|
|
856
898
|
|
|
857
|
-
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.
|
|
899
|
+
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.
|
|
858
900
|
|
|
859
901
|
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
860
902
|
|
|
861
903
|
### Compile
|
|
862
904
|
|
|
863
|
-
This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/compile.ts).
|
|
905
|
+
This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/compile.ts).
|
|
864
906
|
|
|
865
907
|
```typescript
|
|
866
908
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
867
909
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
868
910
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
869
|
-
│ Number │ 2000 │ '
|
|
870
|
-
│ String │ 2000 │ '
|
|
871
|
-
│ Boolean │ 2000 │ '
|
|
872
|
-
│ Null │ 2000 │ '
|
|
873
|
-
│ RegEx │ 2000 │ '
|
|
874
|
-
│ ObjectA │ 2000 │ '
|
|
875
|
-
│ ObjectB │ 2000 │ '
|
|
876
|
-
│ Tuple │ 2000 │ '
|
|
877
|
-
│ Union │ 2000 │ '
|
|
878
|
-
│ Vector4 │ 2000 │ '
|
|
879
|
-
│ Matrix4 │ 2000 │ '
|
|
880
|
-
│ Literal_String │ 2000 │ '
|
|
881
|
-
│ Literal_Number │ 2000 │ '
|
|
882
|
-
│ Literal_Boolean │ 2000 │ '
|
|
883
|
-
│ Array_Number │ 2000 │ '
|
|
884
|
-
│ Array_String │ 2000 │ '
|
|
885
|
-
│ Array_Boolean │ 2000 │ '
|
|
886
|
-
│ Array_ObjectA │ 2000 │ '
|
|
887
|
-
│ Array_ObjectB │ 2000 │ '
|
|
888
|
-
│ Array_Tuple │ 2000 │ '
|
|
889
|
-
│ Array_Union │ 2000 │ '
|
|
890
|
-
│ Array_Vector4 │ 2000 │ '
|
|
891
|
-
│ 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' │
|
|
892
934
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
893
935
|
```
|
|
894
936
|
|
|
895
937
|
### Validate
|
|
896
938
|
|
|
897
|
-
This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/check.ts).
|
|
939
|
+
This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/check.ts).
|
|
898
940
|
|
|
899
941
|
```typescript
|
|
900
|
-
|
|
901
|
-
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
902
|
-
|
|
903
|
-
│ Number │
|
|
904
|
-
│ String │
|
|
905
|
-
│ Boolean │
|
|
906
|
-
│ Null │
|
|
907
|
-
│ RegEx │
|
|
908
|
-
│ ObjectA │
|
|
909
|
-
│ ObjectB │
|
|
910
|
-
│ Tuple │
|
|
911
|
-
│ Union │
|
|
912
|
-
│ Recursive │
|
|
913
|
-
│ Vector4 │
|
|
914
|
-
│ Matrix4 │
|
|
915
|
-
│ Literal_String │
|
|
916
|
-
│ Literal_Number │
|
|
917
|
-
│ Literal_Boolean │
|
|
918
|
-
│ Array_Number │
|
|
919
|
-
│ Array_String │
|
|
920
|
-
│ Array_Boolean │
|
|
921
|
-
│ Array_ObjectA │
|
|
922
|
-
│ Array_ObjectB │
|
|
923
|
-
│ Array_Tuple │
|
|
924
|
-
│ Array_Union │
|
|
925
|
-
│ Array_Recursive │
|
|
926
|
-
│ Array_Vector4 │
|
|
927
|
-
│ Array_Matrix4 │
|
|
928
|
-
|
|
942
|
+
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
943
|
+
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
944
|
+
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
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' │
|
|
970
|
+
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
929
971
|
```
|
|
930
972
|
|
|
973
|
+
### Compression
|
|
974
|
+
|
|
975
|
+
The following table lists esbuild compiled and minified sizes for each TypeBox module.
|
|
931
976
|
|
|
977
|
+
```typescript
|
|
978
|
+
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
979
|
+
│ (index) │ Compiled │ Minified │ Compression │
|
|
980
|
+
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
981
|
+
│ typebox/compiler │ ' 48 kb' │ ' 24 kb' │ '2.00 x' │
|
|
982
|
+
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.47 x' │
|
|
983
|
+
│ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.68 x' │
|
|
984
|
+
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.08 x' │
|
|
985
|
+
│ typebox/value │ ' 55 kb' │ ' 25 kb' │ '2.15 x' │
|
|
986
|
+
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.91 x' │
|
|
987
|
+
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
988
|
+
```
|
|
932
989
|
|
|
933
990
|
## Contribute
|
|
934
991
|
|
package/value/cast.d.ts
CHANGED
package/value/cast.js
CHANGED
|
@@ -27,9 +27,8 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCast = exports.
|
|
30
|
+
exports.ValueCast = exports.ValueCastUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
-
const index_1 = require("../guard/index");
|
|
33
32
|
const create_1 = require("./create");
|
|
34
33
|
const check_1 = require("./check");
|
|
35
34
|
// --------------------------------------------------------------------------
|
|
@@ -72,13 +71,13 @@ var UnionValueCast;
|
|
|
72
71
|
}
|
|
73
72
|
UnionValueCast.Create = Create;
|
|
74
73
|
})(UnionValueCast || (UnionValueCast = {}));
|
|
75
|
-
class
|
|
74
|
+
class ValueCastUnknownTypeError extends Error {
|
|
76
75
|
constructor(schema) {
|
|
77
|
-
super('ValueCast:
|
|
76
|
+
super('ValueCast: Unknown type');
|
|
78
77
|
this.schema = schema;
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
|
-
exports.
|
|
80
|
+
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
82
81
|
var ValueCast;
|
|
83
82
|
(function (ValueCast) {
|
|
84
83
|
function Any(schema, references, value) {
|
|
@@ -197,72 +196,57 @@ var ValueCast;
|
|
|
197
196
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
198
197
|
}
|
|
199
198
|
function Visit(schema, references, value) {
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
253
|
-
return Union(schema, refs, value);
|
|
254
|
-
}
|
|
255
|
-
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
256
|
-
return Uint8Array(schema, refs, value);
|
|
257
|
-
}
|
|
258
|
-
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
259
|
-
return Unknown(schema, refs, value);
|
|
260
|
-
}
|
|
261
|
-
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
262
|
-
return Void(schema, refs, value);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
throw new ValueCastInvalidTypeError(schema);
|
|
199
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
200
|
+
const anySchema = schema;
|
|
201
|
+
switch (schema[Types.Kind]) {
|
|
202
|
+
case 'Any':
|
|
203
|
+
return Any(anySchema, anyReferences, value);
|
|
204
|
+
case 'Array':
|
|
205
|
+
return Array(anySchema, anyReferences, value);
|
|
206
|
+
case 'Boolean':
|
|
207
|
+
return Boolean(anySchema, anyReferences, value);
|
|
208
|
+
case 'Constructor':
|
|
209
|
+
return Constructor(anySchema, anyReferences, value);
|
|
210
|
+
case 'Enum':
|
|
211
|
+
return Enum(anySchema, anyReferences, value);
|
|
212
|
+
case 'Function':
|
|
213
|
+
return Function(anySchema, anyReferences, value);
|
|
214
|
+
case 'Integer':
|
|
215
|
+
return Integer(anySchema, anyReferences, value);
|
|
216
|
+
case 'Literal':
|
|
217
|
+
return Literal(anySchema, anyReferences, value);
|
|
218
|
+
case 'Null':
|
|
219
|
+
return Null(anySchema, anyReferences, value);
|
|
220
|
+
case 'Number':
|
|
221
|
+
return Number(anySchema, anyReferences, value);
|
|
222
|
+
case 'Object':
|
|
223
|
+
return Object(anySchema, anyReferences, value);
|
|
224
|
+
case 'Promise':
|
|
225
|
+
return Promise(anySchema, anyReferences, value);
|
|
226
|
+
case 'Record':
|
|
227
|
+
return Record(anySchema, anyReferences, value);
|
|
228
|
+
case 'Rec':
|
|
229
|
+
return Recursive(anySchema, anyReferences, value);
|
|
230
|
+
case 'Ref':
|
|
231
|
+
return Ref(anySchema, anyReferences, value);
|
|
232
|
+
case 'Self':
|
|
233
|
+
return Self(anySchema, anyReferences, value);
|
|
234
|
+
case 'String':
|
|
235
|
+
return String(anySchema, anyReferences, value);
|
|
236
|
+
case 'Tuple':
|
|
237
|
+
return Tuple(anySchema, anyReferences, value);
|
|
238
|
+
case 'Undefined':
|
|
239
|
+
return Undefined(anySchema, anyReferences, value);
|
|
240
|
+
case 'Union':
|
|
241
|
+
return Union(anySchema, anyReferences, value);
|
|
242
|
+
case 'Uint8Array':
|
|
243
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
244
|
+
case 'Unknown':
|
|
245
|
+
return Unknown(anySchema, anyReferences, value);
|
|
246
|
+
case 'Void':
|
|
247
|
+
return Void(anySchema, anyReferences, value);
|
|
248
|
+
default:
|
|
249
|
+
throw new ValueCastUnknownTypeError(anySchema);
|
|
266
250
|
}
|
|
267
251
|
}
|
|
268
252
|
ValueCast.Visit = Visit;
|
package/value/check.d.ts
CHANGED