typebox 1.0.67 → 1.0.69
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/index.d.mts +2 -6
- package/build/compile/index.mjs +2 -1
- package/build/system/memory/discard.mjs +1 -1
- package/build/system/memory/update.mjs +1 -1
- package/build/type/engine/evaluate/distribute.d.mts +2 -2
- package/build/type/engine/evaluate/distribute.mjs +2 -2
- package/build/type/engine/evaluate/evaluate.d.mts +4 -1
- package/build/type/engine/evaluate/evaluate.mjs +9 -1
- package/build/type/engine/evaluate/narrow.d.mts +2 -2
- package/build/type/engine/evaluate/narrow.mjs +2 -2
- package/build/type/engine/indexed/from-tuple.d.mts +3 -3
- package/build/type/engine/indexed/from-tuple.mjs +3 -3
- package/build/type/engine/keyof/from-object.d.mts +2 -2
- package/build/type/engine/keyof/from-object.mjs +2 -2
- package/build/type/engine/keyof/from-tuple.d.mts +2 -2
- package/build/type/engine/keyof/from-tuple.mjs +2 -2
- package/build/value/create/from-type.mjs +1 -1
- package/package.json +1 -1
- package/readme.md +113 -71
|
@@ -4,9 +4,5 @@ export * from './validator.mjs';
|
|
|
4
4
|
import { Code } from './code.mjs';
|
|
5
5
|
import { Compile } from './compile.mjs';
|
|
6
6
|
import { Validator } from './validator.mjs';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Compile: typeof Compile;
|
|
10
|
-
Validator: typeof Validator;
|
|
11
|
-
};
|
|
12
|
-
export default _default;
|
|
7
|
+
export { Code, Compile, Validator };
|
|
8
|
+
export default Compile;
|
package/build/compile/index.mjs
CHANGED
|
@@ -10,4 +10,5 @@ export * from './validator.mjs';
|
|
|
10
10
|
import { Code } from './code.mjs';
|
|
11
11
|
import { Compile } from './compile.mjs';
|
|
12
12
|
import { Validator } from './validator.mjs';
|
|
13
|
-
export
|
|
13
|
+
export { Code, Compile, Validator };
|
|
14
|
+
export default Compile;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Settings } from '../settings/index.mjs';
|
|
3
|
-
import { Metrics } from
|
|
3
|
+
import { Metrics } from './metrics.mjs';
|
|
4
4
|
import { Clone } from './clone.mjs';
|
|
5
5
|
/**
|
|
6
6
|
* Updates a value with new properties while preserving property enumerability. Use this function to modify
|
|
@@ -5,8 +5,8 @@ import { type TObject } from '../../types/object.mjs';
|
|
|
5
5
|
import { type TTuple } from '../../types/tuple.mjs';
|
|
6
6
|
import { type TComposite } from './composite.mjs';
|
|
7
7
|
import { type TNarrow } from './narrow.mjs';
|
|
8
|
-
import { type TEvaluateType } from
|
|
9
|
-
import { type TEvaluateIntersect } from
|
|
8
|
+
import { type TEvaluateType } from './evaluate.mjs';
|
|
9
|
+
import { type TEvaluateIntersect } from './evaluate.mjs';
|
|
10
10
|
type TCanDistribute<Type extends TSchema> = Type extends TObject | TTuple ? true : false;
|
|
11
11
|
type TDistributeNormalize<Type extends TSchema> = Type extends TIntersect<infer Types extends TSchema[]> ? TEvaluateIntersect<Types> : Type;
|
|
12
12
|
type TDistributeOperation<Left extends TSchema, Right extends TSchema, NormalLeft extends TSchema = TDistributeNormalize<Left>, NormalRight extends TSchema = TDistributeNormalize<Right>, IsObjectLeft extends boolean = TCanDistribute<NormalLeft>, IsObjectRight extends boolean = TCanDistribute<NormalRight>, Result extends TSchema = ([
|
|
@@ -8,8 +8,8 @@ import { IsObject } from '../../types/object.mjs';
|
|
|
8
8
|
import { IsTuple } from '../../types/tuple.mjs';
|
|
9
9
|
import { Composite } from './composite.mjs';
|
|
10
10
|
import { Narrow } from './narrow.mjs';
|
|
11
|
-
import { EvaluateType } from
|
|
12
|
-
import { EvaluateIntersect } from
|
|
11
|
+
import { EvaluateType } from './evaluate.mjs';
|
|
12
|
+
import { EvaluateIntersect } from './evaluate.mjs';
|
|
13
13
|
function CanDistribute(type) {
|
|
14
14
|
return IsObject(type) || IsTuple(type);
|
|
15
15
|
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { type TSchema } from '../../types/schema.mjs';
|
|
2
2
|
import { type TIntersect } from '../../types/intersect.mjs';
|
|
3
|
-
import { type TUnion } from '../../types/union.mjs';
|
|
4
3
|
import { type TDistribute } from './distribute.mjs';
|
|
5
4
|
import { type TBroaden } from './broaden.mjs';
|
|
5
|
+
import { type TUnion } from '../../types/union.mjs';
|
|
6
|
+
import { type TNever } from '../../types/never.mjs';
|
|
6
7
|
export type TEvaluateIntersect<Types extends TSchema[], Distribution extends TSchema[] = TDistribute<Types>, Result extends TSchema = TBroaden<Distribution>> = Result;
|
|
7
8
|
export declare function EvaluateIntersect<Types extends TSchema[]>(types: [...Types]): TEvaluateIntersect<Types>;
|
|
8
9
|
export type TEvaluateUnion<Types extends TSchema[], Result extends TSchema = TBroaden<Types>> = Result;
|
|
9
10
|
export declare function EvaluateUnion<Types extends TSchema[]>(types: [...Types]): TEvaluateUnion<Types>;
|
|
10
11
|
export type TEvaluateType<Type extends TSchema, Result extends TSchema = (Type extends TIntersect<infer Types extends TSchema[]> ? TEvaluateIntersect<Types> : Type extends TUnion<infer Types extends TSchema[]> ? TEvaluateUnion<Types> : Type)> = Result;
|
|
11
12
|
export declare function EvaluateType<Type extends TSchema>(type: Type): TEvaluateType<Type>;
|
|
13
|
+
export type TEvaluateUnionFast<Types extends TSchema[], Result extends TSchema = (Types extends [infer Type extends TSchema] ? Type : Types extends [] ? TNever : TUnion<Types>)> = Result;
|
|
14
|
+
export declare function EvaluateUnionFast<Types extends TSchema[]>(types: [...Types]): TEvaluateUnionFast<Types>;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
2
3
|
import { IsIntersect } from '../../types/intersect.mjs';
|
|
3
|
-
import { IsUnion } from '../../types/union.mjs';
|
|
4
4
|
import { Distribute } from './distribute.mjs';
|
|
5
5
|
import { Broaden } from './broaden.mjs';
|
|
6
|
+
import { Union, IsUnion } from '../../types/union.mjs';
|
|
7
|
+
import { Never } from '../../types/never.mjs';
|
|
6
8
|
export function EvaluateIntersect(types) {
|
|
7
9
|
const distribution = Distribute(types);
|
|
8
10
|
const result = Broaden(distribution);
|
|
@@ -17,3 +19,9 @@ export function EvaluateType(type) {
|
|
|
17
19
|
IsUnion(type) ? EvaluateUnion(type.anyOf) :
|
|
18
20
|
type);
|
|
19
21
|
}
|
|
22
|
+
export function EvaluateUnionFast(types) {
|
|
23
|
+
const result = (Guard.IsEqual(types.length, 1) ? types[0] :
|
|
24
|
+
Guard.IsEqual(types.length, 0) ? Never() :
|
|
25
|
+
Union(types));
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TSchema } from '../../types/schema.mjs';
|
|
2
|
-
import { TNever } from
|
|
3
|
-
import { TCompare, TCompareResult, ResultLeftInside, ResultRightInside, ResultEqual } from
|
|
2
|
+
import { TNever } from '../../types/never.mjs';
|
|
3
|
+
import { TCompare, TCompareResult, ResultLeftInside, ResultRightInside, ResultEqual } from './compare.mjs';
|
|
4
4
|
export type TNarrow<Left extends TSchema, Right extends TSchema, Result extends TCompareResult = TCompare<Left, Right>> = (Result extends typeof ResultLeftInside ? Left : Result extends typeof ResultRightInside ? Right : Result extends typeof ResultEqual ? Right : TNever);
|
|
5
5
|
export declare function Narrow<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TNarrow<Left, Right>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
-
import { Never } from
|
|
4
|
-
import { Compare, ResultLeftInside, ResultRightInside, ResultEqual } from
|
|
3
|
+
import { Never } from '../../types/never.mjs';
|
|
4
|
+
import { Compare, ResultLeftInside, ResultRightInside, ResultEqual } from './compare.mjs';
|
|
5
5
|
export function Narrow(left, right) {
|
|
6
6
|
const result = Compare(left, right);
|
|
7
7
|
return (Guard.IsEqual(result, ResultLeftInside) ? left :
|
|
@@ -2,12 +2,12 @@ import { type TSchema } from '../../types/schema.mjs';
|
|
|
2
2
|
import { type TLiteral } from '../../types/literal.mjs';
|
|
3
3
|
import { type TNumber } from '../../types/number.mjs';
|
|
4
4
|
import { type TInteger } from '../../types/integer.mjs';
|
|
5
|
-
import { type
|
|
5
|
+
import { type TEvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
6
6
|
import { type TExtends, ExtendsResult } from '../../extends/index.mjs';
|
|
7
7
|
import { type TFormatArrayIndexer } from './array-indexer.mjs';
|
|
8
8
|
type TIndexElementsWithIndexer<Types extends TSchema[], Indexer extends TSchema, Result extends TSchema[] = []> = (Types extends [...infer Left extends TSchema[], infer Right extends TSchema] ? TExtends<{}, TLiteral<Left['length']>, Indexer> extends ExtendsResult.TExtendsTrueLike ? TIndexElementsWithIndexer<Left, Indexer, [Right, ...Result]> : TIndexElementsWithIndexer<Left, Indexer, Result> : Result);
|
|
9
|
-
type TFromTupleWithIndexer<Types extends TSchema[], Indexer extends TSchema, ArrayIndexer extends TSchema = TFormatArrayIndexer<Indexer>, Elements extends TSchema[] = TIndexElementsWithIndexer<Types, ArrayIndexer>, Result extends TSchema =
|
|
10
|
-
type TFromTupleWithoutIndexer<Types extends TSchema[], Result extends TSchema =
|
|
9
|
+
type TFromTupleWithIndexer<Types extends TSchema[], Indexer extends TSchema, ArrayIndexer extends TSchema = TFormatArrayIndexer<Indexer>, Elements extends TSchema[] = TIndexElementsWithIndexer<Types, ArrayIndexer>, Result extends TSchema = TEvaluateUnionFast<Elements>> = Result;
|
|
10
|
+
type TFromTupleWithoutIndexer<Types extends TSchema[], Result extends TSchema = TEvaluateUnionFast<Types>> = Result;
|
|
11
11
|
export type TFromTuple<Types extends TSchema[], Indexer extends TSchema, Result extends TSchema = (Indexer extends TNumber | TInteger ? TFromTupleWithoutIndexer<Types> : TFromTupleWithIndexer<Types, Indexer>)> = Result;
|
|
12
12
|
export declare function FromTuple<Types extends TSchema[], Indexer extends TSchema>(types: [...Types], indexer: Indexer): TFromTuple<Types, Indexer>;
|
|
13
13
|
export {};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Literal } from '../../types/literal.mjs';
|
|
3
3
|
import { IsNumber } from '../../types/number.mjs';
|
|
4
4
|
import { IsInteger } from '../../types/integer.mjs';
|
|
5
|
-
import {
|
|
5
|
+
import { EvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
6
6
|
import { Extends, ExtendsResult } from '../../extends/index.mjs';
|
|
7
7
|
import { FormatArrayIndexer } from './array-indexer.mjs';
|
|
8
8
|
function IndexElementsWithIndexer(types, indexer) {
|
|
@@ -16,10 +16,10 @@ function IndexElementsWithIndexer(types, indexer) {
|
|
|
16
16
|
function FromTupleWithIndexer(types, indexer) {
|
|
17
17
|
const formattedArrayIndexer = FormatArrayIndexer(indexer);
|
|
18
18
|
const elements = IndexElementsWithIndexer(types, formattedArrayIndexer);
|
|
19
|
-
return
|
|
19
|
+
return EvaluateUnionFast(elements);
|
|
20
20
|
}
|
|
21
21
|
function FromTupleWithoutIndexer(types) {
|
|
22
|
-
return
|
|
22
|
+
return EvaluateUnionFast(types);
|
|
23
23
|
}
|
|
24
24
|
export function FromTuple(types, indexer) {
|
|
25
25
|
return (IsNumber(indexer) || IsInteger(indexer)
|
|
@@ -3,8 +3,8 @@ import { type TSchema } from '../../types/index.mjs';
|
|
|
3
3
|
import { type TUnionToTuple } from '../helpers/index.mjs';
|
|
4
4
|
import { type TProperties } from '../../types/properties.mjs';
|
|
5
5
|
import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
|
|
6
|
-
import { type
|
|
6
|
+
import { type TEvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
7
7
|
type TFromPropertyKeys<Keys extends PropertyKey[], Result extends TSchema[] = []> = (Keys extends [infer Left extends PropertyKey, ...infer Right extends PropertyKey[]] ? Left extends TLiteralValue ? TFromPropertyKeys<Right, [...Result, TLiteral<Left>]> : TUnreachable : Result);
|
|
8
|
-
export type TFromObject<Properties extends TProperties, PropertyKeys extends PropertyKey[] = TUnionToTuple<keyof Properties>, Variants extends TSchema[] = TFromPropertyKeys<PropertyKeys>, Result extends TSchema =
|
|
8
|
+
export type TFromObject<Properties extends TProperties, PropertyKeys extends PropertyKey[] = TUnionToTuple<keyof Properties>, Variants extends TSchema[] = TFromPropertyKeys<PropertyKeys>, Result extends TSchema = TEvaluateUnionFast<Variants>> = Result;
|
|
9
9
|
export declare function FromObject<Properties extends TProperties>(properties: Properties): TFromObject<Properties>;
|
|
10
10
|
export {};
|
|
@@ -3,7 +3,7 @@ import { Unreachable } from '../../../system/unreachable/index.mjs';
|
|
|
3
3
|
import { Guard } from '../../../guard/index.mjs';
|
|
4
4
|
import { Literal, IsLiteralValue } from '../../types/literal.mjs';
|
|
5
5
|
import { ConvertToIntegerKey } from '../helpers/keys.mjs';
|
|
6
|
-
import {
|
|
6
|
+
import { EvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
7
7
|
function FromPropertyKeys(keys) {
|
|
8
8
|
const result = keys.reduce((result, left) => {
|
|
9
9
|
return IsLiteralValue(left)
|
|
@@ -15,6 +15,6 @@ function FromPropertyKeys(keys) {
|
|
|
15
15
|
export function FromObject(properties) {
|
|
16
16
|
const propertyKeys = Guard.Keys(properties);
|
|
17
17
|
const variants = FromPropertyKeys(propertyKeys);
|
|
18
|
-
const result =
|
|
18
|
+
const result = EvaluateUnionFast(variants);
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type TSchema } from '../../types/index.mjs';
|
|
2
2
|
import { type TLiteral } from '../../types/literal.mjs';
|
|
3
|
-
import { type
|
|
4
|
-
export type TFromTuple<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [...infer Left extends TSchema[], infer _ extends TSchema] ? TFromTuple<Left, [TLiteral<Left['length']>, ...Result]> :
|
|
3
|
+
import { type TEvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
4
|
+
export type TFromTuple<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [...infer Left extends TSchema[], infer _ extends TSchema] ? TFromTuple<Left, [TLiteral<Left['length']>, ...Result]> : TEvaluateUnionFast<Result>);
|
|
5
5
|
export declare function FromTuple<Types extends TSchema[]>(types: [...Types]): TFromTuple<Types>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Literal } from '../../types/literal.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { EvaluateUnionFast } from '../evaluate/evaluate.mjs';
|
|
4
4
|
export function FromTuple(types) {
|
|
5
5
|
const result = types.map((_, index) => Literal(index));
|
|
6
|
-
return
|
|
6
|
+
return EvaluateUnionFast(result);
|
|
7
7
|
}
|
|
@@ -14,7 +14,7 @@ import { FromFunction } from './from-function.mjs';
|
|
|
14
14
|
import { FromInteger } from './from-integer.mjs';
|
|
15
15
|
import { FromIntersect } from './from-intersect.mjs';
|
|
16
16
|
import { FromIterator } from './from-iterator.mjs';
|
|
17
|
-
import { FromLiteral } from
|
|
17
|
+
import { FromLiteral } from './from-literal.mjs';
|
|
18
18
|
import { FromNever } from './from-never.mjs';
|
|
19
19
|
import { FromNull } from './from-null.mjs';
|
|
20
20
|
import { FromNumber } from './from-number.mjs';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1>TypeBox</h1>
|
|
4
4
|
|
|
5
|
-
<p>
|
|
5
|
+
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
6
|
|
|
7
7
|
<img src="typebox.png" />
|
|
8
8
|
|
|
@@ -25,12 +25,12 @@ $ npm install typebox
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
-
A TypeScript engine for
|
|
28
|
+
A TypeScript first validation engine for JSON Schema [Example](https://tsplay.dev/mZMOeN)
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
31
|
import Type from 'typebox'
|
|
32
32
|
|
|
33
|
-
//
|
|
33
|
+
// JSON Schema
|
|
34
34
|
|
|
35
35
|
const T = Type.Object({ // const T = {
|
|
36
36
|
x: Type.Number(), // type: 'object',
|
|
@@ -51,49 +51,39 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
51
51
|
// TypeScript
|
|
52
52
|
|
|
53
53
|
const { S } = Type.Script({ T }, `
|
|
54
|
-
type RenameKey<K> =
|
|
55
|
-
K extends 'x' ? 'a' :
|
|
56
|
-
K extends 'y' ? 'b' :
|
|
57
|
-
K extends 'z' ? 'c' :
|
|
58
|
-
K
|
|
59
|
-
|
|
60
54
|
type S = {
|
|
61
|
-
readonly [K in keyof T as
|
|
55
|
+
readonly [K in keyof T as Uppercase<K>]: string
|
|
62
56
|
}
|
|
63
57
|
`)
|
|
64
58
|
|
|
65
|
-
type S = Type.Static<typeof S>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
type S = Type.Static<typeof S> // type S = {
|
|
60
|
+
// readonly X: string,
|
|
61
|
+
// readonly Y: string,
|
|
62
|
+
// readonly Z: string
|
|
63
|
+
// }
|
|
70
64
|
```
|
|
71
65
|
|
|
72
66
|
## Overview
|
|
73
67
|
|
|
74
68
|
[Documentation](https://sinclairzx81.github.io/typebox/)
|
|
75
69
|
|
|
76
|
-
TypeBox is a runtime type system that creates in-memory
|
|
70
|
+
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.
|
|
77
71
|
|
|
78
|
-
This library is designed to allow
|
|
72
|
+
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.
|
|
79
73
|
|
|
80
74
|
License: MIT
|
|
81
75
|
|
|
82
76
|
## Contents
|
|
83
77
|
|
|
84
|
-
|
|
78
|
+
|
|
85
79
|
- [Type](#Type)
|
|
86
80
|
- [Value](#Value)
|
|
87
|
-
- [Compile](#Compile)
|
|
88
81
|
- [Script](#Script)
|
|
82
|
+
- [Compile](#Compile)
|
|
89
83
|
- [Schema](#Schema)
|
|
84
|
+
- [Legacy](#Legacy)
|
|
90
85
|
- [Contribute](#Contribute)
|
|
91
86
|
|
|
92
|
-
## Upgrade
|
|
93
|
-
|
|
94
|
-
If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the following URL.
|
|
95
|
-
|
|
96
|
-
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
97
87
|
|
|
98
88
|
<a name="Type"></a>
|
|
99
89
|
|
|
@@ -101,11 +91,11 @@ If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the fo
|
|
|
101
91
|
|
|
102
92
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://tsplay.dev/NaMoBN)
|
|
103
93
|
|
|
104
|
-
TypeBox provides many functions to create
|
|
94
|
+
TypeBox provides many functions to create JSON Schema types. Each function returns a small JSON Schema fragment that can be composed into more complex types. TypeBox includes a set of functions that are used to construct JSON Schema compliant schematics as well as a set of extended functions that return schematics for constructs native to JavaScript.
|
|
105
95
|
|
|
106
96
|
## Example
|
|
107
97
|
|
|
108
|
-
The following creates a
|
|
98
|
+
The following creates a JSON Schema type and infers with Static.
|
|
109
99
|
|
|
110
100
|
```typescript
|
|
111
101
|
import Type from 'typebox'
|
|
@@ -130,17 +120,16 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
130
120
|
Schema options can be passed on the last argument of any given type.
|
|
131
121
|
|
|
132
122
|
```typescript
|
|
133
|
-
const T = Type.String({
|
|
134
|
-
format: 'email'
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const S = Type.Number({ // const S = {
|
|
139
|
-
minimum: 0, // type: 'number',
|
|
140
|
-
maximum: 100 // minimum: 0,
|
|
141
|
-
}) // maximum: 100
|
|
142
|
-
// }
|
|
123
|
+
const T = Type.String({ // const T = {
|
|
124
|
+
format: 'email' // type: 'string',
|
|
125
|
+
}) // format: 'email'
|
|
126
|
+
// }
|
|
143
127
|
|
|
128
|
+
const S = Type.Number({ // const S = {
|
|
129
|
+
minimum: 0, // type: 'number',
|
|
130
|
+
maximum: 100 // minimum: 0,
|
|
131
|
+
}) // maximum: 100
|
|
132
|
+
// }
|
|
144
133
|
```
|
|
145
134
|
|
|
146
135
|
<a name="Value"></a>
|
|
@@ -173,42 +162,11 @@ const A = Value.Parse(T, { // const A: {
|
|
|
173
162
|
}) // } = ...
|
|
174
163
|
```
|
|
175
164
|
|
|
176
|
-
|
|
177
|
-
<a name="Compile"></a>
|
|
178
|
-
|
|
179
|
-
## Compile
|
|
180
|
-
|
|
181
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
|
|
182
|
-
|
|
183
|
-
The Compile submodule is a high-performance Json Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
import { Compile } from 'typebox/compile'
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### Example
|
|
190
|
-
|
|
191
|
-
The following uses the compiler to Compile and Parse a value.
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
195
|
-
x: Type.Number(), // x: TNumber,
|
|
196
|
-
y: Type.Number(), // y: TNumber,
|
|
197
|
-
z: Type.Number() // z: TNumber
|
|
198
|
-
})) // }>>
|
|
199
|
-
|
|
200
|
-
const A = C.Parse({ // const A: {
|
|
201
|
-
x: 0, // x: number,
|
|
202
|
-
y: 1, // y: number,
|
|
203
|
-
z: 0 // z: number
|
|
204
|
-
}) // } = ...
|
|
205
|
-
```
|
|
206
|
-
|
|
207
165
|
## Script
|
|
208
166
|
|
|
209
167
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://tsplay.dev/Wk6L1m) | [Example 2](https://tsplay.dev/NnrJoN)
|
|
210
168
|
|
|
211
|
-
TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute
|
|
169
|
+
TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute JSON Schema using native TypeScript syntax. The engine is implemented symmetrically at runtime and within the TypeScript type system, and is intended for use with the TypeScript 7 native compiler and above.
|
|
212
170
|
|
|
213
171
|
```typescript
|
|
214
172
|
// Scripted Type
|
|
@@ -223,7 +181,7 @@ const T = Type.Script(`{
|
|
|
223
181
|
// z: TBoolean
|
|
224
182
|
// }>
|
|
225
183
|
|
|
226
|
-
//
|
|
184
|
+
// JSON Schema Introspection
|
|
227
185
|
|
|
228
186
|
T.type // 'object'
|
|
229
187
|
T.required // ['x', 'y', 'z']
|
|
@@ -248,17 +206,47 @@ type S = Type.Static<typeof S> // type S = {
|
|
|
248
206
|
// }
|
|
249
207
|
```
|
|
250
208
|
|
|
209
|
+
<a name="Compile"></a>
|
|
210
|
+
|
|
211
|
+
## Compile
|
|
212
|
+
|
|
213
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
|
|
214
|
+
|
|
215
|
+
The Compile submodule is a high-performance JSON Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import Compile from 'typebox/compile'
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Example
|
|
222
|
+
|
|
223
|
+
The following uses the compiler to Compile and Parse a value.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
227
|
+
x: Type.Number(), // x: TNumber,
|
|
228
|
+
y: Type.Number(), // y: TNumber,
|
|
229
|
+
z: Type.Number() // z: TNumber
|
|
230
|
+
})) // }>>
|
|
231
|
+
|
|
232
|
+
const A = C.Parse({ // const A: {
|
|
233
|
+
x: 0, // x: number,
|
|
234
|
+
y: 1, // y: number,
|
|
235
|
+
z: 0 // z: number
|
|
236
|
+
}) // } = ...
|
|
237
|
+
```
|
|
238
|
+
|
|
251
239
|
<a name="Schema"></a>
|
|
252
240
|
|
|
253
241
|
## Schema
|
|
254
242
|
|
|
255
243
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://tsplay.dev/Wvrv3W) | [Example 2](https://tsplay.dev/m3g0ym)
|
|
256
244
|
|
|
257
|
-
TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of
|
|
245
|
+
TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of JSON Schema schematics. TypeBox implements Draft 3 to 2020-12 and is compliance tested via the official JSON Schema [Test Suite](https://github.com/JSON-schema-org/JSON-Schema-Test-Suite). It offers high-performance JIT compilation with automatic fallback to dynamic checking in JIT restricted environments.
|
|
258
246
|
|
|
259
247
|
### Example
|
|
260
248
|
|
|
261
|
-
The following compiles
|
|
249
|
+
The following compiles JSON Schema. Type inference is supported.
|
|
262
250
|
|
|
263
251
|
```typescript
|
|
264
252
|
const C = Compile({
|
|
@@ -278,6 +266,60 @@ const A = C.Parse({ // const A: {
|
|
|
278
266
|
}) // } = ...
|
|
279
267
|
```
|
|
280
268
|
|
|
269
|
+
## Legacy
|
|
270
|
+
|
|
271
|
+
If upgrading from `@sinclair/typebox` 0.34.x refer to the 1.0 migration guide at the following URL.
|
|
272
|
+
|
|
273
|
+
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
274
|
+
|
|
275
|
+
Most types created with 0.34.x are compatible with V1, and it is possible to run both `typebox` and `@sinclair/typebox` packages side by side.
|
|
276
|
+
|
|
277
|
+
[Compatibility](https://tsplay.dev/Wzr2rW)
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { Type } from '@sinclair/typebox' // TB: 0.34.x
|
|
281
|
+
import { Static } from 'typebox' // TB: 1.0.0
|
|
282
|
+
|
|
283
|
+
// Legacy Types
|
|
284
|
+
|
|
285
|
+
const A = Type.Object({
|
|
286
|
+
x: Type.Number(),
|
|
287
|
+
y: Type.Number(),
|
|
288
|
+
z: Type.Number()
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const B = Type.Object({
|
|
292
|
+
a: Type.Number(),
|
|
293
|
+
b: Type.Number(),
|
|
294
|
+
c: Type.Number()
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
const C = Type.Composite([A, B])
|
|
298
|
+
|
|
299
|
+
// Modern Inference
|
|
300
|
+
|
|
301
|
+
type C = Static<typeof C> // type C = {
|
|
302
|
+
// x: number;
|
|
303
|
+
// y: number;
|
|
304
|
+
// z: number;
|
|
305
|
+
// a: number;
|
|
306
|
+
// b: number;
|
|
307
|
+
// c: number;
|
|
308
|
+
// }
|
|
309
|
+
|
|
310
|
+
// Modern Compile
|
|
311
|
+
|
|
312
|
+
import Compile from 'typebox/compile'
|
|
313
|
+
|
|
314
|
+
const Result = Compile(C).Check({ ... })
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Revision 0.34.x is actively maintained at the following URL.
|
|
318
|
+
|
|
319
|
+
[TypeBox 0.34.x](https://github.com/sinclairzx81/typebox-legacy)
|
|
320
|
+
|
|
321
|
+
Please submit non-1.0 issues to this repository.
|
|
322
|
+
|
|
281
323
|
## Contribute
|
|
282
324
|
|
|
283
325
|
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.
|