@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/compiler/compiler.d.ts +8 -1
- package/compiler/compiler.js +130 -55
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +3 -0
- package/errors/errors.d.ts +56 -0
- package/errors/errors.js +381 -0
- package/errors/index.d.ts +1 -0
- package/errors/index.js +44 -0
- package/guard/guard.js +82 -14
- package/package.json +1 -1
- package/readme.md +84 -104
- package/typebox.js +4 -2
- package/value/cast.d.ts +4 -0
- package/value/cast.js +75 -52
- package/value/check.d.ts +4 -0
- package/value/check.js +75 -49
- package/value/create.d.ts +4 -0
- package/value/create.js +75 -53
- package/value/index.d.ts +1 -1
- package/value/index.js +3 -0
- package/value/value.d.ts +1 -1
- package/value/value.js +2 -2
- package/value/errors.d.ts +0 -10
- package/value/errors.js +0 -305
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.
|
|
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](#
|
|
56
|
-
- [Overview](#
|
|
57
|
-
- [Usage](#
|
|
58
|
-
- [Types](#
|
|
59
|
-
- [Modifiers](#
|
|
60
|
-
- [Options](#
|
|
61
|
-
- [Extended Types](#
|
|
62
|
-
- [Reference Types](#
|
|
63
|
-
- [Recursive Types](#
|
|
64
|
-
- [Generic Types](#
|
|
65
|
-
- [Unsafe Types](#
|
|
66
|
-
- [Conditional Types](#
|
|
67
|
-
- [Values](#
|
|
68
|
-
- [Guards](#
|
|
69
|
-
- [Strict](#
|
|
70
|
-
- [Validation](#
|
|
71
|
-
- [Compiler](#
|
|
72
|
-
- [Benchmark](#
|
|
73
|
-
- [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
|
-
|
|
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
|
-
//
|
|
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
|
|
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
|
-
|
|
859
|
+
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
882
860
|
|
|
883
|
-
|
|
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 │
|
|
890
|
-
│ String │
|
|
891
|
-
│ Boolean │
|
|
892
|
-
│ Null │
|
|
893
|
-
│ RegEx │
|
|
894
|
-
│ ObjectA │
|
|
895
|
-
│ ObjectB │
|
|
896
|
-
│ Tuple │
|
|
897
|
-
│ Union │
|
|
898
|
-
│
|
|
899
|
-
│
|
|
900
|
-
│
|
|
901
|
-
│
|
|
902
|
-
│
|
|
903
|
-
│
|
|
904
|
-
│
|
|
905
|
-
│
|
|
906
|
-
│
|
|
907
|
-
│
|
|
908
|
-
│
|
|
909
|
-
│
|
|
910
|
-
│
|
|
911
|
-
│
|
|
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
|
-
###
|
|
895
|
+
### Validate
|
|
918
896
|
|
|
919
|
-
This benchmark measures
|
|
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 │
|
|
926
|
-
│ String │
|
|
927
|
-
│ Boolean │
|
|
928
|
-
│ Null │
|
|
929
|
-
│ RegEx │
|
|
930
|
-
│ ObjectA │
|
|
931
|
-
│ ObjectB │
|
|
932
|
-
│ Tuple │
|
|
933
|
-
│ Union │
|
|
934
|
-
│
|
|
935
|
-
│
|
|
936
|
-
│
|
|
937
|
-
│
|
|
938
|
-
│
|
|
939
|
-
│
|
|
940
|
-
│
|
|
941
|
-
│
|
|
942
|
-
│
|
|
943
|
-
│
|
|
944
|
-
│
|
|
945
|
-
│
|
|
946
|
-
│
|
|
947
|
-
│
|
|
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
|
-
|
|
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) =>
|
|
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
|
|
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
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
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
|
}
|