@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/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].nodes[0] // id is string
510
- .nodes[0].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.Extends(...)` to create conditional mapped types.
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.Create(...)` to generate values from types.
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
- const V = Value.Create(T) // const V = {
660
- // x: 1,
661
- // y: 0
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 T = Type.Object({
670
- x: Type.Number(),
671
- y: Type.Number()
672
- })
661
+ const V = Value.Create(T) // const V = { x: 0, y: 0 }
673
662
 
674
- const A = Value.Cast(T, null) // const A = { x: 0, y: 0 }
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 a `TypeGuard` to test if a value meets a TypeBox type specification. Guards can be helpful when reflecting types.
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 type checker is optimized for TypeBox types only whose schematics are known in advance. If defining custom schemas with `Type.Unsafe<T>` please consider Ajv.
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
- import { Type } from '@sinclair/typebox'
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 │ ' 382 ms' │ ' 8 ms' │ ' 47.75 x' │
870
- │ String │ 2000 │ ' 305 ms' │ ' 7 ms' │ ' 43.57 x' │
871
- │ Boolean │ 2000 │ ' 305 ms' │ ' 5 ms' │ ' 61.00 x' │
872
- │ Null │ 2000 │ ' 253 ms' │ ' 5 ms' │ ' 50.60 x' │
873
- │ RegEx │ 2000 │ ' 475 ms' │ ' 10 ms' │ ' 47.50 x' │
874
- │ ObjectA │ 2000 │ ' 2812 ms' │ ' 44 ms' │ ' 63.91 x' │
875
- │ ObjectB │ 2000 │ ' 3045 ms' │ ' 34 ms' │ ' 89.56 x' │
876
- │ Tuple │ 2000 │ ' 1324 ms' │ ' 19 ms' │ ' 69.68 x' │
877
- │ Union │ 2000 │ ' 1347 ms' │ ' 26 ms' │ ' 51.81 x' │
878
- │ Vector4 │ 2000 │ ' 1635 ms' │ ' 18 ms' │ ' 90.83 x' │
879
- │ Matrix4 │ 2000 │ ' 928 ms' │ ' 10 ms' │ ' 92.80 x' │
880
- │ Literal_String │ 2000 │ ' 349 ms' │ ' 5 ms' │ ' 69.80 x' │
881
- │ Literal_Number │ 2000 │ ' 379 ms' │ ' 5 ms' │ ' 75.80 x' │
882
- │ Literal_Boolean │ 2000 │ ' 376 ms' │ ' 6 ms' │ ' 62.67 x' │
883
- │ Array_Number │ 2000 │ ' 733 ms' │ ' 10 ms' │ ' 73.30 x' │
884
- │ Array_String │ 2000 │ ' 766 ms' │ ' 8 ms' │ ' 95.75 x' │
885
- │ Array_Boolean │ 2000 │ ' 860 ms' │ ' 11 ms' │ ' 78.18 x' │
886
- │ Array_ObjectA │ 2000 │ ' 3668 ms' │ ' 42 ms' │ ' 87.33 x' │
887
- │ Array_ObjectB │ 2000 │ ' 3809 ms' │ ' 41 ms' │ ' 92.90 x' │
888
- │ Array_Tuple │ 2000 │ ' 2219 ms' │ ' 17 ms' │ ' 130.53 x' │
889
- │ Array_Union │ 2000 │ ' 1709 ms' │ ' 26 ms' │ ' 65.73 x' │
890
- │ Array_Vector4 │ 2000 │ ' 2301 ms' │ ' 18 ms' │ ' 127.83 x' │
891
- │ Array_Matrix4 │ 2000 │ ' 1594 ms' │ ' 12 ms' │ ' 132.83 x' │
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 │ 4000000 │ ' 18 ms' │ ' 16 ms' │ ' 1.13 x' │
904
- │ String │ 4000000 │ ' 74 ms' │ ' 43 ms' │ ' 1.72 x' │
905
- │ Boolean │ 4000000 │ ' 71 ms' │ ' 37 ms' │ ' 1.92 x' │
906
- │ Null │ 4000000 │ ' 70 ms' │ ' 37 ms' │ ' 1.89 x' │
907
- │ RegEx │ 4000000 │ ' 175 ms' │ ' 153 ms' │ ' 1.14 x' │
908
- │ ObjectA │ 4000000 │ ' 124 ms' │ ' 87 ms' │ ' 1.43 x' │
909
- │ ObjectB │ 4000000 │ ' 215 ms' │ ' 143 ms' │ ' 1.50 x' │
910
- │ Tuple │ 4000000 │ ' 101 ms' │ ' 51 ms' │ ' 1.98 x' │
911
- │ Union │ 4000000 │ ' 108 ms' │ ' 51 ms' │ ' 2.12 x' │
912
- │ Recursive │ 4000000 │ ' 1647 ms' │ ' 626 ms' │ ' 2.63 x' │
913
- │ Vector4 │ 4000000 │ ' 83 ms' │ ' 41 ms' │ ' 2.02 x' │
914
- │ Matrix4 │ 4000000 │ ' 154 ms' │ ' 105 ms' │ ' 1.47 x' │
915
- │ Literal_String │ 4000000 │ ' 101 ms' │ ' 37 ms' │ ' 2.73 x' │
916
- │ Literal_Number │ 4000000 │ ' 69 ms' │ ' 35 ms' │ ' 1.97 x' │
917
- │ Literal_Boolean │ 4000000 │ ' 71 ms' │ ' 35 ms' │ ' 2.03 x' │
918
- │ Array_Number │ 4000000 │ ' 117 ms' │ ' 61 ms' │ ' 1.92 x' │
919
- │ Array_String │ 4000000 │ ' 116 ms' │ ' 73 ms' │ ' 1.59 x' │
920
- │ Array_Boolean │ 4000000 │ ' 129 ms' │ ' 89 ms' │ ' 1.45 x' │
921
- │ Array_ObjectA │ 4000000 │ ' 10582 ms' │ ' 6854 ms' │ ' 1.54 x' │
922
- │ Array_ObjectB │ 4000000 │ ' 11539 ms' │ ' 8224 ms' │ ' 1.40 x' │
923
- │ Array_Tuple │ 4000000 │ ' 362 ms' │ ' 282 ms' │ ' 1.28 x' │
924
- │ Array_Union │ 4000000 │ ' 950 ms' │ ' 355 ms' │ ' 2.68 x' │
925
- │ Array_Recursive │ 4000000 │ ' 28047 ms' │ ' 9567 ms' │ ' 2.93 x' │
926
- │ Array_Vector4 │ 4000000 │ ' 372 ms' │ ' 200 ms' │ ' 1.86 x' │
927
- │ Array_Matrix4 │ 4000000 │ ' 1521 ms' │ ' 1244 ms' │ ' 1.22 x' │
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
@@ -1,5 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- export declare class ValueCastInvalidTypeError extends Error {
2
+ export declare class ValueCastUnknownTypeError extends Error {
3
3
  readonly schema: Types.TSchema;
4
4
  constructor(schema: Types.TSchema);
5
5
  }
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.ValueCastInvalidTypeError = void 0;
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 ValueCastInvalidTypeError extends Error {
74
+ class ValueCastUnknownTypeError extends Error {
76
75
  constructor(schema) {
77
- super('ValueCast: Invalid type');
76
+ super('ValueCast: Unknown type');
78
77
  this.schema = schema;
79
78
  }
80
79
  }
81
- exports.ValueCastInvalidTypeError = ValueCastInvalidTypeError;
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 refs = schema.$id === undefined ? references : [schema, ...references];
201
- if (index_1.TypeGuard.TAny(schema)) {
202
- return Any(schema, refs, value);
203
- }
204
- else if (index_1.TypeGuard.TArray(schema)) {
205
- return Array(schema, refs, value);
206
- }
207
- else if (index_1.TypeGuard.TBoolean(schema)) {
208
- return Boolean(schema, refs, value);
209
- }
210
- else if (index_1.TypeGuard.TConstructor(schema)) {
211
- return Constructor(schema, refs, value);
212
- }
213
- else if (index_1.TypeGuard.TFunction(schema)) {
214
- return Function(schema, refs, value);
215
- }
216
- else if (index_1.TypeGuard.TInteger(schema)) {
217
- return Integer(schema, refs, value);
218
- }
219
- else if (index_1.TypeGuard.TLiteral(schema)) {
220
- return Literal(schema, refs, value);
221
- }
222
- else if (index_1.TypeGuard.TNull(schema)) {
223
- return Null(schema, refs, value);
224
- }
225
- else if (index_1.TypeGuard.TNumber(schema)) {
226
- return Number(schema, refs, value);
227
- }
228
- else if (index_1.TypeGuard.TObject(schema)) {
229
- return Object(schema, refs, value);
230
- }
231
- else if (index_1.TypeGuard.TPromise(schema)) {
232
- return Promise(schema, refs, value);
233
- }
234
- else if (index_1.TypeGuard.TRecord(schema)) {
235
- return Record(schema, refs, value);
236
- }
237
- else if (index_1.TypeGuard.TRef(schema)) {
238
- return Ref(schema, refs, value);
239
- }
240
- else if (index_1.TypeGuard.TSelf(schema)) {
241
- return Self(schema, refs, value);
242
- }
243
- else if (index_1.TypeGuard.TString(schema)) {
244
- return String(schema, refs, value);
245
- }
246
- else if (index_1.TypeGuard.TTuple(schema)) {
247
- return Tuple(schema, refs, value);
248
- }
249
- else if (index_1.TypeGuard.TUndefined(schema)) {
250
- return Undefined(schema, refs, value);
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
@@ -1,5 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- export declare class ValueCheckInvalidTypeError extends Error {
2
+ export declare class ValueCheckUnknownTypeError extends Error {
3
3
  readonly schema: Types.TSchema;
4
4
  constructor(schema: Types.TSchema);
5
5
  }
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.ValueCheckInvalidTypeError = void 0;
31
- const index_1 = require("../guard/index");
32
- class ValueCheckInvalidTypeError extends Error {
30
+ exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
31
+ const Types = require("../typebox");
32
+ class ValueCheckUnknownTypeError extends Error {
33
33
  constructor(schema) {
34
- super('ValueCheck: Invalid type');
34
+ super('ValueCheck: Unknown type');
35
35
  this.schema = schema;
36
36
  }
37
37
  }
38
- exports.ValueCheckInvalidTypeError = ValueCheckInvalidTypeError;
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 refs = schema.$id === undefined ? references : [schema, ...references];
255
- if (index_1.TypeGuard.TAny(schema)) {
256
- return Any(schema, refs, value);
257
- }
258
- else if (index_1.TypeGuard.TArray(schema)) {
259
- return Array(schema, refs, value);
260
- }
261
- else if (index_1.TypeGuard.TBoolean(schema)) {
262
- return Boolean(schema, refs, value);
263
- }
264
- else if (index_1.TypeGuard.TConstructor(schema)) {
265
- return Constructor(schema, refs, value);
266
- }
267
- else if (index_1.TypeGuard.TFunction(schema)) {
268
- return Function(schema, refs, value);
269
- }
270
- else if (index_1.TypeGuard.TInteger(schema)) {
271
- return Integer(schema, refs, value);
272
- }
273
- else if (index_1.TypeGuard.TLiteral(schema)) {
274
- return Literal(schema, refs, value);
275
- }
276
- else if (index_1.TypeGuard.TNull(schema)) {
277
- return Null(schema, refs, value);
278
- }
279
- else if (index_1.TypeGuard.TNumber(schema)) {
280
- return Number(schema, refs, value);
281
- }
282
- else if (index_1.TypeGuard.TObject(schema)) {
283
- return Object(schema, refs, value);
284
- }
285
- else if (index_1.TypeGuard.TPromise(schema)) {
286
- return Promise(schema, refs, value);
287
- }
288
- else if (index_1.TypeGuard.TRecord(schema)) {
289
- return Record(schema, refs, value);
290
- }
291
- else if (index_1.TypeGuard.TRef(schema)) {
292
- return Ref(schema, refs, value);
293
- }
294
- else if (index_1.TypeGuard.TSelf(schema)) {
295
- return Self(schema, refs, value);
296
- }
297
- else if (index_1.TypeGuard.TString(schema)) {
298
- return String(schema, refs, value);
299
- }
300
- else if (index_1.TypeGuard.TTuple(schema)) {
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
  // -------------------------------------------------------------------------