@sinclair/typebox 0.25.23 → 0.26.0-dev
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 +9 -4
- package/compiler/compiler.js +160 -119
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -149
- package/package.json +1 -6
- package/readme.md +395 -396
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +386 -162
- package/typebox.js +1710 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +120 -192
- package/value/check.d.ts +1 -1
- package/value/check.js +162 -107
- 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 -107
- 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/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -1
- package/guard/index.js +0 -44
- 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,9 +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_3 = require("../custom/index");
|
|
35
|
-
const index_4 = require("../hash/index");
|
|
33
|
+
const hash_1 = require("../value/hash");
|
|
36
34
|
// -------------------------------------------------------------------
|
|
37
35
|
// ValueErrorType
|
|
38
36
|
// -------------------------------------------------------------------
|
|
@@ -42,51 +40,61 @@ var ValueErrorType;
|
|
|
42
40
|
ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
|
|
43
41
|
ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
|
|
44
42
|
ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
|
|
45
|
-
ValueErrorType[ValueErrorType["
|
|
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["
|
|
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";
|
|
90
98
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
91
99
|
// -------------------------------------------------------------------
|
|
92
100
|
// ValueErrors
|
|
@@ -101,11 +109,14 @@ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
|
101
109
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
102
110
|
var ValueErrors;
|
|
103
111
|
(function (ValueErrors) {
|
|
112
|
+
function IsBigInt(value) {
|
|
113
|
+
return typeof value === 'bigint';
|
|
114
|
+
}
|
|
104
115
|
function IsNumber(value) {
|
|
105
|
-
return typeof value === 'number' &&
|
|
116
|
+
return typeof value === 'number' && globalThis.isFinite(value);
|
|
106
117
|
}
|
|
107
|
-
function* Any(schema,
|
|
108
|
-
function* Array(schema,
|
|
118
|
+
function* Any(schema, path, value) { }
|
|
119
|
+
function* Array(schema, path, value) {
|
|
109
120
|
if (!globalThis.Array.isArray(value)) {
|
|
110
121
|
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
111
122
|
}
|
|
@@ -117,7 +128,7 @@ var ValueErrors;
|
|
|
117
128
|
}
|
|
118
129
|
// prettier-ignore
|
|
119
130
|
if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
|
|
120
|
-
const hashed =
|
|
131
|
+
const hashed = hash_1.ValueHash.Create(element);
|
|
121
132
|
if (set.has(hashed)) {
|
|
122
133
|
return false;
|
|
123
134
|
}
|
|
@@ -128,22 +139,42 @@ var ValueErrors;
|
|
|
128
139
|
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
129
140
|
}
|
|
130
141
|
for (let i = 0; i < value.length; i++) {
|
|
131
|
-
yield* Visit(schema.items,
|
|
142
|
+
yield* Visit(schema.items, `${path}/${i}`, value[i]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
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}` };
|
|
132
163
|
}
|
|
133
164
|
}
|
|
134
|
-
function* Boolean(schema,
|
|
165
|
+
function* Boolean(schema, path, value) {
|
|
135
166
|
if (!(typeof value === 'boolean')) {
|
|
136
167
|
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
137
168
|
}
|
|
138
169
|
}
|
|
139
|
-
function* Constructor(schema,
|
|
140
|
-
yield* Visit(schema.returns,
|
|
170
|
+
function* Constructor(schema, path, value) {
|
|
171
|
+
yield* Visit(schema.returns, path, value.prototype);
|
|
141
172
|
}
|
|
142
|
-
function* Date(schema,
|
|
173
|
+
function* Date(schema, path, value) {
|
|
143
174
|
if (!(value instanceof globalThis.Date)) {
|
|
144
175
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
145
176
|
}
|
|
146
|
-
if (
|
|
177
|
+
if (!globalThis.isFinite(value.getTime())) {
|
|
147
178
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
|
|
148
179
|
}
|
|
149
180
|
if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
@@ -159,12 +190,12 @@ var ValueErrors;
|
|
|
159
190
|
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
160
191
|
}
|
|
161
192
|
}
|
|
162
|
-
function* Function(schema,
|
|
193
|
+
function* Function(schema, path, value) {
|
|
163
194
|
if (!(typeof value === 'function')) {
|
|
164
195
|
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
165
196
|
}
|
|
166
197
|
}
|
|
167
|
-
function* Integer(schema,
|
|
198
|
+
function* Integer(schema, path, value) {
|
|
168
199
|
if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
|
|
169
200
|
return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
170
201
|
}
|
|
@@ -184,28 +215,67 @@ var ValueErrors;
|
|
|
184
215
|
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
185
216
|
}
|
|
186
217
|
}
|
|
187
|
-
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) {
|
|
188
252
|
if (!(value === schema.const)) {
|
|
189
253
|
const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
|
|
190
254
|
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
191
255
|
}
|
|
192
256
|
}
|
|
193
|
-
function* Never(schema,
|
|
257
|
+
function* Never(schema, path, value) {
|
|
194
258
|
yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
|
|
195
259
|
}
|
|
196
|
-
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) {
|
|
197
267
|
if (!(value === null)) {
|
|
198
268
|
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
199
269
|
}
|
|
200
270
|
}
|
|
201
|
-
function* Number(schema,
|
|
271
|
+
function* Number(schema, path, value) {
|
|
202
272
|
if (index_1.TypeSystem.AllowNaN) {
|
|
203
273
|
if (!(typeof value === 'number')) {
|
|
204
274
|
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
205
275
|
}
|
|
206
276
|
}
|
|
207
277
|
else {
|
|
208
|
-
if (!(typeof value === 'number' &&
|
|
278
|
+
if (!(typeof value === 'number' && globalThis.Number.isFinite(value))) {
|
|
209
279
|
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
210
280
|
}
|
|
211
281
|
}
|
|
@@ -225,7 +295,7 @@ var ValueErrors;
|
|
|
225
295
|
yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
226
296
|
}
|
|
227
297
|
}
|
|
228
|
-
function* Object(schema,
|
|
298
|
+
function* Object(schema, path, value) {
|
|
229
299
|
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
230
300
|
if (!(typeof value === 'object' && value !== null)) {
|
|
231
301
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
@@ -242,47 +312,49 @@ var ValueErrors;
|
|
|
242
312
|
if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
243
313
|
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
244
314
|
}
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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` };
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
if (schemaKey in value) {
|
|
328
|
+
yield* Visit(property, `${path}/${schemaKey}`, value[schemaKey]);
|
|
250
329
|
}
|
|
251
330
|
}
|
|
252
331
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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` };
|
|
336
|
+
}
|
|
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` };
|
|
341
|
+
}
|
|
259
342
|
}
|
|
260
343
|
}
|
|
261
344
|
if (typeof schema.additionalProperties === 'object') {
|
|
262
|
-
for (const
|
|
263
|
-
if (
|
|
345
|
+
for (const valueKey of valueKeys) {
|
|
346
|
+
if (schemaKeys.includes(valueKey))
|
|
264
347
|
continue;
|
|
265
|
-
yield* Visit(schema.additionalProperties,
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
for (const propertyKey of propertyNames) {
|
|
269
|
-
const propertySchema = schema.properties[propertyKey];
|
|
270
|
-
if (schema.required && schema.required.includes(propertyKey)) {
|
|
271
|
-
yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
if (value[propertyKey] !== undefined) {
|
|
275
|
-
yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
|
|
276
|
-
}
|
|
348
|
+
yield* Visit(schema.additionalProperties, `${path}/${valueKey}`, value[valueKey]);
|
|
277
349
|
}
|
|
278
350
|
}
|
|
279
351
|
}
|
|
280
|
-
function* Promise(schema,
|
|
352
|
+
function* Promise(schema, path, value) {
|
|
281
353
|
if (!(typeof value === 'object' && typeof value.then === 'function')) {
|
|
282
354
|
yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
|
|
283
355
|
}
|
|
284
356
|
}
|
|
285
|
-
function* Record(schema,
|
|
357
|
+
function* Record(schema, path, value) {
|
|
286
358
|
if (index_1.TypeSystem.AllowArrayObjects) {
|
|
287
359
|
if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
|
|
288
360
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
@@ -302,22 +374,16 @@ var ValueErrors;
|
|
|
302
374
|
return yield { type, schema, path, value, message };
|
|
303
375
|
}
|
|
304
376
|
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
305
|
-
yield* Visit(valueSchema,
|
|
377
|
+
yield* Visit(valueSchema, `${path}/${propKey}`, propValue);
|
|
306
378
|
}
|
|
307
379
|
}
|
|
308
|
-
function* Ref(schema,
|
|
309
|
-
|
|
310
|
-
if (reference === undefined)
|
|
311
|
-
throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
|
|
312
|
-
yield* Visit(reference, references, path, value);
|
|
380
|
+
function* Ref(schema, path, value) {
|
|
381
|
+
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
|
|
313
382
|
}
|
|
314
|
-
function* Self(schema,
|
|
315
|
-
|
|
316
|
-
if (reference === undefined)
|
|
317
|
-
throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
|
|
318
|
-
yield* Visit(reference, references, path, value);
|
|
383
|
+
function* Self(schema, path, value) {
|
|
384
|
+
yield* Visit(Types.ReferenceRegistry.DerefOne(schema), path, value);
|
|
319
385
|
}
|
|
320
|
-
function* String(schema,
|
|
386
|
+
function* String(schema, path, value) {
|
|
321
387
|
if (!(typeof value === 'string')) {
|
|
322
388
|
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
323
389
|
}
|
|
@@ -334,18 +400,23 @@ var ValueErrors;
|
|
|
334
400
|
}
|
|
335
401
|
}
|
|
336
402
|
if (schema.format !== undefined) {
|
|
337
|
-
if (!
|
|
403
|
+
if (!Types.FormatRegistry.Has(schema.format)) {
|
|
338
404
|
yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
|
|
339
405
|
}
|
|
340
406
|
else {
|
|
341
|
-
const format =
|
|
407
|
+
const format = Types.FormatRegistry.Get(schema.format);
|
|
342
408
|
if (!format(value)) {
|
|
343
409
|
yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
|
|
344
410
|
}
|
|
345
411
|
}
|
|
346
412
|
}
|
|
347
413
|
}
|
|
348
|
-
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) {
|
|
349
420
|
if (!globalThis.Array.isArray(value)) {
|
|
350
421
|
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
351
422
|
}
|
|
@@ -359,18 +430,18 @@ var ValueErrors;
|
|
|
359
430
|
return;
|
|
360
431
|
}
|
|
361
432
|
for (let i = 0; i < schema.items.length; i++) {
|
|
362
|
-
yield* Visit(schema.items[i],
|
|
433
|
+
yield* Visit(schema.items[i], `${path}/${i}`, value[i]);
|
|
363
434
|
}
|
|
364
435
|
}
|
|
365
|
-
function* Undefined(schema,
|
|
436
|
+
function* Undefined(schema, path, value) {
|
|
366
437
|
if (!(value === undefined)) {
|
|
367
438
|
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
368
439
|
}
|
|
369
440
|
}
|
|
370
|
-
function* Union(schema,
|
|
441
|
+
function* Union(schema, path, value) {
|
|
371
442
|
const errors = [];
|
|
372
443
|
for (const inner of schema.anyOf) {
|
|
373
|
-
const variantErrors = [...Visit(inner,
|
|
444
|
+
const variantErrors = [...Visit(inner, path, value)];
|
|
374
445
|
if (variantErrors.length === 0)
|
|
375
446
|
return;
|
|
376
447
|
errors.push(...variantErrors);
|
|
@@ -382,7 +453,7 @@ var ValueErrors;
|
|
|
382
453
|
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
383
454
|
}
|
|
384
455
|
}
|
|
385
|
-
function* Uint8Array(schema,
|
|
456
|
+
function* Uint8Array(schema, path, value) {
|
|
386
457
|
if (!(value instanceof globalThis.Uint8Array)) {
|
|
387
458
|
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
388
459
|
}
|
|
@@ -393,76 +464,90 @@ var ValueErrors;
|
|
|
393
464
|
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
394
465
|
}
|
|
395
466
|
}
|
|
396
|
-
function* Unknown(schema,
|
|
397
|
-
function* Void(schema,
|
|
398
|
-
if (
|
|
399
|
-
|
|
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
|
+
}
|
|
400
478
|
}
|
|
401
479
|
}
|
|
402
|
-
function* UserDefined(schema,
|
|
403
|
-
const
|
|
404
|
-
if (!
|
|
480
|
+
function* UserDefined(schema, path, value) {
|
|
481
|
+
const check = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
482
|
+
if (!check(schema, value)) {
|
|
405
483
|
return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
|
|
406
484
|
}
|
|
407
485
|
}
|
|
408
|
-
function* Visit(schema,
|
|
409
|
-
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
486
|
+
function* Visit(schema, path, value) {
|
|
410
487
|
const anySchema = schema;
|
|
411
488
|
switch (anySchema[Types.Kind]) {
|
|
412
489
|
case 'Any':
|
|
413
|
-
return yield* Any(anySchema,
|
|
490
|
+
return yield* Any(anySchema, path, value);
|
|
414
491
|
case 'Array':
|
|
415
|
-
return yield* Array(anySchema,
|
|
492
|
+
return yield* Array(anySchema, path, value);
|
|
493
|
+
case 'BigInt':
|
|
494
|
+
return yield* BigInt(anySchema, path, value);
|
|
416
495
|
case 'Boolean':
|
|
417
|
-
return yield* Boolean(anySchema,
|
|
496
|
+
return yield* Boolean(anySchema, path, value);
|
|
418
497
|
case 'Constructor':
|
|
419
|
-
return yield* Constructor(anySchema,
|
|
498
|
+
return yield* Constructor(anySchema, path, value);
|
|
420
499
|
case 'Date':
|
|
421
|
-
return yield* Date(anySchema,
|
|
500
|
+
return yield* Date(anySchema, path, value);
|
|
422
501
|
case 'Function':
|
|
423
|
-
return yield* Function(anySchema,
|
|
502
|
+
return yield* Function(anySchema, path, value);
|
|
424
503
|
case 'Integer':
|
|
425
|
-
return yield* Integer(anySchema,
|
|
504
|
+
return yield* Integer(anySchema, path, value);
|
|
505
|
+
case 'Intersect':
|
|
506
|
+
return yield* Intersect(anySchema, path, value);
|
|
426
507
|
case 'Literal':
|
|
427
|
-
return yield* Literal(anySchema,
|
|
508
|
+
return yield* Literal(anySchema, path, value);
|
|
428
509
|
case 'Never':
|
|
429
|
-
return yield* Never(anySchema,
|
|
510
|
+
return yield* Never(anySchema, path, value);
|
|
511
|
+
case 'Not':
|
|
512
|
+
return yield* Not(anySchema, path, value);
|
|
430
513
|
case 'Null':
|
|
431
|
-
return yield* Null(anySchema,
|
|
514
|
+
return yield* Null(anySchema, path, value);
|
|
432
515
|
case 'Number':
|
|
433
|
-
return yield* Number(anySchema,
|
|
516
|
+
return yield* Number(anySchema, path, value);
|
|
434
517
|
case 'Object':
|
|
435
|
-
return yield* Object(anySchema,
|
|
518
|
+
return yield* Object(anySchema, path, value);
|
|
436
519
|
case 'Promise':
|
|
437
|
-
return yield* Promise(anySchema,
|
|
520
|
+
return yield* Promise(anySchema, path, value);
|
|
438
521
|
case 'Record':
|
|
439
|
-
return yield* Record(anySchema,
|
|
522
|
+
return yield* Record(anySchema, path, value);
|
|
440
523
|
case 'Ref':
|
|
441
|
-
return yield* Ref(anySchema,
|
|
524
|
+
return yield* Ref(anySchema, path, value);
|
|
442
525
|
case 'Self':
|
|
443
|
-
return yield* Self(anySchema,
|
|
526
|
+
return yield* Self(anySchema, path, value);
|
|
444
527
|
case 'String':
|
|
445
|
-
return yield* String(anySchema,
|
|
528
|
+
return yield* String(anySchema, path, value);
|
|
529
|
+
case 'Symbol':
|
|
530
|
+
return yield* Symbol(anySchema, path, value);
|
|
446
531
|
case 'Tuple':
|
|
447
|
-
return yield* Tuple(anySchema,
|
|
532
|
+
return yield* Tuple(anySchema, path, value);
|
|
448
533
|
case 'Undefined':
|
|
449
|
-
return yield* Undefined(anySchema,
|
|
534
|
+
return yield* Undefined(anySchema, path, value);
|
|
450
535
|
case 'Union':
|
|
451
|
-
return yield* Union(anySchema,
|
|
536
|
+
return yield* Union(anySchema, path, value);
|
|
452
537
|
case 'Uint8Array':
|
|
453
|
-
return yield* Uint8Array(anySchema,
|
|
538
|
+
return yield* Uint8Array(anySchema, path, value);
|
|
454
539
|
case 'Unknown':
|
|
455
|
-
return yield* Unknown(anySchema,
|
|
540
|
+
return yield* Unknown(anySchema, path, value);
|
|
456
541
|
case 'Void':
|
|
457
|
-
return yield* Void(anySchema,
|
|
542
|
+
return yield* Void(anySchema, path, value);
|
|
458
543
|
default:
|
|
459
|
-
if (!
|
|
544
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
460
545
|
throw new ValueErrorsUnknownTypeError(schema);
|
|
461
|
-
return yield* UserDefined(anySchema,
|
|
546
|
+
return yield* UserDefined(anySchema, path, value);
|
|
462
547
|
}
|
|
463
548
|
}
|
|
464
|
-
function* Errors(schema,
|
|
465
|
-
yield* Visit(schema,
|
|
549
|
+
function* Errors(schema, value) {
|
|
550
|
+
yield* Visit(schema, '', value);
|
|
466
551
|
}
|
|
467
552
|
ValueErrors.Errors = Errors;
|
|
468
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",
|
|
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"
|