@sinclair/typebox 0.25.8 → 0.25.10

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
@@ -19,28 +19,45 @@
19
19
 
20
20
  ## Install
21
21
 
22
- Node
23
-
22
+ ### npm
24
23
  ```bash
25
24
  $ npm install @sinclair/typebox --save
26
25
  ```
27
26
 
28
- Deno and ESM
27
+ ### deno
28
+ ```typescript
29
+ import { Static, Type } from 'npm:@sinclair/typebox'
30
+ ```
31
+
32
+ ### esm
29
33
 
30
34
  ```typescript
31
35
  import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
32
36
  ```
33
37
 
34
- ## Example
38
+ ## Usage
35
39
 
36
40
  ```typescript
37
41
  import { Static, Type } from '@sinclair/typebox'
38
42
 
39
- const T = Type.String() // const T = { type: 'string' }
43
+ const T = Type.Object({ // const T = {
44
+ x: Type.Number(), // type: 'object',
45
+ y: Type.Number(), // required: ['x', 'y', 'z'],
46
+ z: Type.Number() // properties: {
47
+ }) // x: { type: 'number' },
48
+ // y: { type: 'number' },
49
+ // z: { type: 'number' }
50
+ // }
51
+ // }
40
52
 
41
- type T = Static<typeof T> // type T = string
53
+ type T = Static<typeof T> // type T = {
54
+ // x: number,
55
+ // y: number,
56
+ // z: number
57
+ // }
42
58
  ```
43
59
 
60
+
44
61
  <a name="Overview"></a>
45
62
 
46
63
  ## Overview
@@ -54,7 +71,7 @@ License MIT
54
71
  ## Contents
