@sinclair/typebox 0.32.0-dev-19 → 0.32.0-dev-21
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/import/compiler/compiler.mjs +62 -62
- package/build/import/errors/errors.mjs +62 -62
- package/build/import/index.d.mts +6 -6
- package/build/import/index.mjs +3 -3
- package/build/import/type/indexed/indexed-property-keys.d.mts +2 -2
- package/build/import/type/indexed/indexed-property-keys.mjs +6 -11
- package/build/import/type/intrinsic/intrinsic.mjs +3 -3
- package/build/import/type/optional/index.d.mts +1 -0
- package/build/import/type/optional/index.mjs +1 -0
- package/build/import/type/optional/optional-from-mapped-result.d.mts +12 -0
- package/build/import/type/optional/optional-from-mapped-result.mjs +17 -0
- package/build/import/type/optional/optional.d.mts +19 -2
- package/build/import/type/optional/optional.mjs +18 -2
- package/build/import/type/readonly/index.d.mts +1 -0
- package/build/import/type/readonly/index.mjs +1 -0
- package/build/import/type/readonly/readonly-from-mapped-result.d.mts +12 -0
- package/build/import/type/readonly/readonly-from-mapped-result.mjs +17 -0
- package/build/import/type/readonly/readonly.d.mts +18 -1
- package/build/import/type/readonly/readonly.mjs +18 -2
- package/build/import/type/record/record.d.mts +2 -2
- package/build/import/type/record/record.mjs +2 -3
- package/build/import/type/schema/schema.d.mts +1 -1
- package/build/import/type/symbol/symbol.d.mts +1 -1
- package/build/import/type/template-literal/finite.d.mts +7 -5
- package/build/import/type/template-literal/finite.mjs +14 -5
- package/build/import/type/template-literal/generate.d.mts +14 -8
- package/build/import/type/template-literal/generate.mjs +19 -9
- package/build/import/type/template-literal/index.d.mts +1 -1
- package/build/import/type/template-literal/index.mjs +1 -1
- package/build/import/type/template-literal/{parser.mjs → parse.mjs} +7 -2
- package/build/import/type/template-literal/syntax.d.mts +1 -1
- package/build/import/type/template-literal/syntax.mjs +0 -1
- package/build/import/type/template-literal/template-literal.d.mts +2 -2
- package/build/import/type/template-literal/template-literal.mjs +1 -1
- package/build/import/type/template-literal/union.d.mts +2 -2
- package/build/import/type/template-literal/union.mjs +5 -10
- package/build/import/type/type/json.d.mts +19 -7
- package/build/import/type/type/json.mjs +5 -5
- package/build/import/value/cast/cast.mjs +20 -20
- package/build/import/value/check/check.mjs +62 -62
- package/build/import/value/clean/clean.mjs +16 -16
- package/build/import/value/convert/convert.mjs +36 -36
- package/build/import/value/create/create.mjs +66 -67
- package/build/import/value/default/default.mjs +16 -16
- package/build/import/value/transform/decode.mjs +18 -18
- package/build/import/value/transform/encode.mjs +18 -18
- package/build/import/value/transform/has.mjs +28 -28
- package/build/require/compiler/compiler.js +62 -62
- package/build/require/errors/errors.js +62 -62
- package/build/require/index.d.ts +6 -6
- package/build/require/index.js +6 -2
- package/build/require/type/indexed/indexed-property-keys.d.ts +2 -2
- package/build/require/type/indexed/indexed-property-keys.js +4 -9
- package/build/require/type/intrinsic/intrinsic.js +2 -2
- package/build/require/type/optional/index.d.ts +1 -0
- package/build/require/type/optional/index.js +1 -0
- package/build/require/type/optional/optional-from-mapped-result.d.ts +12 -0
- package/build/require/type/optional/optional-from-mapped-result.js +22 -0
- package/build/require/type/optional/optional.d.ts +19 -2
- package/build/require/type/optional/optional.js +18 -2
- package/build/require/type/readonly/index.d.ts +1 -0
- package/build/require/type/readonly/index.js +1 -0
- package/build/require/type/readonly/readonly-from-mapped-result.d.ts +12 -0
- package/build/require/type/readonly/readonly-from-mapped-result.js +22 -0
- package/build/require/type/readonly/readonly.d.ts +18 -1
- package/build/require/type/readonly/readonly.js +18 -2
- package/build/require/type/record/record.d.ts +2 -2
- package/build/require/type/record/record.js +1 -2
- package/build/require/type/schema/schema.d.ts +1 -1
- package/build/require/type/symbol/symbol.d.ts +1 -1
- package/build/require/type/template-literal/finite.d.ts +7 -5
- package/build/require/type/template-literal/finite.js +16 -6
- package/build/require/type/template-literal/generate.d.ts +14 -8
- package/build/require/type/template-literal/generate.js +22 -11
- package/build/require/type/template-literal/index.d.ts +1 -1
- package/build/require/type/template-literal/index.js +1 -1
- package/build/require/type/template-literal/{parser.js → parse.js} +7 -2
- package/build/require/type/template-literal/syntax.d.ts +1 -1
- package/build/require/type/template-literal/syntax.js +0 -1
- package/build/require/type/template-literal/template-literal.d.ts +2 -2
- package/build/require/type/template-literal/template-literal.js +1 -1
- package/build/require/type/template-literal/union.d.ts +2 -2
- package/build/require/type/template-literal/union.js +5 -10
- package/build/require/type/type/json.d.ts +19 -7
- package/build/require/type/type/json.js +5 -5
- package/build/require/value/cast/cast.js +20 -20
- package/build/require/value/check/check.js +62 -62
- package/build/require/value/clean/clean.js +16 -16
- package/build/require/value/convert/convert.js +36 -36
- package/build/require/value/create/create.js +65 -66
- package/build/require/value/default/default.js +16 -16
- package/build/require/value/transform/decode.js +18 -18
- package/build/require/value/transform/encode.js +18 -18
- package/build/require/value/transform/has.js +28 -28
- package/package.json +1 -1
- package/readme.md +52 -52
- /package/build/import/type/template-literal/{parser.d.mts → parse.d.mts} +0 -0
- /package/build/require/type/template-literal/{parser.d.ts → parse.d.ts} +0 -0
|
@@ -13,67 +13,67 @@ const type_1 = require("../../type/guard/type");
|
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
const index_3 = require("../guard/index");
|
|
15
15
|
// prettier-ignore
|
|
16
|
-
function
|
|
16
|
+
function FromArray(schema, references) {
|
|
17
17
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
18
18
|
}
|
|
19
19
|
// prettier-ignore
|
|
20
|
-
function
|
|
20
|
+
function FromAsyncIterator(schema, references) {
|
|
21
21
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
22
22
|
}
|
|
23
23
|
// prettier-ignore
|
|
24
|
-
function
|
|
24
|
+
function FromConstructor(schema, references) {
|
|
25
25
|
return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
26
26
|
}
|
|
27
27
|
// prettier-ignore
|
|
28
|
-
function
|
|
28
|
+
function FromFunction(schema, references) {
|
|
29
29
|
return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
30
30
|
}
|
|
31
31
|
// prettier-ignore
|
|
32
|
-
function
|
|
32
|
+
function FromIntersect(schema, references) {
|
|
33
33
|
return (0, type_1.IsTransform)(schema) || (0, type_1.IsTransform)(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
|
|
34
34
|
}
|
|
35
35
|
// prettier-ignore
|
|
36
|
-
function
|
|
36
|
+
function FromIterator(schema, references) {
|
|
37
37
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
38
38
|
}
|
|
39
39
|
// prettier-ignore
|
|
40
|
-
function
|
|
40
|
+
function FromNot(schema, references) {
|
|
41
41
|
return (0, type_1.IsTransform)(schema) || Visit(schema.not, references);
|
|
42
42
|
}
|
|
43
43
|
// prettier-ignore
|
|
44
|
-
function
|
|
44
|
+
function FromObject(schema, references) {
|
|
45
45
|
return ((0, type_1.IsTransform)(schema) ||
|
|
46
46
|
Object.values(schema.properties).some((schema) => Visit(schema, references)) ||
|
|
47
47
|
((0, type_1.IsSchema)(schema.additionalProperties) && Visit(schema.additionalProperties, references)));
|
|
48
48
|
}
|
|
49
49
|
// prettier-ignore
|
|
50
|
-
function
|
|
50
|
+
function FromPromise(schema, references) {
|
|
51
51
|
return (0, type_1.IsTransform)(schema) || Visit(schema.item, references);
|
|
52
52
|
}
|
|
53
53
|
// prettier-ignore
|
|
54
|
-
function
|
|
54
|
+
function FromRecord(schema, references) {
|
|
55
55
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
56
56
|
const property = schema.patternProperties[pattern];
|
|
57
57
|
return (0, type_1.IsTransform)(schema) || Visit(property, references) || ((0, type_1.IsSchema)(schema.additionalProperties) && (0, type_1.IsTransform)(schema.additionalProperties));
|
|
58
58
|
}
|
|
59
59
|
// prettier-ignore
|
|
60
|
-
function
|
|
60
|
+
function FromRef(schema, references) {
|
|
61
61
|
if ((0, type_1.IsTransform)(schema))
|
|
62
62
|
return true;
|
|
63
63
|
return Visit((0, index_1.Deref)(schema, references), references);
|
|
64
64
|
}
|
|
65
65
|
// prettier-ignore
|
|
66
|
-
function
|
|
66
|
+
function FromThis(schema, references) {
|
|
67
67
|
if ((0, type_1.IsTransform)(schema))
|
|
68
68
|
return true;
|
|
69
69
|
return Visit((0, index_1.Deref)(schema, references), references);
|
|
70
70
|
}
|
|
71
71
|
// prettier-ignore
|
|
72
|
-
function
|
|
72
|
+
function FromTuple(schema, references) {
|
|
73
73
|
return (0, type_1.IsTransform)(schema) || (!(0, index_3.IsUndefined)(schema.items) && schema.items.some((schema) => Visit(schema, references)));
|
|
74
74
|
}
|
|
75
75
|
// prettier-ignore
|
|
76
|
-
function
|
|
76
|
+
function FromUnion(schema, references) {
|
|
77
77
|
return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
78
78
|
}
|
|
79
79
|
// prettier-ignore
|
|
@@ -86,33 +86,33 @@ function Visit(schema, references) {
|
|
|
86
86
|
visited.add(schema.$id);
|
|
87
87
|
switch (schema[index_2.Kind]) {
|
|
88
88
|
case 'Array':
|
|
89
|
-
return
|
|
89
|
+
return FromArray(schema_, references_);
|
|
90
90
|
case 'AsyncIterator':
|
|
91
|
-
return
|
|
91
|
+
return FromAsyncIterator(schema_, references_);
|
|
92
92
|
case 'Constructor':
|
|
93
|
-
return
|
|
93
|
+
return FromConstructor(schema_, references_);
|
|
94
94
|
case 'Function':
|
|
95
|
-
return
|
|
95
|
+
return FromFunction(schema_, references_);
|
|
96
96
|
case 'Intersect':
|
|
97
|
-
return
|
|
97
|
+
return FromIntersect(schema_, references_);
|
|
98
98
|
case 'Iterator':
|
|
99
|
-
return
|
|
99
|
+
return FromIterator(schema_, references_);
|
|
100
100
|
case 'Not':
|
|
101
|
-
return
|
|
101
|
+
return FromNot(schema_, references_);
|
|
102
102
|
case 'Object':
|
|
103
|
-
return
|
|
103
|
+
return FromObject(schema_, references_);
|
|
104
104
|
case 'Promise':
|
|
105
|
-
return
|
|
105
|
+
return FromPromise(schema_, references_);
|
|
106
106
|
case 'Record':
|
|
107
|
-
return
|
|
107
|
+
return FromRecord(schema_, references_);
|
|
108
108
|
case 'Ref':
|
|
109
|
-
return
|
|
109
|
+
return FromRef(schema_, references_);
|
|
110
110
|
case 'This':
|
|
111
|
-
return
|
|
111
|
+
return FromThis(schema_, references_);
|
|
112
112
|
case 'Tuple':
|
|
113
|
-
return
|
|
113
|
+
return FromTuple(schema_, references_);
|
|
114
114
|
case 'Union':
|
|
115
|
-
return
|
|
115
|
+
return FromUnion(schema_, references_);
|
|
116
116
|
default:
|
|
117
117
|
return (0, type_1.IsTransform)(schema);
|
|
118
118
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -51,9 +51,9 @@ type T = Static<typeof T> // type T = {
|
|
|
51
51
|
|
|
52
52
|
## Overview
|
|
53
53
|
|
|
54
|
-
TypeBox is a runtime type builder that creates in-memory Json Schema objects that
|
|
54
|
+
TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The Json Schema produced by this library is designed to match the static type checking rules of the TypeScript compiler. TypeBox provides a unified type that can be statically checked by the TypeScript compiler and runtime asserted using standard Json Schema validation.
|
|
55
55
|
|
|
56
|
-
This library enables Json Schema to
|
|
56
|
+
This library enables Json Schema to be created with the same expressiveness as TypeScript's type system. It can be used either as a simple tool to build up complex schematics or integrated into REST or RPC services to help validate data received over the wire.
|
|
57
57
|
|
|
58
58
|
License MIT
|
|
59
59
|
|
|
@@ -70,9 +70,9 @@ License MIT
|
|
|
70
70
|
- [Generics](#types-generics)
|
|
71
71
|
- [References](#types-references)
|
|
72
72
|
- [Recursive](#types-recursive)
|
|
73
|
+
- [Template Literal](#types-template-literal)
|
|
73
74
|
- [Conditional](#types-conditional)
|
|
74
75
|
- [Mapped](#types-mapped)
|
|
75
|
-
- [Template Literal](#types-templateliteral)
|
|
76
76
|
- [Indexed](#types-indexed)
|
|
77
77
|
- [Intrinsic](#types-intrinsic)
|
|
78
78
|
- [Transform](#types-transform)
|
|
@@ -647,7 +647,7 @@ TypeBox provides an extended type set that can be used to create schematics for
|
|
|
647
647
|
|
|
648
648
|
### Modules
|
|
649
649
|
|
|
650
|
-
TypeBox
|
|
650
|
+
TypeBox supports a modular type system that allows for selective type imports. Using selective imports can be helpful in resource constrained environments as it enables modern bundlers to tree shake unused types. This can lead to reduced bundle sizes.
|
|
651
651
|
|
|
652
652
|
```typescript
|
|
653
653
|
import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
|
|
@@ -882,6 +882,41 @@ function test(node: Node) {
|
|
|
882
882
|
}
|
|
883
883
|
```
|
|
884
884
|
|
|
885
|
+
<a name='types-template-literal'></a>
|
|
886
|
+
|
|
887
|
+
### Template Literal Types
|
|
888
|
+
|
|
889
|
+
TypeBox supports template literals with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
|
|
890
|
+
|
|
891
|
+
```typescript
|
|
892
|
+
// TypeScript
|
|
893
|
+
|
|
894
|
+
type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
|
|
895
|
+
|
|
896
|
+
type R = Record<K, string> // type R = {
|
|
897
|
+
// propA: string
|
|
898
|
+
// propB: string
|
|
899
|
+
// propC: string
|
|
900
|
+
// }
|
|
901
|
+
|
|
902
|
+
// TypeBox
|
|
903
|
+
|
|
904
|
+
const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
|
|
905
|
+
// TLiteral<'prop'>,
|
|
906
|
+
// TUnion<[
|
|
907
|
+
// TLiteral<'A'>,
|
|
908
|
+
// TLiteral<'B'>,
|
|
909
|
+
// TLiteral<'C'>,
|
|
910
|
+
// ]>
|
|
911
|
+
// ]>
|
|
912
|
+
|
|
913
|
+
const R = Type.Record(K, Type.String()) // const R: TObject<{
|
|
914
|
+
// hello1: TString,
|
|
915
|
+
// hello2: TString,
|
|
916
|
+
// hello3: TString,
|
|
917
|
+
// }>
|
|
918
|
+
```
|
|
919
|
+
|
|
885
920
|
<a name='types-conditional'></a>
|
|
886
921
|
|
|
887
922
|
### Conditional Types
|
|
@@ -931,7 +966,7 @@ const T = Type.Exclude( // const T: TUnion<[
|
|
|
931
966
|
|
|
932
967
|
### Mapped Types
|
|
933
968
|
|
|
934
|
-
TypeBox supports Mapped types with Type.Mapped. This type accepts a union of
|
|
969
|
+
TypeBox supports Mapped types with Type.Mapped. This type accepts two arguments, the first a union of literal keys and the second a type mapping function which receives a mapping key `K`. The following remaps the type `T` to be `Partial<T>` using type mapping.
|
|
935
970
|
|
|
936
971
|
```typescript
|
|
937
972
|
const T = Type.Object({ // const T: TObject<{
|
|
@@ -940,51 +975,16 @@ const T = Type.Object({ // const T: TObject<{
|
|
|
940
975
|
z: Type.Boolean() // z: TBoolean
|
|
941
976
|
}) // }>
|
|
942
977
|
|
|
943
|
-
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = {
|
|
944
|
-
return Type.
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
978
|
+
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]?: T[K] }
|
|
979
|
+
return Type.Optional(Type.Index(T, K)) //
|
|
980
|
+
}) //
|
|
981
|
+
|
|
982
|
+
// ... runtime mapped as
|
|
983
|
+
//
|
|
949
984
|
// const M: TObject<{
|
|
950
|
-
// x:
|
|
951
|
-
// y:
|
|
952
|
-
// z:
|
|
953
|
-
// }>
|
|
954
|
-
```
|
|
955
|
-
|
|
956
|
-
<a name='types-templateliteral'></a>
|
|
957
|
-
|
|
958
|
-
### Template Literal Types
|
|
959
|
-
|
|
960
|
-
TypeBox supports template literals with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
|
|
961
|
-
|
|
962
|
-
```typescript
|
|
963
|
-
// TypeScript
|
|
964
|
-
|
|
965
|
-
type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
|
|
966
|
-
|
|
967
|
-
type R = Record<K, string> // type R = {
|
|
968
|
-
// propA: string
|
|
969
|
-
// propB: string
|
|
970
|
-
// propC: string
|
|
971
|
-
// }
|
|
972
|
-
|
|
973
|
-
// TypeBox
|
|
974
|
-
|
|
975
|
-
const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
|
|
976
|
-
// TLiteral<'prop'>,
|
|
977
|
-
// TUnion<[
|
|
978
|
-
// TLiteral<'A'>,
|
|
979
|
-
// TLiteral<'B'>,
|
|
980
|
-
// TLiteral<'C'>,
|
|
981
|
-
// ]>
|
|
982
|
-
// ]>
|
|
983
|
-
|
|
984
|
-
const R = Type.Record(K, Type.String()) // const R: TObject<{
|
|
985
|
-
// hello1: TString,
|
|
986
|
-
// hello2: TString,
|
|
987
|
-
// hello3: TString,
|
|
985
|
+
// x: TOptional<TNumber>,
|
|
986
|
+
// y: TOptional<TString>,
|
|
987
|
+
// z: TOptional<TBoolean>
|
|
988
988
|
// }>
|
|
989
989
|
```
|
|
990
990
|
|
|
@@ -1157,13 +1157,13 @@ TypeBox types contain various symbol properties that are used for reflection, co
|
|
|
1157
1157
|
|
|
1158
1158
|
```typescript
|
|
1159
1159
|
const T = Type.Object({ // const T = {
|
|
1160
|
-
name: Type.Optional(Type.String()) // [Kind]: 'Object',
|
|
1160
|
+
name: Type.Optional(Type.String()) // [Symbol(TypeBox.Kind)]: 'Object',
|
|
1161
1161
|
}) // type: 'object',
|
|
1162
1162
|
// properties: {
|
|
1163
1163
|
// name: {
|
|
1164
1164
|
// type: 'string',
|
|
1165
|
-
// [Kind]: 'String',
|
|
1166
|
-
// [Optional]: 'Optional'
|
|
1165
|
+
// [Symbol(TypeBox.Kind)]: 'String',
|
|
1166
|
+
// [Symbol(TypeBox.Optional)]: 'Optional'
|
|
1167
1167
|
// }
|
|
1168
1168
|
// }
|
|
1169
1169
|
// }
|
|
File without changes
|
|
File without changes
|