typebox 1.0.7 → 1.0.9

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.
@@ -0,0 +1,3 @@
1
+ export declare function IsExactOptional(required: string[], key: string): boolean;
2
+ export declare function InexactOptionalBuild(value: string, key: string): string;
3
+ export declare function InexactOptionalCheck(value: Record<PropertyKey, unknown>, key: string): boolean;
@@ -0,0 +1,20 @@
1
+ import { Settings } from '../../system/settings/index.mjs';
2
+ import { EmitGuard as E, Guard as G } from '../../guard/index.mjs';
3
+ // ------------------------------------------------------------------
4
+ // IsExactOptional
5
+ // ------------------------------------------------------------------
6
+ export function IsExactOptional(required, key) {
7
+ return required.includes(key) || Settings.Get().exactOptionalPropertyTypes;
8
+ }
9
+ // ------------------------------------------------------------------
10
+ // ExactOptionalBuild
11
+ // ------------------------------------------------------------------
12
+ export function InexactOptionalBuild(value, key) {
13
+ return E.IsUndefined(E.Member(value, key));
14
+ }
15
+ // ------------------------------------------------------------------
16
+ // ExactOptionalCheck
17
+ // ------------------------------------------------------------------
18
+ export function InexactOptionalCheck(value, key) {
19
+ return G.IsUndefined(value[key]);
20
+ }
@@ -2,6 +2,7 @@
2
2
  import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
3
3
  import * as S from '../types/index.mjs';
4
4
  import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
5
+ import { InexactOptionalCheck, InexactOptionalBuild, IsExactOptional } from './_exact_optional.mjs';
5
6
  // ------------------------------------------------------------------
6
7
  // Build
7
8
  // ------------------------------------------------------------------
@@ -11,9 +12,35 @@ export function BuildProperties(context, schema, value) {
11
12
  const notKey = E.Not(E.HasPropertyKey(value, E.Constant(key)));
12
13
  const isSchema = BuildSchema(context, schema, E.Member(value, key));
13
14
  const addKey = context.AddKey(E.Constant(key));
14
- // optimization: E.Or(notKey, E.And(isSchema, addKey))
15
15
  const guarded = context.UseUnevaluated() ? E.And(isSchema, addKey) : isSchema;
16
- return !required.includes(key) ? E.Or(notKey, guarded) : guarded;
16
+ // --------------------------------------------------------------
17
+ // Optimization
18
+ //
19
+ // If a key is required, we can skip the `notKey` check since this
20
+ // condition is already enforced by Required. This optimization is
21
+ // only valid when Required is evaluated before Properties.
22
+ //
23
+ // --------------------------------------------------------------
24
+ const isProperty = required.includes(key) ? guarded : E.Or(notKey, guarded);
25
+ // --------------------------------------------------------------
26
+ // ExactOptionalProperties
27
+ //
28
+ // By default, TypeScript allows optional properties to be assigned
29
+ // undefined. This is a bit misleading, since 'optional' is usually
30
+ // understood to mean 'the key may be absent', not 'the key may be
31
+ // present with an undefined value'.
32
+ //
33
+ // The 'IsExactOptional' check returns false by default, matching
34
+ // TypeScript's behavior. When exactOptionalPropertyTypes is enabled
35
+ // in tsconfig.json, TypeBox can be configured to use the stricter
36
+ // semantics via System settings:
37
+ //
38
+ // Settings.Set({ exactOptionalPropertyTypes: true })
39
+ //
40
+ // --------------------------------------------------------------
41
+ return IsExactOptional(required, key)
42
+ ? isProperty
43
+ : E.Or(InexactOptionalBuild(value, key), isProperty);
17
44
  });
18
45
  return E.ReduceAnd(everyKey);
19
46
  }