55
72
  - [Install](#install)
56
73
  - [Overview](#overview)
57
- - [Usage](#usage)
74
+ - [Example](#Example)
58
75
  - [Types](#types)
59
76
  - [Standard Types](#types-standard)
60
77
  - [Extended Types](#types-extended)
@@ -80,7 +97,8 @@ License MIT
80
97
  - [TypeCheck](#typecheck)
81
98
  - [Ajv](#typecheck-ajv)
82
99
  - [TypeCompiler](#typecheck-typecompiler)
83
- - [Formats](#typecheck-formats)
100
+ - [Custom Types](#typecheck-custom-types)
101
+ - [Custom Formats](#typecheck-custom-formats)
84
102
  - [Benchmark](#benchmark)
85
103
  - [Compile](#benchmark-compile)
86
104
  - [Validate](#benchmark-validate)
@@ -89,7 +107,7 @@ License MIT
89
107
 
90
108
  <a name="Example"></a>
91
109
 
92
- ## Usage
110
+ ## Example
93
111
 
94
112
  The following demonstrates TypeBox's general usage.
95
113
 
@@ -1100,11 +1118,39 @@ console.log(C.Code()) // return function check(va
1100
1118
  // }
1101
1119
  ```
1102
1120
 
1103
- <a name='typecheck-formats'></a>
1121
+ <a name='typecheck-custom-types'></a>
1122
+
1123
+ ### Custom Types
1124
+
1125
+ Use the custom module to create user defined types. User defined types require a `[Kind]` symbol property which is used to match the schema against a registered type. Custom types are specific to TypeBox and can only be used with the TypeCompiler and Value modules.
1126
+
1127
+ The custom module is an optional import.
1128
+
1129
+ ```typescript
1130
+ import { Custom } from '@sinclair/typebox/custom'
1131
+ ```
1132
+
1133
+ The following creates a `BigInt` type.
1134
+
1135
+ ```typescript
1136
+ import { Type, Kind } from '@sinclair/typebox'
1137
+
1138
+ Custom.Set('BigInt', (schema, value) => typeof value === 'bigint')
1139
+ // │
1140
+ // └────────────────────────────┐ The [Kind] is used to match registered type
1141
+ // │
1142
+ const T = Type.Unsafe<bigint>({ [Kind]: 'BigInt' }) // const T = { [Kind]: 'BigInt' }
1143
+
1144
+ const A = TypeCompiler.Compile(T).Check(65536) // const A = false
1145
+
1146
+ const B = Value.Check(T, 65536n) // const B = true
1147
+ ```
1148
+
1149
+ <a name='typecheck-custom-formats'></a>
1104
1150
 
1105
- ### Formats
1151
+ ### Custom Formats
1106
1152
 
1107
- Use the format module to create user defined string formats. The format module is used by the Value and TypeCompiler modules only. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html).
1153
+ Use the format module to create user defined string formats. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html). The format module is specific to TypeBox can only be used with the TypeCompiler and Value modules.
1108
1154
 
1109
1155
  The format module is an optional import.
1110
1156
 
@@ -1112,7 +1158,7 @@ The format module is an optional import.
1112
1158
  import { Format } from '@sinclair/typebox/format'
1113
1159
  ```
1114
1160
 
1115
- The following creates a `palindrome` string format.
1161
+ The following creates a palindrome string format.
1116
1162
 
1117
1163
  ```typescript
1118
1164
  Format.Set('palindrome', value => value === value.split('').reverse().join(''))
@@ -1132,7 +1178,7 @@ const B = Value.Check(T, 'kayak') // const B = true
1132
1178
 
1133
1179
  ## Benchmark
1134
1180
 
1135
- This project maintains a set of benchmarks that measure Ajv, Value 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.
1181
+ This project maintains a set of benchmarks that measure Ajv, Value 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.2.
1136
1182
 
1137
1183
  For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
1138
1184
 
@@ -1146,29 +1192,29 @@ This benchmark measures compilation performance for varying types. You can revie
1146
1192
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
1147
1193
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
1148
1194
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
1149
- │ Number │ 2000 │ ' 421 ms' │ ' 11 ms' │ ' 38.27 x' │
1150
- │ String │ 2000 │ ' 332 ms' │ ' 11 ms' │ ' 30.18 x' │
1151
- │ Boolean │ 2000 │ ' 291 ms' │ ' 12 ms' │ ' 24.25 x' │
1152
- │ Null │ 2000 │ ' 266 ms' │ ' 9 ms' │ ' 29.56 x' │
1153
- │ RegEx │ 2000 │ ' 486 ms' │ ' 17 ms' │ ' 28.59 x' │
1154
- │ ObjectA │ 2000 │ ' 2791 ms' │ ' 50 ms' │ ' 55.82 x' │
1155
- │ ObjectB │ 2000 │ ' 2896 ms' │ ' 37 ms' │ ' 78.27 x' │
1156
- │ Tuple │ 2000 │ ' 1244 ms' │ ' 21 ms' │ ' 59.24 x' │
1157
- │ Union │ 2000 │ ' 1258 ms' │ ' 25 ms' │ ' 50.32 x' │
1158
- │ Vector4 │ 2000 │ ' 1513 ms' │ ' 21 ms' │ ' 72.05 x' │
1159
- │ Matrix4 │ 2000 │ ' 850 ms' │ ' 11 ms' │ ' 77.27 x' │
1160
- │ Literal_String │ 2000 │ ' 335 ms' │ ' 7 ms' │ ' 47.86 x' │
1161
- │ Literal_Number │ 2000 │ ' 358 ms' │ ' 7 ms' │ ' 51.14 x' │
1162
- │ Literal_Boolean │ 2000 │ ' 356 ms' │ ' 7 ms' │ ' 50.86 x' │
1163
- │ Array_Number │ 2000 │ ' 689 ms' │ ' 9 ms' │ ' 76.56 x' │
1164
- │ Array_String │ 2000 │ ' 728 ms' │ ' 12 ms' │ ' 60.67 x' │
1165
- │ Array_Boolean │ 2000 │ ' 726 ms' │ ' 7 ms' │ ' 103.71 x' │
1166
- │ Array_ObjectA │ 2000 │ ' 3631 ms' │ ' 35 ms' │ ' 103.74 x' │
1167
- │ Array_ObjectB │ 2000 │ ' 3636 ms' │ ' 40 ms' │ ' 90.90 x' │
1168
- │ Array_Tuple │ 2000 │ ' 2119 ms' │ ' 31 ms' │ ' 68.35 x' │
1169
- │ Array_Union │ 2000 │ ' 1550 ms' │ ' 23 ms' │ ' 67.39 x' │
1170
- │ Array_Vector4 │ 2000 │ ' 2200 ms' │ ' 18 ms' │ ' 122.22 x' │
1171
- │ Array_Matrix4 │ 2000 │ ' 1494 ms' │ ' 14 ms' │ ' 106.71 x' │
1195
+ │ Number │ 2000 │ ' 414 ms' │ ' 14 ms' │ ' 29.57 x' │
1196
+ │ String │ 2000 │ ' 326 ms' │ ' 12 ms' │ ' 27.17 x' │
1197
+ │ Boolean │ 2000 │ ' 301 ms' │ ' 11 ms' │ ' 27.36 x' │
1198
+ │ Null │ 2000 │ ' 253 ms' │ ' 8 ms' │ ' 31.63 x' │
1199
+ │ RegEx │ 2000 │ ' 480 ms' │ ' 18 ms' │ ' 26.67 x' │
1200
+ │ ObjectA │ 2000 │ ' 2704 ms' │ ' 53 ms' │ ' 51.02 x' │
1201
+ │ ObjectB │ 2000 │ ' 2846 ms' │ ' 35 ms' │ ' 81.31 x' │
1202
+ │ Tuple │ 2000 │ ' 1244 ms' │ ' 23 ms' │ ' 54.09 x' │
1203
+ │ Union │ 2000 │ ' 1222 ms' │ ' 26 ms' │ ' 47.00 x' │
1204
+ │ Vector4 │ 2000 │ ' 1522 ms' │ ' 22 ms' │ ' 69.18 x' │
1205
+ │ Matrix4 │ 2000 │ ' 830 ms' │ ' 10 ms' │ ' 83.00 x' │
1206
+ │ Literal_String │ 2000 │ ' 348 ms' │ ' 9 ms' │ ' 38.67 x' │
1207
+ │ Literal_Number │ 2000 │ ' 356 ms' │ ' 9 ms' │ ' 39.56 x' │
1208
+ │ Literal_Boolean │ 2000 │ ' 353 ms' │ ' 6 ms' │ ' 58.83 x' │
1209
+ │ Array_Number │ 2000 │ ' 686 ms' │ ' 11 ms' │ ' 62.36 x' │
1210
+ │ Array_String │ 2000 │ ' 722 ms' │ ' 11 ms' │ ' 65.64 x' │
1211
+ │ Array_Boolean │ 2000 │ ' 724 ms' │ ' 9 ms' │ ' 80.44 x' │
1212
+ │ Array_ObjectA │ 2000 │ ' 3706 ms' │ ' 36 ms' │ ' 102.94 x' │
1213
+ │ Array_ObjectB │ 2000 │ ' 3678 ms' │ ' 38 ms' │ ' 96.79 x' │
1214
+ │ Array_Tuple │ 2000 │ ' 2217 ms' │ ' 23 ms' │ ' 96.39 x' │
1215
+ │ Array_Union │ 2000 │ ' 1654 ms' │ ' 24 ms' │ ' 68.92 x' │
1216
+ │ Array_Vector4 │ 2000 │ ' 2283 ms' │ ' 20 ms' │ ' 114.15 x' │
1217
+ │ Array_Matrix4 │ 2000 │ ' 1567 ms' │ ' 19 ms' │ ' 82.47 x' │
1172
1218
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
1173
1219
  ```
1174
1220
 
@@ -1182,31 +1228,31 @@ This benchmark measures validation performance for varying types. You can review
1182
1228
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
1183
1229
  │ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
1184
1230
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
1185
- │ Number │ 1000000 │ ' 28 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
1186
- │ String │ 1000000 │ ' 25 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
1187
- │ Boolean │ 1000000 │ ' 34 ms' │ ' 22 ms' │ ' 13 ms' │ ' 1.69 x' │
1188
- │ Null │ 1000000 │ ' 37 ms' │ ' 28 ms' │ ' 10 ms' │ ' 2.80 x' │
1189
- │ RegEx │ 1000000 │ ' 162 ms' │ ' 50 ms' │ ' 37 ms' │ ' 1.35 x' │
1190
- │ ObjectA │ 1000000 │ ' 550 ms' │ ' 38 ms' │ ' 22 ms' │ ' 1.73 x' │
1191
- │ ObjectB │ 1000000 │ ' 1033 ms' │ ' 58 ms' │ ' 38 ms' │ ' 1.53 x' │
1192
- │ Tuple │ 1000000 │ ' 126 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' │
1193
- │ Union │ 1000000 │ ' 326 ms' │ ' 25 ms' │ ' 16 ms' │ ' 1.56 x' │
1194
- │ Recursive │ 1000000 │ ' 3089 ms' │ ' 436 ms' │ ' 101 ms' │ ' 4.32 x' │
1195
- │ Vector4 │ 1000000 │ ' 155 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
1196
- │ Matrix4 │ 1000000 │ ' 579 ms' │ ' 41 ms' │ ' 28 ms' │ ' 1.46 x' │
1197
- │ Literal_String │ 1000000 │ ' 50 ms' │ ' 21 ms' │ ' 13 ms' │ ' 1.62 x' │
1198
- │ Literal_Number │ 1000000 │ ' 46 ms' │ ' 22 ms' │ ' 11 ms' │ ' 2.00 x' │
1199
- │ Literal_Boolean │ 1000000 │ ' 48 ms' │ ' 20 ms' │ ' 10 ms' │ ' 2.00 x' │
1200
- │ Array_Number │ 1000000 │ ' 424 ms' │ ' 32 ms' │ ' 19 ms' │ ' 1.68 x' │
1201
- │ Array_String │ 1000000 │ ' 483 ms' │ ' 34 ms' │ ' 28 ms' │ ' 1.21 x' │
1202
- │ Array_Boolean │ 1000000 │ ' 432 ms' │ ' 35 ms' │ ' 26 ms' │ ' 1.35 x' │
1203
- │ Array_ObjectA │ 1000000 │ ' 13895 ms' │ ' 2440 ms' │ ' 1495 ms' │ ' 1.63 x' │
1204
- │ Array_ObjectB │ 1000000 │ ' 16261 ms' │ ' 2633 ms' │ ' 2011 ms' │ ' 1.31 x' │
1205
- │ Array_Tuple │ 1000000 │ ' 1741 ms' │ ' 98 ms' │ ' 66 ms' │ ' 1.48 x' │
1206
- │ Array_Union │ 1000000 │ ' 4825 ms' │ ' 232 ms' │ ' 87 ms' │ ' 2.67 x' │
1207
- │ Array_Recursive │ 1000000 │ ' 54158 ms' │ ' 6966 ms' │ ' 1173 ms' │ ' 5.94 x' │
1208
- │ Array_Vector4 │ 1000000 │ ' 2577 ms' │ ' 99 ms' │ ' 48 ms' │ ' 2.06 x' │
1209
- │ Array_Matrix4 │ 1000000 │ ' 12648 ms' │ ' 397 ms' │ ' 249 ms' │ ' 1.59 x' │
1231
+ │ Number │ 1000000 │ ' 33 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
1232
+ │ String │ 1000000 │ ' 26 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
1233
+ │ Boolean │ 1000000 │ ' 24 ms' │ ' 22 ms' │ ' 11 ms' │ ' 2.00 x' │
1234
+ │ Null │ 1000000 │ ' 28 ms' │ ' 24 ms' │ ' 11 ms' │ ' 2.18 x' │
1235
+ │ RegEx │ 1000000 │ ' 171 ms' │ ' 56 ms' │ ' 41 ms' │ ' 1.37 x' │
1236
+ │ ObjectA │ 1000000 │ ' 614 ms' │ ' 42 ms' │ ' 24 ms' │ ' 1.75 x' │
1237
+ │ ObjectB │ 1000000 │ ' 1109 ms' │ ' 59 ms' │ ' 45 ms' │ ' 1.31 x' │
1238
+ │ Tuple │ 1000000 │ ' 122 ms' │ ' 25 ms' │ ' 13 ms' │ ' 1.92 x' │
1239
+ │ Union │ 1000000 │ ' 316 ms' │ ' 26 ms' │ ' 16 ms' │ ' 1.63 x' │
1240
+ │ Recursive │ 1000000 │ ' 3219 ms' │ ' 453 ms' │ ' 128 ms' │ ' 3.54 x' │
1241
+ │ Vector4 │ 1000000 │ ' 145 ms' │ ' 26 ms' │ ' 13 ms' │ ' 2.00 x' │
1242
+ │ Matrix4 │ 1000000 │ ' 537 ms' │ ' 42 ms' │ ' 28 ms' │ ' 1.50 x' │
1243
+ │ Literal_String │ 1000000 │ ' 51 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1244
+ │ Literal_Number │ 1000000 │ ' 48 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
1245
+ │ Literal_Boolean │ 1000000 │ ' 49 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
1246
+ │ Array_Number │ 1000000 │ ' 438 ms' │ ' 35 ms' │ ' 19 ms' │ ' 1.84 x' │
1247
+ │ Array_String │ 1000000 │ ' 468 ms' │ ' 33 ms' │ ' 23 ms' │ ' 1.43 x' │
1248
+ │ Array_Boolean │ 1000000 │ ' 448 ms' │ ' 36 ms' │ ' 29 ms' │ ' 1.24 x' │
1249
+ │ Array_ObjectA │ 1000000 │ ' 13904 ms' │ ' 2536 ms' │ ' 1530 ms' │ ' 1.66 x' │
1250
+ │ Array_ObjectB │ 1000000 │ ' 16188 ms' │ ' 2724 ms' │ ' 1834 ms' │ ' 1.49 x' │
1251
+ │ Array_Tuple │ 1000000 │ ' 1725 ms' │ ' 98 ms' │ ' 64 ms' │ ' 1.53 x' │
1252
+ │ Array_Union │ 1000000 │ ' 4762 ms' │ ' 237 ms' │ ' 87 ms' │ ' 2.72 x' │
1253
+ │ Array_Recursive │ 1000000 │ ' 54754 ms' │ ' 7707 ms' │ ' 1152 ms' │ ' 6.69 x' │
1254
+ │ Array_Vector4 │ 1000000 │ ' 2485 ms' │ ' 100 ms' │ ' 48 ms' │ ' 2.08 x' │
1255
+ │ Array_Matrix4 │ 1000000 │ ' 13116 ms' │ ' 387 ms' │ ' 232 ms' │ ' 1.67 x' │
1210
1256
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
1211
1257
  ```
1212
1258
 
@@ -1220,11 +1266,12 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1220
1266
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1221
1267
  │ (index) │ Compiled │ Minified │ Compression │
1222
1268
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1223
- │ typebox/compiler │ ' 54 kb' │ ' 27 kb' │ '1.97 x' │
1224
- │ typebox/conditional │ ' 44 kb' │ ' 17 kb' │ '2.45 x' │
1269
+ │ typebox/compiler │ ' 56 kb' │ ' 28 kb' │ '2.01 x' │
1270
+ │ typebox/conditional │ ' 45 kb' │ ' 18 kb' │ '2.44 x' │
1271
+ │ typebox/custom │ ' 0 kb' │ ' 0 kb' │ '2.61 x' │
1225
1272
  │ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.66 x' │
1226
- │ typebox/guard │ ' 22 kb' │ ' 11 kb' │ '2.05 x' │
1227
- │ typebox/value │ ' 78 kb' │ ' 36 kb' │ '2.13 x' │
1273
+ │ typebox/guard │ ' 23 kb' │ ' 11 kb' │ '2.07 x' │
1274
+ │ typebox/value │ ' 80 kb' │ ' 37 kb' │ '2.16 x' │
1228
1275
  │ typebox │ ' 12 kb' │ ' 6 kb' │ '1.89 x' │
1229
1276
  └──────────────────────┴────────────┴────────────┴─────────────┘
1230
1277
  ```
package/typebox.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  export declare const Kind: unique symbol;
2
2
  export declare const Hint: unique symbol;
3
3
  export declare const Modifier: unique symbol;
4
- export declare type TModifier = TReadonlyOptional<TSchema> | TOptional<TSchema> | TReadonly<TSchema>;
5
- export declare type TReadonly<T extends TSchema> = T & {
4
+ export type TModifier = TReadonlyOptional<TSchema> | TOptional<TSchema> | TReadonly<TSchema>;
5
+ export type TReadonly<T extends TSchema> = T & {
6
6
  [Modifier]: 'Readonly';
7
7
  };
8
- export declare type TOptional<T extends TSchema> = T & {
8
+ export type TOptional<T extends TSchema> = T & {
9
9
  [Modifier]: 'Optional';
10
10
  };
11
- export declare type TReadonlyOptional<T extends TSchema> = T & {
11
+ export type TReadonlyOptional<T extends TSchema> = T & {
12
12
  [Modifier]: 'ReadonlyOptional';
13
13
  };
14
14
  export interface SchemaOptions {
@@ -32,7 +32,7 @@ export interface TSchema extends SchemaOptions {
32
32
  params: unknown[];
33
33
  static: unknown;
34
34
  }
35
- export declare type TAnySchema = TSchema | TAny | TArray | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TLiteral | TNull | TNumber | TObject | TPromise | TRecord | TSelf | TRef | TString | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
35
+ export type TAnySchema = TSchema | TAny | TArray | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TLiteral | TNull | TNumber | TObject | TPromise | TRecord | TSelf | TRef | TString | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
36
36
  export interface NumericOptions extends SchemaOptions {
37
37
  exclusiveMaximum?: number;
38
38
  exclusiveMinimum?: number;
@@ -40,7 +40,7 @@ export interface NumericOptions extends SchemaOptions {
40
40
  minimum?: number;
41
41
  multipleOf?: number;
42
42
  }
43
- export declare type TNumeric = TInteger | TNumber;
43
+ export type TNumeric = TInteger | TNumber;
44
44
  export interface TAny extends TSchema {
45
45
  [Kind]: 'Any';
46
46
  static: any;
@@ -61,9 +61,9 @@ export interface TBoolean extends TSchema {
61
61
  static: boolean;
62
62
  type: 'boolean';
63
63
  }
64
- export declare type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
65
- export declare type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
66
- export declare type StaticContructorParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
64
+ export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
65
+ export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
66
+ export type StaticContructorParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
67
67
  [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
68
68
  }];
69
69
  export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
@@ -95,9 +95,9 @@ export interface TEnum<T extends Record<string, string | number> = Record<string
95
95
  static: T[keyof T];
96
96
  anyOf: TLiteral<string | number>[];
97
97
  }
98
- export declare type TParameters<T extends TFunction> = TTuple<T['parameters']>;
99
- export declare type TReturnType<T extends TFunction> = T['returns'];
100
- export declare type StaticFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
98
+ export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
99
+ export type TReturnType<T extends TFunction> = T['returns'];
100
+ export type StaticFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
101
101
  [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
102
102
  }];
103
103
  export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
@@ -113,29 +113,29 @@ export interface TInteger extends TSchema, NumericOptions {
113
113
  static: number;
114
114
  type: 'integer';
115
115
  }
116
- export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
117
- export declare type IntersectEvaluate<T extends readonly TSchema[], P extends unknown[]> = {
116
+ export type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
117
+ export type IntersectEvaluate<T extends readonly TSchema[], P extends unknown[]> = {
118
118
  [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
119
119
  };
120
- export declare type IntersectProperties<T extends readonly TObject[]> = {
120
+ export type IntersectProperties<T extends readonly TObject[]> = {
121
121
  [K in keyof T]: T[K] extends TObject<infer P> ? P : {};
122
122
  };
123
123
  export interface TIntersect<T extends TObject[] = TObject[]> extends TObject {
124
124
  static: IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>;
125
125
  properties: IntersectReduce<unknown, IntersectProperties<T>>;
126
126
  }
127
- export declare type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
128
- export declare type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
129
- export declare type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L];
130
- export declare type UnionStringLiteralToTuple<T> = T extends TUnion<infer L> ? {
127
+ export type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
128
+ export type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
129
+ export type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L];
130
+ export type UnionStringLiteralToTuple<T> = T extends TUnion<infer L> ? {
131
131
  [I in keyof L]: L[I] extends TLiteral<infer C> ? C : never;
132
132
  } : never;
133
- export declare type UnionLiteralsFromObject<T extends TObject> = {
133
+ export type UnionLiteralsFromObject<T extends TObject> = {
134
134
  [K in ObjectPropertyKeys<T>]: TLiteral<K>;
135
135
  } extends infer R ? UnionToTuple<R[keyof R]> : never;
136
136
  export interface TKeyOf<T extends TObject> extends TUnion<UnionLiteralsFromObject<T>> {
137
137
  }
138
- export declare type TLiteralValue = string | number | boolean;
138
+ export type TLiteralValue = string | number | boolean;
139
139
  export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
140
140
  [Kind]: 'Literal';
141
141
  static: T;
@@ -162,17 +162,17 @@ export interface TNumber extends TSchema, NumericOptions {
162
162
  static: number;
163
163
  type: 'number';
164
164
  }
165
- export declare type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
165
+ export type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
166
166
  [K in keyof T]: T[K] extends TReadonlyOptional<TSchema> ? K : never;
167
167
  }[keyof T];
168
- export declare type ReadonlyPropertyKeys<T extends TProperties> = {
168
+ export type ReadonlyPropertyKeys<T extends TProperties> = {
169
169
  [K in keyof T]: T[K] extends TReadonly<TSchema> ? K : never;
170
170
  }[keyof T];
171
- export declare type OptionalPropertyKeys<T extends TProperties> = {
171
+ export type OptionalPropertyKeys<T extends TProperties> = {
172
172
  [K in keyof T]: T[K] extends TOptional<TSchema> ? K : never;
173
173
  }[keyof T];
174
- export declare type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
175
- export declare type PropertiesReduce<T extends TProperties, P extends unknown[]> = {
174
+ export type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
175
+ export type PropertiesReduce<T extends TProperties, P extends unknown[]> = {
176
176
  readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K], P>;
177
177
  } & {
178
178
  readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K], P>;
@@ -183,15 +183,15 @@ export declare type PropertiesReduce<T extends TProperties, P extends unknown[]>
183
183
  } extends infer R ? {
184
184
  [K in keyof R]: R[K];
185
185
  } : never;
186
- export declare type TRecordProperties<K extends TUnion<TLiteral[]>, T extends TSchema> = Static<K> extends string ? {
186
+ export type TRecordProperties<K extends TUnion<TLiteral[]>, T extends TSchema> = Static<K> extends string ? {
187
187
  [X in Static<K>]: T;
188
188
  } : never;
189
189
  export interface TProperties {
190
190
  [key: string]: TSchema;
191
191
  }
192
- export declare type ObjectProperties<T> = T extends TObject<infer U> ? U : never;
193
- export declare type ObjectPropertyKeys<T> = T extends TObject<infer U> ? keyof U : never;
194
- export declare type TAdditionalProperties = undefined | TSchema | boolean;
192
+ export type ObjectProperties<T> = T extends TObject<infer U> ? U : never;
193
+ export type ObjectPropertyKeys<T> = T extends TObject<infer U> ? keyof U : never;
194
+ export type TAdditionalProperties = undefined | TSchema | boolean;
195
195
  export interface ObjectOptions extends SchemaOptions {
196
196
  additionalProperties?: TAdditionalProperties;
197
197
  minProperties?: number;
@@ -215,7 +215,7 @@ export interface TPartial<T extends TObject> extends TObject {
215
215
  [K in keyof T['properties']]: T['properties'][K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T['properties'][K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T['properties'][K] extends TOptional<infer U> ? TOptional<U> : TOptional<T['properties'][K]>;
216
216
  };
217
217
  }
218
- export declare type TPick<T extends TObject, Properties extends ObjectPropertyKeys<T>[]> = TObject<{
218
+ export type TPick<T extends TObject, Properties extends ObjectPropertyKeys<T>[]> = TObject<{
219
219
  [K in Properties[number]]: T['properties'][K];
220
220
  }>;
221
221
  export interface TPromise<T extends TSchema = TSchema> extends TSchema {
@@ -225,7 +225,7 @@ export interface TPromise<T extends TSchema = TSchema> extends TSchema {
225
225
  instanceOf: 'Promise';
226
226
  item: TSchema;
227
227
  }
228
- export declare type TRecordKey = TString | TNumeric | TUnion<TLiteral<any>[]>;
228
+ export type TRecordKey = TString | TNumeric | TUnion<TLiteral<any>[]>;
229
229
  export interface TRecord<K extends TRecordKey = TRecordKey, T extends TSchema = TSchema> extends TSchema {
230
230
  [Kind]: 'Record';
231
231
  static: Record<Static<K>, Static<T, this['params']>>;
@@ -240,7 +240,7 @@ export interface TSelf extends TSchema {
240
240
  static: this['params'][0];
241
241
  $ref: string;
242
242
  }
243
- export declare type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>]>;
243
+ export type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>]>;
244
244
  export interface TRecursive<T extends TSchema> extends TSchema {
245
245
  static: TRecursiveReduce<T>;
246
246
  }
@@ -255,7 +255,7 @@ export interface TRequired<T extends TObject | TRef<TObject>> extends TObject {
255
255
  [K in keyof T['properties']]: T['properties'][K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T['properties'][K] extends TReadonly<infer U> ? TReadonly<U> : T['properties'][K] extends TOptional<infer U> ? U : T['properties'][K];
256
256
  };
257
257
  }
258
- export declare 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';
258
+ 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';
259
259
  export interface StringOptions<Format extends string> extends SchemaOptions {
260
260
  minLength?: number;
261
261
  maxLength?: number;
@@ -269,7 +269,7 @@ export interface TString<Format extends string = string> extends TSchema, String
269
269
  static: string;
270
270
  type: 'string';
271
271
  }
272
- export declare type TupleToArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? R : never;
272
+ export type TupleToArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? R : never;
273
273
  export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
274
274
  [Kind]: 'Tuple';
275
275
  static: {
@@ -322,7 +322,7 @@ export interface TVoid extends TSchema {
322
322
  typeOf: 'Void';
323
323
  }
324
324
  /** Creates a static type from a TypeBox type */
325
- export declare type Static<T extends TSchema, P extends unknown[] = []> = (T & {
325
+ export type Static<T extends TSchema, P extends unknown[] = []> = (T & {
326
326
  params: P;
327
327
  })['static'];
328
328
  export declare class TypeBuilder {
package/value/cast.js CHANGED
@@ -32,6 +32,7 @@ const Types = require("../typebox");
32
32
  const create_1 = require("./create");
33
33
  const check_1 = require("./check");
34
34
  const clone_1 = require("./clone");
35
+ const index_1 = require("../custom/index");
35
36
  // ----------------------------------------------------------------------------------------------
36
37
  // Errors
37
38
  // ----------------------------------------------------------------------------------------------
@@ -306,6 +307,9 @@ var ValueCast;
306
307
  function Void(schema, references, value) {
307
308
  return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
308
309
  }
310
+ function UserDefined(schema, references, value) {
311
+ return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
312
+ }
309
313
  function Visit(schema, references, value) {
310
314
  const anyReferences = schema.$id === undefined ? references : [schema, ...references];
311
315
  const anySchema = schema;
@@ -359,7 +363,9 @@ var ValueCast;
359
363
  case 'Void':
360
364
  return Void(anySchema, anyReferences, value);
361
365
  default:
362
- throw new ValueCastUnknownTypeError(anySchema);
366
+ if (!index_1.Custom.Has(anySchema[Types.Kind]))
367
+ throw new ValueCastUnknownTypeError(anySchema);
368
+ return UserDefined(anySchema, anyReferences, value);
363
369
  }
364
370
  }
365
371
  ValueCast.Visit = Visit;
package/value/check.js CHANGED
@@ -29,10 +29,11 @@ THE SOFTWARE.
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
31
31
  const Types = require("../typebox");
32
- const format_1 = require("../format");
32
+ const index_1 = require("../format/index");
33
+ const index_2 = require("../custom/index");
33
34
  class ValueCheckUnknownTypeError extends Error {
34
35
  constructor(schema) {
35
- super('ValueCheck: Unknown type');
36
+ super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
36
37
  this.schema = schema;
37
38
  }
38
39
  }
@@ -240,9 +241,9 @@ var ValueCheck;
240
241
  return false;
241
242
  }
242
243
  if (schema.format !== undefined) {
243
- if (!format_1.Format.Has(schema.format))
244
+ if (!index_1.Format.Has(schema.format))
244
245
  return false;
245
- const func = format_1.Format.Get(schema.format);
246
+ const func = index_1.Format.Get(schema.format);
246
247
  return func(value);
247
248
  }
248
249
  return true;
@@ -290,6 +291,12 @@ var ValueCheck;
290
291
  function Void(schema, references, value) {
291
292
  return value === null;
292
293
  }
294
+ function UserDefined(schema, references, value) {
295
+ if (!index_2.Custom.Has(schema[Types.Kind]))
296
+ return false;
297
+ const func = index_2.Custom.Get(schema[Types.Kind]);
298
+ return func(schema, value);
299
+ }
293
300
  function Visit(schema, references, value) {
294
301
  const anyReferences = schema.$id === undefined ? references : [schema, ...references];
295
302
  const anySchema = schema;
@@ -341,7 +348,9 @@ var ValueCheck;
341
348
  case 'Void':
342
349
  return Void(anySchema, anyReferences, value);
343
350
  default:
344
- throw new ValueCheckUnknownTypeError(anySchema);
351
+ if (!index_2.Custom.Has(anySchema[Types.Kind]))
352
+ throw new ValueCheckUnknownTypeError(anySchema);
353
+ return UserDefined(anySchema, anyReferences, value);
345
354
  }
346
355
  }
347
356
  // -------------------------------------------------------------------------
package/value/create.js CHANGED
@@ -29,6 +29,7 @@ THE SOFTWARE.
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.ValueCreate = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
31
31
  const Types = require("../typebox");
32
+ const index_1 = require("../custom/index");
32
33
  class ValueCreateUnknownTypeError extends Error {
33
34
  constructor(schema) {
34
35
  super('ValueCreate: Unknown type');
@@ -292,6 +293,14 @@ var ValueCreate;
292
293
  function Void(schema, references) {
293
294
  return null;
294
295
  }
296
+ function UserDefined(schema, references) {
297
+ if (schema.default !== undefined) {
298
+ return schema.default;
299
+ }
300
+ else {
301
+ throw new Error('ValueCreate.UserDefined: User defined types must specify a default value');
302
+ }
303
+ }
295
304
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
296
305
  function Visit(schema, references) {
297
306
  const anyReferences = schema.$id === undefined ? references : [schema, ...references];
@@ -346,7 +355,9 @@ var ValueCreate;
346
355
  case 'Void':
347
356
  return Void(anySchema, anyReferences);
348
357
  default:
349
- throw new ValueCreateUnknownTypeError(anySchema);
358
+ if (!index_1.Custom.Has(anySchema[Types.Kind]))
359
+ throw new ValueCreateUnknownTypeError(anySchema);
360
+ return UserDefined(anySchema, anyReferences);
350
361
  }
351
362
  }
352
363
  ValueCreate.Visit = Visit;
package/value/delta.d.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { Static } from '../typebox';
2
- export declare type Insert = Static<typeof Insert>;
2
+ export type Insert = Static<typeof Insert>;
3
3
  export declare const Insert: import("../typebox").TObject<{
4
4
  type: import("../typebox").TLiteral<"insert">;
5
5
  path: import("../typebox").TString<string>;
6
6
  value: import("../typebox").TUnknown;
7
7
  }>;
8
- export declare type Update = Static<typeof Update>;
8
+ export type Update = Static<typeof Update>;
9
9
  export declare const Update: import("../typebox").TObject<{
10
10
  type: import("../typebox").TLiteral<"update">;
11
11
  path: import("../typebox").TString<string>;
12
12
  value: import("../typebox").TUnknown;
13
13
  }>;
14
- export declare type Delete = Static<typeof Delete>;
14
+ export type Delete = Static<typeof Delete>;
15
15
  export declare const Delete: import("../typebox").TObject<{
16
16
  type: import("../typebox").TLiteral<"delete">;
17
17
  path: import("../typebox").TString<string>;
18
18
  }>;
19
- export declare type Edit = Static<typeof Edit>;
19
+ export type Edit = Static<typeof Edit>;
20
20
  export declare const Edit: import("../typebox").TUnion<[import("../typebox").TObject<{
21
21
  type: import("../typebox").TLiteral<"insert">;
22
22
  path: import("../typebox").TString<string>;
package/value/is.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export declare type ValueType = null | undefined | Function | symbol | bigint | number | boolean | string;
2
- export declare type ObjectType = Record<string | number | symbol, unknown>;
3
- export declare type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
4
- export declare type ArrayType = unknown[];
1
+ export type ValueType = null | undefined | Function | symbol | bigint | number | boolean | string;
2
+ export type ObjectType = Record<string | number | symbol, unknown>;
3
+ export type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
4
+ export type ArrayType = unknown[];
5
5
  export declare namespace Is {
6
6
  function Object(value: unknown): value is ObjectType;
7
7
  function Date(value: unknown): value is Date;