@sinclair/typebox 0.32.0-dev-11 → 0.32.0-dev-13
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 +3 -3
- package/build/import/errors/errors.mjs +3 -3
- package/build/import/index.d.mts +6 -6
- package/build/import/index.mjs +4 -4
- package/build/import/type/composite/composite.d.mts +2 -2
- package/build/import/type/composite/composite.mjs +2 -2
- package/build/import/type/extends/extends-mapped-result-from-result.d.mts +13 -0
- package/build/import/type/extends/extends-mapped-result-from-result.mjs +14 -0
- package/build/import/type/extends/extends-mapped-result.d.mts +17 -0
- package/build/import/type/extends/extends-mapped-result.mjs +21 -0
- package/build/import/type/extends/extends.d.mts +8 -2
- package/build/import/type/extends/extends.mjs +7 -3
- package/build/import/type/extends/index.d.mts +2 -0
- package/build/import/type/extends/index.mjs +2 -0
- package/build/import/type/guard/type.d.mts +5 -1
- package/build/import/type/guard/type.mjs +18 -2
- package/build/import/type/indexed/index.d.mts +3 -2
- package/build/import/type/indexed/index.mjs +3 -2
- package/build/import/type/indexed/indexed-mapped-result.d.mts +16 -0
- package/build/import/type/indexed/indexed-mapped-result.mjs +18 -0
- package/build/import/type/indexed/{indexed-key.d.mts → indexed-property-keys.d.mts} +3 -3
- package/build/import/type/indexed/{indexed-key.mjs → indexed-property-keys.mjs} +2 -2
- package/build/import/type/indexed/{indexed-type.d.mts → indexed-result.d.mts} +2 -2
- package/build/import/type/indexed/{indexed-type.mjs → indexed-result.mjs} +1 -1
- package/build/import/type/indexed/indexed.d.mts +7 -4
- package/build/import/type/indexed/indexed.mjs +7 -6
- package/build/import/type/intrinsic/capitalize.d.mts +4 -0
- package/build/import/type/intrinsic/capitalize.mjs +5 -0
- package/build/import/type/intrinsic/index.d.mts +5 -0
- package/build/import/type/intrinsic/index.mjs +5 -0
- package/build/import/type/intrinsic/intrinsic-mapped-result.d.mts +15 -0
- package/build/import/type/intrinsic/intrinsic-mapped-result.mjs +21 -0
- package/build/import/type/intrinsic/intrinsic.d.mts +9 -14
- package/build/import/type/intrinsic/intrinsic.mjs +10 -21
- package/build/import/type/intrinsic/lowercase.d.mts +4 -0
- package/build/import/type/intrinsic/lowercase.mjs +5 -0
- package/build/import/type/intrinsic/uncapitalize.d.mts +4 -0
- package/build/import/type/intrinsic/uncapitalize.mjs +5 -0
- package/build/import/type/intrinsic/uppercase.d.mts +4 -0
- package/build/import/type/intrinsic/uppercase.mjs +5 -0
- package/build/import/type/keyof/index.d.mts +1 -2
- package/build/import/type/keyof/index.mjs +1 -2
- package/build/import/type/keyof/{keyof-string.d.mts → keyof-property-keys.d.mts} +4 -4
- package/build/import/type/keyof/{keyof-string.mjs → keyof-property-keys.mjs} +4 -4
- package/build/import/type/keyof/keyof.d.mts +12 -4
- package/build/import/type/keyof/keyof.mjs +14 -3
- package/build/import/type/mapped/index.d.mts +2 -0
- package/build/import/type/mapped/index.mjs +2 -0
- package/build/import/type/mapped/mapped-key.d.mts +8 -0
- package/build/import/type/mapped/mapped-key.mjs +7 -0
- package/build/import/type/mapped/mapped-result.d.mts +9 -0
- package/build/import/type/mapped/mapped-result.mjs +7 -0
- package/build/import/type/mapped/mapped.d.mts +39 -23
- package/build/import/type/mapped/mapped.mjs +65 -11
- package/build/import/type/omit/index.d.mts +1 -0
- package/build/import/type/omit/index.mjs +1 -0
- package/build/import/type/omit/omit-mapped-result.d.mts +15 -0
- package/build/import/type/omit/omit-mapped-result.mjs +20 -0
- package/build/import/type/omit/omit.d.mts +5 -2
- package/build/import/type/omit/omit.mjs +12 -6
- package/build/import/type/pick/index.d.mts +1 -0
- package/build/import/type/pick/index.mjs +1 -0
- package/build/import/type/pick/pick-mapped-result.d.mts +15 -0
- package/build/import/type/pick/pick-mapped-result.mjs +20 -0
- package/build/import/type/pick/pick.d.mts +5 -2
- package/build/import/type/pick/pick.mjs +12 -6
- package/build/import/type/record/record.mjs +4 -4
- package/build/import/type/type/json.d.mts +17 -8
- package/build/import/type/type/json.mjs +6 -2
- package/build/import/value/check/check.mjs +3 -3
- package/build/import/value/clean/clean.mjs +2 -2
- package/build/import/value/transform/decode.mjs +5 -5
- package/build/import/value/transform/encode.mjs +5 -5
- package/build/require/compiler/compiler.js +2 -2
- package/build/require/errors/errors.js +2 -2
- package/build/require/index.d.ts +6 -6
- package/build/require/index.js +9 -8
- package/build/require/type/composite/composite.d.ts +2 -2
- package/build/require/type/composite/composite.js +1 -1
- package/build/require/type/extends/extends-mapped-result-from-result.d.ts +13 -0
- package/build/require/type/extends/extends-mapped-result-from-result.js +18 -0
- package/build/require/type/extends/extends-mapped-result.d.ts +17 -0
- package/build/require/type/extends/extends-mapped-result.js +25 -0
- package/build/require/type/extends/extends.d.ts +8 -2
- package/build/require/type/extends/extends.js +8 -5
- package/build/require/type/extends/index.d.ts +2 -0
- package/build/require/type/extends/index.js +2 -0
- package/build/require/type/guard/type.d.ts +5 -1
- package/build/require/type/guard/type.js +53 -34
- package/build/require/type/indexed/index.d.ts +3 -2
- package/build/require/type/indexed/index.js +3 -2
- package/build/require/type/indexed/indexed-mapped-result.d.ts +16 -0
- package/build/require/type/indexed/indexed-mapped-result.js +22 -0
- package/build/require/type/indexed/{indexed-key.d.ts → indexed-property-keys.d.ts} +3 -3
- package/build/require/type/indexed/{indexed-key.js → indexed-property-keys.js} +4 -4
- package/build/require/type/indexed/{indexed-type.d.ts → indexed-result.d.ts} +2 -2
- package/build/require/type/indexed/{indexed-type.js → indexed-result.js} +3 -3
- package/build/require/type/indexed/indexed.d.ts +7 -4
- package/build/require/type/indexed/indexed.js +8 -7
- package/build/require/type/intrinsic/capitalize.d.ts +4 -0
- package/build/require/type/intrinsic/capitalize.js +9 -0
- package/build/require/type/intrinsic/index.d.ts +5 -0
- package/build/require/type/intrinsic/index.js +5 -0
- package/build/require/type/intrinsic/intrinsic-mapped-result.d.ts +15 -0
- package/build/require/type/intrinsic/intrinsic-mapped-result.js +25 -0
- package/build/require/type/intrinsic/intrinsic.d.ts +9 -14
- package/build/require/type/intrinsic/intrinsic.js +10 -25
- package/build/require/type/intrinsic/lowercase.d.ts +4 -0
- package/build/require/type/intrinsic/lowercase.js +9 -0
- package/build/require/type/intrinsic/uncapitalize.d.ts +4 -0
- package/build/require/type/intrinsic/uncapitalize.js +9 -0
- package/build/require/type/intrinsic/uppercase.d.ts +4 -0
- package/build/require/type/intrinsic/uppercase.js +9 -0
- package/build/require/type/keyof/index.d.ts +1 -2
- package/build/require/type/keyof/index.js +1 -2
- package/build/require/type/keyof/{keyof-string.d.ts → keyof-property-keys.d.ts} +4 -4
- package/build/require/type/keyof/{keyof-string.js → keyof-property-keys.js} +7 -7
- package/build/require/type/keyof/keyof.d.ts +12 -4
- package/build/require/type/keyof/keyof.js +14 -3
- package/build/require/type/mapped/index.d.ts +2 -0
- package/build/require/type/mapped/index.js +2 -0
- package/build/require/type/mapped/mapped-key.d.ts +8 -0
- package/build/require/type/mapped/mapped-key.js +11 -0
- package/build/require/type/mapped/mapped-result.d.ts +9 -0
- package/build/require/type/mapped/mapped-result.js +11 -0
- package/build/require/type/mapped/mapped.d.ts +39 -23
- package/build/require/type/mapped/mapped.js +65 -11
- package/build/require/type/omit/index.d.ts +1 -0
- package/build/require/type/omit/index.js +1 -0
- package/build/require/type/omit/omit-mapped-result.d.ts +15 -0
- package/build/require/type/omit/omit-mapped-result.js +24 -0
- package/build/require/type/omit/omit.d.ts +5 -2
- package/build/require/type/omit/omit.js +10 -4
- package/build/require/type/pick/index.d.ts +1 -0
- package/build/require/type/pick/index.js +1 -0
- package/build/require/type/pick/pick-mapped-result.d.ts +15 -0
- package/build/require/type/pick/pick-mapped-result.js +24 -0
- package/build/require/type/pick/pick.d.ts +5 -2
- package/build/require/type/pick/pick.js +10 -4
- package/build/require/type/record/record.js +2 -2
- package/build/require/type/type/json.d.ts +17 -8
- package/build/require/type/type/json.js +54 -50
- package/build/require/value/check/check.js +2 -2
- package/build/require/value/clean/clean.js +15 -15
- package/build/require/value/transform/decode.js +3 -3
- package/build/require/value/transform/encode.js +3 -3
- package/package.json +1 -1
- package/readme.md +115 -47
- package/build/import/type/keyof/keyof-type.d.mts +0 -11
- package/build/import/type/keyof/keyof-type.mjs +0 -15
- package/build/require/type/keyof/keyof-type.d.ts +0 -11
- package/build/require/type/keyof/keyof-type.js +0 -19
|
@@ -17,42 +17,43 @@ const index_13 = require("../intersect/index");
|
|
|
17
17
|
const index_14 = require("../intrinsic/index");
|
|
18
18
|
const index_15 = require("../keyof/index");
|
|
19
19
|
const index_16 = require("../literal/index");
|
|
20
|
-
const index_17 = require("../
|
|
21
|
-
const index_18 = require("../
|
|
22
|
-
const index_19 = require("../
|
|
23
|
-
const index_20 = require("../
|
|
24
|
-
const index_21 = require("../
|
|
25
|
-
const index_22 = require("../
|
|
26
|
-
const index_23 = require("../
|
|
27
|
-
const index_24 = require("../
|
|
28
|
-
const index_25 = require("../
|
|
29
|
-
const index_26 = require("../
|
|
30
|
-
const index_27 = require("../readonly
|
|
31
|
-
const index_28 = require("../
|
|
32
|
-
const index_29 = require("../
|
|
33
|
-
const index_30 = require("../
|
|
34
|
-
const index_31 = require("../
|
|
35
|
-
const index_32 = require("../
|
|
36
|
-
const index_33 = require("../
|
|
37
|
-
const index_34 = require("../
|
|
38
|
-
const index_35 = require("../
|
|
39
|
-
const index_36 = require("../
|
|
40
|
-
const index_37 = require("../
|
|
41
|
-
const index_38 = require("../
|
|
42
|
-
const index_39 = require("../
|
|
43
|
-
const index_40 = require("../
|
|
20
|
+
const index_17 = require("../mapped/index");
|
|
21
|
+
const index_18 = require("../never/index");
|
|
22
|
+
const index_19 = require("../not/index");
|
|
23
|
+
const index_20 = require("../null/index");
|
|
24
|
+
const index_21 = require("../number/index");
|
|
25
|
+
const index_22 = require("../object/index");
|
|
26
|
+
const index_23 = require("../omit/index");
|
|
27
|
+
const index_24 = require("../optional/index");
|
|
28
|
+
const index_25 = require("../partial/index");
|
|
29
|
+
const index_26 = require("../pick/index");
|
|
30
|
+
const index_27 = require("../readonly/index");
|
|
31
|
+
const index_28 = require("../readonly-optional/index");
|
|
32
|
+
const index_29 = require("../record/index");
|
|
33
|
+
const index_30 = require("../recursive/index");
|
|
34
|
+
const index_31 = require("../ref/index");
|
|
35
|
+
const index_32 = require("../required/index");
|
|
36
|
+
const index_33 = require("../rest/index");
|
|
37
|
+
const index_34 = require("../strict/index");
|
|
38
|
+
const index_35 = require("../string/index");
|
|
39
|
+
const index_36 = require("../template-literal/index");
|
|
40
|
+
const index_37 = require("../transform/index");
|
|
41
|
+
const index_38 = require("../tuple/index");
|
|
42
|
+
const index_39 = require("../union/index");
|
|
43
|
+
const index_40 = require("../unknown/index");
|
|
44
|
+
const index_41 = require("../unsafe/index");
|
|
44
45
|
class JsonTypeBuilder {
|
|
45
46
|
Strict(schema) {
|
|
46
|
-
return (0,
|
|
47
|
+
return (0, index_34.Strict)(schema);
|
|
47
48
|
}
|
|
48
49
|
ReadonlyOptional(schema) {
|
|
49
|
-
return (0,
|
|
50
|
+
return (0, index_28.ReadonlyOptional)(schema);
|
|
50
51
|
}
|
|
51
52
|
Readonly(schema) {
|
|
52
|
-
return (0,
|
|
53
|
+
return (0, index_27.Readonly)(schema);
|
|
53
54
|
}
|
|
54
55
|
Optional(schema) {
|
|
55
|
-
return (0,
|
|
56
|
+
return (0, index_24.Optional)(schema);
|
|
56
57
|
}
|
|
57
58
|
Any(options = {}) {
|
|
58
59
|
return (0, index_1.Any)(options);
|
|
@@ -78,8 +79,8 @@ class JsonTypeBuilder {
|
|
|
78
79
|
Enum(item, options = {}) {
|
|
79
80
|
return (0, index_7.Enum)(item, options);
|
|
80
81
|
}
|
|
81
|
-
Extends(
|
|
82
|
-
return (0, index_9.Extends)(
|
|
82
|
+
Extends(L, R, T, F, options = {}) {
|
|
83
|
+
return (0, index_9.Extends)(L, R, T, F, options);
|
|
83
84
|
}
|
|
84
85
|
Exclude(unionType, excludedMembers, options = {}) {
|
|
85
86
|
return (0, index_8.Exclude)(unionType, excludedMembers, options);
|
|
@@ -105,68 +106,71 @@ class JsonTypeBuilder {
|
|
|
105
106
|
Lowercase(schema, options = {}) {
|
|
106
107
|
return (0, index_14.Lowercase)(schema, options);
|
|
107
108
|
}
|
|
109
|
+
Mapped(key, map, options = {}) {
|
|
110
|
+
return (0, index_17.Mapped)(key, map, options);
|
|
111
|
+
}
|
|
108
112
|
Never(options = {}) {
|
|
109
|
-
return (0,
|
|
113
|
+
return (0, index_18.Never)(options);
|
|
110
114
|
}
|
|
111
115
|
Not(schema, options) {
|
|
112
|
-
return (0,
|
|
116
|
+
return (0, index_19.Not)(schema, options);
|
|
113
117
|
}
|
|
114
118
|
Null(options = {}) {
|
|
115
|
-
return (0,
|
|
119
|
+
return (0, index_20.Null)(options);
|
|
116
120
|
}
|
|
117
121
|
Number(options = {}) {
|
|
118
|
-
return (0,
|
|
122
|
+
return (0, index_21.Number)(options);
|
|
119
123
|
}
|
|
120
124
|
Object(properties, options = {}) {
|
|
121
|
-
return (0,
|
|
125
|
+
return (0, index_22.Object)(properties, options);
|
|
122
126
|
}
|
|
123
127
|
Omit(schema, unresolved, options = {}) {
|
|
124
|
-
return (0,
|
|
128
|
+
return (0, index_23.Omit)(schema, unresolved, options);
|
|
125
129
|
}
|
|
126
130
|
Partial(schema, options = {}) {
|
|
127
|
-
return (0,
|
|
131
|
+
return (0, index_25.Partial)(schema, options);
|
|
128
132
|
}
|
|
129
133
|
Pick(schema, unresolved, options = {}) {
|
|
130
|
-
return (0,
|
|
134
|
+
return (0, index_26.Pick)(schema, unresolved, options);
|
|
131
135
|
}
|
|
132
136
|
Record(key, schema, options = {}) {
|
|
133
|
-
return (0,
|
|
137
|
+
return (0, index_29.Record)(key, schema);
|
|
134
138
|
}
|
|
135
139
|
Recursive(callback, options = {}) {
|
|
136
|
-
return (0,
|
|
140
|
+
return (0, index_30.Recursive)(callback, options);
|
|
137
141
|
}
|
|
138
142
|
Ref(unresolved, options = {}) {
|
|
139
|
-
return (0,
|
|
143
|
+
return (0, index_31.Ref)(unresolved, options);
|
|
140
144
|
}
|
|
141
145
|
Required(schema, options = {}) {
|
|
142
|
-
return (0,
|
|
146
|
+
return (0, index_32.Required)(schema, options);
|
|
143
147
|
}
|
|
144
148
|
Rest(schema) {
|
|
145
|
-
return (0,
|
|
149
|
+
return (0, index_33.Rest)(schema);
|
|
146
150
|
}
|
|
147
151
|
String(options = {}) {
|
|
148
|
-
return (0,
|
|
152
|
+
return (0, index_35.String)(options);
|
|
149
153
|
}
|
|
150
154
|
TemplateLiteral(unresolved, options = {}) {
|
|
151
|
-
return (0,
|
|
155
|
+
return (0, index_36.TemplateLiteral)(unresolved, options);
|
|
152
156
|
}
|
|
153
157
|
Transform(schema) {
|
|
154
|
-
return (0,
|
|
158
|
+
return (0, index_37.Transform)(schema);
|
|
155
159
|
}
|
|
156
160
|
Tuple(items, options = {}) {
|
|
157
|
-
return (0,
|
|
161
|
+
return (0, index_38.Tuple)(items, options);
|
|
158
162
|
}
|
|
159
163
|
Uncapitalize(schema, options = {}) {
|
|
160
164
|
return (0, index_14.Uncapitalize)(schema, options);
|
|
161
165
|
}
|
|
162
166
|
Union(schemas, options = {}) {
|
|
163
|
-
return (0,
|
|
167
|
+
return (0, index_39.Union)(schemas, options);
|
|
164
168
|
}
|
|
165
169
|
Unknown(options = {}) {
|
|
166
|
-
return (0,
|
|
170
|
+
return (0, index_40.Unknown)(options);
|
|
167
171
|
}
|
|
168
172
|
Unsafe(options = {}) {
|
|
169
|
-
return (0,
|
|
173
|
+
return (0, index_41.Unsafe)(options);
|
|
170
174
|
}
|
|
171
175
|
Uppercase(schema, options = {}) {
|
|
172
176
|
return (0, index_14.Uppercase)(schema, options);
|
|
@@ -142,12 +142,12 @@ function TInteger(schema, references, value) {
|
|
|
142
142
|
function TIntersect(schema, references, value) {
|
|
143
143
|
const check1 = schema.allOf.every((schema) => Visit(schema, references, value));
|
|
144
144
|
if (schema.unevaluatedProperties === false) {
|
|
145
|
-
const keyPattern = new RegExp((0, index_6.
|
|
145
|
+
const keyPattern = new RegExp((0, index_6.KeyOfPattern)(schema));
|
|
146
146
|
const check2 = Object.getOwnPropertyNames(value).every((key) => keyPattern.test(key));
|
|
147
147
|
return check1 && check2;
|
|
148
148
|
}
|
|
149
149
|
else if ((0, type_1.TSchema)(schema.unevaluatedProperties)) {
|
|
150
|
-
const keyCheck = new RegExp((0, index_6.
|
|
150
|
+
const keyCheck = new RegExp((0, index_6.KeyOfPattern)(schema));
|
|
151
151
|
const check2 = Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key) || Visit(schema.unevaluatedProperties, references, value[key]));
|
|
152
152
|
return check1 && check2;
|
|
153
153
|
}
|
|
@@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Clean = void 0;
|
|
4
4
|
const index_1 = require("../guard/index");
|
|
5
5
|
const type_1 = require("../../type/guard/type");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
6
|
+
const index_2 = require("../../type/keyof/index");
|
|
7
|
+
const index_3 = require("../check/index");
|
|
8
|
+
const index_4 = require("../clone/index");
|
|
9
|
+
const index_5 = require("../deref/index");
|
|
10
|
+
const index_6 = require("../../type/symbols/index");
|
|
11
11
|
function IsSchema(schema) {
|
|
12
12
|
return (0, type_1.TSchema)(schema);
|
|
13
13
|
}
|
|
14
14
|
function IsCheckable(schema) {
|
|
15
|
-
return (0, type_1.TSchema)(schema) && schema[
|
|
15
|
+
return (0, type_1.TSchema)(schema) && schema[index_6.Kind] !== 'Unsafe';
|
|
16
16
|
}
|
|
17
17
|
function TArray(schema, references, value) {
|
|
18
18
|
if (!(0, index_1.IsArray)(value))
|
|
@@ -21,15 +21,15 @@ function TArray(schema, references, value) {
|
|
|
21
21
|
}
|
|
22
22
|
function TIntersect(schema, references, value) {
|
|
23
23
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
24
|
-
const intersections = schema.allOf.map((schema) => Visit(schema, references, (0,
|
|
24
|
+
const intersections = schema.allOf.map((schema) => Visit(schema, references, (0, index_4.Clone)(value)));
|
|
25
25
|
const composite = intersections.reduce((acc, value) => ((0, index_1.IsObject)(value) ? { ...acc, ...value } : value), {});
|
|
26
26
|
if (!(0, index_1.IsObject)(value) || !(0, index_1.IsObject)(composite) || !IsSchema(unevaluatedProperties))
|
|
27
27
|
return composite;
|
|
28
|
-
const knownkeys = (0,
|
|
28
|
+
const knownkeys = (0, index_2.KeyOfPropertyKeys)(schema);
|
|
29
29
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
30
30
|
if (knownkeys.includes(key))
|
|
31
31
|
continue;
|
|
32
|
-
if ((0,
|
|
32
|
+
if ((0, index_3.Check)(unevaluatedProperties, references, value[key])) {
|
|
33
33
|
composite[key] = Visit(unevaluatedProperties, references, value[key]);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -44,7 +44,7 @@ function TObject(schema, references, value) {
|
|
|
44
44
|
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
45
45
|
continue;
|
|
46
46
|
}
|
|
47
|
-
if (IsSchema(additionalProperties) && (0,
|
|
47
|
+
if (IsSchema(additionalProperties) && (0, index_3.Check)(additionalProperties, references, value[key])) {
|
|
48
48
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
49
49
|
continue;
|
|
50
50
|
}
|
|
@@ -64,7 +64,7 @@ function TRecord(schema, references, value) {
|
|
|
64
64
|
value[key] = Visit(propertySchema, references, value[key]);
|
|
65
65
|
continue;
|
|
66
66
|
}
|
|
67
|
-
if (IsSchema(additionalProperties) && (0,
|
|
67
|
+
if (IsSchema(additionalProperties) && (0, index_3.Check)(additionalProperties, references, value[key])) {
|
|
68
68
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
69
69
|
continue;
|
|
70
70
|
}
|
|
@@ -73,10 +73,10 @@ function TRecord(schema, references, value) {
|
|
|
73
73
|
return value;
|
|
74
74
|
}
|
|
75
75
|
function TRef(schema, references, value) {
|
|
76
|
-
return Visit((0,
|
|
76
|
+
return Visit((0, index_5.Deref)(schema, references), references, value);
|
|
77
77
|
}
|
|
78
78
|
function TThis(schema, references, value) {
|
|
79
|
-
return Visit((0,
|
|
79
|
+
return Visit((0, index_5.Deref)(schema, references), references, value);
|
|
80
80
|
}
|
|
81
81
|
function TTuple(schema, references, value) {
|
|
82
82
|
if (!(0, index_1.IsArray)(value))
|
|
@@ -93,7 +93,7 @@ function TTuple(schema, references, value) {
|
|
|
93
93
|
}
|
|
94
94
|
function TUnion(schema, references, value) {
|
|
95
95
|
for (const inner of schema.anyOf) {
|
|
96
|
-
if (IsCheckable(inner) && (0,
|
|
96
|
+
if (IsCheckable(inner) && (0, index_3.Check)(inner, value)) {
|
|
97
97
|
return Visit(inner, references, value);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
@@ -102,7 +102,7 @@ function TUnion(schema, references, value) {
|
|
|
102
102
|
function Visit(schema, references, value) {
|
|
103
103
|
const references_ = (0, index_1.IsString)(schema.$id) ? [...references, schema] : references;
|
|
104
104
|
const schema_ = schema;
|
|
105
|
-
switch (schema_[
|
|
105
|
+
switch (schema_[index_6.Kind]) {
|
|
106
106
|
case 'Array':
|
|
107
107
|
return TArray(schema_, references_, value);
|
|
108
108
|
case 'Intersect':
|
|
@@ -41,10 +41,10 @@ function TArray(schema, references, value) {
|
|
|
41
41
|
function TIntersect(schema, references, value) {
|
|
42
42
|
if (!(0, index_2.IsPlainObject)(value) || (0, index_2.IsValueType)(value))
|
|
43
43
|
return Default(schema, value);
|
|
44
|
-
const knownKeys = (0, index_3.
|
|
44
|
+
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
45
45
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
46
46
|
return (key in value)
|
|
47
|
-
? { ...value, [key]: Visit((0, index_4.
|
|
47
|
+
? { ...value, [key]: Visit((0, index_4.Index)(schema, [key]), references, value[key]) }
|
|
48
48
|
: value;
|
|
49
49
|
}, value);
|
|
50
50
|
if (!(0, type_1.TTransform)(schema.unevaluatedProperties)) {
|
|
@@ -65,7 +65,7 @@ function TNot(schema, references, value) {
|
|
|
65
65
|
function TObject(schema, references, value) {
|
|
66
66
|
if (!(0, index_2.IsPlainObject)(value))
|
|
67
67
|
return Default(schema, value);
|
|
68
|
-
const knownKeys = (0, index_3.
|
|
68
|
+
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
69
69
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
70
70
|
return (key in value)
|
|
71
71
|
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
|
|
@@ -43,10 +43,10 @@ function TIntersect(schema, references, value) {
|
|
|
43
43
|
const defaulted = Default(schema, value);
|
|
44
44
|
if (!(0, index_2.IsPlainObject)(value) || (0, index_2.IsValueType)(value))
|
|
45
45
|
return defaulted;
|
|
46
|
-
const knownKeys = (0, index_3.
|
|
46
|
+
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
47
47
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
48
48
|
return key in defaulted
|
|
49
|
-
? { ...value, [key]: Visit((0, index_4.
|
|
49
|
+
? { ...value, [key]: Visit((0, index_4.Index)(schema, [key]), references, value[key]) }
|
|
50
50
|
: value;
|
|
51
51
|
}, defaulted);
|
|
52
52
|
if (!(0, type_1.TTransform)(schema.unevaluatedProperties)) {
|
|
@@ -67,7 +67,7 @@ function TObject(schema, references, value) {
|
|
|
67
67
|
const defaulted = Default(schema, value);
|
|
68
68
|
if (!(0, index_2.IsPlainObject)(value))
|
|
69
69
|
return defaulted;
|
|
70
|
-
const knownKeys = (0, index_3.
|
|
70
|
+
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
71
71
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
72
72
|
return key in value
|
|
73
73
|
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -67,15 +67,17 @@ License MIT
|
|
|
67
67
|
- [JavaScript](#types-javascript)
|
|
68
68
|
- [Options](#types-options)
|
|
69
69
|
- [Properties](#types-properties)
|
|
70
|
+
- [Imports](#types-imports)
|
|
70
71
|
- [Generics](#types-generics)
|
|
71
72
|
- [References](#types-references)
|
|
72
73
|
- [Recursive](#types-recursive)
|
|
73
74
|
- [Conditional](#types-conditional)
|
|
75
|
+
- [Mapped](#types-mapped)
|
|
74
76
|
- [Template Literal](#types-templateliteral)
|
|
75
77
|
- [Indexed](#types-indexed)
|
|
76
|
-
- [Rest](#types-rest)
|
|
77
|
-
- [Transform](#types-transform)
|
|
78
78
|
- [Intrinsic](#types-intrinsic)
|
|
79
|
+
- [Transform](#types-transform)
|
|
80
|
+
- [Rest](#types-rest)
|
|
79
81
|
- [Guard](#types-guard)
|
|
80
82
|
- [Unsafe](#types-unsafe)
|
|
81
83
|
- [Strict](#types-strict)
|
|
@@ -375,8 +377,8 @@ The following table lists the supported Json types. These types are fully compat
|
|
|
375
377
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
376
378
|
│ const T = Type.Extends( │ type T = │ const T = { │
|
|
377
379
|
│ Type.String(), │ string extends number │ const: false, │
|
|
378
|
-
│ Type.Number(), │
|
|
379
|
-
│ Type.Literal(true), │
|
|
380
|
+
│ Type.Number(), │ ? true │ type: 'boolean' │
|
|
381
|
+
│ Type.Literal(true), │ : false │ } │
|
|
380
382
|
│ Type.Literal(false) │ │ │
|
|
381
383
|
│ ) │ │ │
|
|
382
384
|
│ │ │ │
|
|
@@ -399,6 +401,20 @@ The following table lists the supported Json types. These types are fully compat
|
|
|
399
401
|
│ ) │ │ │
|
|
400
402
|
│ │ │ │
|
|
401
403
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
404
|
+
│ const T = Type.Mapped( │ type T = { │ const T = { │
|
|
405
|
+
│ Type.Union([ │ [_ in 'x' | 'y'] : number │ type: 'object', │
|
|
406
|
+
│ Type.Literal('x'), │ } │ required: ['x', 'y'], │
|
|
407
|
+
│ Type.Literal('y') │ │ properties: { │
|
|
408
|
+
│ ]), │ │ x: { │
|
|
409
|
+
│ () => Type.Number() │ │ type: 'number' │
|
|
410
|
+
│ ) │ │ }, │
|
|
411
|
+
│ │ │ y: { │
|
|
412
|
+
│ │ │ type: 'number' │
|
|
413
|
+
│ │ │ } │
|
|
414
|
+
│ │ │ } │
|
|
415
|
+
│ │ │ } │
|
|
416
|
+
│ │ │ │
|
|
417
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
402
418
|
│ const U = Type.Union([ │ type U = 'open' | 'close' │ const T = { │
|
|
403
419
|
│ Type.Literal('open'), │ │ type: 'string', │
|
|
404
420
|
│ Type.Literal('close') │ type T = `on${U}` │ pattern: '^on(open|close)$' │
|
|
@@ -700,6 +716,32 @@ Object properties can be modified with Readonly and Optional. The following tabl
|
|
|
700
716
|
│ │ │ │
|
|
701
717
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
702
718
|
```
|
|
719
|
+
|
|
720
|
+
<a name='types-imports'></a>
|
|
721
|
+
|
|
722
|
+
### Import Types
|
|
723
|
+
|
|
724
|
+
TypeBox supports a modular type system. For most users, importing the full type system is generally recommended as it offers the best overall user experience as well as enabling easier discovery of available types.
|
|
725
|
+
|
|
726
|
+
```typescript
|
|
727
|
+
import { Type } from '@sinclair/typebox' // 34.9 kb minified
|
|
728
|
+
|
|
729
|
+
const T = Type.Object({
|
|
730
|
+
x: Type.Number(),
|
|
731
|
+
y: Type.Number()
|
|
732
|
+
})
|
|
733
|
+
```
|
|
734
|
+
For developers building for resource constrained environments, selective imports can allow bundle optimizers to tree shake unused types. This can dramatically reduce bundle output sizes.
|
|
735
|
+
```typescript
|
|
736
|
+
import { Object, Number } from '@sinclair/typebox' // 6.4 kb minified
|
|
737
|
+
|
|
738
|
+
const T = Object({
|
|
739
|
+
x: Number(),
|
|
740
|
+
y: Number()
|
|
741
|
+
})
|
|
742
|
+
```
|
|
743
|
+
For additional information on bundle optimizations, refer to the documentation provided by your bundler of choice.
|
|
744
|
+
|
|
703
745
|
<a name='types-generics'></a>
|
|
704
746
|
|
|
705
747
|
### Generic Types
|
|
@@ -848,7 +890,7 @@ function test(node: Node) {
|
|
|
848
890
|
|
|
849
891
|
### Conditional Types
|
|
850
892
|
|
|
851
|
-
TypeBox supports runtime
|
|
893
|
+
TypeBox supports runtime Conditional Types with Type.Extends. This type runs a structural assignability check against the first and second arguments and will return either the third (`true`) or fourth (`false`) argument based on the result. Additionally, the conditional backed types Exclude and Extract are also supported. The following shows both TypeScript and TypeBox examples of this feature.
|
|
852
894
|
|
|
853
895
|
```typescript
|
|
854
896
|
// Extends
|
|
@@ -889,11 +931,37 @@ const T = Type.Exclude( // const T: TUnion<[
|
|
|
889
931
|
)
|
|
890
932
|
```
|
|
891
933
|
|
|
934
|
+
<a name='types-mapped'></a>
|
|
935
|
+
|
|
936
|
+
### Mapped Types
|
|
937
|
+
|
|
938
|
+
TypeBox supports Mapped Types with Type.Mapped. This type accepts a union set of property keys as the first argument, and a type mapping function which accepts each key (`K`) as the second. This type can be used with Conditional and Indexed Access types to allow for generalized property remapping irrespective of property type. The following example remaps each property of `T` to be `T[K] | null`.
|
|
939
|
+
|
|
940
|
+
```typescript
|
|
941
|
+
const T = Type.Object({ // const T: TObject<{
|
|
942
|
+
x: Type.Number(), // x: TNumber,
|
|
943
|
+
y: Type.String(), // y: TString,
|
|
944
|
+
z: Type.Boolean() // z: TBoolean
|
|
945
|
+
}) // }>
|
|
946
|
+
|
|
947
|
+
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = {
|
|
948
|
+
return Type.Union([ // [K in keyof T]: T[K] | null
|
|
949
|
+
Type.Index(T, K), // }
|
|
950
|
+
Type.Null() //
|
|
951
|
+
]) // ... runtime mapped as
|
|
952
|
+
}) //
|
|
953
|
+
// const M: TObject<{
|
|
954
|
+
// x: TUnion<[TNumber, TNull]>,
|
|
955
|
+
// y: TUnion<[TString, TNull]>,
|
|
956
|
+
// z: TUnion<[TBoolean, TNull]>
|
|
957
|
+
// }>
|
|
958
|
+
```
|
|
959
|
+
|
|
892
960
|
<a name='types-templateliteral'></a>
|
|
893
961
|
|
|
894
962
|
### Template Literal Types
|
|
895
963
|
|
|
896
|
-
TypeBox supports
|
|
964
|
+
TypeBox supports Template Literal types with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript Template Literal syntax. TypeBox encodes Template Literals as regular expression string 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.
|
|
897
965
|
|
|
898
966
|
```typescript
|
|
899
967
|
// TypeScript
|
|
@@ -928,7 +996,7 @@ const R = Type.Record(K, Type.String()) // const R: TObject<{
|
|
|
928
996
|
|
|
929
997
|
### Indexed Access Types
|
|
930
998
|
|
|
931
|
-
TypeBox supports Indexed Access Types with Index. This type enables uniform access to interior property and array element types without having to extract them from the underlying schema representation. This type is supported for Object, Array, Tuple, Union and Intersect types.
|
|
999
|
+
TypeBox supports Indexed Access Types with Type.Index. This type enables uniform access to interior property and array element types without having to extract them from the underlying schema representation. This type is supported for Object, Array, Tuple, Union and Intersect types.
|
|
932
1000
|
|
|
933
1001
|
```typescript
|
|
934
1002
|
const T = Type.Object({ // const T: TObject<{
|
|
@@ -951,23 +1019,35 @@ const C = Type.Index(T, Type.KeyOf(T)) // const C: TUnion<[
|
|
|
951
1019
|
// ]>
|
|
952
1020
|
```
|
|
953
1021
|
|
|
954
|
-
<a name='types-
|
|
1022
|
+
<a name='types-intrinsic'></a>
|
|
955
1023
|
|
|
956
|
-
###
|
|
1024
|
+
### Intrinsic Types
|
|
957
1025
|
|
|
958
|
-
TypeBox
|
|
1026
|
+
TypeBox supports the TypeScript Intrinsic String Manipulation types Uppercase, Lowercase, Capitalize and Uncapitalize. These types can be used to remap String Literal, Template Literal and Union of Literal types.
|
|
959
1027
|
|
|
960
1028
|
```typescript
|
|
961
|
-
|
|
962
|
-
Type.String(), // TString,
|
|
963
|
-
Type.Number() // TNumber
|
|
964
|
-
]) // ]>
|
|
1029
|
+
// TypeScript
|
|
965
1030
|
|
|
966
|
-
|
|
1031
|
+
type A = Capitalize<'hello'> // type A = 'Hello'
|
|
1032
|
+
type B = Capitalize<'hello' | 'world'> // type C = 'Hello' | 'World'
|
|
1033
|
+
type C = Capitalize<`hello${1|2|3}`> // type B = 'Hello1' | 'Hello2' | 'Hello3'
|
|
967
1034
|
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1035
|
+
// TypeBox
|
|
1036
|
+
|
|
1037
|
+
const A = Type.Capitalize(Type.Literal('hello')) // const A: TLiteral<'Hello'>
|
|
1038
|
+
|
|
1039
|
+
const B = Type.Capitalize(Type.Union([ // const B: TUnion<[
|
|
1040
|
+
Type.Literal('hello'), // TLiteral<'Hello'>,
|
|
1041
|
+
Type.Literal('world') // TLiteral<'World'>
|
|
1042
|
+
])) // ]>
|
|
1043
|
+
|
|
1044
|
+
const C = Type.Capitalize( // const C: TTemplateLiteral<[
|
|
1045
|
+
Type.TemplateLiteral('hello${1|2|3}') // TLiteral<'Hello'>,
|
|
1046
|
+
) // TUnion<[
|
|
1047
|
+
// TLiteral<'1'>,
|
|
1048
|
+
// TLiteral<'2'>,
|
|
1049
|
+
// TLiteral<'3'>
|
|
1050
|
+
// ]>
|
|
971
1051
|
// ]>
|
|
972
1052
|
```
|
|
973
1053
|
|
|
@@ -1000,35 +1080,23 @@ type E = StaticEncode<typeof T> // type E = Array<number>
|
|
|
1000
1080
|
type T = Static<typeof T> // type T = Array<number>
|
|
1001
1081
|
```
|
|
1002
1082
|
|
|
1003
|
-
<a name='types-
|
|
1083
|
+
<a name='types-rest'></a>
|
|
1004
1084
|
|
|
1005
|
-
###
|
|
1085
|
+
### Rest Types
|
|
1006
1086
|
|
|
1007
|
-
TypeBox
|
|
1087
|
+
TypeBox provides the Rest type to uniformly extract variadic tuples from Intersect, Union and Tuple types. This type can be useful to remap variadic types into different forms. The following uses Rest to remap a Tuple into a Union.
|
|
1008
1088
|
|
|
1009
1089
|
```typescript
|
|
1010
|
-
//
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
type C = Capitalize<`hello${1|2|3}`> // type B = 'Hello1' | 'Hello2' | 'Hello3'
|
|
1015
|
-
|
|
1016
|
-
// TypeBox
|
|
1017
|
-
|
|
1018
|
-
const A = Type.Capitalize(Type.Literal('hello')) // const A: TLiteral<'Hello'>
|
|
1090
|
+
const T = Type.Tuple([ // const T: TTuple<[
|
|
1091
|
+
Type.String(), // TString,
|
|
1092
|
+
Type.Number() // TNumber
|
|
1093
|
+
]) // ]>
|
|
1019
1094
|
|
|
1020
|
-
const
|
|
1021
|
-
Type.Literal('hello'), // TLiteral<'Hello'>,
|
|
1022
|
-
Type.Literal('world') // TLiteral<'World'>
|
|
1023
|
-
])) // ]>
|
|
1095
|
+
const R = Type.Rest(T) // const R: [TString, TNumber]
|
|
1024
1096
|
|
|
1025
|
-
const
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
// TLiteral<'1'>,
|
|
1029
|
-
// TLiteral<'2'>,
|
|
1030
|
-
// TLiteral<'3'>
|
|
1031
|
-
// ]>
|
|
1097
|
+
const U = Type.Union(R) // const T: TUnion<[
|
|
1098
|
+
// TString,
|
|
1099
|
+
// TNumber
|
|
1032
1100
|
// ]>
|
|
1033
1101
|
```
|
|
1034
1102
|
|
|
@@ -1751,12 +1819,12 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1751
1819
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1752
1820
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1753
1821
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1754
|
-
│ typebox/compiler │ '109.
|
|
1755
|
-
│ typebox/errors │ '
|
|
1756
|
-
│ typebox/system │ '
|
|
1757
|
-
│ typebox/type │ '
|
|
1758
|
-
│ typebox/value │ '
|
|
1759
|
-
│ typebox │ '
|
|
1822
|
+
│ typebox/compiler │ '109.2 kb' │ ' 48.6 kb' │ '2.25 x' │
|
|
1823
|
+
│ typebox/errors │ ' 55.0 kb' │ ' 25.1 kb' │ '2.19 x' │
|
|
1824
|
+
│ typebox/system │ ' 4.6 kb' │ ' 2.0 kb' │ '2.31 x' │
|
|
1825
|
+
│ typebox/type │ ' 82.9 kb' │ ' 34.9 kb' │ '2.38 x' │
|
|
1826
|
+
│ typebox/value │ '144.2 kb' │ ' 61.5 kb' │ '2.34 x' │
|
|
1827
|
+
│ typebox │ ' 82.9 kb' │ ' 34.9 kb' │ '2.37 x' │
|
|
1760
1828
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1761
1829
|
```
|
|
1762
1830
|
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from '../schema/index.mjs';
|
|
2
|
-
import type { Ensure } from '../helpers/index.mjs';
|
|
3
|
-
import { type TLiteral, TLiteralValue } from '../literal/index.mjs';
|
|
4
|
-
import { type TNumber } from '../number/index.mjs';
|
|
5
|
-
import { KeyOfStringResolve } from './keyof-string.mjs';
|
|
6
|
-
import { UnionResolve } from '../union/index.mjs';
|
|
7
|
-
type FromLiterals<T extends TLiteralValue[]> = (T extends [infer L extends TLiteralValue, ...infer R extends TLiteralValue[]] ? L extends '[number]' ? [TNumber, ...FromLiterals<R>] : [TLiteral<L>, ...FromLiterals<R>] : []);
|
|
8
|
-
declare function FromLiterals<T extends TLiteralValue[]>(T: [...T]): FromLiterals<T>;
|
|
9
|
-
export type KeyOfTypeResolve<T extends TSchema> = (Ensure<UnionResolve<FromLiterals<KeyOfStringResolve<T>>>>);
|
|
10
|
-
export declare function KeyOfTypeResolve<T extends TSchema>(T: T): KeyOfTypeResolve<T>;
|
|
11
|
-
export {};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Literal } from '../literal/index.mjs';
|
|
2
|
-
import { Number } from '../number/index.mjs';
|
|
3
|
-
import { KeyOfStringResolve } from './keyof-string.mjs';
|
|
4
|
-
import { UnionResolve } from '../union/index.mjs';
|
|
5
|
-
function FromLiterals(T) {
|
|
6
|
-
const [L, ...R] = T;
|
|
7
|
-
return (T.length > 0
|
|
8
|
-
? L === '[number]'
|
|
9
|
-
? [Number(), ...FromLiterals(R)]
|
|
10
|
-
: [Literal(L), ...FromLiterals(R)]
|
|
11
|
-
: []);
|
|
12
|
-
}
|
|
13
|
-
export function KeyOfTypeResolve(T) {
|
|
14
|
-
return (UnionResolve(FromLiterals(KeyOfStringResolve(T))));
|
|
15
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from '../schema/index';
|
|
2
|
-
import type { Ensure } from '../helpers/index';
|
|
3
|
-
import { type TLiteral, TLiteralValue } from '../literal/index';
|
|
4
|
-
import { type TNumber } from '../number/index';
|
|
5
|
-
import { KeyOfStringResolve } from './keyof-string';
|
|
6
|
-
import { UnionResolve } from '../union/index';
|
|
7
|
-
type FromLiterals<T extends TLiteralValue[]> = (T extends [infer L extends TLiteralValue, ...infer R extends TLiteralValue[]] ? L extends '[number]' ? [TNumber, ...FromLiterals<R>] : [TLiteral<L>, ...FromLiterals<R>] : []);
|
|
8
|
-
declare function FromLiterals<T extends TLiteralValue[]>(T: [...T]): FromLiterals<T>;
|
|
9
|
-
export type KeyOfTypeResolve<T extends TSchema> = (Ensure<UnionResolve<FromLiterals<KeyOfStringResolve<T>>>>);
|
|
10
|
-
export declare function KeyOfTypeResolve<T extends TSchema>(T: T): KeyOfTypeResolve<T>;
|
|
11
|
-
export {};
|