@@ -21,9 +48,12 @@ export function BuildProperties(context, schema, value) {
21
48
  // Check
22
49
  // ------------------------------------------------------------------
23
50
  export function CheckProperties(context, schema, value) {
51
+ const required = S.IsRequired(schema) ? schema.required : [];
24
52
  const isProperties = G.Every(G.Entries(schema.properties), ([key, schema]) => {
25
- return !G.HasPropertyKey(value, key)
26
- || (CheckSchema(context, schema, value[key]) && context.AddKey(key));
53
+ const isProperty = !G.HasPropertyKey(value, key) || (CheckSchema(context, schema, value[key]) && context.AddKey(key));
54
+ return IsExactOptional(required, key)
55
+ ? isProperty
56
+ : InexactOptionalCheck(value, key) || isProperty;
27
57
  });
28
58
  return isProperties;
29
59
  }
@@ -31,11 +61,15 @@ export function CheckProperties(context, schema, value) {
31
61
  // Error
32
62
  // ------------------------------------------------------------------
33
63
  export function ErrorProperties(context, schemaPath, instancePath, schema, value) {
64
+ const required = S.IsRequired(schema) ? schema.required : [];
34
65
  const isProperties = G.EveryAll(G.Entries(schema.properties), ([key, schema]) => {
35
66
  const nextSchemaPath = `${schemaPath}/properties/${key}`;
36
67
  const nextInstancePath = `${instancePath}/${key}`;
37
- return !G.HasPropertyKey(value, key)
38
- || (ErrorSchema(context, nextSchemaPath, nextInstancePath, schema, value[key]) && context.AddKey(key));
68
+ // Defer error generation for IsExactOptional
69
+ const isProperty = () => (!G.HasPropertyKey(value, key) || (ErrorSchema(context, nextSchemaPath, nextInstancePath, schema, value[key]) && context.AddKey(key)));
70
+ return IsExactOptional(required, key)
71
+ ? isProperty()
72
+ : InexactOptionalCheck(value, key) || isProperty();
39
73
  });
40
74
  return isProperties;
41
75
  }
@@ -23,6 +23,16 @@ export interface TSettings {
23
23
  * @default true
24
24
  */
25
25
  useEval: boolean;
26
+ /**
27
+ * Enables or disables 'exactOptionalPropertyTypes' check semantics. By default, TypeScript
28
+ * allows optional properties to be assigned 'undefined'. While this behavior differs from the
29
+ * common interpretation of 'optional' as meaning 'key may be absent', TypeBox adopts the default
30
+ * TypeScript semantics to remain consistent with the language. This option is provided to align
31
+ * runtime check semantics with projects that configure 'exactOptionalPropertyTypes: true' in
32
+ * tsconfig.json.
33
+ * @default false
34
+ */
35
+ exactOptionalPropertyTypes: boolean;
26
36
  /**
27
37
  * Controls whether internal compositor properties (`~kind`, `~readonly`, `~optional`) are enumerable.
28
38
  * @default false
@@ -4,6 +4,7 @@ const settings = {
4
4
  immutableTypes: false,
5
5
  maxErrors: 8,
6
6
  useEval: true,
7
+ exactOptionalPropertyTypes: false,
7
8
  enumerableKind: false
8
9
  };
9
10
  /** Resets system settings to defaults */
@@ -11,6 +12,7 @@ export function Reset() {
11
12
  settings.immutableTypes = false;
12
13
  settings.maxErrors = 8;
13
14
  settings.useEval = true;
15
+ settings.exactOptionalPropertyTypes = false;
14
16
  settings.enumerableKind = false;
15
17
  }
16
18
  /** Sets system settings */
@@ -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.7",
4
+ "version": "1.0.9",
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
- "./guard": {
28
- "import": "./build/guard/index.mjs"
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
- "./value": {
40
- "import": "./build/value/index.mjs"
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
- "guard": [
58
- "./build/guard/index.d.mts"
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
- "value": [
70
- "./build/value/index.d.mts"
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
- [![npm version](https://badge.fury.io/js/typebox.svg)](https://badge.fury.io/js/typebox)
13
- [![Downloads](https://img.shields.io/npm/dm/typebox.svg)](https://www.npmjs.com/package/typebox)
14
- [![Build](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml/badge.svg)](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml)
15
- [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](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
- [1.0 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
-
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
+ [![npm version](https://badge.fury.io/js/typebox.svg)](https://badge.fury.io/js/typebox)
13
+ [![Downloads](https://img.shields.io/npm/dm/typebox.svg)](https://www.npmjs.com/package/typebox)
14
+ [![Build](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml/badge.svg)](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml)
15
+ [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](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.