typebox 1.0.7 → 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/typebox.d.mts +1 -1
- package/license +22 -22
- package/package.json +27 -27
- package/readme.md +235 -235
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';
|
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,236 +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() // 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
|
-
[
|
|
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
|
-
|
|
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
|
+
|
|
236
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.
|