@sinclair/typebox 0.25.24 → 0.26.0-dev.1
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/compiler/compiler.d.ts +10 -5
- package/compiler/compiler.js +161 -123
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -153
- package/package.json +1 -6
- package/readme.md +294 -207
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +388 -162
- package/typebox.js +1716 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +121 -188
- package/value/check.d.ts +1 -1
- package/value/check.js +156 -111
- package/value/convert.d.ts +13 -0
- package/value/convert.js +345 -0
- package/value/create.d.ts +6 -2
- package/value/create.js +149 -97
- package/{hash → value}/hash.js +39 -14
- package/value/index.d.ts +1 -0
- package/value/index.js +3 -1
- package/value/value.d.ts +2 -8
- package/value/value.js +20 -14
- package/conditional/conditional.d.ts +0 -17
- package/conditional/conditional.js +0 -91
- package/conditional/index.d.ts +0 -2
- package/conditional/index.js +0 -45
- package/conditional/structural.d.ts +0 -11
- package/conditional/structural.js +0 -685
- package/custom/custom.d.ts +0 -12
- package/custom/custom.js +0 -55
- package/custom/index.d.ts +0 -1
- package/custom/index.js +0 -44
- package/format/format.d.ts +0 -12
- package/format/format.js +0 -55
- package/format/index.d.ts +0 -1
- package/format/index.js +0 -44
- package/guard/extends.d.ts +0 -10
- package/guard/extends.js +0 -50
- package/guard/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -2
- package/guard/index.js +0 -45
- package/hash/index.d.ts +0 -1
- package/hash/index.js +0 -44
- /package/{hash → value}/hash.d.ts +0 -0
package/errors/errors.js
CHANGED
|
@@ -30,10 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
30
|
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const index_1 = require("../system/index");
|
|
33
|
-
const
|
|
34
|
-
const index_2 = require("../format/index");
|
|
35
|
-
const index_3 = require("../custom/index");
|
|
36
|
-
const index_4 = require("../hash/index");
|
|
33
|
+
const hash_1 = require("../value/hash");
|
|
37
34
|
// -------------------------------------------------------------------
|
|
38
35
|
// ValueErrorType
|
|
39
36
|
// -------------------------------------------------------------------
|
|
@@ -43,51 +40,61 @@ var ValueErrorType;
|
|
|
43
40
|
ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
|
|
44
41
|
ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
|
|
45
42
|
ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
|
|
46
|
-
ValueErrorType[ValueErrorType["
|
|
47
|
-
ValueErrorType[ValueErrorType["
|
|
48
|
-
ValueErrorType[ValueErrorType["
|
|
49
|
-
ValueErrorType[ValueErrorType["
|
|
50
|
-
ValueErrorType[ValueErrorType["
|
|
51
|
-
ValueErrorType[ValueErrorType["
|
|
52
|
-
ValueErrorType[ValueErrorType["
|
|
53
|
-
ValueErrorType[ValueErrorType["
|
|
54
|
-
ValueErrorType[ValueErrorType["
|
|
55
|
-
ValueErrorType[ValueErrorType["
|
|
56
|
-
ValueErrorType[ValueErrorType["
|
|
57
|
-
ValueErrorType[ValueErrorType["
|
|
58
|
-
ValueErrorType[ValueErrorType["
|
|
59
|
-
ValueErrorType[ValueErrorType["
|
|
60
|
-
ValueErrorType[ValueErrorType["
|
|
61
|
-
ValueErrorType[ValueErrorType["
|
|
62
|
-
ValueErrorType[ValueErrorType["
|
|
63
|
-
ValueErrorType[ValueErrorType["
|
|
64
|
-
ValueErrorType[ValueErrorType["
|
|
65
|
-
ValueErrorType[ValueErrorType["
|
|
66
|
-
ValueErrorType[ValueErrorType["
|
|
67
|
-
ValueErrorType[ValueErrorType["
|
|
68
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
76
|
-
ValueErrorType[ValueErrorType["
|
|
77
|
-
ValueErrorType[ValueErrorType["
|
|
78
|
-
ValueErrorType[ValueErrorType["
|
|
79
|
-
ValueErrorType[ValueErrorType["
|
|
80
|
-
ValueErrorType[ValueErrorType["
|
|
81
|
-
ValueErrorType[ValueErrorType["
|
|
82
|
-
ValueErrorType[ValueErrorType["
|
|
83
|
-
ValueErrorType[ValueErrorType["
|
|
84
|
-
ValueErrorType[ValueErrorType["
|
|
85
|
-
ValueErrorType[ValueErrorType["
|
|
86
|
-
ValueErrorType[ValueErrorType["
|
|
87
|
-
ValueErrorType[ValueErrorType["
|
|
88
|
-
ValueErrorType[ValueErrorType["
|
|
89
|
-
ValueErrorType[ValueErrorType["
|
|
90
|
-
ValueErrorType[ValueErrorType["
|
|
43
|
+
ValueErrorType[ValueErrorType["BigInt"] = 4] = "BigInt";
|
|
44
|
+
ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 5] = "BigIntMultipleOf";
|
|
45
|
+
ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 6] = "BigIntExclusiveMinimum";
|
|
46
|
+
ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 7] = "BigIntExclusiveMaximum";
|
|
47
|
+
ValueErrorType[ValueErrorType["BigIntMinimum"] = 8] = "BigIntMinimum";
|
|
48
|
+
ValueErrorType[ValueErrorType["BigIntMaximum"] = 9] = "BigIntMaximum";
|
|
49
|
+
ValueErrorType[ValueErrorType["Boolean"] = 10] = "Boolean";
|
|
50
|
+
ValueErrorType[ValueErrorType["Date"] = 11] = "Date";
|
|
51
|
+
ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 12] = "DateExclusiveMinimumTimestamp";
|
|
52
|
+
ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 13] = "DateExclusiveMaximumTimestamp";
|
|
53
|
+
ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 14] = "DateMinimumTimestamp";
|
|
54
|
+
ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 15] = "DateMaximumTimestamp";
|
|
55
|
+
ValueErrorType[ValueErrorType["Function"] = 16] = "Function";
|
|
56
|
+
ValueErrorType[ValueErrorType["Integer"] = 17] = "Integer";
|
|
57
|
+
ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 18] = "IntegerMultipleOf";
|
|
58
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 19] = "IntegerExclusiveMinimum";
|
|
59
|
+
ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 20] = "IntegerExclusiveMaximum";
|
|
60
|
+
ValueErrorType[ValueErrorType["IntegerMinimum"] = 21] = "IntegerMinimum";
|
|
61
|
+
ValueErrorType[ValueErrorType["IntegerMaximum"] = 22] = "IntegerMaximum";
|
|
62
|
+
ValueErrorType[ValueErrorType["Intersect"] = 23] = "Intersect";
|
|
63
|
+
ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 24] = "IntersectUnevaluatedProperties";
|
|
64
|
+
ValueErrorType[ValueErrorType["Literal"] = 25] = "Literal";
|
|
65
|
+
ValueErrorType[ValueErrorType["Never"] = 26] = "Never";
|
|
66
|
+
ValueErrorType[ValueErrorType["Not"] = 27] = "Not";
|
|
67
|
+
ValueErrorType[ValueErrorType["Null"] = 28] = "Null";
|
|
68
|
+
ValueErrorType[ValueErrorType["Number"] = 29] = "Number";
|
|
69
|
+
ValueErrorType[ValueErrorType["NumberMultipleOf"] = 30] = "NumberMultipleOf";
|
|
70
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 31] = "NumberExclusiveMinimum";
|
|
71
|
+
ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 32] = "NumberExclusiveMaximum";
|
|
72
|
+
ValueErrorType[ValueErrorType["NumberMinumum"] = 33] = "NumberMinumum";
|
|
73
|
+
ValueErrorType[ValueErrorType["NumberMaximum"] = 34] = "NumberMaximum";
|
|
74
|
+
ValueErrorType[ValueErrorType["Object"] = 35] = "Object";
|
|
75
|
+
ValueErrorType[ValueErrorType["ObjectMinProperties"] = 36] = "ObjectMinProperties";
|
|
76
|
+
ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 37] = "ObjectMaxProperties";
|
|
77
|
+
ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 38] = "ObjectAdditionalProperties";
|
|
78
|
+
ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 39] = "ObjectRequiredProperties";
|
|
79
|
+
ValueErrorType[ValueErrorType["Promise"] = 40] = "Promise";
|
|
80
|
+
ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 41] = "RecordKeyNumeric";
|
|
81
|
+
ValueErrorType[ValueErrorType["RecordKeyString"] = 42] = "RecordKeyString";
|
|
82
|
+
ValueErrorType[ValueErrorType["String"] = 43] = "String";
|
|
83
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 44] = "StringMinLength";
|
|
84
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 45] = "StringMaxLength";
|
|
85
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 46] = "StringPattern";
|
|
86
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 47] = "StringFormatUnknown";
|
|
87
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 48] = "StringFormat";
|
|
88
|
+
ValueErrorType[ValueErrorType["Symbol"] = 49] = "Symbol";
|
|
89
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 50] = "TupleZeroLength";
|
|
90
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 51] = "TupleLength";
|
|
91
|
+
ValueErrorType[ValueErrorType["Undefined"] = 52] = "Undefined";
|
|
92
|
+
ValueErrorType[ValueErrorType["Union"] = 53] = "Union";
|
|
93
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 54] = "Uint8Array";
|
|
94
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 55] = "Uint8ArrayMinByteLength";
|
|
95
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 56] = "Uint8ArrayMaxByteLength";
|
|
96
|
+
ValueErrorType[ValueErrorType["Void"] = 57] = "Void";
|
|
97
|
+
ValueErrorType[ValueErrorType["Custom"] = 58] = "Custom";
|
|
91
98
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
92
99
|
// -------------------------------------------------------------------
|
|
93
100
|
// ValueErrors
|
|
@@ -102,11 +109,14 @@ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
|
102
109
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
103
110
|
var ValueErrors;
|
|
104
111
|
(function (ValueErrors) {
|
|
112
|
+
function IsBigInt(value) {
|
|
113
|
+
return typeof value === 'bigint';
|
|
114
|
+
}
|
|
105
115
|
function IsNumber(value) {
|
|
106
|
-
return typeof value === 'number' &&
|
|
116
|
+
return typeof value === 'number' && globalThis.isFinite(value);
|
|
107
117
|
}
|
|
108
|
-
function* Any(schema,
|
|
109
|
-
function* Array(schema,
|
|
118
|
+
function* Any(schema, path, value) { }
|
|
119
|
+
function* Array(schema, path, value) {
|
|
110
120
|
if (!globalThis.Array.isArray(value)) {
|
|
111
121
|
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
112
122
|
}
|
|
@@ -118,7 +128,7 @@ var ValueErrors;
|
|
|
118
128
|
}
|
|
119
129
|
// prettier-ignore
|
|
120
130
|
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
121
|
-
const hashed =
|
|
131
|
+
const hashed = hash_1.ValueHash.Create(element);
|
|
122
132
|
if (set.has(hashed)) {
|
|
123
133
|
return false;
|
|
124
134
|
}
|
|
@@ -129,22 +139,42 @@ var ValueErrors;
|
|
|
129
139
|
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
130
140
|
}
|
|
131
141
|
for (let i = 0; i < value.length; i++) {
|
|
132
|
-
yield* Visit(schema.items,
|
|
142
|
+
yield* Visit(schema.items, `${path}/${i}`, value[i]);
|
|
133
143
|
}
|
|
134
144
|
}
|
|
135
|
-
function*
|
|
145
|
+
function* BigInt(schema, path, value) {
|
|
146
|
+
if (!(typeof value === 'bigint')) {
|
|
147
|
+
return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
|
|
148
|
+
}
|
|
149
|
+
if (IsBigInt(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
|
|
150
|
+
yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
|
|
151
|
+
}
|
|
152
|
+
if (IsBigInt(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
153
|
+
yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
|
|
154
|
+
}
|
|
155
|
+
if (IsBigInt(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
156
|
+
yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
|
|
157
|
+
}
|
|
158
|
+
if (IsBigInt(schema.minimum) && !(value >= schema.minimum)) {
|
|
159
|
+
yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
|
|
160
|
+
}
|
|
161
|
+
if (IsBigInt(schema.maximum) && !(value <= schema.maximum)) {
|
|
162
|
+
yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function* Boolean(schema, path, value) {
|
|
136
166
|
if (!(typeof value === 'boolean')) {
|
|
137
167
|
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
138
168
|
}
|
|
139
169
|
}
|
|
140
|
-
function* Constructor(schema,
|
|
141
|
-
yield* Visit(schema.returns,
|
|
170
|
+
function* Constructor(schema, path, value) {
|
|
171
|
+
yield* Visit(schema.returns, path, value.prototype);
|
|
142
172
|
}
|
|
143
|
-
function* Date(schema,
|
|
173
|
+
function* Date(schema, path, value) {
|
|
144
174
|
if (!(value instanceof globalThis.Date)) {
|
|
145
175
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
146
176
|
}
|
|
147
|
-
if (
|
|
177
|
+
if (!globalThis.isFinite(value.getTime())) {
|
|
148
178
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
|
|
149
179
|
}
|
|
150
180
|
if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
@@ -160,12 +190,12 @@ var ValueErrors;
|
|
|
160
190
|
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
161
191
|
}
|
|
162
192
|
}
|
|
163
|
-
function* Function(schema,
|
|
193
|
+
function* Function(schema, path, value) {
|
|
164
194
|
if (!(typeof value === 'function')) {
|
|
165
195
|
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
166
196
|
}
|
|
167
197
|
}
|
|
168
|
-
function* Integer(schema,
|
|
198
|
+
function* Integer(schema, path, value) {
|
|
169
199
|
if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
|
|
170
200
|
return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
171
201
|
}
|
|
@@ -185,28 +215,67 @@ var ValueErrors;
|
|
|
185
215
|
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
186
216
|
}
|
|
187
217
|
}
|
|
188
|
-
function*
|
|
218
|
+
function* Intersect(schema, path, value) {
|
|
219
|
+
for (const subschema of schema.allOf) {
|
|
220
|
+
const next = Visit(subschema, path, value).next();
|
|
221
|
+
if (!next.done) {
|
|
222
|
+
yield next.value;
|
|
223
|
+
yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (schema.unevaluatedProperties === false) {
|
|
228
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema);
|
|
229
|
+
const valueKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
230
|
+
for (const valueKey of valueKeys) {
|
|
231
|
+
if (!schemaKeys.includes(valueKey)) {
|
|
232
|
+
yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (typeof schema.unevaluatedProperties === 'object') {
|
|
237
|
+
const schemaKeys = Types.KeyResolver.Resolve(schema);
|
|
238
|
+
const valueKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
239
|
+
for (const valueKey of valueKeys) {
|
|
240
|
+
if (!schemaKeys.includes(valueKey)) {
|
|
241
|
+
const next = Visit(schema.unevaluatedProperties, `${path}/${valueKey}`, value[valueKey]).next();
|
|
242
|
+
if (!next.done) {
|
|
243
|
+
yield next.value;
|
|
244
|
+
yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function* Literal(schema, path, value) {
|
|
189
252
|
if (!(value === schema.const)) {
|
|
190
253
|
const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
|
|
191
254
|
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
192
255
|
}
|
|
193
256
|
}
|
|
194
|
-
function* Never(schema,
|
|
257
|
+
function* Never(schema, path, value) {
|
|
195
258
|
yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
|
|
196
259
|
}
|
|
197
|
-
function*
|
|
260
|
+
function* Not(schema, path, value) {
|
|
261
|
+
if (Visit(schema.allOf[0].not, path, value).next().done === true) {
|
|
262
|
+
yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
|
|
263
|
+
}
|
|
264
|
+
yield* Visit(schema.allOf[1], path, value);
|
|
265
|
+
}
|
|
266
|
+
function* Null(schema, path, value) {
|
|
198
267
|
if (!(value === null)) {
|
|
199
268
|
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
200
269
|
}
|
|
201
270
|
}
|
|
202
|
-
function* Number(schema,
|
|
271
|
+
function* Number(schema, path, value) {
|
|
203
272
|
if (index_1.TypeSystem.AllowNaN) {
|
|
204
273
|
if (!(typeof value === 'number')) {
|
|
205
274
|
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
206
275
|
}
|
|
207
276
|
}
|
|
208
277
|
else {
|
|
209
|
-
if (!(typeof value === 'number' &&
|
|
278
|
+
if (!(typeof value === 'number' && globalThis.Number.isFinite(value))) {
|
|
210
279
|
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
211
280
|
}
|
|
212
281
|
}
|
|
@@ -226,7 +295,7 @@ var ValueErrors;
|
|
|
226
295
|
yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
227
296
|
}
|
|
228
297
|
}
|
|
229
|
-
function* Object(schema,
|
|
298
|
+
function* Object(schema, path, value) {
|
|
230
299
|
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
231
300
|
if (!(typeof value === 'object' && value !== null)) {
|
|
232
301
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
@@ -243,50 +312,49 @@ var ValueErrors;
|
|
|
243
312
|
if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
244
313
|
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
245
314
|
}
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
315
|
+
const requiredKeys = globalThis.Array.isArray(schema.required) ? schema.required : [];
|
|
316
|
+
const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
317
|
+
const valueKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
318
|
+
for (const schemaKey of schemaKeys) {
|
|
319
|
+
const property = schema.properties[schemaKey];
|
|
320
|
+
if (schema.required && schema.required.includes(schemaKey)) {
|
|
321
|
+
yield* Visit(property, `${path}/${schemaKey}`, value[schemaKey]);
|
|
322
|
+
if (Types.ExtendsUndefined.Check(schema) && !(schemaKey in value)) {
|
|
323
|
+
yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${schemaKey}`, value: undefined, message: `Expected required property` };
|
|
251
324
|
}
|
|
252
325
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (propertyNames.includes(requiredKey))
|
|
258
|
-
continue;
|
|
259
|
-
yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
|
|
326
|
+
else {
|
|
327
|
+
if (schemaKey in value) {
|
|
328
|
+
yield* Visit(property, `${path}/${schemaKey}`, value[schemaKey]);
|
|
329
|
+
}
|
|
260
330
|
}
|
|
261
331
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
yield* Visit(schema.additionalProperties, references, `${path}/${propertyName}`, value[propertyName]);
|
|
267
|
-
}
|
|
332
|
+
for (const requiredKey of requiredKeys) {
|
|
333
|
+
if (valueKeys.includes(requiredKey))
|
|
334
|
+
continue;
|
|
335
|
+
yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
|
|
268
336
|
}
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if (extends_1.TypeExtends.Undefined(schema) && !(propertyKey in value)) {
|
|
274
|
-
yield { type: ValueErrorType.ObjectRequiredProperties, schema: propertySchema, path: `${path}/${propertyKey}`, value: undefined, message: `Expected required property` };
|
|
337
|
+
if (schema.additionalProperties === false) {
|
|
338
|
+
for (const valueKey of valueKeys) {
|
|
339
|
+
if (!schemaKeys.includes(valueKey)) {
|
|
340
|
+
yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
|
|
275
341
|
}
|
|
276
342
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
343
|
+
}
|
|
344
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
345
|
+
for (const valueKey of valueKeys) {
|
|
346
|
+
if (schemaKeys.includes(valueKey))
|
|
347
|
+
continue;
|
|
348
|
+
yield* Visit(schema.additionalProperties, `${path}/${valueKey}`, value[valueKey]);
|
|
281
349
|
}
|
|
282
350
|
}
|
|
283
351
|
}
|
|
284
|
-
function* Promise(schema,
|
|
352
|
+
function* Promise(schema, path, value) {
|
|
285
353
|
if (!(typeof value === 'object' && typeof value.then === 'function')) {
|
|
286
354
|
yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
|
|
287
355
|
}
|
|
288
356
|
}
|
|
289
|
-
function* Record(schema,
|
|
357
|
+
function* Record(schema, path, value) {
|
|
290
358
|
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
291
359
|
if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
|
|
292
360
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
@@ -306,22 +374,16 @@ var ValueErrors;
|
|
|
306
374
|
return yield { type, schema, path, value, message };
|
|
307
375
|
}
|
|
308
376
|
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
309
|
-
yield* Visit(valueSchema,
|
|
377
|
+
yield* Visit(valueSchema, `${path}/${propKey}`, propValue);
|
|
310
378
|
}
|
|
311
379
|
}
|
|
312
|
-
function* Ref(schema,
|
|
313
|
-
|
|
314
|
-
if (reference === undefined)
|
|
315
|
-
throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
316
|
-
yield* Visit(reference, references, path, value);
|
|
380
|
+
function* Ref(schema, path, value) {
|
|
381
|
+
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
|
|
317
382
|
}
|
|
318
|
-
function* Self(schema,
|
|
319
|
-
|
|
320
|
-
if (reference === undefined)
|
|
321
|
-
throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
322
|
-
yield* Visit(reference, references, path, value);
|
|
383
|
+
function* Self(schema, path, value) {
|
|
384
|
+
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
|
|
323
385
|
}
|
|
324
|
-
function* String(schema,
|
|
386
|
+
function* String(schema, path, value) {
|
|
325
387
|
if (!(typeof value === 'string')) {
|
|
326
388
|
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
327
389
|
}
|
|
@@ -338,18 +400,23 @@ var ValueErrors;
|
|
|
338
400
|
}
|
|
339
401
|
}
|
|
340
402
|
if (schema.format !== undefined) {
|
|
341
|
-
if (!
|
|
403
|
+
if (!Types.FormatRegistry.Has(schema.format)) {
|
|
342
404
|
yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
|
|
343
405
|
}
|
|
344
406
|
else {
|
|
345
|
-
const format =
|
|
407
|
+
const format = Types.FormatRegistry.Get(schema.format);
|
|
346
408
|
if (!format(value)) {
|
|
347
409
|
yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
|
|
348
410
|
}
|
|
349
411
|
}
|
|
350
412
|
}
|
|
351
413
|
}
|
|
352
|
-
function*
|
|
414
|
+
function* Symbol(schema, path, value) {
|
|
415
|
+
if (!(typeof value === 'symbol')) {
|
|
416
|
+
return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function* Tuple(schema, path, value) {
|
|
353
420
|
if (!globalThis.Array.isArray(value)) {
|
|
354
421
|
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
355
422
|
}
|
|
@@ -363,18 +430,18 @@ var ValueErrors;
|
|
|
363
430
|
return;
|
|
364
431
|
}
|
|
365
432
|
for (let i = 0; i < schema.items.length; i++) {
|
|
366
|
-
yield* Visit(schema.items[i],
|
|
433
|
+
yield* Visit(schema.items[i], `${path}/${i}`, value[i]);
|
|
367
434
|
}
|
|
368
435
|
}
|
|
369
|
-
function* Undefined(schema,
|
|
436
|
+
function* Undefined(schema, path, value) {
|
|
370
437
|
if (!(value === undefined)) {
|
|
371
438
|
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
372
439
|
}
|
|
373
440
|
}
|
|
374
|
-
function* Union(schema,
|
|
441
|
+
function* Union(schema, path, value) {
|
|
375
442
|
const errors = [];
|
|
376
443
|
for (const inner of schema.anyOf) {
|
|
377
|
-
const variantErrors = [...Visit(inner,
|
|
444
|
+
const variantErrors = [...Visit(inner, path, value)];
|
|
378
445
|
if (variantErrors.length === 0)
|
|
379
446
|
return;
|
|
380
447
|
errors.push(...variantErrors);
|
|
@@ -386,7 +453,7 @@ var ValueErrors;
|
|
|
386
453
|
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
387
454
|
}
|
|
388
455
|
}
|
|
389
|
-
function* Uint8Array(schema,
|
|
456
|
+
function* Uint8Array(schema, path, value) {
|
|
390
457
|
if (!(value instanceof globalThis.Uint8Array)) {
|
|
391
458
|
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
392
459
|
}
|
|
@@ -397,76 +464,90 @@ var ValueErrors;
|
|
|
397
464
|
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
398
465
|
}
|
|
399
466
|
}
|
|
400
|
-
function* Unknown(schema,
|
|
401
|
-
function* Void(schema,
|
|
402
|
-
if (
|
|
403
|
-
|
|
467
|
+
function* Unknown(schema, path, value) { }
|
|
468
|
+
function* Void(schema, path, value) {
|
|
469
|
+
if (index_1.TypeSystem.AllowVoidNull) {
|
|
470
|
+
if (!(value === undefined || value === null)) {
|
|
471
|
+
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null or undefined` };
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
if (!(value === undefined)) {
|
|
476
|
+
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected undefined` };
|
|
477
|
+
}
|
|
404
478
|
}
|
|
405
479
|
}
|
|
406
|
-
function* UserDefined(schema,
|
|
407
|
-
const
|
|
408
|
-
if (!
|
|
480
|
+
function* UserDefined(schema, path, value) {
|
|
481
|
+
const check = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
482
|
+
if (!check(schema, value)) {
|
|
409
483
|
return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
|
|
410
484
|
}
|
|
411
485
|
}
|
|
412
|
-
function* Visit(schema,
|
|
413
|
-
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
486
|
+
function* Visit(schema, path, value) {
|
|
414
487
|
const anySchema = schema;
|
|
415
488
|
switch (anySchema[Types.Kind]) {
|
|
416
489
|
case 'Any':
|
|
417
|
-
return yield* Any(anySchema,
|
|
490
|
+
return yield* Any(anySchema, path, value);
|
|
418
491
|
case 'Array':
|
|
419
|
-
return yield* Array(anySchema,
|
|
492
|
+
return yield* Array(anySchema, path, value);
|
|
493
|
+
case 'BigInt':
|
|
494
|
+
return yield* BigInt(anySchema, path, value);
|
|
420
495
|
case 'Boolean':
|
|
421
|
-
return yield* Boolean(anySchema,
|
|
496
|
+
return yield* Boolean(anySchema, path, value);
|
|
422
497
|
case 'Constructor':
|
|
423
|
-
return yield* Constructor(anySchema,
|
|
498
|
+
return yield* Constructor(anySchema, path, value);
|
|
424
499
|
case 'Date':
|
|
425
|
-
return yield* Date(anySchema,
|
|
500
|
+
return yield* Date(anySchema, path, value);
|
|
426
501
|
case 'Function':
|
|
427
|
-
return yield* Function(anySchema,
|
|
502
|
+
return yield* Function(anySchema, path, value);
|
|
428
503
|
case 'Integer':
|
|
429
|
-
return yield* Integer(anySchema,
|
|
504
|
+
return yield* Integer(anySchema, path, value);
|
|
505
|
+
case 'Intersect':
|
|
506
|
+
return yield* Intersect(anySchema, path, value);
|
|
430
507
|
case 'Literal':
|
|
431
|
-
return yield* Literal(anySchema,
|
|
508
|
+
return yield* Literal(anySchema, path, value);
|
|
432
509
|
case 'Never':
|
|
433
|
-
return yield* Never(anySchema,
|
|
510
|
+
return yield* Never(anySchema, path, value);
|
|
511
|
+
case 'Not':
|
|
512
|
+
return yield* Not(anySchema, path, value);
|
|
434
513
|
case 'Null':
|
|
435
|
-
return yield* Null(anySchema,
|
|
514
|
+
return yield* Null(anySchema, path, value);
|
|
436
515
|
case 'Number':
|
|
437
|
-
return yield* Number(anySchema,
|
|
516
|
+
return yield* Number(anySchema, path, value);
|
|
438
517
|
case 'Object':
|
|
439
|
-
return yield* Object(anySchema,
|
|
518
|
+
return yield* Object(anySchema, path, value);
|
|
440
519
|
case 'Promise':
|
|
441
|
-
return yield* Promise(anySchema,
|
|
520
|
+
return yield* Promise(anySchema, path, value);
|
|
442
521
|
case 'Record':
|
|
443
|
-
return yield* Record(anySchema,
|
|
522
|
+
return yield* Record(anySchema, path, value);
|
|
444
523
|
case 'Ref':
|
|
445
|
-
return yield* Ref(anySchema,
|
|
524
|
+
return yield* Ref(anySchema, path, value);
|
|
446
525
|
case 'Self':
|
|
447
|
-
return yield* Self(anySchema,
|
|
526
|
+
return yield* Self(anySchema, path, value);
|
|
448
527
|
case 'String':
|
|
449
|
-
return yield* String(anySchema,
|
|
528
|
+
return yield* String(anySchema, path, value);
|
|
529
|
+
case 'Symbol':
|
|
530
|
+
return yield* Symbol(anySchema, path, value);
|
|
450
531
|
case 'Tuple':
|
|
451
|
-
return yield* Tuple(anySchema,
|
|
532
|
+
return yield* Tuple(anySchema, path, value);
|
|
452
533
|
case 'Undefined':
|
|
453
|
-
return yield* Undefined(anySchema,
|
|
534
|
+
return yield* Undefined(anySchema, path, value);
|
|
454
535
|
case 'Union':
|
|
455
|
-
return yield* Union(anySchema,
|
|
536
|
+
return yield* Union(anySchema, path, value);
|
|
456
537
|
case 'Uint8Array':
|
|
457
|
-
return yield* Uint8Array(anySchema,
|
|
538
|
+
return yield* Uint8Array(anySchema, path, value);
|
|
458
539
|
case 'Unknown':
|
|
459
|
-
return yield* Unknown(anySchema,
|
|
540
|
+
return yield* Unknown(anySchema, path, value);
|
|
460
541
|
case 'Void':
|
|
461
|
-
return yield* Void(anySchema,
|
|
542
|
+
return yield* Void(anySchema, path, value);
|
|
462
543
|
default:
|
|
463
|
-
if (!
|
|
544
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
464
545
|
throw new ValueErrorsUnknownTypeError(schema);
|
|
465
|
-
return yield* UserDefined(anySchema,
|
|
546
|
+
return yield* UserDefined(anySchema, path, value);
|
|
466
547
|
}
|
|
467
548
|
}
|
|
468
|
-
function* Errors(schema,
|
|
469
|
-
yield* Visit(schema,
|
|
549
|
+
function* Errors(schema, value) {
|
|
550
|
+
yield* Visit(schema, '', value);
|
|
470
551
|
}
|
|
471
552
|
ValueErrors.Errors = Errors;
|
|
472
553
|
})(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0-dev.1",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -14,12 +14,7 @@
|
|
|
14
14
|
"types": "./typebox.d.ts",
|
|
15
15
|
"exports": {
|
|
16
16
|
"./compiler": "./compiler/index.js",
|
|
17
|
-
"./conditional": "./conditional/index.js",
|
|
18
|
-
"./custom": "./custom/index.js",
|
|
19
17
|
"./errors": "./errors/index.js",
|
|
20
|
-
"./format": "./format/index.js",
|
|
21
|
-
"./guard": "./guard/index.js",
|
|
22
|
-
"./hash": "./hash/index.js",
|
|
23
18
|
"./system": "./system/index.js",
|
|
24
19
|
"./value": "./value/index.js",
|
|
25
20
|
".": "./typebox.js"
|