@sinclair/typebox 0.26.0-dev.4 → 0.26.0
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 +9 -4
- package/compiler/compiler.js +135 -108
- package/errors/errors.d.ts +5 -1
- package/errors/errors.js +173 -157
- package/package.json +1 -1
- package/readme.md +114 -101
- package/system/system.d.ts +4 -0
- package/system/system.js +15 -2
- package/typebox.d.ts +27 -36
- package/typebox.js +108 -173
- package/value/cast.d.ts +6 -2
- package/value/cast.js +130 -111
- package/value/check.d.ts +5 -1
- package/value/check.js +168 -154
- package/value/convert.d.ts +6 -6
- package/value/convert.js +83 -74
- package/value/create.d.ts +6 -2
- package/value/create.js +102 -77
- package/value/value.d.ts +10 -0
- package/value/value.js +15 -15
package/readme.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
6
|
|
|
7
|
-
<img src="
|
|
7
|
+
<img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
|
|
8
8
|
|
|
9
9
|
<br />
|
|
10
10
|
<br />
|
|
@@ -62,9 +62,9 @@ type T = Static<typeof T> // type T = {
|
|
|
62
62
|
|
|
63
63
|
## Overview
|
|
64
64
|
|
|
65
|
-
TypeBox is a runtime type builder that
|
|
65
|
+
TypeBox is a runtime type builder 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 assertion rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
|
|
66
66
|
|
|
67
|
-
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST
|
|
67
|
+
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
|
|
68
68
|
|
|
69
69
|
License MIT
|
|
70
70
|
|
|
@@ -189,13 +189,13 @@ function receive(value: T) { // ... as a Static Type
|
|
|
189
189
|
|
|
190
190
|
## Types
|
|
191
191
|
|
|
192
|
-
TypeBox types are
|
|
192
|
+
TypeBox types are JSON schema fragments that can be composed into more complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
|
|
193
193
|
|
|
194
194
|
<a name='types-standard'></a>
|
|
195
195
|
|
|
196
196
|
### Standard Types
|
|
197
197
|
|
|
198
|
-
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema specification.
|
|
198
|
+
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
|
|
199
199
|
|
|
200
200
|
```typescript
|
|
201
201
|
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
@@ -457,17 +457,6 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
457
457
|
│ │ │ │
|
|
458
458
|
│ │ │ │
|
|
459
459
|
│ │ │ │
|
|
460
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
461
|
-
│ const A = Type.Object({ │ type T = { │ const T = { │
|
|
462
|
-
│ x: Type.Number(), │ x: number, │ $id: 'T' │
|
|
463
|
-
│ y: Type.Number() │ y: number │ type: 'object', │
|
|
464
|
-
│ }, { $id: 'T' }) | } │ required: ['x', 'y'], │
|
|
465
|
-
│ │ │ properties: { │
|
|
466
|
-
│ const T = Type.Deref( │ │ x: { type: 'number' }, │
|
|
467
|
-
│ Type.Ref(A) │ │ y: { type: 'number' }, │
|
|
468
|
-
│ ) │ │ } │
|
|
469
|
-
│ │ │ } │
|
|
470
|
-
│ │ │ │
|
|
471
460
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
472
461
|
```
|
|
473
462
|
|
|
@@ -475,9 +464,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
475
464
|
|
|
476
465
|
### Extended Types
|
|
477
466
|
|
|
478
|
-
TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types
|
|
479
|
-
|
|
480
|
-
The following lists the supported types
|
|
467
|
+
TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types can not be used with standard JSON schema validators; but are useful to help frame schematics for RPC interfaces that may receive JSON validated data. Extended types are prefixed with the `[Extended]` doc comment for convenience. The following table lists the supported types.
|
|
481
468
|
|
|
482
469
|
```typescript
|
|
483
470
|
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
@@ -570,7 +557,7 @@ The following lists the supported types
|
|
|
570
557
|
|
|
571
558
|
### Modifiers
|
|
572
559
|
|
|
573
|
-
TypeBox provides
|
|
560
|
+
TypeBox provides modifiers that allow schema properties to be statically inferred as `readonly` or `optional`. The following table shows the supported modifiers and how they map between TypeScript and JSON Schema.
|
|
574
561
|
|
|
575
562
|
```typescript
|
|
576
563
|
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
@@ -614,7 +601,7 @@ TypeBox provides property modifier types that allow properties to be mapped with
|
|
|
614
601
|
|
|
615
602
|
### Options
|
|
616
603
|
|
|
617
|
-
You can pass JSON Schema
|
|
604
|
+
You can pass JSON Schema options on the last argument of any type. Option hints specific to each type are provided for convenience.
|
|
618
605
|
|
|
619
606
|
```typescript
|
|
620
607
|
// String must be an email
|
|
@@ -644,7 +631,7 @@ const T = Type.Array(Type.Integer(), { // const T = {
|
|
|
644
631
|
|
|
645
632
|
### Generic Types
|
|
646
633
|
|
|
647
|
-
Generic types can be created with generic functions
|
|
634
|
+
Generic types can be created with generic functions constrained to type `TSchema`. The following creates a generic `Vector<T>` type.
|
|
648
635
|
|
|
649
636
|
```typescript
|
|
650
637
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -661,6 +648,12 @@ const NumberVector = Vector(Type.Number()) // const NumberVector = {
|
|
|
661
648
|
// }
|
|
662
649
|
// }
|
|
663
650
|
|
|
651
|
+
type NumberVector = Static<typeof NumberVector> // type NumberVector = {
|
|
652
|
+
// x: number,
|
|
653
|
+
// y: number,
|
|
654
|
+
// z: number
|
|
655
|
+
// }
|
|
656
|
+
|
|
664
657
|
const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
|
|
665
658
|
// type: 'object',
|
|
666
659
|
// required: ['x', 'y', 'z'],
|
|
@@ -670,13 +663,34 @@ const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
|
|
|
670
663
|
// z: { type: 'boolean' }
|
|
671
664
|
// }
|
|
672
665
|
// }
|
|
666
|
+
|
|
667
|
+
type BooleanVector = Static<typeof BooleanVector> // type BooleanVector = {
|
|
668
|
+
// x: boolean,
|
|
669
|
+
// y: boolean,
|
|
670
|
+
// z: boolean
|
|
671
|
+
// }
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
The following creates a generic `Nullable<T>` type.
|
|
675
|
+
|
|
676
|
+
```typescript
|
|
677
|
+
const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
|
|
678
|
+
|
|
679
|
+
const T = Nullable(Type.String()) // const T = {
|
|
680
|
+
// anyOf: [
|
|
681
|
+
// { type: 'string' },
|
|
682
|
+
// { type: 'null' }
|
|
683
|
+
// ]
|
|
684
|
+
// }
|
|
685
|
+
|
|
686
|
+
type T = Static<typeof T> // type T = string | null
|
|
673
687
|
```
|
|
674
688
|
|
|
675
689
|
<a name='types-references'></a>
|
|
676
690
|
|
|
677
691
|
### Reference Types
|
|
678
692
|
|
|
679
|
-
Reference types are supported with `Type.Ref(...)`.
|
|
693
|
+
Reference types are supported with `Type.Ref(...)`. The target type must specify a valid `$id`.
|
|
680
694
|
|
|
681
695
|
```typescript
|
|
682
696
|
const T = Type.String({ $id: 'T' }) // const T = {
|
|
@@ -687,11 +701,6 @@ const T = Type.String({ $id: 'T' }) // const T = {
|
|
|
687
701
|
const R = Type.Ref(T) // const R = {
|
|
688
702
|
// $ref: 'T'
|
|
689
703
|
// }
|
|
690
|
-
|
|
691
|
-
const U = Type.Deref(R) // const U = {
|
|
692
|
-
// $id: 'T',
|
|
693
|
-
// type: 'string'
|
|
694
|
-
// }
|
|
695
704
|
```
|
|
696
705
|
|
|
697
706
|
<a name='types-recursive'></a>
|
|
@@ -735,9 +744,9 @@ function test(node: Node) {
|
|
|
735
744
|
|
|
736
745
|
### Conditional Types
|
|
737
746
|
|
|
738
|
-
Conditional types are supported with `Extends`, `Exclude` and `Extract`.
|
|
747
|
+
Conditional types are supported with `Extends`, `Exclude` and `Extract`.
|
|
739
748
|
|
|
740
|
-
|
|
749
|
+
**TypeScript**
|
|
741
750
|
|
|
742
751
|
```typescript
|
|
743
752
|
type T0 = string extends number ? true : false
|
|
@@ -747,7 +756,7 @@ type T1 = Extract<string | number, number>
|
|
|
747
756
|
type T2 = Exclude<string | number, number>
|
|
748
757
|
// ^ string
|
|
749
758
|
```
|
|
750
|
-
|
|
759
|
+
**TypeBox**
|
|
751
760
|
```typescript
|
|
752
761
|
const T0 = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false))
|
|
753
762
|
// ^ TLiteral<false>
|
|
@@ -761,7 +770,7 @@ const T2 = Type.Exclude(Type.Union([Type.String(), Type.Number()]), Type.Number(
|
|
|
761
770
|
|
|
762
771
|
### Unsafe
|
|
763
772
|
|
|
764
|
-
Use `Type.Unsafe(...)` to create custom
|
|
773
|
+
Use `Type.Unsafe(...)` to create custom schematics with user defined inference rules.
|
|
765
774
|
|
|
766
775
|
```typescript
|
|
767
776
|
const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
@@ -771,7 +780,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
771
780
|
type T = Static<typeof T> // type T = string
|
|
772
781
|
```
|
|
773
782
|
|
|
774
|
-
The `Type.Unsafe(...)` type
|
|
783
|
+
The `Type.Unsafe(...)` type can be useful to express specific OpenAPI schema representations.
|
|
775
784
|
|
|
776
785
|
```typescript
|
|
777
786
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -806,7 +815,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
|
806
815
|
|
|
807
816
|
### Guards
|
|
808
817
|
|
|
809
|
-
TypeBox provides a `TypeGuard` module for reflection and
|
|
818
|
+
TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
|
|
810
819
|
|
|
811
820
|
```typescript
|
|
812
821
|
import { Type, TypeGuard } from '@sinclair/typebox'
|
|
@@ -823,7 +832,7 @@ if(TypeGuard.TString(T)) {
|
|
|
823
832
|
|
|
824
833
|
### Strict
|
|
825
834
|
|
|
826
|
-
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and
|
|
835
|
+
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. 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.
|
|
827
836
|
|
|
828
837
|
```typescript
|
|
829
838
|
const T = Type.Object({ // const T = {
|
|
@@ -852,7 +861,7 @@ const U = Type.Strict(T) // const U = {
|
|
|
852
861
|
|
|
853
862
|
## Values
|
|
854
863
|
|
|
855
|
-
TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via
|
|
864
|
+
TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via optional import.
|
|
856
865
|
|
|
857
866
|
```typescript
|
|
858
867
|
import { Value } from '@sinclair/typebox/value'
|
|
@@ -1222,33 +1231,37 @@ For additional comparative benchmarks, please refer to [typescript-runtime-type-
|
|
|
1222
1231
|
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).
|
|
1223
1232
|
|
|
1224
1233
|
```typescript
|
|
1225
|
-
|
|
1226
|
-
│
|
|
1227
|
-
|
|
1228
|
-
│
|
|
1229
|
-
│
|
|
1230
|
-
│
|
|
1231
|
-
│
|
|
1232
|
-
│
|
|
1233
|
-
│
|
|
1234
|
-
│
|
|
1235
|
-
│
|
|
1236
|
-
│
|
|
1237
|
-
│
|
|
1238
|
-
│
|
|
1239
|
-
│
|
|
1240
|
-
│
|
|
1241
|
-
│
|
|
1242
|
-
│
|
|
1243
|
-
│
|
|
1244
|
-
│
|
|
1245
|
-
│
|
|
1246
|
-
│
|
|
1247
|
-
│
|
|
1248
|
-
│
|
|
1249
|
-
│
|
|
1250
|
-
│
|
|
1251
|
-
|
|
1234
|
+
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1235
|
+
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1236
|
+
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1237
|
+
│ Literal_String │ 1000 │ ' 260 ms' │ ' 8 ms' │ ' 32.50 x' │
|
|
1238
|
+
│ Literal_Number │ 1000 │ ' 198 ms' │ ' 4 ms' │ ' 49.50 x' │
|
|
1239
|
+
│ Literal_Boolean │ 1000 │ ' 185 ms' │ ' 5 ms' │ ' 37.00 x' │
|
|
1240
|
+
│ Primitive_Number │ 1000 │ ' 176 ms' │ ' 9 ms' │ ' 19.56 x' │
|
|
1241
|
+
│ Primitive_String │ 1000 │ ' 161 ms' │ ' 9 ms' │ ' 17.89 x' │
|
|
1242
|
+
│ Primitive_String_Pattern │ 1000 │ ' 215 ms' │ ' 12 ms' │ ' 17.92 x' │
|
|
1243
|
+
│ Primitive_Boolean │ 1000 │ ' 133 ms' │ ' 5 ms' │ ' 26.60 x' │
|
|
1244
|
+
│ Primitive_Null │ 1000 │ ' 143 ms' │ ' 8 ms' │ ' 17.88 x' │
|
|
1245
|
+
│ Object_Unconstrained │ 1000 │ ' 1181 ms' │ ' 38 ms' │ ' 31.08 x' │
|
|
1246
|
+
│ Object_Constrained │ 1000 │ ' 1168 ms' │ ' 32 ms' │ ' 36.50 x' │
|
|
1247
|
+
│ Tuple_Primitive │ 1000 │ ' 557 ms' │ ' 16 ms' │ ' 34.81 x' │
|
|
1248
|
+
│ Tuple_Object │ 1000 │ ' 1119 ms' │ ' 17 ms' │ ' 65.82 x' │
|
|
1249
|
+
│ Composite_Intersect │ 1000 │ ' 569 ms' │ ' 22 ms' │ ' 25.86 x' │
|
|
1250
|
+
│ Composite_Union │ 1000 │ ' 513 ms' │ ' 23 ms' │ ' 22.30 x' │
|
|
1251
|
+
│ Math_Vector4 │ 1000 │ ' 802 ms' │ ' 10 ms' │ ' 80.20 x' │
|
|
1252
|
+
│ Math_Matrix4 │ 1000 │ ' 395 ms' │ ' 12 ms' │ ' 32.92 x' │
|
|
1253
|
+
│ Array_Primitive_Number │ 1000 │ ' 282 ms' │ ' 8 ms' │ ' 35.25 x' │
|
|
1254
|
+
│ Array_Primitive_String │ 1000 │ ' 321 ms' │ ' 5 ms' │ ' 64.20 x' │
|
|
1255
|
+
│ Array_Primitive_Boolean │ 1000 │ ' 364 ms' │ ' 5 ms' │ ' 72.80 x' │
|
|
1256
|
+
│ Array_Object_Unconstrained │ 1000 │ ' 1573 ms' │ ' 18 ms' │ ' 87.39 x' │
|
|
1257
|
+
│ Array_Object_Constrained │ 1000 │ ' 1270 ms' │ ' 20 ms' │ ' 63.50 x' │
|
|
1258
|
+
│ Array_Tuple_Primitive │ 1000 │ ' 973 ms' │ ' 18 ms' │ ' 54.06 x' │
|
|
1259
|
+
│ Array_Tuple_Object │ 1000 │ ' 1253 ms' │ ' 16 ms' │ ' 78.31 x' │
|
|
1260
|
+
│ Array_Composite_Intersect │ 1000 │ ' 927 ms' │ ' 20 ms' │ ' 46.35 x' │
|
|
1261
|
+
│ Array_Composite_Union │ 1000 │ ' 1123 ms' │ ' 16 ms' │ ' 70.19 x' │
|
|
1262
|
+
│ Array_Math_Vector4 │ 1000 │ ' 1068 ms' │ ' 10 ms' │ ' 106.80 x' │
|
|
1263
|
+
│ Array_Math_Matrix4 │ 1000 │ ' 488 ms' │ ' 7 ms' │ ' 69.71 x' │
|
|
1264
|
+
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1252
1265
|
```
|
|
1253
1266
|
|
|
1254
1267
|
<a name='benchmark-validate'></a>
|
|
@@ -1258,35 +1271,39 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
1258
1271
|
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).
|
|
1259
1272
|
|
|
1260
1273
|
```typescript
|
|
1261
|
-
|
|
1262
|
-
│
|
|
1263
|
-
|
|
1264
|
-
│
|
|
1265
|
-
│
|
|
1266
|
-
│
|
|
1267
|
-
│
|
|
1268
|
-
│
|
|
1269
|
-
│
|
|
1270
|
-
│
|
|
1271
|
-
│
|
|
1272
|
-
│
|
|
1273
|
-
│
|
|
1274
|
-
│
|
|
1275
|
-
│
|
|
1276
|
-
│
|
|
1277
|
-
│
|
|
1278
|
-
│
|
|
1279
|
-
│
|
|
1280
|
-
│
|
|
1281
|
-
│
|
|
1282
|
-
│
|
|
1283
|
-
│
|
|
1284
|
-
│
|
|
1285
|
-
│
|
|
1286
|
-
│
|
|
1287
|
-
│
|
|
1288
|
-
│
|
|
1289
|
-
|
|
1274
|
+
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1275
|
+
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1276
|
+
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1277
|
+
│ Literal_String │ 1000000 │ ' 26 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
|
|
1278
|
+
│ Literal_Number │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1279
|
+
│ Literal_Boolean │ 1000000 │ ' 19 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1280
|
+
│ Primitive_Number │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1281
|
+
│ Primitive_String │ 1000000 │ ' 26 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1282
|
+
│ Primitive_String_Pattern │ 1000000 │ ' 159 ms' │ ' 45 ms' │ ' 37 ms' │ ' 1.22 x' │
|
|
1283
|
+
│ Primitive_Boolean │ 1000000 │ ' 23 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1284
|
+
│ Primitive_Null │ 1000000 │ ' 23 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1285
|
+
│ Object_Unconstrained │ 1000000 │ ' 809 ms' │ ' 35 ms' │ ' 30 ms' │ ' 1.17 x' │
|
|
1286
|
+
│ Object_Constrained │ 1000000 │ ' 1060 ms' │ ' 56 ms' │ ' 45 ms' │ ' 1.24 x' │
|
|
1287
|
+
│ Object_Recursive │ 1000000 │ ' 4965 ms' │ ' 397 ms' │ ' 100 ms' │ ' 3.97 x' │
|
|
1288
|
+
│ Tuple_Primitive │ 1000000 │ ' 159 ms' │ ' 22 ms' │ ' 16 ms' │ ' 1.38 x' │
|
|
1289
|
+
│ Tuple_Object │ 1000000 │ ' 658 ms' │ ' 31 ms' │ ' 27 ms' │ ' 1.15 x' │
|
|
1290
|
+
│ Composite_Intersect │ 1000000 │ ' 695 ms' │ ' 26 ms' │ ' 22 ms' │ ' 1.18 x' │
|
|
1291
|
+
│ Composite_Union │ 1000000 │ ' 503 ms' │ ' 24 ms' │ ' 19 ms' │ ' 1.26 x' │
|
|
1292
|
+
│ Math_Vector4 │ 1000000 │ ' 259 ms' │ ' 22 ms' │ ' 14 ms' │ ' 1.57 x' │
|
|
1293
|
+
│ Math_Matrix4 │ 1000000 │ ' 1007 ms' │ ' 40 ms' │ ' 29 ms' │ ' 1.38 x' │
|
|
1294
|
+
│ Array_Primitive_Number │ 1000000 │ ' 262 ms' │ ' 23 ms' │ ' 17 ms' │ ' 1.35 x' │
|
|
1295
|
+
│ Array_Primitive_String │ 1000000 │ ' 241 ms' │ ' 27 ms' │ ' 24 ms' │ ' 1.13 x' │
|
|
1296
|
+
│ Array_Primitive_Boolean │ 1000000 │ ' 141 ms' │ ' 23 ms' │ ' 20 ms' │ ' 1.15 x' │
|
|
1297
|
+
│ Array_Object_Unconstrained │ 1000000 │ ' 4976 ms' │ ' 70 ms' │ ' 67 ms' │ ' 1.04 x' │
|
|
1298
|
+
│ Array_Object_Constrained │ 1000000 │ ' 5234 ms' │ ' 143 ms' │ ' 120 ms' │ ' 1.19 x' │
|
|
1299
|
+
│ Array_Object_Recursive │ 1000000 │ ' 19605 ms' │ ' 1909 ms' │ ' 350 ms' │ ' 5.45 x' │
|
|
1300
|
+
│ Array_Tuple_Primitive │ 1000000 │ ' 706 ms' │ ' 39 ms' │ ' 32 ms' │ ' 1.22 x' │
|
|
1301
|
+
│ Array_Tuple_Object │ 1000000 │ ' 2951 ms' │ ' 67 ms' │ ' 63 ms' │ ' 1.06 x' │
|
|
1302
|
+
│ Array_Composite_Intersect │ 1000000 │ ' 2969 ms' │ ' 49 ms' │ ' 44 ms' │ ' 1.11 x' │
|
|
1303
|
+
│ Array_Composite_Union │ 1000000 │ ' 2191 ms' │ ' 77 ms' │ ' 41 ms' │ ' 1.88 x' │
|
|
1304
|
+
│ Array_Math_Vector4 │ 1000000 │ ' 1164 ms' │ ' 41 ms' │ ' 25 ms' │ ' 1.64 x' │
|
|
1305
|
+
│ Array_Math_Matrix4 │ 1000000 │ ' 4903 ms' │ ' 115 ms' │ ' 99 ms' │ ' 1.16 x' │
|
|
1306
|
+
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1290
1307
|
```
|
|
1291
1308
|
|
|
1292
1309
|
<a name='benchmark-compression'></a>
|
|
@@ -1299,15 +1316,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1299
1316
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1300
1317
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1301
1318
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1302
|
-
│ typebox/compiler │ '
|
|
1303
|
-
│ typebox/
|
|
1304
|
-
│ typebox/
|
|
1305
|
-
│ typebox/
|
|
1306
|
-
│ typebox
|
|
1307
|
-
│ typebox/hash │ ' 4.2 kb' │ ' 1.8 kb' │ '2.30 x' │
|
|
1308
|
-
│ typebox/system │ ' 14.0 kb' │ ' 7.1 kb' │ '1.96 x' │
|
|
1309
|
-
│ typebox/value │ ' 90.0 kb' │ ' 41.8 kb' │ '2.15 x' │
|
|
1310
|
-
│ typebox │ ' 12.0 kb' │ ' 6.4 kb' │ '1.89 x' │
|
|
1319
|
+
│ typebox/compiler │ '108.8 kb' │ ' 48.9 kb' │ '2.23 x' │
|
|
1320
|
+
│ typebox/errors │ ' 93.2 kb' │ ' 41.5 kb' │ '2.24 x' │
|
|
1321
|
+
│ typebox/system │ ' 60.0 kb' │ ' 24.6 kb' │ '2.43 x' │
|
|
1322
|
+
│ typebox/value │ '153.5 kb' │ ' 66.7 kb' │ '2.30 x' │
|
|
1323
|
+
│ typebox │ ' 58.7 kb' │ ' 24.1 kb' │ '2.43 x' │
|
|
1311
1324
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1312
1325
|
```
|
|
1313
1326
|
|
package/system/system.d.ts
CHANGED
|
@@ -17,4 +17,8 @@ export declare namespace TypeSystem {
|
|
|
17
17
|
function Type<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
|
|
18
18
|
/** Creates a new string format */
|
|
19
19
|
function Format<F extends string>(format: F, check: (value: string) => boolean): F;
|
|
20
|
+
/** @deprecated Use `TypeSystem.Type()` instead. */
|
|
21
|
+
function CreateType<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
|
|
22
|
+
/** @deprecated Use `TypeSystem.Format()` instead. */
|
|
23
|
+
function CreateFormat<F extends string>(format: F, check: (value: string) => boolean): F;
|
|
20
24
|
}
|
package/system/system.js
CHANGED
|
@@ -31,13 +31,13 @@ exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDupli
|
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
class TypeSystemDuplicateTypeKind extends Error {
|
|
33
33
|
constructor(kind) {
|
|
34
|
-
super(`Duplicate kind '${kind}' detected`);
|
|
34
|
+
super(`Duplicate type kind '${kind}' detected`);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
exports.TypeSystemDuplicateTypeKind = TypeSystemDuplicateTypeKind;
|
|
38
38
|
class TypeSystemDuplicateFormat extends Error {
|
|
39
39
|
constructor(kind) {
|
|
40
|
-
super(`Duplicate format '${kind}' detected`);
|
|
40
|
+
super(`Duplicate string format '${kind}' detected`);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat;
|
|
@@ -66,4 +66,17 @@ var TypeSystem;
|
|
|
66
66
|
return format;
|
|
67
67
|
}
|
|
68
68
|
TypeSystem.Format = Format;
|
|
69
|
+
// ------------------------------------------------------------------------
|
|
70
|
+
// Deprecated
|
|
71
|
+
// ------------------------------------------------------------------------
|
|
72
|
+
/** @deprecated Use `TypeSystem.Type()` instead. */
|
|
73
|
+
function CreateType(kind, check) {
|
|
74
|
+
return Type(kind, check);
|
|
75
|
+
}
|
|
76
|
+
TypeSystem.CreateType = CreateType;
|
|
77
|
+
/** @deprecated Use `TypeSystem.Format()` instead. */
|
|
78
|
+
function CreateFormat(format, check) {
|
|
79
|
+
return Format(format, check);
|
|
80
|
+
}
|
|
81
|
+
TypeSystem.CreateFormat = CreateFormat;
|
|
69
82
|
})(TypeSystem = exports.TypeSystem || (exports.TypeSystem = {}));
|
package/typebox.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export declare const Recursive: unique symbol;
|
|
2
1
|
export declare const Modifier: unique symbol;
|
|
3
2
|
export declare const Hint: unique symbol;
|
|
4
3
|
export declare const Kind: unique symbol;
|
|
@@ -27,7 +26,7 @@ export type TReadonlyOptional<T extends TSchema> = T & {
|
|
|
27
26
|
export interface SchemaOptions {
|
|
28
27
|
$schema?: string;
|
|
29
28
|
/** Id for this schema */
|
|
30
|
-
|
|
29
|
+
$id?: string;
|
|
31
30
|
/** Title of this schema */
|
|
32
31
|
title?: string;
|
|
33
32
|
/** Description of this schema */
|
|
@@ -43,7 +42,6 @@ export interface TKind {
|
|
|
43
42
|
}
|
|
44
43
|
export interface TSchema extends SchemaOptions, TKind {
|
|
45
44
|
[Modifier]?: string;
|
|
46
|
-
[Recursive]?: string;
|
|
47
45
|
[Hint]?: string;
|
|
48
46
|
params: unknown[];
|
|
49
47
|
static: unknown;
|
|
@@ -84,16 +82,17 @@ export interface TBoolean extends TSchema {
|
|
|
84
82
|
type: 'boolean';
|
|
85
83
|
}
|
|
86
84
|
export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
|
|
87
|
-
export type TDeref<T> = T extends TRef<infer U> ? TDeref<U> : T;
|
|
88
85
|
export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
|
|
89
86
|
export type TCompositeUnion<Left extends TSchema, Right extends TSchema> = Ensure<TUnion<[Left, Right]>>;
|
|
90
|
-
export type
|
|
91
|
-
[
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
export type
|
|
87
|
+
export type TCompositeUnionLeft<T extends TObject, Acc extends TProperties> = {
|
|
88
|
+
[K in keyof T['properties']]: K extends keyof Acc ? TCompositeUnion<T['properties'][K], Acc[K]> : T['properties'][K];
|
|
89
|
+
};
|
|
90
|
+
export type TCompositeUnionRight<T extends TObject, Acc extends TProperties> = {
|
|
91
|
+
[K in keyof Acc]: K extends keyof T['properties'] ? TCompositeUnion<T['properties'][K], Acc[K]> : Acc[K];
|
|
92
|
+
};
|
|
93
|
+
export type TCompositeUnionObject<T extends TObject, Acc extends TProperties> = Evaluate<TCompositeUnionLeft<T, Acc> & TCompositeUnionRight<T, Acc>>;
|
|
94
|
+
export type TCompositeProperties<T extends TObject[], Acc extends TProperties> = T extends [...infer L, infer R] ? TCompositeProperties<Assert<L, TObject[]>, TCompositeUnionObject<Assert<R, TObject>, Acc>> : T extends [] ? Acc : never;
|
|
95
|
+
export type TComposite<T extends TObject[] = TObject[]> = Ensure<TObject<TCompositeProperties<T, {}>>>;
|
|
97
96
|
export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{
|
|
98
97
|
[K in keyof T]: Static<Assert<T[K], TSchema>, P>;
|
|
99
98
|
}];
|
|
@@ -170,9 +169,9 @@ export type TKeyOfTuple<T extends TSchema> = {
|
|
|
170
169
|
} extends infer U ? UnionToTuple<Exclude<{
|
|
171
170
|
[K in keyof U]: U[K];
|
|
172
171
|
}[keyof U], undefined>> : never;
|
|
173
|
-
export type TKeyOf<T extends TSchema = TSchema
|
|
172
|
+
export type TKeyOf<T extends TSchema = TSchema> = (T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : [
|
|
174
173
|
]) extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
|
|
175
|
-
export type TLiteralValue = string | number | boolean
|
|
174
|
+
export type TLiteralValue = string | number | boolean;
|
|
176
175
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
177
176
|
[Kind]: 'Literal';
|
|
178
177
|
static: T;
|
|
@@ -242,7 +241,7 @@ export type TOmitArray<T extends TSchema[], K extends keyof any> = Assert<{
|
|
|
242
241
|
[K2 in keyof T]: TOmit<Assert<T[K2], TSchema>, K>;
|
|
243
242
|
}, TSchema[]>;
|
|
244
243
|
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<Assert<Omit<T, K>, TProperties>>;
|
|
245
|
-
export type TOmit<T extends TSchema, K extends keyof any
|
|
244
|
+
export type TOmit<T extends TSchema, K extends keyof any> = T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
246
245
|
export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
|
|
247
246
|
export type TPartialArray<T extends TSchema[]> = Assert<{
|
|
248
247
|
[K in keyof T]: TPartial<Assert<T[K], TSchema>>;
|
|
@@ -250,14 +249,14 @@ export type TPartialArray<T extends TSchema[]> = Assert<{
|
|
|
250
249
|
export type TPartialProperties<T extends TProperties> = Evaluate<Assert<{
|
|
251
250
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
|
|
252
251
|
}, TProperties>>;
|
|
253
|
-
export type TPartial<T extends TSchema
|
|
252
|
+
export type TPartial<T extends TSchema> = T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
|
|
254
253
|
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
255
254
|
[K2 in keyof T]: TPick<Assert<T[K2], TSchema>, K>;
|
|
256
255
|
};
|
|
257
256
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
258
257
|
[K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
|
|
259
258
|
}) : never;
|
|
260
|
-
export type TPick<T extends TSchema, K extends keyof any
|
|
259
|
+
export type TPick<T extends TSchema, K extends keyof any> = T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
261
260
|
export type TPromiseStatic<T extends TSchema, P extends unknown[]> = Promise<Static<T, P>>;
|
|
262
261
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
263
262
|
[Kind]: 'Promise';
|
|
@@ -305,7 +304,7 @@ export type TRequiredArray<T extends TSchema[]> = Assert<{
|
|
|
305
304
|
export type TRequiredProperties<T extends TProperties> = Evaluate<Assert<{
|
|
306
305
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
|
|
307
306
|
}, TProperties>>;
|
|
308
|
-
export type TRequired<T extends TSchema
|
|
307
|
+
export type TRequired<T extends TSchema> = T extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : T extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T;
|
|
309
308
|
export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
|
|
310
309
|
export interface StringOptions<Format extends string> extends SchemaOptions {
|
|
311
310
|
minLength?: number;
|
|
@@ -385,12 +384,15 @@ export interface TVoid extends TSchema {
|
|
|
385
384
|
type: 'null';
|
|
386
385
|
typeOf: 'Void';
|
|
387
386
|
}
|
|
388
|
-
/** Creates a static type from a TypeBox type */
|
|
387
|
+
/** Creates a TypeScript static type from a TypeBox type */
|
|
389
388
|
export type Static<T extends TSchema, P extends unknown[] = []> = (T & {
|
|
390
389
|
params: P;
|
|
391
390
|
})['static'];
|
|
392
391
|
export type TypeRegistryValidationFunction<TSchema> = (schema: TSchema, value: unknown) => boolean;
|
|
392
|
+
/** A registry for user defined types */
|
|
393
393
|
export declare namespace TypeRegistry {
|
|
394
|
+
/** Returns the entries in this registry */
|
|
395
|
+
function Entries(): Map<string, TypeRegistryValidationFunction<any>>;
|
|
394
396
|
/** Clears all user defined types */
|
|
395
397
|
function Clear(): void;
|
|
396
398
|
/** Returns true if this registry contains this kind */
|
|
@@ -400,21 +402,11 @@ export declare namespace TypeRegistry {
|
|
|
400
402
|
/** Gets a custom validation function for a user defined type */
|
|
401
403
|
function Get(kind: string): TypeRegistryValidationFunction<any> | undefined;
|
|
402
404
|
}
|
|
403
|
-
export declare namespace ReferenceRegistry {
|
|
404
|
-
/** Clears the reference registry */
|
|
405
|
-
function Clear(): void;
|
|
406
|
-
/** Returns true if this registry contains this schema */
|
|
407
|
-
function Has(schema: TSchema): boolean;
|
|
408
|
-
/** Sets this schema on this registry if a $id exists */
|
|
409
|
-
function Set(schema: TSchema): void;
|
|
410
|
-
/** Dereferences the schema one level deep */
|
|
411
|
-
function DerefOne(schema: TSchema): TSchema;
|
|
412
|
-
/** Dereferences the schema recursively */
|
|
413
|
-
function Deref(schema: TSchema): TSchema;
|
|
414
|
-
}
|
|
415
405
|
export type FormatRegistryValidationFunction = (value: string) => boolean;
|
|
416
|
-
/**
|
|
406
|
+
/** A registry for user defined string formats */
|
|
417
407
|
export declare namespace FormatRegistry {
|
|
408
|
+
/** Returns the entries in this registry */
|
|
409
|
+
function Entries(): Map<string, FormatRegistryValidationFunction>;
|
|
418
410
|
/** Clears all user defined string formats */
|
|
419
411
|
function Clear(): void;
|
|
420
412
|
/** Returns true if the user defined string format exists */
|
|
@@ -428,6 +420,7 @@ export declare class TypeGuardUnknownTypeError extends Error {
|
|
|
428
420
|
readonly schema: unknown;
|
|
429
421
|
constructor(schema: unknown);
|
|
430
422
|
}
|
|
423
|
+
/** Provides functions to test if JavaScript values are TypeBox types */
|
|
431
424
|
export declare namespace TypeGuard {
|
|
432
425
|
/** Returns true if the given schema is TAny */
|
|
433
426
|
function TAny(schema: unknown): schema is TAny;
|
|
@@ -496,8 +489,8 @@ export declare namespace TypeGuard {
|
|
|
496
489
|
/** Returns true if the given schema is TSchema */
|
|
497
490
|
function TSchema(schema: unknown): schema is TSchema;
|
|
498
491
|
}
|
|
492
|
+
/** Fast undefined check used for properties of type undefined */
|
|
499
493
|
export declare namespace ExtendsUndefined {
|
|
500
|
-
/** Fast undefined check for properties of type undefined */
|
|
501
494
|
function Check(schema: TSchema): boolean;
|
|
502
495
|
}
|
|
503
496
|
export declare enum TypeExtendsResult {
|
|
@@ -508,7 +501,7 @@ export declare enum TypeExtendsResult {
|
|
|
508
501
|
export declare namespace TypeExtends {
|
|
509
502
|
function Extends(left: TSchema, right: TSchema): TypeExtendsResult;
|
|
510
503
|
}
|
|
511
|
-
/** Specialized Clone for Types
|
|
504
|
+
/** Specialized Clone for Types */
|
|
512
505
|
export declare namespace TypeClone {
|
|
513
506
|
/** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
|
|
514
507
|
function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
|
|
@@ -538,10 +531,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
538
531
|
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
|
|
539
532
|
/** `[Standard]` Creates a Boolean type */
|
|
540
533
|
Boolean(options?: SchemaOptions): TBoolean;
|
|
541
|
-
/** `[Standard]` Creates a Composite object type that will
|
|
534
|
+
/** `[Standard]` Creates a Composite object type that will union any overlapping properties of the given object array */
|
|
542
535
|
Composite<T extends TObject[]>(schemas: [...T], options?: ObjectOptions): TComposite<T>;
|
|
543
|
-
/** `[Standard]` Dereferences the given TRef to its target type */
|
|
544
|
-
Deref<T extends TRef>(schema: T): TDeref<T>;
|
|
545
536
|
/** `[Standard]` Creates a Enum type */
|
|
546
537
|
Enum<T extends Record<string, string | number>>(item: T, options?: SchemaOptions): TEnum<T>;
|
|
547
538
|
/** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
|