@sinclair/typebox 0.25.18 → 0.25.19
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/package.json +1 -1
- package/readme.md +122 -94
- package/system/system.d.ts +13 -6
- package/system/system.js +35 -7
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -96,10 +96,11 @@ License MIT
|
|
|
96
96
|
- [Errors](#values-errors)
|
|
97
97
|
- [Pointer](#values-pointer)
|
|
98
98
|
- [TypeCheck](#typecheck)
|
|
99
|
-
- [Ajv](#typecheck-ajv)
|
|
100
99
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
101
|
-
- [
|
|
102
|
-
|
|
100
|
+
- [Ajv](#typecheck-ajv)
|
|
101
|
+
- [TypeSystem](#typecheck)
|
|
102
|
+
- [Types](#typesystem-types)
|
|
103
|
+
- [Formats](#typesystem-formats)
|
|
103
104
|
- [Benchmark](#benchmark)
|
|
104
105
|
- [Compile](#benchmark-compile)
|
|
105
106
|
- [Validate](#benchmark-validate)
|
|
@@ -936,6 +937,71 @@ TypeBox targets JSON Schema Draft 6 and is built and tested against the Ajv JSON
|
|
|
936
937
|
|
|
937
938
|
The following sections detail using these validators.
|
|
938
939
|
|
|
940
|
+
<a name='typecheck-typecompiler'></a>
|
|
941
|
+
|
|
942
|
+
### TypeCompiler
|
|
943
|
+
|
|
944
|
+
TypeBox includes an high performance just-in-time (JIT) compiler and type checker that can be used in applications that require extremely fast validation. Note that this compiler is optimized for TypeBox types only where the schematics are known in advance.
|
|
945
|
+
|
|
946
|
+
The compiler module is provided as an optional import.
|
|
947
|
+
|
|
948
|
+
```typescript
|
|
949
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
950
|
+
```
|
|
951
|
+
|
|
952
|
+
Use the `Compile(...)` function to compile a type.
|
|
953
|
+
|
|
954
|
+
```typescript
|
|
955
|
+
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
956
|
+
x: Type.Number(), // x: TNumber;
|
|
957
|
+
y: Type.Number(), // y: TNumber;
|
|
958
|
+
z: Type.Number() // z: TNumber;
|
|
959
|
+
})) // }>>
|
|
960
|
+
|
|
961
|
+
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
962
|
+
```
|
|
963
|
+
|
|
964
|
+
Validation errors can be read with the `Errors(...)` function.
|
|
965
|
+
|
|
966
|
+
```typescript
|
|
967
|
+
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
968
|
+
x: Type.Number(), // x: TNumber;
|
|
969
|
+
y: Type.Number(), // y: TNumber;
|
|
970
|
+
z: Type.Number() // z: TNumber;
|
|
971
|
+
})) // }>>
|
|
972
|
+
|
|
973
|
+
const value = { }
|
|
974
|
+
|
|
975
|
+
const errors = [...C.Errors(value)] // const errors = [{
|
|
976
|
+
// schema: { type: 'number' },
|
|
977
|
+
// path: '/x',
|
|
978
|
+
// value: undefined,
|
|
979
|
+
// message: 'Expected number'
|
|
980
|
+
// }, {
|
|
981
|
+
// schema: { type: 'number' },
|
|
982
|
+
// path: '/y',
|
|
983
|
+
// value: undefined,
|
|
984
|
+
// message: 'Expected number'
|
|
985
|
+
// }, {
|
|
986
|
+
// schema: { type: 'number' },
|
|
987
|
+
// path: '/z',
|
|
988
|
+
// value: undefined,
|
|
989
|
+
// message: 'Expected number'
|
|
990
|
+
// }]
|
|
991
|
+
```
|
|
992
|
+
|
|
993
|
+
Compiled routines can be inspected with the `.Code()` function.
|
|
994
|
+
|
|
995
|
+
```typescript
|
|
996
|
+
const C = TypeCompiler.Compile(Type.String()) // const C: TypeCheck<TString>
|
|
997
|
+
|
|
998
|
+
console.log(C.Code()) // return function check(value) {
|
|
999
|
+
// return (
|
|
1000
|
+
// (typeof value === 'string')
|
|
1001
|
+
// )
|
|
1002
|
+
// }
|
|
1003
|
+
```
|
|
1004
|
+
|
|
939
1005
|
<a name='typecheck-ajv'></a>
|
|
940
1006
|
|
|
941
1007
|
### Ajv
|
|
@@ -1066,125 +1132,87 @@ const R = ajv.validate(Type.Object({ // const R = true
|
|
|
1066
1132
|
</details>
|
|
1067
1133
|
|
|
1068
1134
|
|
|
1069
|
-
<a name='
|
|
1135
|
+
<a name='typesystem'></a>
|
|
1070
1136
|
|
|
1071
|
-
|
|
1137
|
+
## TypeSystem
|
|
1072
1138
|
|
|
1073
|
-
TypeBox provides an
|
|
1139
|
+
TypeBox provides an extensible TypeSystem module that enables developers to register additional types above and beyond the standard or extended type set. This module also allows developers to define custom string formats as well as override certain type checking behaviours.
|
|
1074
1140
|
|
|
1075
|
-
The
|
|
1141
|
+
The TypeSystem module is provided as an optional import.
|
|
1076
1142
|
|
|
1077
1143
|
```typescript
|
|
1078
|
-
import {
|
|
1144
|
+
import { TypeSystem } from '@sinclair/typebox/system'
|
|
1079
1145
|
```
|
|
1080
1146
|
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
```typescript
|
|
1084
|
-
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
1085
|
-
x: Type.Number(), // x: TNumber;
|
|
1086
|
-
y: Type.Number(), // y: TNumber;
|
|
1087
|
-
z: Type.Number() // z: TNumber;
|
|
1088
|
-
})) // }>>
|
|
1147
|
+
<a name='typesystem-types'></a>
|
|
1089
1148
|
|
|
1090
|
-
|
|
1091
|
-
```
|
|
1149
|
+
### Types
|
|
1092
1150
|
|
|
1093
|
-
|
|
1151
|
+
Use the `CreateType(...)` function to specify and return a custom type. This function will return a type factory function that can be used to construct the type. The following creates and registers a BigNumber type which will statically infer as `bigint`.
|
|
1094
1152
|
|
|
1095
1153
|
```typescript
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1154
|
+
//--------------------------------------------------------------------------------------------
|
|
1155
|
+
//
|
|
1156
|
+
// Use TypeSystem.CreateType(...) to define and return a type factory function
|
|
1157
|
+
//
|
|
1158
|
+
//--------------------------------------------------------------------------------------------
|
|
1101
1159
|
|
|
1102
|
-
|
|
1160
|
+
type BigNumberOptions = { minimum?: bigint; maximum?: bigint }
|
|
1103
1161
|
|
|
1104
|
-
const
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
// message: 'Expected number'
|
|
1114
|
-
// }, {
|
|
1115
|
-
// schema: { type: 'number' },
|
|
1116
|
-
// path: '/z',
|
|
1117
|
-
// value: undefined,
|
|
1118
|
-
// message: 'Expected number'
|
|
1119
|
-
// }]
|
|
1120
|
-
```
|
|
1162
|
+
const BigNumber = TypeSystem.CreateType<bigint, BigNumberOptions>(
|
|
1163
|
+
'BigNumber',
|
|
1164
|
+
(options, value) => {
|
|
1165
|
+
if (typeof value !== 'bigint') return false
|
|
1166
|
+
if (options.maximum !== undefined && value > options.maximum) return false
|
|
1167
|
+
if (options.minimum !== undefined && value < options.minimum) return false
|
|
1168
|
+
return true
|
|
1169
|
+
}
|
|
1170
|
+
)
|
|
1121
1171
|
|
|
1122
|
-
|
|
1172
|
+
//--------------------------------------------------------------------------------------------
|
|
1173
|
+
//
|
|
1174
|
+
// Use the custom type like any other type
|
|
1175
|
+
//
|
|
1176
|
+
//--------------------------------------------------------------------------------------------
|
|
1123
1177
|
|
|
1124
|
-
|
|
1125
|
-
|
|
1178
|
+
const T = BigNumber({ minimum: 10n, maximum: 20n }) // const T = {
|
|
1179
|
+
// minimum: 10n,
|
|
1180
|
+
// maximum: 20n,
|
|
1181
|
+
// [Symbol(TypeBox.Kind)]: 'BigNumber'
|
|
1182
|
+
// }
|
|
1126
1183
|
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
// }
|
|
1184
|
+
const C = TypeCompiler.Compile(T)
|
|
1185
|
+
const X = C.Check(15n) // const X = true
|
|
1186
|
+
const Y = C.Check(5n) // const Y = false
|
|
1187
|
+
const Z = C.Check(25n) // const Z = false
|
|
1132
1188
|
```
|
|
1133
1189
|
|
|
1134
|
-
<a name='
|
|
1190
|
+
<a name='typesystem-formats'></a>
|
|
1135
1191
|
|
|
1136
|
-
###
|
|
1192
|
+
### Formats
|
|
1137
1193
|
|
|
1138
|
-
Use the
|
|
1139
|
-
|
|
1140
|
-
The custom module is an optional import.
|
|
1194
|
+
Use the `CreateFormat(...)` function to specify user defined string formats. The following creates a custom string format that checks for lowercase.
|
|
1141
1195
|
|
|
1142
1196
|
```typescript
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
```typescript
|
|
1149
|
-
import { Type, Kind } from '@sinclair/typebox'
|
|
1150
|
-
|
|
1151
|
-
Custom.Set('bigint', (schema, value) => typeof value === 'bigint')
|
|
1152
|
-
// ▲
|
|
1153
|
-
// │
|
|
1154
|
-
// └────────────────────────────┐
|
|
1155
|
-
// │
|
|
1156
|
-
const T = Type.Unsafe<bigint>({ [Kind]: 'bigint' }) // const T = { [Kind]: 'BigInt' }
|
|
1157
|
-
|
|
1158
|
-
const A = TypeCompiler.Compile(T).Check(1n) // const A = true
|
|
1159
|
-
|
|
1160
|
-
const B = Value.Check(T, 1) // const B = false
|
|
1161
|
-
```
|
|
1162
|
-
|
|
1163
|
-
<a name='typecheck-custom-formats'></a>
|
|
1164
|
-
|
|
1165
|
-
### Custom Formats
|
|
1166
|
-
|
|
1167
|
-
Use the format module to create user defined string formats. The format module is specific to TypeBox can only be used with the TypeCompiler and Value modules. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html).
|
|
1168
|
-
|
|
1169
|
-
The format module is an optional import.
|
|
1197
|
+
//--------------------------------------------------------------------------------------------
|
|
1198
|
+
//
|
|
1199
|
+
// Use TypeSystem.CreateFormat(...) to define a custom string format
|
|
1200
|
+
//
|
|
1201
|
+
//--------------------------------------------------------------------------------------------
|
|
1170
1202
|
|
|
1171
|
-
|
|
1172
|
-
import { Format } from '@sinclair/typebox/format'
|
|
1173
|
-
```
|
|
1203
|
+
TypeSystem.CreateFormat('lowercase', value => value === value.toLowerCase())
|
|
1174
1204
|
|
|
1175
|
-
|
|
1205
|
+
//--------------------------------------------------------------------------------------------
|
|
1206
|
+
//
|
|
1207
|
+
// Use the format by creating string types with the 'format' option
|
|
1208
|
+
//
|
|
1209
|
+
//--------------------------------------------------------------------------------------------
|
|
1176
1210
|
|
|
1177
|
-
```typescript
|
|
1178
|
-
Format.Set('lowercase', value => value === value.toLowerCase())
|
|
1179
|
-
// ▲
|
|
1180
|
-
// │
|
|
1181
|
-
// └────────────────────┐
|
|
1182
|
-
// │
|
|
1183
1211
|
const T = Type.String({ format: 'lowercase' })
|
|
1184
1212
|
|
|
1185
|
-
const A =
|
|
1213
|
+
const A = Value.Check(T, 'action') // const A = true
|
|
1186
1214
|
|
|
1187
|
-
const B = Value.Check(T, '
|
|
1215
|
+
const B = Value.Check(T, 'ACTION') // const B = false
|
|
1188
1216
|
```
|
|
1189
1217
|
|
|
1190
1218
|
<a name='benchmark'></a>
|
package/system/system.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
+
export declare class TypeSystemDuplicateTypeKind extends Error {
|
|
2
|
+
constructor(kind: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class TypeSystemDuplicateFormat extends Error {
|
|
5
|
+
constructor(kind: string);
|
|
6
|
+
}
|
|
7
|
+
/** Creates user defined types and formats and provides overrides for value checking behaviours */
|
|
1
8
|
export declare namespace TypeSystem {
|
|
2
|
-
/**
|
|
3
|
-
* Sets whether arrays should be treated as kinds of objects. The default is `false`
|
|
4
|
-
*/
|
|
9
|
+
/** Sets whether arrays should be treated as kinds of objects. The default is `false` */
|
|
5
10
|
let AllowArrayObjects: boolean;
|
|
6
|
-
/**
|
|
7
|
-
* Sets whether numeric checks should consider NaN a valid number type. The default is `false`
|
|
8
|
-
*/
|
|
11
|
+
/** Sets whether numeric checks should consider NaN a valid number type. The default is `false` */
|
|
9
12
|
let AllowNaN: boolean;
|
|
13
|
+
/** Creates a custom type */
|
|
14
|
+
function CreateType<Type, Options = object>(kind: string, callback: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => import("@sinclair/typebox").TUnsafe<Type>;
|
|
15
|
+
/** Creates a custom string format */
|
|
16
|
+
function CreateFormat(format: string, callback: (value: string) => boolean): (value: string) => boolean;
|
|
10
17
|
}
|
package/system/system.js
CHANGED
|
@@ -27,15 +27,43 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeSystem = void 0;
|
|
30
|
+
exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDuplicateTypeKind = void 0;
|
|
31
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
32
|
+
const index_1 = require("../custom/index");
|
|
33
|
+
const index_2 = require("../format/index");
|
|
34
|
+
class TypeSystemDuplicateTypeKind extends Error {
|
|
35
|
+
constructor(kind) {
|
|
36
|
+
super(`Duplicate kind '${kind}' detected`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.TypeSystemDuplicateTypeKind = TypeSystemDuplicateTypeKind;
|
|
40
|
+
class TypeSystemDuplicateFormat extends Error {
|
|
41
|
+
constructor(kind) {
|
|
42
|
+
super(`Duplicate format '${kind}' detected`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat;
|
|
46
|
+
/** Creates user defined types and formats and provides overrides for value checking behaviours */
|
|
31
47
|
var TypeSystem;
|
|
32
48
|
(function (TypeSystem) {
|
|
33
|
-
/**
|
|
34
|
-
* Sets whether arrays should be treated as kinds of objects. The default is `false`
|
|
35
|
-
*/
|
|
49
|
+
/** Sets whether arrays should be treated as kinds of objects. The default is `false` */
|
|
36
50
|
TypeSystem.AllowArrayObjects = false;
|
|
37
|
-
/**
|
|
38
|
-
* Sets whether numeric checks should consider NaN a valid number type. The default is `false`
|
|
39
|
-
*/
|
|
51
|
+
/** Sets whether numeric checks should consider NaN a valid number type. The default is `false` */
|
|
40
52
|
TypeSystem.AllowNaN = false;
|
|
53
|
+
/** Creates a custom type */
|
|
54
|
+
function CreateType(kind, callback) {
|
|
55
|
+
if (index_1.Custom.Has(kind))
|
|
56
|
+
throw new TypeSystemDuplicateTypeKind(kind);
|
|
57
|
+
index_1.Custom.Set(kind, callback);
|
|
58
|
+
return (options = {}) => typebox_1.Type.Unsafe({ ...options, [typebox_1.Kind]: kind });
|
|
59
|
+
}
|
|
60
|
+
TypeSystem.CreateType = CreateType;
|
|
61
|
+
/** Creates a custom string format */
|
|
62
|
+
function CreateFormat(format, callback) {
|
|
63
|
+
if (index_2.Format.Has(format))
|
|
64
|
+
throw new TypeSystemDuplicateFormat(format);
|
|
65
|
+
index_2.Format.Set(format, callback);
|
|
66
|
+
return callback;
|
|
67
|
+
}
|
|
68
|
+
TypeSystem.CreateFormat = CreateFormat;
|
|
41
69
|
})(TypeSystem = exports.TypeSystem || (exports.TypeSystem = {}));
|