typebox 1.0.6 → 1.0.8
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/build/compile/validator.d.mts +10 -3
- package/build/compile/validator.mjs +16 -7
- package/build/typebox.d.mts +1 -1
- package/build/value/check/check.d.mts +2 -2
- package/build/value/check/check.mjs +1 -1
- package/build/value/codec/has.mjs +8 -0
- package/license +22 -22
- package/package.json +27 -27
- package/readme.md +235 -232
|
@@ -4,6 +4,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
4
4
|
private readonly context;
|
|
5
5
|
private readonly type;
|
|
6
6
|
private readonly isEvaluated;
|
|
7
|
+
private readonly hasCodec;
|
|
7
8
|
private readonly code;
|
|
8
9
|
private readonly check;
|
|
9
10
|
constructor(context: Context, type: Type);
|
|
@@ -15,16 +16,22 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
15
16
|
Type(): Type;
|
|
16
17
|
/** Returns the generated code for this validator */
|
|
17
18
|
Code(): string;
|
|
19
|
+
/** Checks a value matches the Validator type. */
|
|
18
20
|
Check(value: unknown): value is StaticEncode<Type, Context>;
|
|
21
|
+
/** Returns errors for the given value. */
|
|
19
22
|
Errors(value: unknown): TLocalizedValidationError[];
|
|
23
|
+
/** Cleans a value using the Validator type. */
|
|
20
24
|
Clean(value: unknown): unknown;
|
|
25
|
+
/** Converts a value using the Validator type. */
|
|
21
26
|
Convert(value: unknown): unknown;
|
|
27
|
+
/** Creates a value using the Validator type. */
|
|
22
28
|
Create(): StaticEncode<Type, Context>;
|
|
29
|
+
/** Creates defaults using the Validator type. */
|
|
23
30
|
Default(value: unknown): unknown;
|
|
24
|
-
/** Parses a value
|
|
31
|
+
/** Parses a value */
|
|
25
32
|
Parse(value: unknown): StaticDecode<Type, Context>;
|
|
26
|
-
/** Decodes a value
|
|
33
|
+
/** Decodes a value */
|
|
27
34
|
Decode(value: unknown): StaticDecode<Type, Context>;
|
|
28
|
-
/** Encodes a value
|
|
35
|
+
/** Encodes a value */
|
|
29
36
|
Encode(value: unknown): StaticEncode<Type, Context>;
|
|
30
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Environment } from '../system/environment/index.mjs';
|
|
3
3
|
import { Base } from '../type/index.mjs';
|
|
4
|
-
import { Errors, Clean, Convert, Create, Default,
|
|
4
|
+
import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser } from '../value/index.mjs';
|
|
5
5
|
import { Build } from '../schema/index.mjs';
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// ValidatorType<...>
|
|
@@ -12,6 +12,7 @@ export class Validator extends Base {
|
|
|
12
12
|
this.context = context;
|
|
13
13
|
this.type = type;
|
|
14
14
|
const result = Build(context, type).Evaluate();
|
|
15
|
+
this.hasCodec = HasCodec(context, type);
|
|
15
16
|
this.isEvaluated = result.IsEvaluated;
|
|
16
17
|
this.code = result.Code;
|
|
17
18
|
this.check = result.Check;
|
|
@@ -44,40 +45,48 @@ export class Validator extends Base {
|
|
|
44
45
|
// ----------------------------------------------------------------
|
|
45
46
|
// Base<...>
|
|
46
47
|
// ----------------------------------------------------------------
|
|
48
|
+
/** Checks a value matches the Validator type. */
|
|
47
49
|
Check(value) {
|
|
48
50
|
return this.check(value);
|
|
49
51
|
}
|
|
52
|
+
/** Returns errors for the given value. */
|
|
50
53
|
Errors(value) {
|
|
51
54
|
if (Environment.CanEvaluate() && this.check(value))
|
|
52
55
|
return [];
|
|
53
56
|
return Errors(this.context, this.type, value);
|
|
54
57
|
}
|
|
58
|
+
/** Cleans a value using the Validator type. */
|
|
55
59
|
Clean(value) {
|
|
56
60
|
return Clean(this.context, this.type, value);
|
|
57
61
|
}
|
|
62
|
+
/** Converts a value using the Validator type. */
|
|
58
63
|
Convert(value) {
|
|
59
64
|
return Convert(this.context, this.type, value);
|
|
60
65
|
}
|
|
66
|
+
/** Creates a value using the Validator type. */
|
|
61
67
|
Create() {
|
|
62
68
|
return Create(this.context, this.type);
|
|
63
69
|
}
|
|
70
|
+
/** Creates defaults using the Validator type. */
|
|
64
71
|
Default(value) {
|
|
65
72
|
return Default(this.context, this.type, value);
|
|
66
73
|
}
|
|
67
74
|
// ----------------------------------------------------------------
|
|
68
|
-
// Parse
|
|
75
|
+
// Parse | Decode | Encode
|
|
69
76
|
// ----------------------------------------------------------------
|
|
70
|
-
/** Parses a value
|
|
77
|
+
/** Parses a value */
|
|
71
78
|
Parse(value) {
|
|
72
79
|
const result = this.Check(value) ? value : Parser(this.context, this.type, value);
|
|
73
80
|
return result;
|
|
74
81
|
}
|
|
75
|
-
/** Decodes a value
|
|
82
|
+
/** Decodes a value */
|
|
76
83
|
Decode(value) {
|
|
77
|
-
|
|
84
|
+
const result = this.hasCodec ? Decode(this.context, this.type, value) : this.Parse(value);
|
|
85
|
+
return result;
|
|
78
86
|
}
|
|
79
|
-
/** Encodes a value
|
|
87
|
+
/** Encodes a value */
|
|
80
88
|
Encode(value) {
|
|
81
|
-
|
|
89
|
+
const result = this.hasCodec ? Encode(this.context, this.type, value) : this.Parse(value);
|
|
90
|
+
return result;
|
|
82
91
|
}
|
|
83
92
|
}
|
package/build/typebox.d.mts
CHANGED
|
@@ -58,7 +58,7 @@ export { IsRecord, Record, RecordKey, RecordPattern as RecordKeyAsPattern, Recor
|
|
|
58
58
|
export { IsRef, Ref, type TRef } from './type/types/ref.mjs';
|
|
59
59
|
export { IsRest, Rest, type TRest } from './type/types/rest.mjs';
|
|
60
60
|
export { IsKind, IsSchema, type TArrayOptions, type TFormat, type TIntersectOptions, type TNumberOptions, type TObjectOptions, type TSchema, type TSchemaOptions, type TStringOptions, type TTupleOptions } from './type/types/schema.mjs';
|
|
61
|
-
export { type Static } from './type/types/static.mjs';
|
|
61
|
+
export { type Static, type StaticDecode, type StaticEncode, type StaticParse } from './type/types/static.mjs';
|
|
62
62
|
export { IsString, String, type TString } from './type/types/string.mjs';
|
|
63
63
|
export { IsSymbol, Symbol, type TSymbol } from './type/types/symbol.mjs';
|
|
64
64
|
export { IsTemplateLiteral, TemplateLiteral, type TTemplateLiteral } from './type/types/template-literal.mjs';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Static, TProperties, TSchema } from '../../type/index.mjs';
|
|
2
|
-
/** Checks a value matches the provided type.
|
|
2
|
+
/** Checks a value matches the provided type. */
|
|
3
3
|
export declare function Check<const Type extends TSchema, Result extends unknown = Static<Type>>(type: Type, value: unknown): value is Result;
|
|
4
|
-
/** Checks a value matches the provided type.
|
|
4
|
+
/** Checks a value matches the provided type. */
|
|
5
5
|
export declare function Check<Context extends TProperties, const Type extends TSchema, Result extends unknown = Static<Type, Context>>(context: Context, type: Type, value: unknown): value is Result;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Arguments } from '../../system/arguments/index.mjs';
|
|
3
3
|
import { Check as SchemaCheck } from '../../schema/index.mjs';
|
|
4
|
-
/** Checks a value matches the provided type.
|
|
4
|
+
/** Checks a value matches the provided type. */
|
|
5
5
|
export function Check(...args) {
|
|
6
6
|
const [context, type, value] = Arguments.Match(args, {
|
|
7
7
|
3: (context, type, value) => [context, type, value],
|
|
@@ -46,6 +46,9 @@ function FromRecord(context, type) {
|
|
|
46
46
|
// Ref
|
|
47
47
|
// ------------------------------------------------------------------
|
|
48
48
|
function FromRef(context, type) {
|
|
49
|
+
if (visited.has(type.$ref))
|
|
50
|
+
return false;
|
|
51
|
+
visited.add(type.$ref);
|
|
49
52
|
return IsCodec(type) || (Guard.HasPropertyKey(context, type.$ref)
|
|
50
53
|
&& FromType(context, context[type.$ref]));
|
|
51
54
|
}
|
|
@@ -75,11 +78,16 @@ function FromType(context, type) {
|
|
|
75
78
|
IsUnion(type) ? FromUnion(context, type) :
|
|
76
79
|
IsCodec(type));
|
|
77
80
|
}
|
|
81
|
+
// ------------------------------------------------------------------
|
|
82
|
+
// Visited
|
|
83
|
+
// ------------------------------------------------------------------
|
|
84
|
+
const visited = new Set();
|
|
78
85
|
/** Returns true if this type contains a Codec */
|
|
79
86
|
export function HasCodec(...args) {
|
|
80
87
|
const [context, type] = Arguments.Match(args, {
|
|
81
88
|
2: (context, type) => [context, type],
|
|
82
89
|
1: (type) => [{}, type]
|
|
83
90
|
});
|
|
91
|
+
visited.clear();
|
|
84
92
|
return FromType(context, type);
|
|
85
93
|
}
|
package/license
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
TypeBox
|
|
2
|
-
|
|
3
|
-
The MIT License (MIT)
|
|
4
|
-
|
|
5
|
-
Copyright (c) 2017-2025 Haydn Paterson
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
in the Software without restriction, including without limitation the rights
|
|
10
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
furnished to do so, subject to the following conditions:
|
|
13
|
-
|
|
14
|
-
The above copyright notice and this permission notice shall be included in
|
|
15
|
-
all copies or substantial portions of the Software.
|
|
16
|
-
|
|
17
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1
|
+
TypeBox
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2017-2025 Haydn Paterson
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
23
|
THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typebox",
|
|
3
3
|
"description": "A Runtime Type System for JavaScript",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.8",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"jsonschema"
|
|
@@ -15,20 +15,11 @@
|
|
|
15
15
|
"types": "./build/index.d.mts",
|
|
16
16
|
"module": "./build/index.mjs",
|
|
17
17
|
"exports": {
|
|
18
|
-
"./compile": {
|
|
19
|
-
"import": "./build/compile/index.mjs"
|
|
20
|
-
},
|
|
21
|
-
"./error": {
|
|
22
|
-
"import": "./build/error/index.mjs"
|
|
23
|
-
},
|
|
24
18
|
"./format": {
|
|
25
19
|
"import": "./build/format/index.mjs"
|
|
26
20
|
},
|
|
27
|
-
"./
|
|
28
|
-
"import": "./build/
|
|
29
|
-
},
|
|
30
|
-
"./schema": {
|
|
31
|
-
"import": "./build/schema/index.mjs"
|
|
21
|
+
"./value": {
|
|
22
|
+
"import": "./build/value/index.mjs"
|
|
32
23
|
},
|
|
33
24
|
"./system": {
|
|
34
25
|
"import": "./build/system/index.mjs"
|
|
@@ -36,8 +27,17 @@
|
|
|
36
27
|
"./type": {
|
|
37
28
|
"import": "./build/type/index.mjs"
|
|
38
29
|
},
|
|
39
|
-
"./
|
|
40
|
-
"import": "./build/
|
|
30
|
+
"./schema": {
|
|
31
|
+
"import": "./build/schema/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./compile": {
|
|
34
|
+
"import": "./build/compile/index.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./guard": {
|
|
37
|
+
"import": "./build/guard/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"./error": {
|
|
40
|
+
"import": "./build/error/index.mjs"
|
|
41
41
|
},
|
|
42
42
|
".": {
|
|
43
43
|
"import": "./build/index.mjs"
|
|
@@ -45,20 +45,11 @@
|
|
|
45
45
|
},
|
|
46
46
|
"typesVersions": {
|
|
47
47
|
"*": {
|
|
48
|
-
"compile": [
|
|
49
|
-
"./build/compile/index.d.mts"
|
|
50
|
-
],
|
|
51
|
-
"error": [
|
|
52
|
-
"./build/error/index.d.mts"
|
|
53
|
-
],
|
|
54
48
|
"format": [
|
|
55
49
|
"./build/format/index.d.mts"
|
|
56
50
|
],
|
|
57
|
-
"
|
|
58
|
-
"./build/
|
|
59
|
-
],
|
|
60
|
-
"schema": [
|
|
61
|
-
"./build/schema/index.d.mts"
|
|
51
|
+
"value": [
|
|
52
|
+
"./build/value/index.d.mts"
|
|
62
53
|
],
|
|
63
54
|
"system": [
|
|
64
55
|
"./build/system/index.d.mts"
|
|
@@ -66,8 +57,17 @@
|
|
|
66
57
|
"type": [
|
|
67
58
|
"./build/type/index.d.mts"
|
|
68
59
|
],
|
|
69
|
-
"
|
|
70
|
-
"./build/
|
|
60
|
+
"schema": [
|
|
61
|
+
"./build/schema/index.d.mts"
|
|
62
|
+
],
|
|
63
|
+
"compile": [
|
|
64
|
+
"./build/compile/index.d.mts"
|
|
65
|
+
],
|
|
66
|
+
"guard": [
|
|
67
|
+
"./build/guard/index.d.mts"
|
|
68
|
+
],
|
|
69
|
+
"error": [
|
|
70
|
+
"./build/error/index.d.mts"
|
|
71
71
|
],
|
|
72
72
|
".": [
|
|
73
73
|
"./build/./index.d.mts"
|
package/readme.md
CHANGED
|
@@ -1,233 +1,236 @@
|
|
|
1
|
-
<div align='center'>
|
|
2
|
-
|
|
3
|
-
<h1>TypeBox</h1>
|
|
4
|
-
|
|
5
|
-
<p>A Runtime Type System for JavaScript</p>
|
|
6
|
-
|
|
7
|
-
<img src="typebox.png" />
|
|
8
|
-
|
|
9
|
-
<br />
|
|
10
|
-
<br />
|
|
11
|
-
|
|
12
|
-
[](https://badge.fury.io/js/typebox)
|
|
13
|
-
[](https://www.npmjs.com/package/typebox)
|
|
14
|
-
[](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml)
|
|
15
|
-
[](https://opensource.org/licenses/MIT)
|
|
16
|
-
|
|
17
|
-
</div>
|
|
18
|
-
|
|
19
|
-
## Install
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
$ npm install typebox --save # 1.0.0
|
|
23
|
-
|
|
24
|
-
$ npm install @sinclair/typebox --save # 0.34.x
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
## Usage
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import Type, { type Static } from 'typebox'
|
|
32
|
-
|
|
33
|
-
const T = Type.Object({ // const T = {
|
|
34
|
-
x: Type.Number(), // type: 'object',
|
|
35
|
-
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
36
|
-
z: Type.Number()
|
|
37
|
-
}) // x: { type: 'number' },
|
|
38
|
-
// y: { type: 'number' },
|
|
39
|
-
// z: { type: 'number' }
|
|
40
|
-
// }
|
|
41
|
-
// }
|
|
42
|
-
|
|
43
|
-
type T = Static<typeof T> // type T = {
|
|
44
|
-
// x: number,
|
|
45
|
-
// y: number,
|
|
46
|
-
// z: number
|
|
47
|
-
// }
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Overview
|
|
51
|
-
|
|
52
|
-
[Documentation](https://sinclairzx81.github.io/typebox/)
|
|
53
|
-
|
|
54
|
-
TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
|
|
55
|
-
|
|
56
|
-
This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
- [
|
|
65
|
-
- [
|
|
66
|
-
- [
|
|
67
|
-
- [
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// }
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
//
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
1
|
+
<div align='center'>
|
|
2
|
+
|
|
3
|
+
<h1>TypeBox</h1>
|
|
4
|
+
|
|
5
|
+
<p>A Runtime Type System for JavaScript</p>
|
|
6
|
+
|
|
7
|
+
<img src="typebox.png" />
|
|
8
|
+
|
|
9
|
+
<br />
|
|
10
|
+
<br />
|
|
11
|
+
|
|
12
|
+
[](https://badge.fury.io/js/typebox)
|
|
13
|
+
[](https://www.npmjs.com/package/typebox)
|
|
14
|
+
[](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml)
|
|
15
|
+
[](https://opensource.org/licenses/MIT)
|
|
16
|
+
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
$ npm install typebox --save # 1.0.0
|
|
23
|
+
|
|
24
|
+
$ npm install @sinclair/typebox --save # 0.34.x
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import Type, { type Static } from 'typebox'
|
|
32
|
+
|
|
33
|
+
const T = Type.Object({ // const T = {
|
|
34
|
+
x: Type.Number(), // type: 'object',
|
|
35
|
+
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
36
|
+
z: Type.Number() // properties: {
|
|
37
|
+
}) // x: { type: 'number' },
|
|
38
|
+
// y: { type: 'number' },
|
|
39
|
+
// z: { type: 'number' }
|
|
40
|
+
// }
|
|
41
|
+
// }
|
|
42
|
+
|
|
43
|
+
type T = Static<typeof T> // type T = {
|
|
44
|
+
// x: number,
|
|
45
|
+
// y: number,
|
|
46
|
+
// z: number
|
|
47
|
+
// }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Overview
|
|
51
|
+
|
|
52
|
+
[Documentation](https://sinclairzx81.github.io/typebox/)
|
|
53
|
+
|
|
54
|
+
TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
|
|
55
|
+
|
|
56
|
+
This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|
|
57
|
+
|
|
58
|
+
License: MIT
|
|
59
|
+
|
|
60
|
+
## Contents
|
|
61
|
+
|
|
62
|
+
- [Upgrade](#Upgrade)
|
|
63
|
+
- [Type](#Type)
|
|
64
|
+
- [Script](#Script)
|
|
65
|
+
- [Value](#Value)
|
|
66
|
+
- [Compile](#Compile)
|
|
67
|
+
- [Contribute](#Contribute)
|
|
68
|
+
|
|
69
|
+
## Upgrade
|
|
70
|
+
|
|
71
|
+
Refer to the following URL for information on upgrading from 0.34.x to 1.0.
|
|
72
|
+
|
|
73
|
+
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
74
|
+
|
|
75
|
+
<a name="Type"></a>
|
|
76
|
+
|
|
77
|
+
## Type
|
|
78
|
+
|
|
79
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKYBo4G84xauAZRgEMZgBjOAXzgDMoIQ4ByPNAIwgA8WAoPhQgA7AM7wEcALyJ8AOgDyHAFaoKMABQ44O3Xv0HDAeiNwhYidOx8d3AFyy0cgHIBXEB1RQNASkyGAwICTXXZUBxYIFTUYFnQbOCQHZCc3Dy9ffyDsoJCdKFQAR1dgAoATBwBtFl5MFiQ41gAvFgBdeJ0m5Pk0z28fHMHAvLgwRjRYYFRRByw+agGhpeW9EdtZ3HwI4Xc+lhoOlaPltcSNsO3dr33qQ+P77NOu7E20S-SoG4SHn+NTHWo31+wJ0IUBfDCiCsxDIlAAPGEIHREAA+EGg0yQyQyObo9EjexwHYfO54n4jJJEq5QUlk+4jZ7EvpAunHMFAA)
|
|
80
|
+
|
|
81
|
+
TypeBox includes many functions to create Json Schema types. Each function returns a small Json Schema fragment that corresponds to a TypeScript type. TypeBox uses function composition to combine schema fragments into more complex types. It provides a set of functions that are used to model Json Schema schematics as well as a set of functions that model constructs native to JavaScript and TypeScript.
|
|
82
|
+
|
|
83
|
+
## Example
|
|
84
|
+
|
|
85
|
+
The following creates a Json Schema type and infers with Static.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import Type, { type Static } from 'typebox'
|
|
89
|
+
|
|
90
|
+
const T = Type.Object({ // const T = {
|
|
91
|
+
x: Type.Number(), // type: 'object',
|
|
92
|
+
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
93
|
+
z: Type.Number() // properties: {
|
|
94
|
+
}) // x: { type: 'number' },
|
|
95
|
+
// y: { type: 'number' },
|
|
96
|
+
// z: { type: 'number' }
|
|
97
|
+
// }
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
type T = Static<typeof T> // type T = {
|
|
101
|
+
// x: number,
|
|
102
|
+
// y: number,
|
|
103
|
+
// z: number
|
|
104
|
+
// }
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
<a name="Script"></a>
|
|
110
|
+
|
|
111
|
+
## Script
|
|
112
|
+
|
|
113
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKYBo4G84xauAZRgEMZgBjOAXzgDMoIQ4ByPNAIwgA8WAoPhQgA7AM7wEcALyJ8AOkIUowMDAAUAAxx84cbgC44wgK4gOqKJh1wkhk2YtXdALzunzUOH2oaAlLoDAoOCQ0LCggHoIuCExCWlEAHkOACtUChgAHixrcLz8gsCogINEADl3R1zCmtqi6N1bcsrLarr2guKXQwQKhyg2jqHQ4uoAPgFY8SIE5DQFJRV1HElqTC1rAG0AaThgYTgAa1QkCDpEAF0enYu4AB8jYwAbJ+8-YY-g4qn4Qh7ktIZbKDT6fLp6HoAVWEwBEmU2vRamERLwuY3QINBw3BTQQ0NhwnhiP6yIqqPRmKxHXBrkQ+LhCL6HlJzyeaMpVLqowmfHYBEICWIZEomT5ZyIY05xT5MxkOU5WPBpXsHnujxeGIVoJxbn6apMGo5Ws6DTgtJVFn1rKNxryowEfCAA)
|
|
114
|
+
|
|
115
|
+
TypeBox is a runtime type system that uses Json Schema as an AST for runtime type representation. The Script function provides a syntactic frontend to the type system and allows Json Schema to be created using native TypeScript syntax. TypeBox provides full static and runtime type safety for string-encoded types.
|
|
116
|
+
|
|
117
|
+
### Example
|
|
118
|
+
|
|
119
|
+
The following uses Script to create and map types.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import Type, { type Static } from 'typebox'
|
|
123
|
+
|
|
124
|
+
const T = Type.Script(`{
|
|
125
|
+
x: number,
|
|
126
|
+
y: number,
|
|
127
|
+
z: number
|
|
128
|
+
}`) // const T = TObject<{
|
|
129
|
+
// x: TNumber,
|
|
130
|
+
// y: TNumber,
|
|
131
|
+
// z: TNumber
|
|
132
|
+
// }>
|
|
133
|
+
|
|
134
|
+
const S = Type.Script({ T }, `{
|
|
135
|
+
[K in keyof T]: T[K] | null
|
|
136
|
+
}`) // const S: TObject<{
|
|
137
|
+
// x: TUnion<[TNumber, TNull]>,
|
|
138
|
+
// y: TUnion<[TNumber, TNull]>,
|
|
139
|
+
// z: TUnion<[TNumber, TNull]>
|
|
140
|
+
// }>
|
|
141
|
+
|
|
142
|
+
type S = Static<typeof S> // type S = {
|
|
143
|
+
// x: number | null,
|
|
144
|
+
// y: number | null,
|
|
145
|
+
// z: number | null
|
|
146
|
+
// }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
<a name="Value"></a>
|
|
150
|
+
|
|
151
|
+
## Value
|
|
152
|
+
|
|
153
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoUSWOANQEMAbAV3Sx30LVLIHoAbi3ZVKAYwgA7AM7wEcALyIiAOgDyxAFapxMABQBvSnDhkAXCrSqAcqxDFUUfQEoANCbhJLya3YdOrh6mAF4+av6Ozi6UAL4xlPz8cADCABa6ANaUEtJycACCSgwiqKrpWfoIbnCGpvUNjU1NSXCSsvAFlqQQzKiMUsUwUOyeFnAAjDXNM7Nzs57ecABMwXBhcADMcQmtAAqMUDKoOe35AELFTGxlB0eoVTV18y+mrWfw55bGpuNTrwDAfVWr9LFJ7FE1ktVkDYS8QV4wRCnGsNps4RjZgiNuCAlAdpjCVjkrFiqpyUA)
|
|
154
|
+
|
|
155
|
+
The TypeBox Value module provides functions to Check and Parse JavaScript values. It also includes functions such as Clone, Repair, Encode, Decode, Diff and Patch which perform various structural operations on JavaScript values. This module provides unified support for both Json Schema and Standard Schema.
|
|
156
|
+
|
|
157
|
+
The Value module is available via optional import.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import Value from 'typebox/value'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Example
|
|
164
|
+
|
|
165
|
+
The following uses the Value module to Check and Parse a value.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const T = Type.Object({
|
|
169
|
+
x: Type.Number(),
|
|
170
|
+
y: Type.Number(),
|
|
171
|
+
z: Type.Number()
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
// Check
|
|
175
|
+
|
|
176
|
+
const A = Value.Check(T, { // const A: boolean = true
|
|
177
|
+
x: 1,
|
|
178
|
+
y: 2,
|
|
179
|
+
z: 3
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// Parse
|
|
183
|
+
|
|
184
|
+
const B = Value.Parse(T, { // const B: {
|
|
185
|
+
x: 1, // x: number,
|
|
186
|
+
y: 2, // y: number,
|
|
187
|
+
z: 3 // z: number
|
|
188
|
+
}) // } = ...
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
<a name="Compile"></a>
|
|
193
|
+
|
|
194
|
+
## Compile
|
|
195
|
+
|
|
196
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1IChRJY4AKlRxES5YXXrcurAHYBneMjgBeFGw4AKIdQB0AeRoArLMxiakcK9Zu3bjRnDmKUALjgA1AIYZgAEy8YaAAeBFwAGkFDEzNQrit6dx0sXQA5AFcQGiwoTQBKSLsi4pKrB2tEwQysnPD4uAok4TTM7NyC0s6u8qtGqtba+oAvJr1qtvyuqZKeuBH+mqguXDy86fWNm3LcAD4dri5y5AALUwBrA+d4AEFVFF0T84tNjfKruGv3OghsL1k7mBQdJYeqVACMhReUNs9T6ACY6lZ5gBmZZ5A7lAAKXig8hBMggCngACE7shdNjcVhntDuo53sT3AhQe4IbT2WVHAl3LIBlBEQ13AiOezZn1eYsBSiRaKuXMeXy0TKZds7rp1UA)
|
|
197
|
+
|
|
198
|
+
The TypeBox Compile module provides functions to convert types into high-performance validators. The compiler is tuned for fast compilation as well as fast validation. This module provides unified support to compile both Json Schema and Standard Schema, however performance optimizations are only possible with Json Schema.
|
|
199
|
+
|
|
200
|
+
The Compile module is available via optional import.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { Compile } from 'typebox/compile'
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Example
|
|
207
|
+
|
|
208
|
+
The following uses the Compile module to Check and Parse a value.
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
212
|
+
x: Type.Number(), // x: TNumber,
|
|
213
|
+
y: Type.Number(), // y: TNumber,
|
|
214
|
+
z: Type.Number() // z: TNumber
|
|
215
|
+
})) // }>>
|
|
216
|
+
|
|
217
|
+
// Check
|
|
218
|
+
|
|
219
|
+
const A = C.Check({ // const A: boolean = true
|
|
220
|
+
x: 1,
|
|
221
|
+
y: 2,
|
|
222
|
+
z: 3
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
// Parse
|
|
226
|
+
|
|
227
|
+
const B = C.Parse({ // const B: {
|
|
228
|
+
x: 1, // x: number,
|
|
229
|
+
y: 2, // y: number,
|
|
230
|
+
z: 3 // z: number
|
|
231
|
+
}) // } = ...
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Contribute
|
|
235
|
+
|
|
233
236
|
TypeBox is open to community contribution. Please ensure you submit an issue before submitting a pull request. The TypeBox project prefers open community discussion before accepting new features.
|