@sinclair/typebox 0.25.9 → 0.25.11
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 +1 -1
- package/compiler/compiler.js +36 -27
- package/conditional/conditional.d.ts +1 -1
- package/conditional/structural.js +2 -2
- package/custom/custom.d.ts +8 -8
- package/custom/custom.js +5 -5
- package/errors/errors.js +14 -4
- package/format/format.d.ts +6 -6
- package/format/format.js +5 -5
- package/guard/guard.d.ts +2 -2
- package/guard/guard.js +4 -4
- package/hash/hash.d.ts +8 -0
- package/hash/hash.js +183 -0
- package/hash/index.d.ts +1 -0
- package/hash/index.js +44 -0
- package/package.json +5 -4
- package/readme.md +82 -64
- package/typebox.d.ts +36 -36
- package/value/cast.js +2 -2
- package/value/check.js +15 -5
- package/value/create.js +3 -3
- package/value/delta.d.ts +4 -4
- package/value/index.d.ts +1 -0
- package/value/index.js +6 -1
- package/value/is.d.ts +4 -4
- package/value/value.d.ts +4 -3
- package/value/value.js +12 -11
package/readme.md
CHANGED
|
@@ -90,6 +90,7 @@ License MIT
|
|
|
90
90
|
- [Check](#values-check)
|
|
91
91
|
- [Cast](#values-cast)
|
|
92
92
|
- [Equal](#values-equal)
|
|
93
|
+
- [Hash](#values-hash)
|
|
93
94
|
- [Diff](#values-diff)
|
|
94
95
|
- [Patch](#values-patch)
|
|
95
96
|
- [Errors](#values-errors)
|
|
@@ -97,8 +98,8 @@ License MIT
|
|
|
97
98
|
- [TypeCheck](#typecheck)
|
|
98
99
|
- [Ajv](#typecheck-ajv)
|
|
99
100
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
100
|
-
- [Custom](#typecheck-custom)
|
|
101
|
-
- [Formats](#typecheck-formats)
|
|
101
|
+
- [Custom Types](#typecheck-custom-types)
|
|
102
|
+
- [Custom Formats](#typecheck-custom-formats)
|
|
102
103
|
- [Benchmark](#benchmark)
|
|
103
104
|
- [Compile](#benchmark-compile)
|
|
104
105
|
- [Validate](#benchmark-validate)
|
|
@@ -843,6 +844,18 @@ const R = Value.Equal( // const R = true
|
|
|
843
844
|
)
|
|
844
845
|
```
|
|
845
846
|
|
|
847
|
+
<a name='values-hash'></a>
|
|
848
|
+
|
|
849
|
+
### Hash
|
|
850
|
+
|
|
851
|
+
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non cryptographic hash of a value.
|
|
852
|
+
|
|
853
|
+
```typescript
|
|
854
|
+
const A = Value.Hash({ x: 1, y: 2, z: 3 }) // const A = 2910466848807138541n
|
|
855
|
+
|
|
856
|
+
const B = Value.Hash({ x: 1, y: 4, z: 3 }) // const B = 1418369778807423581n
|
|
857
|
+
```
|
|
858
|
+
|
|
846
859
|
<a name='values-diff'></a>
|
|
847
860
|
|
|
848
861
|
### Diff
|
|
@@ -1118,11 +1131,11 @@ console.log(C.Code()) // return function check(va
|
|
|
1118
1131
|
// }
|
|
1119
1132
|
```
|
|
1120
1133
|
|
|
1121
|
-
<a name='typecheck-custom'></a>
|
|
1134
|
+
<a name='typecheck-custom-types'></a>
|
|
1122
1135
|
|
|
1123
|
-
### Custom
|
|
1136
|
+
### Custom Types
|
|
1124
1137
|
|
|
1125
|
-
Use custom module to create
|
|
1138
|
+
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
1139
|
|
|
1127
1140
|
The custom module is an optional import.
|
|
1128
1141
|
|
|
@@ -1130,23 +1143,27 @@ The custom module is an optional import.
|
|
|
1130
1143
|
import { Custom } from '@sinclair/typebox/custom'
|
|
1131
1144
|
```
|
|
1132
1145
|
|
|
1133
|
-
The following
|
|
1146
|
+
The following creates a `BigInt` type.
|
|
1134
1147
|
|
|
1135
1148
|
```typescript
|
|
1136
1149
|
import { Type, Kind } from '@sinclair/typebox'
|
|
1137
1150
|
|
|
1138
|
-
Custom.Set('BigInt', value => typeof value === 'bigint')
|
|
1139
|
-
|
|
1151
|
+
Custom.Set('BigInt', (schema, value) => typeof value === 'bigint')
|
|
1152
|
+
// │
|
|
1153
|
+
// └────────────────────────────┐ The [Kind] is used to match registered type
|
|
1154
|
+
// │
|
|
1140
1155
|
const T = Type.Unsafe<bigint>({ [Kind]: 'BigInt' }) // const T = { [Kind]: 'BigInt' }
|
|
1141
1156
|
|
|
1142
|
-
const
|
|
1157
|
+
const A = TypeCompiler.Compile(T).Check(65536) // const A = false
|
|
1158
|
+
|
|
1159
|
+
const B = Value.Check(T, 65536n) // const B = true
|
|
1143
1160
|
```
|
|
1144
1161
|
|
|
1145
|
-
<a name='typecheck-formats'></a>
|
|
1162
|
+
<a name='typecheck-custom-formats'></a>
|
|
1146
1163
|
|
|
1147
|
-
### Formats
|
|
1164
|
+
### Custom Formats
|
|
1148
1165
|
|
|
1149
|
-
Use the format module to create user defined string formats.
|
|
1166
|
+
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.
|
|
1150
1167
|
|
|
1151
1168
|
The format module is an optional import.
|
|
1152
1169
|
|
|
@@ -1154,7 +1171,7 @@ The format module is an optional import.
|
|
|
1154
1171
|
import { Format } from '@sinclair/typebox/format'
|
|
1155
1172
|
```
|
|
1156
1173
|
|
|
1157
|
-
The following creates a
|
|
1174
|
+
The following creates a palindrome string format.
|
|
1158
1175
|
|
|
1159
1176
|
```typescript
|
|
1160
1177
|
Format.Set('palindrome', value => value === value.split('').reverse().join(''))
|
|
@@ -1174,7 +1191,7 @@ const B = Value.Check(T, 'kayak') // const B = true
|
|
|
1174
1191
|
|
|
1175
1192
|
## Benchmark
|
|
1176
1193
|
|
|
1177
|
-
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.
|
|
1194
|
+
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.
|
|
1178
1195
|
|
|
1179
1196
|
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
1180
1197
|
|
|
@@ -1188,29 +1205,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
1188
1205
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1189
1206
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1190
1207
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1191
|
-
│ Number │ 2000 │ '
|
|
1192
|
-
│ String │ 2000 │ '
|
|
1193
|
-
│ Boolean │ 2000 │ '
|
|
1194
|
-
│ Null │ 2000 │ ' 255 ms' │ '
|
|
1195
|
-
│ RegEx │ 2000 │ '
|
|
1196
|
-
│ ObjectA │ 2000 │ '
|
|
1197
|
-
│ ObjectB │ 2000 │ '
|
|
1198
|
-
│ Tuple │ 2000 │ '
|
|
1199
|
-
│ Union │ 2000 │ '
|
|
1200
|
-
│ Vector4 │ 2000 │ '
|
|
1201
|
-
│ Matrix4 │ 2000 │ '
|
|
1202
|
-
│ Literal_String │ 2000 │ '
|
|
1203
|
-
│ Literal_Number │ 2000 │ '
|
|
1204
|
-
│ Literal_Boolean │ 2000 │ '
|
|
1205
|
-
│ Array_Number │ 2000 │ '
|
|
1206
|
-
│ Array_String │ 2000 │ '
|
|
1207
|
-
│ Array_Boolean │ 2000 │ '
|
|
1208
|
-
│ Array_ObjectA │ 2000 │ '
|
|
1209
|
-
│ Array_ObjectB │ 2000 │ '
|
|
1210
|
-
│ Array_Tuple │ 2000 │ '
|
|
1211
|
-
│ Array_Union │ 2000 │ '
|
|
1212
|
-
│ Array_Vector4 │ 2000 │ '
|
|
1213
|
-
│ Array_Matrix4 │ 2000 │ '
|
|
1208
|
+
│ Number │ 2000 │ ' 415 ms' │ ' 14 ms' │ ' 29.64 x' │
|
|
1209
|
+
│ String │ 2000 │ ' 335 ms' │ ' 12 ms' │ ' 27.92 x' │
|
|
1210
|
+
│ Boolean │ 2000 │ ' 295 ms' │ ' 13 ms' │ ' 22.69 x' │
|
|
1211
|
+
│ Null │ 2000 │ ' 255 ms' │ ' 8 ms' │ ' 31.88 x' │
|
|
1212
|
+
│ RegEx │ 2000 │ ' 482 ms' │ ' 17 ms' │ ' 28.35 x' │
|
|
1213
|
+
│ ObjectA │ 2000 │ ' 2700 ms' │ ' 55 ms' │ ' 49.09 x' │
|
|
1214
|
+
│ ObjectB │ 2000 │ ' 2871 ms' │ ' 36 ms' │ ' 79.75 x' │
|
|
1215
|
+
│ Tuple │ 2000 │ ' 1229 ms' │ ' 22 ms' │ ' 55.86 x' │
|
|
1216
|
+
│ Union │ 2000 │ ' 1216 ms' │ ' 26 ms' │ ' 46.77 x' │
|
|
1217
|
+
│ Vector4 │ 2000 │ ' 1575 ms' │ ' 25 ms' │ ' 63.00 x' │
|
|
1218
|
+
│ Matrix4 │ 2000 │ ' 908 ms' │ ' 13 ms' │ ' 69.85 x' │
|
|
1219
|
+
│ Literal_String │ 2000 │ ' 353 ms' │ ' 9 ms' │ ' 39.22 x' │
|
|
1220
|
+
│ Literal_Number │ 2000 │ ' 371 ms' │ ' 7 ms' │ ' 53.00 x' │
|
|
1221
|
+
│ Literal_Boolean │ 2000 │ ' 361 ms' │ ' 8 ms' │ ' 45.13 x' │
|
|
1222
|
+
│ Array_Number │ 2000 │ ' 694 ms' │ ' 7 ms' │ ' 99.14 x' │
|
|
1223
|
+
│ Array_String │ 2000 │ ' 778 ms' │ ' 8 ms' │ ' 97.25 x' │
|
|
1224
|
+
│ Array_Boolean │ 2000 │ ' 735 ms' │ ' 7 ms' │ ' 105.00 x' │
|
|
1225
|
+
│ Array_ObjectA │ 2000 │ ' 3732 ms' │ ' 44 ms' │ ' 84.82 x' │
|
|
1226
|
+
│ Array_ObjectB │ 2000 │ ' 3624 ms' │ ' 40 ms' │ ' 90.60 x' │
|
|
1227
|
+
│ Array_Tuple │ 2000 │ ' 2101 ms' │ ' 15 ms' │ ' 140.07 x' │
|
|
1228
|
+
│ Array_Union │ 2000 │ ' 1480 ms' │ ' 25 ms' │ ' 59.20 x' │
|
|
1229
|
+
│ Array_Vector4 │ 2000 │ ' 2175 ms' │ ' 18 ms' │ ' 120.83 x' │
|
|
1230
|
+
│ Array_Matrix4 │ 2000 │ ' 1516 ms' │ ' 13 ms' │ ' 116.62 x' │
|
|
1214
1231
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1215
1232
|
```
|
|
1216
1233
|
|
|
@@ -1224,31 +1241,31 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
1224
1241
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1225
1242
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1226
1243
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1227
|
-
│ Number │ 1000000 │ '
|
|
1228
|
-
│ String │ 1000000 │ ' 22 ms' │ '
|
|
1229
|
-
│ Boolean │ 1000000 │ '
|
|
1230
|
-
│ Null │ 1000000 │ '
|
|
1231
|
-
│ RegEx │ 1000000 │ '
|
|
1232
|
-
│ ObjectA │ 1000000 │ '
|
|
1233
|
-
│ ObjectB │ 1000000 │ '
|
|
1234
|
-
│ Tuple │ 1000000 │ '
|
|
1235
|
-
│ Union │ 1000000 │ '
|
|
1236
|
-
│ Recursive │ 1000000 │ '
|
|
1237
|
-
│ Vector4 │ 1000000 │ '
|
|
1238
|
-
│ Matrix4 │ 1000000 │ '
|
|
1239
|
-
│ Literal_String │ 1000000 │ '
|
|
1240
|
-
│ Literal_Number │ 1000000 │ '
|
|
1241
|
-
│ Literal_Boolean │ 1000000 │ '
|
|
1242
|
-
│ Array_Number │ 1000000 │ '
|
|
1243
|
-
│ Array_String │ 1000000 │ '
|
|
1244
|
-
│ Array_Boolean │ 1000000 │ '
|
|
1245
|
-
│ Array_ObjectA │ 1000000 │ '
|
|
1246
|
-
│ Array_ObjectB │ 1000000 │ '
|
|
1247
|
-
│ Array_Tuple │ 1000000 │ '
|
|
1248
|
-
│ Array_Union │ 1000000 │ '
|
|
1249
|
-
│ Array_Recursive │ 1000000 │ '
|
|
1250
|
-
│ Array_Vector4 │ 1000000 │ '
|
|
1251
|
-
│ Array_Matrix4 │ 1000000 │ '
|
|
1244
|
+
│ Number │ 1000000 │ ' 28 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
|
|
1245
|
+
│ String │ 1000000 │ ' 22 ms' │ ' 21 ms' │ ' 13 ms' │ ' 1.62 x' │
|
|
1246
|
+
│ Boolean │ 1000000 │ ' 22 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
1247
|
+
│ Null │ 1000000 │ ' 25 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
|
|
1248
|
+
│ RegEx │ 1000000 │ ' 167 ms' │ ' 48 ms' │ ' 38 ms' │ ' 1.26 x' │
|
|
1249
|
+
│ ObjectA │ 1000000 │ ' 591 ms' │ ' 36 ms' │ ' 22 ms' │ ' 1.64 x' │
|
|
1250
|
+
│ ObjectB │ 1000000 │ ' 1168 ms' │ ' 61 ms' │ ' 37 ms' │ ' 1.65 x' │
|
|
1251
|
+
│ Tuple │ 1000000 │ ' 127 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' │
|
|
1252
|
+
│ Union │ 1000000 │ ' 340 ms' │ ' 26 ms' │ ' 16 ms' │ ' 1.63 x' │
|
|
1253
|
+
│ Recursive │ 1000000 │ ' 3174 ms' │ ' 432 ms' │ ' 100 ms' │ ' 4.32 x' │
|
|
1254
|
+
│ Vector4 │ 1000000 │ ' 142 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
|
|
1255
|
+
│ Matrix4 │ 1000000 │ ' 577 ms' │ ' 40 ms' │ ' 28 ms' │ ' 1.43 x' │
|
|
1256
|
+
│ Literal_String │ 1000000 │ ' 47 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
|
|
1257
|
+
│ Literal_Number │ 1000000 │ ' 47 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
|
|
1258
|
+
│ Literal_Boolean │ 1000000 │ ' 45 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1259
|
+
│ Array_Number │ 1000000 │ ' 432 ms' │ ' 33 ms' │ ' 19 ms' │ ' 1.74 x' │
|
|
1260
|
+
│ Array_String │ 1000000 │ ' 462 ms' │ ' 32 ms' │ ' 22 ms' │ ' 1.45 x' │
|
|
1261
|
+
│ Array_Boolean │ 1000000 │ ' 455 ms' │ ' 32 ms' │ ' 26 ms' │ ' 1.23 x' │
|
|
1262
|
+
│ Array_ObjectA │ 1000000 │ ' 14595 ms' │ ' 2306 ms' │ ' 1496 ms' │ ' 1.54 x' │
|
|
1263
|
+
│ Array_ObjectB │ 1000000 │ ' 17310 ms' │ ' 2625 ms' │ ' 2105 ms' │ ' 1.25 x' │
|
|
1264
|
+
│ Array_Tuple │ 1000000 │ ' 1794 ms' │ ' 97 ms' │ ' 66 ms' │ ' 1.47 x' │
|
|
1265
|
+
│ Array_Union │ 1000000 │ ' 5085 ms' │ ' 246 ms' │ ' 91 ms' │ ' 2.70 x' │
|
|
1266
|
+
│ Array_Recursive │ 1000000 │ ' 59747 ms' │ ' 7866 ms' │ ' 1136 ms' │ ' 6.92 x' │
|
|
1267
|
+
│ Array_Vector4 │ 1000000 │ ' 2375 ms' │ ' 99 ms' │ ' 48 ms' │ ' 2.06 x' │
|
|
1268
|
+
│ Array_Matrix4 │ 1000000 │ ' 13115 ms' │ ' 385 ms' │ ' 246 ms' │ ' 1.57 x' │
|
|
1252
1269
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1253
1270
|
```
|
|
1254
1271
|
|
|
@@ -1262,12 +1279,13 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1262
1279
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1263
1280
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1264
1281
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1265
|
-
│ typebox/compiler │ '
|
|
1282
|
+
│ typebox/compiler │ ' 61 kb' │ ' 30 kb' │ '2.03 x' │
|
|
1266
1283
|
│ typebox/conditional │ ' 45 kb' │ ' 18 kb' │ '2.44 x' │
|
|
1267
1284
|
│ typebox/custom │ ' 0 kb' │ ' 0 kb' │ '2.61 x' │
|
|
1268
1285
|
│ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.66 x' │
|
|
1269
1286
|
│ typebox/guard │ ' 23 kb' │ ' 11 kb' │ '2.07 x' │
|
|
1270
|
-
│ typebox/
|
|
1287
|
+
│ typebox/hash │ ' 4 kb' │ ' 1 kb' │ '2.27 x' │
|
|
1288
|
+
│ typebox/value │ ' 85 kb' │ ' 39 kb' │ '2.16 x' │
|
|
1271
1289
|
│ typebox │ ' 12 kb' │ ' 6 kb' │ '1.89 x' │
|
|
1272
1290
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1273
1291
|
```
|
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
|
|
5
|
-
export
|
|
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
|
|
8
|
+
export type TOptional<T extends TSchema> = T & {
|
|
9
9
|
[Modifier]: 'Optional';
|
|
10
10
|
};
|
|
11
|
-
export
|
|
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
|
|
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
|
|
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
|
|
65
|
-
export
|
|
66
|
-
export
|
|
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
|
|
99
|
-
export
|
|
100
|
-
export
|
|
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
|
|
117
|
-
export
|
|
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
|
|
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
|
|
128
|
-
export
|
|
129
|
-
export
|
|
130
|
-
export
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
175
|
-
export
|
|
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
|
|
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
|
|
193
|
-
export
|
|
194
|
-
export
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
@@ -307,7 +307,7 @@ var ValueCast;
|
|
|
307
307
|
function Void(schema, references, value) {
|
|
308
308
|
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
309
309
|
}
|
|
310
|
-
function
|
|
310
|
+
function UserDefined(schema, references, value) {
|
|
311
311
|
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
312
312
|
}
|
|
313
313
|
function Visit(schema, references, value) {
|
|
@@ -365,7 +365,7 @@ var ValueCast;
|
|
|
365
365
|
default:
|
|
366
366
|
if (!index_1.Custom.Has(anySchema[Types.Kind]))
|
|
367
367
|
throw new ValueCastUnknownTypeError(anySchema);
|
|
368
|
-
return
|
|
368
|
+
return UserDefined(anySchema, anyReferences, value);
|
|
369
369
|
}
|
|
370
370
|
}
|
|
371
371
|
ValueCast.Visit = Visit;
|
package/value/check.js
CHANGED
|
@@ -31,9 +31,10 @@ exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
|
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const index_1 = require("../format/index");
|
|
33
33
|
const index_2 = require("../custom/index");
|
|
34
|
+
const index_3 = require("../hash/index");
|
|
34
35
|
class ValueCheckUnknownTypeError extends Error {
|
|
35
36
|
constructor(schema) {
|
|
36
|
-
super(
|
|
37
|
+
super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
|
|
37
38
|
this.schema = schema;
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -56,7 +57,16 @@ var ValueCheck;
|
|
|
56
57
|
if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
57
58
|
return false;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
// prettier-ignore
|
|
61
|
+
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
62
|
+
const hashed = index_3.ValueHash.Create(element);
|
|
63
|
+
if (set.has(hashed)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
set.add(hashed);
|
|
68
|
+
}
|
|
69
|
+
} return true; })())) {
|
|
60
70
|
return false;
|
|
61
71
|
}
|
|
62
72
|
return value.every((val) => Visit(schema.items, references, val));
|
|
@@ -291,11 +301,11 @@ var ValueCheck;
|
|
|
291
301
|
function Void(schema, references, value) {
|
|
292
302
|
return value === null;
|
|
293
303
|
}
|
|
294
|
-
function
|
|
304
|
+
function UserDefined(schema, references, value) {
|
|
295
305
|
if (!index_2.Custom.Has(schema[Types.Kind]))
|
|
296
306
|
return false;
|
|
297
307
|
const func = index_2.Custom.Get(schema[Types.Kind]);
|
|
298
|
-
return func(value);
|
|
308
|
+
return func(schema, value);
|
|
299
309
|
}
|
|
300
310
|
function Visit(schema, references, value) {
|
|
301
311
|
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
@@ -350,7 +360,7 @@ var ValueCheck;
|
|
|
350
360
|
default:
|
|
351
361
|
if (!index_2.Custom.Has(anySchema[Types.Kind]))
|
|
352
362
|
throw new ValueCheckUnknownTypeError(anySchema);
|
|
353
|
-
return
|
|
363
|
+
return UserDefined(anySchema, anyReferences, value);
|
|
354
364
|
}
|
|
355
365
|
}
|
|
356
366
|
// -------------------------------------------------------------------------
|
package/value/create.js
CHANGED
|
@@ -293,12 +293,12 @@ var ValueCreate;
|
|
|
293
293
|
function Void(schema, references) {
|
|
294
294
|
return null;
|
|
295
295
|
}
|
|
296
|
-
function
|
|
296
|
+
function UserDefined(schema, references) {
|
|
297
297
|
if (schema.default !== undefined) {
|
|
298
298
|
return schema.default;
|
|
299
299
|
}
|
|
300
300
|
else {
|
|
301
|
-
throw new Error('ValueCreate.
|
|
301
|
+
throw new Error('ValueCreate.UserDefined: User defined types must specify a default value');
|
|
302
302
|
}
|
|
303
303
|
}
|
|
304
304
|
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
@@ -357,7 +357,7 @@ var ValueCreate;
|
|
|
357
357
|
default:
|
|
358
358
|
if (!index_1.Custom.Has(anySchema[Types.Kind]))
|
|
359
359
|
throw new ValueCreateUnknownTypeError(anySchema);
|
|
360
|
-
return
|
|
360
|
+
return UserDefined(anySchema, anyReferences);
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
363
|
ValueCreate.Visit = Visit;
|
package/value/delta.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { Static } from '../typebox';
|
|
2
|
-
export
|
|
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
|
|
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
|
|
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
|
|
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/index.d.ts
CHANGED
package/value/index.js
CHANGED
|
@@ -41,8 +41,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
41
41
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.ValueErrorType = void 0;
|
|
44
|
+
exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueErrorType = void 0;
|
|
45
45
|
var index_1 = require("../errors/index");
|
|
46
46
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
47
|
+
var delta_1 = require("./delta");
|
|
48
|
+
Object.defineProperty(exports, "Edit", { enumerable: true, get: function () { return delta_1.Edit; } });
|
|
49
|
+
Object.defineProperty(exports, "Insert", { enumerable: true, get: function () { return delta_1.Insert; } });
|
|
50
|
+
Object.defineProperty(exports, "Update", { enumerable: true, get: function () { return delta_1.Update; } });
|
|
51
|
+
Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return delta_1.Delete; } });
|
|
47
52
|
__exportStar(require("./pointer"), exports);
|
|
48
53
|
__exportStar(require("./value"), exports);
|
package/value/is.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
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;
|