@sinclair/typebox 0.24.26 → 0.24.27
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 +53 -73
- package/conditional/conditional.js +2 -2
- package/errors/errors.d.ts +1 -1
- package/errors/errors.js +52 -71
- package/guard/guard.d.ts +28 -22
- package/guard/guard.js +59 -19
- package/package.json +1 -1
- package/readme.md +106 -91
- package/value/cast.d.ts +1 -1
- package/value/cast.js +55 -71
- package/value/check.d.ts +1 -1
- package/value/check.js +52 -71
- package/value/create.d.ts +1 -1
- package/value/create.js +56 -71
package/readme.md
CHANGED
|
@@ -194,7 +194,7 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
|
|
|
194
194
|
│ │ │ │
|
|
195
195
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
196
196
|
│ const T = Type.Literal(42) │ type T = 42 │ const T = { │
|
|
197
|
-
│ │ │ const: 42
|
|
197
|
+
│ │ │ const: 42, │
|
|
198
198
|
│ │ │ type: 'number' │
|
|
199
199
|
│ │ │ } │
|
|
200
200
|
│ │ │ │
|
|
@@ -506,9 +506,8 @@ type Node = Static<typeof Node> // type Node = {
|
|
|
506
506
|
// }
|
|
507
507
|
|
|
508
508
|
function test(node: Node) {
|
|
509
|
-
const id = node.nodes[0].nodes[0]
|
|
510
|
-
.nodes[0].nodes[0]
|
|
511
|
-
.nodes[0].nodes[0].nodes[0]
|
|
509
|
+
const id = node.nodes[0].nodes[0] // id is string
|
|
510
|
+
.nodes[0].nodes[0]
|
|
512
511
|
.id
|
|
513
512
|
}
|
|
514
513
|
```
|
|
@@ -577,6 +576,7 @@ const T = Nullable(Type.String()) // const T = {
|
|
|
577
576
|
|
|
578
577
|
type T = Static<typeof T> // type T = string | null
|
|
579
578
|
|
|
579
|
+
|
|
580
580
|
//--------------------------------------------------------------------------------------------
|
|
581
581
|
//
|
|
582
582
|
// StringEnum<string[]>
|
|
@@ -596,7 +596,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
|
596
596
|
|
|
597
597
|
## Conditional Types
|
|
598
598
|
|
|
599
|
-
Use `Conditional
|
|
599
|
+
Use the `Conditional` module to create conditionally mapped types.
|
|
600
600
|
|
|
601
601
|
```typescript
|
|
602
602
|
import { Conditional } from '@sinclair/typebox/conditional'
|
|
@@ -645,46 +645,50 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
645
645
|
|
|
646
646
|
## Values
|
|
647
647
|
|
|
648
|
-
Use `Value
|
|
648
|
+
Use the `Value` module to perform type operations on values.
|
|
649
649
|
|
|
650
650
|
```typescript
|
|
651
651
|
import { Value } from '@sinclair/typebox/value'
|
|
652
|
-
import { Type } from '@sinclair/typebox'
|
|
653
652
|
|
|
654
|
-
const T = Type.Object({
|
|
655
|
-
x: Type.Number({ default: 1 }),
|
|
656
|
-
y: Type.Number()
|
|
657
|
-
})
|
|
653
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
658
654
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
Use `Value.Cast(...)` to cast a value into a given type.
|
|
665
|
-
```typescript
|
|
666
|
-
import { Value } from '@sinclair/typebox/value'
|
|
667
|
-
import { Type } from '@sinclair/typebox'
|
|
655
|
+
//--------------------------------------------------------------------------------------------
|
|
656
|
+
//
|
|
657
|
+
// Use Value.Create(T) to create a value from T.
|
|
658
|
+
//
|
|
659
|
+
//--------------------------------------------------------------------------------------------
|
|
668
660
|
|
|
669
|
-
const
|
|
670
|
-
x: Type.Number(),
|
|
671
|
-
y: Type.Number()
|
|
672
|
-
})
|
|
661
|
+
const V = Value.Create(T) // const V = { x: 0, y: 0 }
|
|
673
662
|
|
|
674
|
-
|
|
663
|
+
//--------------------------------------------------------------------------------------------
|
|
664
|
+
//
|
|
665
|
+
// Use Value.Check(T, ...) to check if a value is of type T.
|
|
666
|
+
//
|
|
667
|
+
//--------------------------------------------------------------------------------------------
|
|
668
|
+
|
|
669
|
+
const R = Value.Check(T, { x: 1 }) // const R = false
|
|
670
|
+
|
|
671
|
+
//--------------------------------------------------------------------------------------------
|
|
672
|
+
//
|
|
673
|
+
// Use Value.Cast(T, ...) to immutably cast a value into T.
|
|
674
|
+
//
|
|
675
|
+
//--------------------------------------------------------------------------------------------
|
|
676
|
+
|
|
677
|
+
const A = Value.Cast(T, null) // const A = { x: 0, y: 0 }
|
|
678
|
+
|
|
679
|
+
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
|
|
680
|
+
|
|
681
|
+
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
675
682
|
|
|
676
|
-
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
|
|
677
683
|
|
|
678
|
-
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
679
684
|
```
|
|
680
685
|
|
|
681
686
|
## Guards
|
|
682
687
|
|
|
683
|
-
Use
|
|
688
|
+
Use the `TypeGuard` module to test if values are valid TypeBox types.
|
|
684
689
|
|
|
685
690
|
```typescript
|
|
686
691
|
import { TypeGuard } from '@sinclair/typebox/guard'
|
|
687
|
-
import { Type } from '@sinclair/typebox'
|
|
688
692
|
|
|
689
693
|
const T = Type.String()
|
|
690
694
|
|
|
@@ -692,7 +696,6 @@ if(TypeGuard.TString(T)) {
|
|
|
692
696
|
|
|
693
697
|
// T is TString
|
|
694
698
|
}
|
|
695
|
-
|
|
696
699
|
```
|
|
697
700
|
|
|
698
701
|
## Strict
|
|
@@ -789,14 +792,15 @@ Please refer to the official Ajv [documentation](https://ajv.js.org/guide/gettin
|
|
|
789
792
|
|
|
790
793
|
## Compiler
|
|
791
794
|
|
|
792
|
-
TypeBox provides an optional high performance runtime type checker that can be used in applications that require extremely fast validation. This
|
|
793
|
-
|
|
794
|
-
The following demonstrates its use.
|
|
795
|
+
TypeBox provides an optional high performance runtime compiler and type checker that can be used in applications that require extremely fast validation. This compiler is optimized for TypeBox types whose schematics are known in advance. If defining custom types with `Type.Unsafe<T>` please consider Ajv.
|
|
795
796
|
|
|
796
797
|
```typescript
|
|
797
798
|
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
798
|
-
|
|
799
|
+
```
|
|
799
800
|
|
|
801
|
+
Use the `Compile(...)` function to compile a type.
|
|
802
|
+
|
|
803
|
+
```typescript
|
|
800
804
|
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
801
805
|
x: Type.Number(), // x: TNumber;
|
|
802
806
|
y: Type.Number(), // y: TNumber;
|
|
@@ -818,19 +822,16 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
|
|
|
818
822
|
const value = { }
|
|
819
823
|
|
|
820
824
|
const errors = [...C.Errors(value)] // const errors = [{
|
|
821
|
-
// type: 14,
|
|
822
825
|
// schema: { type: 'number' },
|
|
823
826
|
// path: '/x',
|
|
824
827
|
// value: undefined,
|
|
825
828
|
// message: 'Expected number'
|
|
826
829
|
// }, {
|
|
827
|
-
// type: 14,
|
|
828
830
|
// schema: { type: 'number' },
|
|
829
831
|
// path: '/y',
|
|
830
832
|
// value: undefined,
|
|
831
833
|
// message: 'Expected number'
|
|
832
834
|
// }, {
|
|
833
|
-
// type: 14,
|
|
834
835
|
// schema: { type: 'number' },
|
|
835
836
|
// path: '/z',
|
|
836
837
|
// value: undefined,
|
|
@@ -860,75 +861,89 @@ For additional comparative benchmarks, please refer to [typescript-runtime-type-
|
|
|
860
861
|
|
|
861
862
|
### Compile
|
|
862
863
|
|
|
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).
|
|
864
|
+
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
865
|
|
|
865
866
|
```typescript
|
|
866
867
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
867
868
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
868
869
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
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 │ '
|
|
870
|
+
│ Number │ 2000 │ ' 394 ms' │ ' 9 ms' │ ' 43.78 x' │
|
|
871
|
+
│ String │ 2000 │ ' 320 ms' │ ' 9 ms' │ ' 35.56 x' │
|
|
872
|
+
│ Boolean │ 2000 │ ' 326 ms' │ ' 6 ms' │ ' 54.33 x' │
|
|
873
|
+
│ Null │ 2000 │ ' 256 ms' │ ' 6 ms' │ ' 42.67 x' │
|
|
874
|
+
│ RegEx │ 2000 │ ' 494 ms' │ ' 12 ms' │ ' 41.17 x' │
|
|
875
|
+
│ ObjectA │ 2000 │ ' 2813 ms' │ ' 41 ms' │ ' 68.61 x' │
|
|
876
|
+
│ ObjectB │ 2000 │ ' 2949 ms' │ ' 30 ms' │ ' 98.30 x' │
|
|
877
|
+
│ Tuple │ 2000 │ ' 1258 ms' │ ' 19 ms' │ ' 66.21 x' │
|
|
878
|
+
│ Union │ 2000 │ ' 1308 ms' │ ' 22 ms' │ ' 59.45 x' │
|
|
879
|
+
│ Vector4 │ 2000 │ ' 1589 ms' │ ' 17 ms' │ ' 93.47 x' │
|
|
880
|
+
│ Matrix4 │ 2000 │ ' 932 ms' │ ' 11 ms' │ ' 84.73 x' │
|
|
881
|
+
│ Literal_String │ 2000 │ ' 343 ms' │ ' 6 ms' │ ' 57.17 x' │
|
|
882
|
+
│ Literal_Number │ 2000 │ ' 380 ms' │ ' 6 ms' │ ' 63.33 x' │
|
|
883
|
+
│ Literal_Boolean │ 2000 │ ' 369 ms' │ ' 4 ms' │ ' 92.25 x' │
|
|
884
|
+
│ Array_Number │ 2000 │ ' 730 ms' │ ' 6 ms' │ ' 121.67 x' │
|
|
885
|
+
│ Array_String │ 2000 │ ' 764 ms' │ ' 7 ms' │ ' 109.14 x' │
|
|
886
|
+
│ Array_Boolean │ 2000 │ ' 791 ms' │ ' 8 ms' │ ' 98.88 x' │
|
|
887
|
+
│ Array_ObjectA │ 2000 │ ' 3550 ms' │ ' 33 ms' │ ' 107.58 x' │
|
|
888
|
+
│ Array_ObjectB │ 2000 │ ' 3709 ms' │ ' 33 ms' │ ' 112.39 x' │
|
|
889
|
+
│ Array_Tuple │ 2000 │ ' 2209 ms' │ ' 15 ms' │ ' 147.27 x' │
|
|
890
|
+
│ Array_Union │ 2000 │ ' 1733 ms' │ ' 18 ms' │ ' 96.28 x' │
|
|
891
|
+
│ Array_Vector4 │ 2000 │ ' 2279 ms' │ ' 16 ms' │ ' 142.44 x' │
|
|
892
|
+
│ Array_Matrix4 │ 2000 │ ' 1587 ms' │ ' 11 ms' │ ' 144.27 x' │
|
|
892
893
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
893
894
|
```
|
|
894
895
|
|
|
895
896
|
### Validate
|
|
896
897
|
|
|
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).
|
|
898
|
+
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
899
|
|
|
899
900
|
```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
|
-
|
|
901
|
+
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
902
|
+
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
903
|
+
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
904
|
+
│ Number │ 1000000 │ ' 27 ms' │ ' 6 ms' │ ' 4 ms' │ ' 1.50 x' │
|
|
905
|
+
│ String │ 1000000 │ ' 23 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
|
|
906
|
+
│ Boolean │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
|
|
907
|
+
│ Null │ 1000000 │ ' 24 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
908
|
+
│ RegEx │ 1000000 │ ' 170 ms' │ ' 43 ms' │ ' 36 ms' │ ' 1.19 x' │
|
|
909
|
+
│ ObjectA │ 1000000 │ ' 567 ms' │ ' 34 ms' │ ' 23 ms' │ ' 1.48 x' │
|
|
910
|
+
│ ObjectB │ 1000000 │ ' 985 ms' │ ' 50 ms' │ ' 36 ms' │ ' 1.39 x' │
|
|
911
|
+
│ Tuple │ 1000000 │ ' 119 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' │
|
|
912
|
+
│ Union │ 1000000 │ ' 302 ms' │ ' 26 ms' │ ' 14 ms' │ ' 1.86 x' │
|
|
913
|
+
│ Recursive │ 1000000 │ ' 3071 ms' │ ' 397 ms' │ ' 177 ms' │ ' 2.24 x' │
|
|
914
|
+
│ Vector4 │ 1000000 │ ' 135 ms' │ ' 24 ms' │ ' 11 ms' │ ' 2.18 x' │
|
|
915
|
+
│ Matrix4 │ 1000000 │ ' 632 ms' │ ' 41 ms' │ ' 30 ms' │ ' 1.37 x' │
|
|
916
|
+
│ Literal_String │ 1000000 │ ' 49 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
|
|
917
|
+
│ Literal_Number │ 1000000 │ ' 56 ms' │ ' 18 ms' │ ' 9 ms' │ ' 2.00 x' │
|
|
918
|
+
│ Literal_Boolean │ 1000000 │ ' 56 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
|
|
919
|
+
│ Array_Number │ 1000000 │ ' 408 ms' │ ' 31 ms' │ ' 17 ms' │ ' 1.82 x' │
|
|
920
|
+
│ Array_String │ 1000000 │ ' 458 ms' │ ' 32 ms' │ ' 20 ms' │ ' 1.60 x' │
|
|
921
|
+
│ Array_Boolean │ 1000000 │ ' 431 ms' │ ' 34 ms' │ ' 24 ms' │ ' 1.42 x' │
|
|
922
|
+
│ Array_ObjectA │ 1000000 │ ' 13322 ms' │ ' 2549 ms' │ ' 1636 ms' │ ' 1.56 x' │
|
|
923
|
+
│ Array_ObjectB │ 1000000 │ ' 16341 ms' │ ' 2865 ms' │ ' 2074 ms' │ ' 1.38 x' │
|
|
924
|
+
│ Array_Tuple │ 1000000 │ ' 1640 ms' │ ' 92 ms' │ ' 71 ms' │ ' 1.30 x' │
|
|
925
|
+
│ Array_Union │ 1000000 │ ' 4803 ms' │ ' 237 ms' │ ' 89 ms' │ ' 2.66 x' │
|
|
926
|
+
│ Array_Recursive │ 1000000 │ ' 53759 ms' │ ' 7694 ms' │ ' 2600 ms' │ ' 2.96 x' │
|
|
927
|
+
│ Array_Vector4 │ 1000000 │ ' 2099 ms' │ ' 96 ms' │ ' 52 ms' │ ' 1.85 x' │
|
|
928
|
+
│ Array_Matrix4 │ 1000000 │ ' 11436 ms' │ ' 384 ms' │ ' 310 ms' │ ' 1.24 x' │
|
|
929
|
+
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
929
930
|
```
|
|
930
931
|
|
|
932
|
+
### Compression
|
|
933
|
+
|
|
934
|
+
The following table lists esbuild compiled and minified sizes for each TypeBox import.
|
|
931
935
|
|
|
936
|
+
```typescript
|
|
937
|
+
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
938
|
+
│ (index) │ Compiled │ Minified │ Compression │
|
|
939
|
+
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
940
|
+
│ typebox/compiler │ ' 47 kb' │ ' 23 kb' │ '1.99 x' │
|
|
941
|
+
│ typebox/conditional │ ' 41 kb' │ ' 16 kb' │ '2.46 x' │
|
|
942
|
+
│ typebox/guard │ ' 20 kb' │ ' 9 kb' │ '2.06 x' │
|
|
943
|
+
│ typebox/value │ ' 54 kb' │ ' 25 kb' │ '2.14 x' │
|
|
944
|
+
│ typebox │ ' 11 kb' │ ' 5 kb' │ '1.89 x' │
|
|
945
|
+
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
946
|
+
```
|
|
932
947
|
|
|
933
948
|
## Contribute
|
|
934
949
|
|
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
package/value/check.js
CHANGED
|
@@ -27,15 +27,15 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCheck = exports.
|
|
31
|
-
const
|
|
32
|
-
class
|
|
30
|
+
exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
31
|
+
const Types = require("../typebox");
|
|
32
|
+
class ValueCheckUnknownTypeError extends Error {
|
|
33
33
|
constructor(schema) {
|
|
34
|
-
super('ValueCheck:
|
|
34
|
+
super('ValueCheck: Unknown type');
|
|
35
35
|
this.schema = schema;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
exports.
|
|
38
|
+
exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
|
|
39
39
|
var ValueCheck;
|
|
40
40
|
(function (ValueCheck) {
|
|
41
41
|
function Any(schema, references, value) {
|
|
@@ -251,72 +251,53 @@ var ValueCheck;
|
|
|
251
251
|
return value === null;
|
|
252
252
|
}
|
|
253
253
|
function Visit(schema, references, value) {
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return Tuple(schema, refs, value);
|
|
302
|
-
}
|
|
303
|
-
else if (index_1.TypeGuard.TUndefined(schema)) {
|
|
304
|
-
return Undefined(schema, refs, value);
|
|
305
|
-
}
|
|
306
|
-
else if (index_1.TypeGuard.TUnion(schema)) {
|
|
307
|
-
return Union(schema, refs, value);
|
|
308
|
-
}
|
|
309
|
-
else if (index_1.TypeGuard.TUint8Array(schema)) {
|
|
310
|
-
return Uint8Array(schema, refs, value);
|
|
311
|
-
}
|
|
312
|
-
else if (index_1.TypeGuard.TUnknown(schema)) {
|
|
313
|
-
return Unknown(schema, refs, value);
|
|
314
|
-
}
|
|
315
|
-
else if (index_1.TypeGuard.TVoid(schema)) {
|
|
316
|
-
return Void(schema, refs, value);
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
throw new ValueCheckInvalidTypeError(schema);
|
|
254
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
255
|
+
const anySchema = schema;
|
|
256
|
+
switch (anySchema[Types.Kind]) {
|
|
257
|
+
case 'Any':
|
|
258
|
+
return Any(anySchema, anyReferences, value);
|
|
259
|
+
case 'Array':
|
|
260
|
+
return Array(anySchema, anyReferences, value);
|
|
261
|
+
case 'Boolean':
|
|
262
|
+
return Boolean(anySchema, anyReferences, value);
|
|
263
|
+
case 'Constructor':
|
|
264
|
+
return Constructor(anySchema, anyReferences, value);
|
|
265
|
+
case 'Function':
|
|
266
|
+
return Function(anySchema, anyReferences, value);
|
|
267
|
+
case 'Integer':
|
|
268
|
+
return Integer(anySchema, anyReferences, value);
|
|
269
|
+
case 'Literal':
|
|
270
|
+
return Literal(anySchema, anyReferences, value);
|
|
271
|
+
case 'Null':
|
|
272
|
+
return Null(anySchema, anyReferences, value);
|
|
273
|
+
case 'Number':
|
|
274
|
+
return Number(anySchema, anyReferences, value);
|
|
275
|
+
case 'Object':
|
|
276
|
+
return Object(anySchema, anyReferences, value);
|
|
277
|
+
case 'Promise':
|
|
278
|
+
return Promise(anySchema, anyReferences, value);
|
|
279
|
+
case 'Record':
|
|
280
|
+
return Record(anySchema, anyReferences, value);
|
|
281
|
+
case 'Ref':
|
|
282
|
+
return Ref(anySchema, anyReferences, value);
|
|
283
|
+
case 'Self':
|
|
284
|
+
return Self(anySchema, anyReferences, value);
|
|
285
|
+
case 'String':
|
|
286
|
+
return String(anySchema, anyReferences, value);
|
|
287
|
+
case 'Tuple':
|
|
288
|
+
return Tuple(anySchema, anyReferences, value);
|
|
289
|
+
case 'Undefined':
|
|
290
|
+
return Undefined(anySchema, anyReferences, value);
|
|
291
|
+
case 'Union':
|
|
292
|
+
return Union(anySchema, anyReferences, value);
|
|
293
|
+
case 'Uint8Array':
|
|
294
|
+
return Uint8Array(anySchema, anyReferences, value);
|
|
295
|
+
case 'Unknown':
|
|
296
|
+
return Unknown(anySchema, anyReferences, value);
|
|
297
|
+
case 'Void':
|
|
298
|
+
return Void(anySchema, anyReferences, value);
|
|
299
|
+
default:
|
|
300
|
+
throw new ValueCheckUnknownTypeError(anySchema);
|
|
320
301
|
}
|
|
321
302
|
}
|
|
322
303
|
// -------------------------------------------------------------------------
|