@sinclair/typebox 0.24.21 → 0.24.24

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
@@ -47,30 +47,30 @@ type T = Static<typeof T> // type T = string
47
47
 
48
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 types 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 RPC or REST services to help validate data received over the wire. It offers a type system aligned to the capabilities of TypeScript and can be used equally well in JavaScript environments.
50
+ TypeBox can be used as a simple tool to build up complex schemas or integrated into RPC or REST services to help validate JSON data received over the wire.
51
51
 
52
52
  License MIT
53
53
 
54
54
  ## Contents
55
- - [Install](#Install)
56
- - [Overview](#Overview)
57
- - [Usage](#Usage)
58
- - [Types](#Types)
59
- - [Modifiers](#Modifiers)
60
- - [Options](#Options)
61
- - [Extended Types](#Extended-Types)
62
- - [Reference Types](#Reference-Types)
63
- - [Recursive Types](#Recursive-Types)
64
- - [Generic Types](#Generic-Types)
65
- - [Unsafe Types](#Unsafe-Types)
66
- - [Conditional Types](#Conditional-Types)
67
- - [Values](#Values)
68
- - [Guards](#Guards)
69
- - [Strict](#Strict)
70
- - [Validation](#Validation)
71
- - [Compiler](#Compiler)
72
- - [Benchmark](#Benchmark)
73
- - [Contribute](#Contribute)
55
+ - [Install](#install)
56
+ - [Overview](#overview)
57
+ - [Usage](#usage)
58
+ - [Types](#types)
59
+ - [Modifiers](#modifiers)
60
+ - [Options](#options)
61
+ - [Extended Types](#extended-types)
62
+ - [Reference Types](#reference-types)
63
+ - [Recursive Types](#recursive-types)
64
+ - [Generic Types](#generic-types)
65
+ - [Unsafe Types](#unsafe-types)
66
+ - [Conditional Types](#conditional-types)
67
+ - [Values](#values)
68
+ - [Guards](#guards)
69
+ - [Strict](#strict)
70
+ - [Validation](#validation)
71
+ - [Compiler](#compiler)
72
+ - [Benchmark](#benchmark)
73
+ - [Contribute](#contribute)
74
74
 
75
75
  <a name="Example"></a>
76
76
 
@@ -147,8 +147,6 @@ function receive(value: T) { // ... as a Type
147
147
  }
148
148
  ```
149
149
 
150
- <a name="Types"></a>
151
-
152
150
  ## Types
153
151
 
154
152
  The following table outlines the TypeBox mappings between TypeScript and JSON schema.
@@ -342,7 +340,6 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
342
340
  │ │ │ │
343
341
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
344
342
  ```
345
- <a name="Modifiers"></a>
346
343
 
347
344
  ## Modifiers
348
345
 
@@ -386,8 +383,6 @@ TypeBox provides modifiers that can be applied to an objects properties. This al
386
383
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
387
384
  ```
388
385
 
389
- <a name="Options"></a>
390
-
391
386
  ## Options
392
387
 
393
388
  You can pass additional JSON schema options on the last argument of any given type. The following are some examples.
@@ -403,8 +398,6 @@ const T = Type.Number({ multipleOf: 2 })
403
398
  const T = Type.Array(Type.Integer(), { minItems: 5 })
404
399
  ```
405
400
 
406
- <a name="Extended-Types"></a>
407
-
408
401
  ## Extended Types
409
402
 
410
403
  In addition to JSON schema types, TypeBox provides several extended types that allow for `function` and `constructor` types to be composed. These additional types are not valid JSON Schema and will not validate using typical JSON Schema validation. However, these types can be used to frame JSON schema and describe callable interfaces that may receive JSON validated data. These types are as follows.
@@ -467,8 +460,6 @@ In addition to JSON schema types, TypeBox provides several extended types that a
467
460
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
468
461
  ```
469
462
 
470
- <a name="Reference-Types"></a>
471
-
472
463
  ## Reference Types
473
464
 
474
465
  Use `Type.Ref(...)` to create referenced types. The target type must specify an `$id`.
@@ -484,8 +475,6 @@ const R = Type.Ref(T) // const R = {
484
475
  // }
485
476
  ```
486
477
 
487
- <a name="Recursive-Types"></a>
488
-
489
478
  ## Recursive Types
490
479
 
491
480
  Use `Type.Recursive(...)` to create recursive types.
@@ -524,11 +513,9 @@ function test(node: Node) {
524
513
  }
525
514
  ```
526
515
 
527
- <a name="Generic-Types"></a>
528
-
529
516
  ## Generic Types
530
517
 
531
- Generic types can be created using functions. The following creates a generic `Nullable<T>` type.
518
+ Use functions to create generic types. The following creates a generic `Nullable<T>` type.
532
519
 
533
520
  ```typescript
534
521
  import { Type, Static, TSchema } from '@sinclair/typebox'
@@ -556,8 +543,6 @@ const U = Nullable(Type.Number()) // const U = {
556
543
  type U = Static<typeof U> // type U = number | null
557
544
  ```
558
545
 
559
- <a name="Unsafe-Types"></a>
560
-
561
546
  ## Unsafe Types
562
547
 
563
548
  Use `Type.Unsafe(...)` to create custom schemas with user defined inference rules.
@@ -594,12 +579,12 @@ type T = Static<typeof T> // type T = string | null
594
579
 
595
580
  //--------------------------------------------------------------------------------------------
596
581
  //
597
- // StringUnion<[...]>
582
+ // StringEnum<string[]>
598
583
  //
599
584
  //--------------------------------------------------------------------------------------------
600
585
 
601
586
  function StringEnum<T extends string[]>(values: [...T]) {
602
- return Type.Unsafe<T[number]>({ enum: values })
587
+ return Type.Unsafe<T[number]>({ type: 'string', enum: values })
603
588
  }
604
589
 
605
590
  const T = StringEnum(['A', 'B', 'C']) // const T = {
@@ -609,8 +594,6 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
609
594
  type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
610
595
  ```
611
596
 
612
- <a name="Conditional-Types"></a>
613
-
614
597
  ## Conditional Types
615
598
 
616
599
  Use `Conditional.Extends(...)` to create conditional mapped types.
@@ -660,8 +643,6 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
660
643
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
661
644
  ```
662
645
 
663
- <a name="Values"></a>
664
-
665
646
  ## Values
666
647
 
667
648
  Use `Value.Create(...)` to generate values from types.
@@ -697,8 +678,6 @@ const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0
697
678
  const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
698
679
  ```
699
680
 
700
- <a name="Guards"></a>
701
-
702
681
  ## Guards
703
682
 
704
683
  Use a `TypeGuard` to test if a value meets a TypeBox type specification. Guards can be helpful when reflecting types.
@@ -716,8 +695,6 @@ if(TypeGuard.TString(T)) {
716
695
 
717
696
  ```
718
697
 
719
- <a name="Strict"></a>
720
-
721
698
  ## Strict
722
699
 
723
700
  TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are provided to enable runtime type reflection on schemas, as well as helping TypeBox internally compose types. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict()` function that will omit these properties if necessary.
@@ -745,8 +722,6 @@ const U = Type.Strict(T) // const U = {
745
722
  // }
746
723
  ```
747
724
 
748
- <a name="Validation"></a>
749
-
750
725
  ## Validation
751
726
 
752
727
  TypeBox schemas target JSON Schema draft 6 so any validator capable of draft 6 should be fine. A good library to use for validation in JavaScript environments is [Ajv](https://www.npmjs.com/package/ajv). The following example shows setting up Ajv to work with TypeBox.
@@ -812,8 +787,6 @@ const R = ajv.validate(T, { x: 1, y: 2, z: 3 }) // const R = true
812
787
 
813
788
  Please refer to the official Ajv [documentation](https://ajv.js.org/guide/getting-started.html) for additional information on using Ajv.
814
789
 
815
- <a name="Compiler"></a>
816
-
817
790
  ## Compiler
818
791
 
819
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.
@@ -845,16 +818,19 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
845
818
  const value = { }
846
819
 
847
820
  const errors = [...C.Errors(value)] // const errors = [{
821
+ // type: 14,
848
822
  // schema: { type: 'number' },
849
823
  // path: '/x',
850
824
  // value: undefined,
851
825
  // message: 'Expected number'
852
826
  // }, {
827
+ // type: 14,
853
828
  // schema: { type: 'number' },
854
829
  // path: '/y',
855
830
  // value: undefined,
856
831
  // message: 'Expected number'
857
832
  // }, {
833
+ // type: 14,
858
834
  // schema: { type: 'number' },
859
835
  // path: '/z',
860
836
  // value: undefined,
@@ -874,81 +850,85 @@ console.log(C.Code()) // return function check(va
874
850
  // }
875
851
  ```
876
852
 
853
+
854
+
877
855
  ## Benchmark
878
856
 
879
- This project maintains benchmarks that measure Ajv and TypeCompiler validate and compile performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. Results show against Ajv version 8.11.0.
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.
880
858
 
881
- ### Validate
859
+ For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
882
860
 
883
- This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/check.ts).
861
+ ### Compile
862
+
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).
884
864
 
885
865
  ```typescript
886
866
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
887
867
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
888
868
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
889
- │ Number │ 16000000 │ ' 73 ms' │ ' 65 ms' │ ' 1.12 x' │
890
- │ String │ 16000000 │ ' 281 ms' │ ' 161 ms' │ ' 1.75 x' │
891
- │ Boolean │ 16000000 │ ' 302 ms' │ ' 151 ms' │ ' 2.00 x' │
892
- │ Null │ 16000000 │ ' 281 ms' │ ' 149 ms' │ ' 1.89 x' │
893
- │ RegEx │ 16000000 │ ' 689 ms' │ ' 550 ms' │ ' 1.25 x' │
894
- │ ObjectA │ 16000000 │ ' 470 ms' │ ' 311 ms' │ ' 1.51 x' │
895
- │ ObjectB │ 16000000 │ ' 667 ms' │ ' 556 ms' │ ' 1.20 x' │
896
- │ Tuple │ 16000000 │ ' 362 ms' │ ' 190 ms' │ ' 1.91 x' │
897
- │ Union │ 16000000 │ ' 387 ms' │ ' 209 ms' │ ' 1.85 x' │
898
- Recursive 16000000 │ ' 6327 ms' │ ' 2773 ms' │ ' 2.28 x' │
899
- Vector4 16000000 │ ' 362 ms' │ ' 172 ms' │ ' 2.10 x' │
900
- Matrix4 16000000 │ ' 607 ms' │ ' 418 ms' │ ' 1.45 x' │
901
- Literal_String 16000000 │ ' 314 ms' │ ' 151 ms' │ ' 2.08 x' │
902
- Literal_Number 16000000 │ ' 281 ms' │ ' 147 ms' │ ' 1.91 x' │
903
- Literal_Boolean 16000000 │ ' 289 ms' │ ' 148 ms' │ ' 1.95 x' │
904
- Array_Number 16000000 │ ' 475 ms' │ ' 230 ms' │ ' 2.07 x' │
905
- Array_String 16000000 │ ' 463 ms' │ ' 297 ms' │ ' 1.56 x' │
906
- Array_Boolean 16000000 │ ' 521 ms' │ ' 348 ms' │ ' 1.50 x' │
907
- Array_ObjectA 16000000 │ ' 43842 ms' │ ' 28375 ms' │ ' 1.55 x' │
908
- Array_ObjectB 16000000 │ ' 45055 ms' │ ' 32882 ms' │ ' 1.37 x' │
909
- Array_Tuple 16000000 │ ' 1424 ms' │ ' 1105 ms' │ ' 1.29 x' │
910
- Array_Union 16000000 │ ' 3505 ms' │ ' 1350 ms' │ ' 2.60 x' │
911
- Array_Recursive 16000000 │ ' 117087 ms' │ ' 42982 ms' │ ' 2.72 x' │
912
- │ Array_Vector4 │ 16000000 │ ' 1500 ms' │ ' 817 ms' │ ' 1.84 x' │
913
- │ Array_Matrix4 │ 16000000 │ ' 6126 ms' │ ' 4965 ms' │ ' 1.23 x' │
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' │
914
892
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
915
893
  ```
916
894
 
917
- ### Compile
895
+ ### Validate
918
896
 
919
- This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/compile.ts).
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).
920
898
 
921
899
  ```typescript
922
900
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
923
901
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
924
902
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
925
- │ Number │ 2000 │ ' 387 ms' │ ' 7 ms' │ ' 55.29 x' │
926
- │ String │ 2000 │ ' 340 ms' │ ' 6 ms' │ ' 56.67 x' │
927
- │ Boolean │ 2000 │ ' 314 ms' │ ' 5 ms' │ ' 62.80 x' │
928
- │ Null │ 2000 │ ' 259 ms' │ ' 5 ms' │ ' 51.80 x' │
929
- │ RegEx │ 2000 │ ' 491 ms' │ ' 7 ms' │ ' 70.14 x' │
930
- │ ObjectA │ 2000 │ ' 2850 ms' │ ' 26 ms' │ ' 109.62 x' │
931
- │ ObjectB │ 2000 │ ' 2989 ms' │ ' 22 ms' │ ' 135.86 x' │
932
- │ Tuple │ 2000 │ ' 1283 ms' │ ' 17 ms' │ ' 75.47 x' │
933
- │ Union │ 2000 │ ' 1292 ms' │ ' 16 ms' │ ' 80.75 x' │
934
- Vector4 2000 │ ' 1588 ms' │ ' 12 ms' │ ' 132.33 x' │
935
- Matrix4 2000 │ ' 934 ms' │ ' 7 ms' │ ' 133.43 x' │
936
- Literal_String 2000 │ ' 340 ms' │ ' 5 ms' │ ' 68.00 x' │
937
- Literal_Number 2000 │ ' 402 ms' │ ' 4 ms' │ ' 100.50 x' │
938
- Literal_Boolean 2000 │ ' 381 ms' │ ' 6 ms' │ ' 63.50 x' │
939
- Array_Number 2000 │ ' 750 ms' │ ' 8 ms' │ ' 93.75 x' │
940
- Array_String 2000 │ ' 760 ms' │ ' 6 ms' │ ' 126.67 x' │
941
- Array_Boolean 2000 │ ' 796 ms' │ ' 7 ms' │ ' 113.71 x' │
942
- Array_ObjectA 2000 │ ' 3617 ms' │ ' 26 ms' │ ' 139.12 x' │
943
- Array_ObjectB 2000 │ ' 3823 ms' │ ' 26 ms' │ ' 147.04 x' │
944
- Array_Tuple 2000 │ ' 2263 ms' │ ' 13 ms' │ ' 174.08 x' │
945
- Array_Union 2000 │ ' 1725 ms' │ ' 16 ms' │ ' 107.81 x' │
946
- Array_Vector4 2000 │ ' 2445 ms' │ ' 14 ms' │ ' 174.64 x' │
947
- Array_Matrix4 2000 │ ' 1638 ms' │ ' 11 ms' │ ' 148.91 x' │
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' │
948
928
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
949
929
  ```
950
930
 
951
- <a name="Contribute"></a>
931
+
952
932
 
953
933
  ## Contribute
954
934
 
package/typebox.js CHANGED
@@ -91,7 +91,7 @@ class TypeBuilder {
91
91
  const values = Object.keys(item)
92
92
  .filter((key) => isNaN(key))
93
93
  .map((key) => item[key]);
94
- const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
94
+ const anyOf = values.map((value) => typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value });
95
95
  return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'Enum', anyOf });
96
96
  }
97
97
  /** Creates a function type */
@@ -308,7 +308,9 @@ class TypeBuilder {
308
308
  const additionalItems = false;
309
309
  const minItems = items.length;
310
310
  const maxItems = items.length;
311
- const schema = (items.length > 0 ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
311
+ const schema = (items.length > 0
312
+ ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems }
313
+ : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
312
314
  return this.Create(schema);
313
315
  }
314
316
  /** Creates a undefined type */
package/value/cast.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class ValueCastInvalidTypeError extends Error {
3
+ readonly schema: Types.TSchema;
4
+ constructor(schema: Types.TSchema);
5
+ }
2
6
  export declare namespace ValueCast {
3
7
  function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
4
8
  function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
package/value/cast.js CHANGED
@@ -27,8 +27,9 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCast = void 0;
30
+ exports.ValueCast = exports.ValueCastInvalidTypeError = void 0;
31
31
  const Types = require("../typebox");
32
+ const index_1 = require("../guard/index");
32
33
  const create_1 = require("./create");
33
34
  const check_1 = require("./check");
34
35
  // --------------------------------------------------------------------------
@@ -71,6 +72,13 @@ var UnionValueCast;
71
72
  }
72
73
  UnionValueCast.Create = Create;
73
74
  })(UnionValueCast || (UnionValueCast = {}));
75
+ class ValueCastInvalidTypeError extends Error {
76
+ constructor(schema) {
77
+ super('ValueCast: Invalid type');
78
+ this.schema = schema;
79
+ }
80
+ }
81
+ exports.ValueCastInvalidTypeError = ValueCastInvalidTypeError;
74
82
  var ValueCast;
75
83
  (function (ValueCast) {
76
84
  function Any(schema, references, value) {
@@ -189,57 +197,72 @@ var ValueCast;
189
197
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
190
198
  }
191
199
  function Visit(schema, references, value) {
192
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
193
- const anySchema = schema;
194
- switch (schema[Types.Kind]) {
195
- case 'Any':
196
- return Any(anySchema, anyReferences, value);
197
- case 'Array':
198
- return Array(anySchema, anyReferences, value);
199
- case 'Boolean':
200
- return Boolean(anySchema, anyReferences, value);
201
- case 'Constructor':
202
- return Constructor(anySchema, anyReferences, value);
203
- case 'Enum':
204
- return Enum(anySchema, anyReferences, value);
205
- case 'Function':
206
- return Function(anySchema, anyReferences, value);
207
- case 'Integer':
208
- return Integer(anySchema, anyReferences, value);
209
- case 'Literal':
210
- return Literal(anySchema, anyReferences, value);
211
- case 'Null':
212
- return Null(anySchema, anyReferences, value);
213
- case 'Number':
214
- return Number(anySchema, anyReferences, value);
215
- case 'Object':
216
- return Object(anySchema, anyReferences, value);
217
- case 'Promise':
218
- return Promise(anySchema, anyReferences, value);
219
- case 'Record':
220
- return Record(anySchema, anyReferences, value);
221
- case 'Rec':
222
- return Recursive(anySchema, anyReferences, value);
223
- case 'Ref':
224
- return Ref(anySchema, anyReferences, value);
225
- case 'Self':
226
- return Self(anySchema, anyReferences, value);
227
- case 'String':
228
- return String(anySchema, anyReferences, value);
229
- case 'Tuple':
230
- return Tuple(anySchema, anyReferences, value);
231
- case 'Undefined':
232
- return Undefined(anySchema, anyReferences, value);
233
- case 'Union':
234
- return Union(anySchema, anyReferences, value);
235
- case 'Uint8Array':
236
- return Uint8Array(anySchema, anyReferences, value);
237
- case 'Unknown':
238
- return Unknown(anySchema, anyReferences, value);
239
- case 'Void':
240
- return Void(anySchema, anyReferences, value);
241
- default:
242
- throw Error(`ValueCast.Visit: Unknown schema kind '${schema[Types.Kind]}'`);
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);
243
266
  }
244
267
  }
245
268
  ValueCast.Visit = Visit;
package/value/check.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class ValueCheckInvalidTypeError extends Error {
3
+ readonly schema: Types.TSchema;
4
+ constructor(schema: Types.TSchema);
5
+ }
2
6
  export declare namespace ValueCheck {
3
7
  function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): boolean;
4
8
  }