@sinclair/typebox 0.26.0-dev.4 → 0.26.0-dev.5
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 +121 -97
- package/errors/errors.d.ts +5 -1
- package/errors/errors.js +173 -157
- package/package.json +1 -1
- package/readme.md +70 -71
- package/typebox.d.ts +16 -27
- package/typebox.js +104 -163
- package/value/cast.d.ts +6 -2
- package/value/cast.js +130 -111
- package/value/check.d.ts +5 -1
- package/value/check.js +168 -154
- package/value/convert.d.ts +6 -6
- package/value/convert.js +83 -74
- package/value/create.d.ts +6 -2
- package/value/create.js +102 -77
- package/value/value.d.ts +13 -3
- package/value/value.js +15 -15
package/errors/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
3
|
+
exports.ValueErrors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
4
4
|
/*--------------------------------------------------------------------------
|
|
5
5
|
|
|
6
6
|
@sinclair/typebox/errors
|
|
@@ -123,24 +123,58 @@ class ValueErrorsUnknownTypeError extends Error {
|
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
126
|
+
class ValueErrorsDereferenceError extends Error {
|
|
127
|
+
constructor(schema) {
|
|
128
|
+
super(`ValueErrors: Unable to dereference schema with $id '${schema.$ref}'`);
|
|
129
|
+
this.schema = schema;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.ValueErrorsDereferenceError = ValueErrorsDereferenceError;
|
|
126
133
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
127
134
|
var ValueErrors;
|
|
128
135
|
(function (ValueErrors) {
|
|
136
|
+
// ----------------------------------------------------------------------
|
|
137
|
+
// Guards
|
|
138
|
+
// ----------------------------------------------------------------------
|
|
139
|
+
function IsObject(value) {
|
|
140
|
+
const result = typeof value === 'object' && value !== null;
|
|
141
|
+
return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value);
|
|
142
|
+
}
|
|
143
|
+
function IsRecordObject(value) {
|
|
144
|
+
return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array);
|
|
145
|
+
}
|
|
129
146
|
function IsBigInt(value) {
|
|
130
147
|
return typeof value === 'bigint';
|
|
131
148
|
}
|
|
132
149
|
function IsNumber(value) {
|
|
133
|
-
|
|
150
|
+
const result = typeof value === 'number';
|
|
151
|
+
return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value);
|
|
152
|
+
}
|
|
153
|
+
function IsInteger(value) {
|
|
154
|
+
return globalThis.Number.isInteger(value);
|
|
134
155
|
}
|
|
135
|
-
function
|
|
136
|
-
|
|
156
|
+
function IsString(value) {
|
|
157
|
+
return typeof value === 'string';
|
|
158
|
+
}
|
|
159
|
+
function IsVoid(value) {
|
|
160
|
+
const result = value === undefined;
|
|
161
|
+
return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
|
|
162
|
+
}
|
|
163
|
+
function IsDefined(value) {
|
|
164
|
+
return value !== undefined;
|
|
165
|
+
}
|
|
166
|
+
// ----------------------------------------------------------------------
|
|
167
|
+
// Types
|
|
168
|
+
// ----------------------------------------------------------------------
|
|
169
|
+
function* Any(schema, references, path, value) { }
|
|
170
|
+
function* Array(schema, references, path, value) {
|
|
137
171
|
if (!globalThis.Array.isArray(value)) {
|
|
138
172
|
return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
|
|
139
173
|
}
|
|
140
|
-
if (
|
|
174
|
+
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
141
175
|
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
|
|
142
176
|
}
|
|
143
|
-
if (
|
|
177
|
+
if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
|
|
144
178
|
yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
|
|
145
179
|
}
|
|
146
180
|
// prettier-ignore
|
|
@@ -156,85 +190,85 @@ var ValueErrors;
|
|
|
156
190
|
yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
|
|
157
191
|
}
|
|
158
192
|
for (let i = 0; i < value.length; i++) {
|
|
159
|
-
yield* Visit(schema.items, `${path}/${i}`, value[i]);
|
|
193
|
+
yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
|
|
160
194
|
}
|
|
161
195
|
}
|
|
162
|
-
function* BigInt(schema, path, value) {
|
|
163
|
-
if (!(
|
|
196
|
+
function* BigInt(schema, references, path, value) {
|
|
197
|
+
if (!IsBigInt(value)) {
|
|
164
198
|
return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
|
|
165
199
|
}
|
|
166
|
-
if (
|
|
200
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
|
|
167
201
|
yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
|
|
168
202
|
}
|
|
169
|
-
if (
|
|
203
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
170
204
|
yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
|
|
171
205
|
}
|
|
172
|
-
if (
|
|
206
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
173
207
|
yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
|
|
174
208
|
}
|
|
175
|
-
if (
|
|
209
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
176
210
|
yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
|
|
177
211
|
}
|
|
178
|
-
if (
|
|
212
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
179
213
|
yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
|
|
180
214
|
}
|
|
181
215
|
}
|
|
182
|
-
function* Boolean(schema, path, value) {
|
|
216
|
+
function* Boolean(schema, references, path, value) {
|
|
183
217
|
if (!(typeof value === 'boolean')) {
|
|
184
218
|
return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
|
|
185
219
|
}
|
|
186
220
|
}
|
|
187
|
-
function* Constructor(schema, path, value) {
|
|
188
|
-
yield* Visit(schema.returns, path, value.prototype);
|
|
221
|
+
function* Constructor(schema, references, path, value) {
|
|
222
|
+
yield* Visit(schema.returns, references, path, value.prototype);
|
|
189
223
|
}
|
|
190
|
-
function* Date(schema, path, value) {
|
|
224
|
+
function* Date(schema, references, path, value) {
|
|
191
225
|
if (!(value instanceof globalThis.Date)) {
|
|
192
226
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
|
|
193
227
|
}
|
|
194
228
|
if (!globalThis.isFinite(value.getTime())) {
|
|
195
229
|
return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
|
|
196
230
|
}
|
|
197
|
-
if (
|
|
231
|
+
if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
198
232
|
yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
|
|
199
233
|
}
|
|
200
|
-
if (
|
|
234
|
+
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
201
235
|
yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
|
|
202
236
|
}
|
|
203
|
-
if (
|
|
237
|
+
if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
204
238
|
yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
|
|
205
239
|
}
|
|
206
|
-
if (
|
|
240
|
+
if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
207
241
|
yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
|
|
208
242
|
}
|
|
209
243
|
}
|
|
210
|
-
function* Function(schema, path, value) {
|
|
244
|
+
function* Function(schema, references, path, value) {
|
|
211
245
|
if (!(typeof value === 'function')) {
|
|
212
246
|
return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
|
|
213
247
|
}
|
|
214
248
|
}
|
|
215
|
-
function* Integer(schema, path, value) {
|
|
216
|
-
if (!(
|
|
249
|
+
function* Integer(schema, references, path, value) {
|
|
250
|
+
if (!IsInteger(value)) {
|
|
217
251
|
return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
|
|
218
252
|
}
|
|
219
|
-
if (
|
|
253
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
220
254
|
yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
|
|
221
255
|
}
|
|
222
|
-
if (
|
|
256
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
223
257
|
yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
|
|
224
258
|
}
|
|
225
|
-
if (
|
|
259
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
226
260
|
yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
|
|
227
261
|
}
|
|
228
|
-
if (
|
|
262
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
229
263
|
yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
|
|
230
264
|
}
|
|
231
|
-
if (
|
|
265
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
232
266
|
yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
|
|
233
267
|
}
|
|
234
268
|
}
|
|
235
|
-
function* Intersect(schema, path, value) {
|
|
269
|
+
function* Intersect(schema, references, path, value) {
|
|
236
270
|
for (const subschema of schema.allOf) {
|
|
237
|
-
const next = Visit(subschema, path, value).next();
|
|
271
|
+
const next = Visit(subschema, references, path, value).next();
|
|
238
272
|
if (!next.done) {
|
|
239
273
|
yield next.value;
|
|
240
274
|
yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
|
|
@@ -255,7 +289,7 @@ var ValueErrors;
|
|
|
255
289
|
const valueKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
256
290
|
for (const valueKey of valueKeys) {
|
|
257
291
|
if (!schemaKeys.includes(valueKey)) {
|
|
258
|
-
const next = Visit(schema.unevaluatedProperties, `${path}/${valueKey}`, value[valueKey]).next();
|
|
292
|
+
const next = Visit(schema.unevaluatedProperties, references, `${path}/${valueKey}`, value[valueKey]).next();
|
|
259
293
|
if (!next.done) {
|
|
260
294
|
yield next.value;
|
|
261
295
|
yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
|
|
@@ -265,122 +299,101 @@ var ValueErrors;
|
|
|
265
299
|
}
|
|
266
300
|
}
|
|
267
301
|
}
|
|
268
|
-
function* Literal(schema, path, value) {
|
|
302
|
+
function* Literal(schema, references, path, value) {
|
|
269
303
|
if (!(value === schema.const)) {
|
|
270
304
|
const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
|
|
271
305
|
return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
|
|
272
306
|
}
|
|
273
307
|
}
|
|
274
|
-
function* Never(schema, path, value) {
|
|
308
|
+
function* Never(schema, references, path, value) {
|
|
275
309
|
yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
|
|
276
310
|
}
|
|
277
|
-
function* Not(schema, path, value) {
|
|
278
|
-
if (Visit(schema.allOf[0].not, path, value).next().done === true) {
|
|
311
|
+
function* Not(schema, references, path, value) {
|
|
312
|
+
if (Visit(schema.allOf[0].not, references, path, value).next().done === true) {
|
|
279
313
|
yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
|
|
280
314
|
}
|
|
281
|
-
yield* Visit(schema.allOf[1], path, value);
|
|
315
|
+
yield* Visit(schema.allOf[1], references, path, value);
|
|
282
316
|
}
|
|
283
|
-
function* Null(schema, path, value) {
|
|
317
|
+
function* Null(schema, references, path, value) {
|
|
284
318
|
if (!(value === null)) {
|
|
285
319
|
return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
|
|
286
320
|
}
|
|
287
321
|
}
|
|
288
|
-
function* Number(schema, path, value) {
|
|
289
|
-
if (
|
|
290
|
-
|
|
291
|
-
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
if (!(typeof value === 'number' && globalThis.Number.isFinite(value))) {
|
|
296
|
-
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
297
|
-
}
|
|
322
|
+
function* Number(schema, references, path, value) {
|
|
323
|
+
if (!IsNumber(value)) {
|
|
324
|
+
return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
|
|
298
325
|
}
|
|
299
|
-
if (
|
|
326
|
+
if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
|
|
300
327
|
yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
|
|
301
328
|
}
|
|
302
|
-
if (
|
|
329
|
+
if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
|
|
303
330
|
yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
|
|
304
331
|
}
|
|
305
|
-
if (
|
|
332
|
+
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
306
333
|
yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
|
|
307
334
|
}
|
|
308
|
-
if (
|
|
335
|
+
if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
|
|
309
336
|
yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
|
|
310
337
|
}
|
|
311
|
-
if (
|
|
338
|
+
if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
|
|
312
339
|
yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
|
|
313
340
|
}
|
|
314
341
|
}
|
|
315
|
-
function* Object(schema, path, value) {
|
|
316
|
-
if (
|
|
317
|
-
|
|
318
|
-
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
323
|
-
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
324
|
-
}
|
|
342
|
+
function* Object(schema, references, path, value) {
|
|
343
|
+
if (!IsObject(value)) {
|
|
344
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
325
345
|
}
|
|
326
|
-
if (
|
|
346
|
+
if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
327
347
|
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
328
348
|
}
|
|
329
|
-
if (
|
|
349
|
+
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
330
350
|
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
331
351
|
}
|
|
332
352
|
const requiredKeys = globalThis.Array.isArray(schema.required) ? schema.required : [];
|
|
333
|
-
const
|
|
334
|
-
const
|
|
335
|
-
for (const
|
|
336
|
-
const property = schema.properties[
|
|
337
|
-
if (schema.required && schema.required.includes(
|
|
338
|
-
yield* Visit(property, `${path}/${
|
|
339
|
-
if (Types.ExtendsUndefined.Check(schema) && !(
|
|
340
|
-
yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${
|
|
353
|
+
const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
354
|
+
const unknownKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
355
|
+
for (const knownKey of knownKeys) {
|
|
356
|
+
const property = schema.properties[knownKey];
|
|
357
|
+
if (schema.required && schema.required.includes(knownKey)) {
|
|
358
|
+
yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
|
|
359
|
+
if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) {
|
|
360
|
+
yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${knownKey}`, value: undefined, message: `Expected required property` };
|
|
341
361
|
}
|
|
342
362
|
}
|
|
343
363
|
else {
|
|
344
|
-
if (
|
|
345
|
-
yield* Visit(property, `${path}/${
|
|
364
|
+
if (knownKey in value) {
|
|
365
|
+
yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
|
|
346
366
|
}
|
|
347
367
|
}
|
|
348
368
|
}
|
|
349
369
|
for (const requiredKey of requiredKeys) {
|
|
350
|
-
if (
|
|
370
|
+
if (unknownKeys.includes(requiredKey))
|
|
351
371
|
continue;
|
|
352
372
|
yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
|
|
353
373
|
}
|
|
354
374
|
if (schema.additionalProperties === false) {
|
|
355
|
-
for (const valueKey of
|
|
356
|
-
if (!
|
|
375
|
+
for (const valueKey of unknownKeys) {
|
|
376
|
+
if (!knownKeys.includes(valueKey)) {
|
|
357
377
|
yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
|
|
358
378
|
}
|
|
359
379
|
}
|
|
360
380
|
}
|
|
361
381
|
if (typeof schema.additionalProperties === 'object') {
|
|
362
|
-
for (const valueKey of
|
|
363
|
-
if (
|
|
382
|
+
for (const valueKey of unknownKeys) {
|
|
383
|
+
if (knownKeys.includes(valueKey))
|
|
364
384
|
continue;
|
|
365
|
-
yield* Visit(schema.additionalProperties, `${path}/${valueKey}`, value[valueKey]);
|
|
385
|
+
yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
|
|
366
386
|
}
|
|
367
387
|
}
|
|
368
388
|
}
|
|
369
|
-
function* Promise(schema, path, value) {
|
|
389
|
+
function* Promise(schema, references, path, value) {
|
|
370
390
|
if (!(typeof value === 'object' && typeof value.then === 'function')) {
|
|
371
391
|
yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
|
|
372
392
|
}
|
|
373
393
|
}
|
|
374
|
-
function* Record(schema, path, value) {
|
|
375
|
-
if (
|
|
376
|
-
|
|
377
|
-
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
|
|
382
|
-
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
|
|
383
|
-
}
|
|
394
|
+
function* Record(schema, references, path, value) {
|
|
395
|
+
if (!IsRecordObject(value)) {
|
|
396
|
+
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` };
|
|
384
397
|
}
|
|
385
398
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
386
399
|
const regex = new RegExp(keyPattern);
|
|
@@ -391,23 +404,31 @@ var ValueErrors;
|
|
|
391
404
|
return yield { type, schema, path, value, message };
|
|
392
405
|
}
|
|
393
406
|
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
394
|
-
yield* Visit(valueSchema, `${path}/${propKey}`, propValue);
|
|
407
|
+
yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
|
|
395
408
|
}
|
|
396
409
|
}
|
|
397
|
-
function* Ref(schema, path, value) {
|
|
398
|
-
|
|
410
|
+
function* Ref(schema, references, path, value) {
|
|
411
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
412
|
+
if (index === -1)
|
|
413
|
+
throw new ValueErrorsDereferenceError(schema);
|
|
414
|
+
const target = references[index];
|
|
415
|
+
yield* Visit(target, references, path, value);
|
|
399
416
|
}
|
|
400
|
-
function* Self(schema, path, value) {
|
|
401
|
-
|
|
417
|
+
function* Self(schema, references, path, value) {
|
|
418
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
419
|
+
if (index === -1)
|
|
420
|
+
throw new ValueErrorsDereferenceError(schema);
|
|
421
|
+
const target = references[index];
|
|
422
|
+
yield* Visit(target, references, path, value);
|
|
402
423
|
}
|
|
403
|
-
function* String(schema, path, value) {
|
|
404
|
-
if (!(
|
|
424
|
+
function* String(schema, references, path, value) {
|
|
425
|
+
if (!IsString(value)) {
|
|
405
426
|
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
406
427
|
}
|
|
407
|
-
if (
|
|
428
|
+
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
408
429
|
yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
|
|
409
430
|
}
|
|
410
|
-
if (
|
|
431
|
+
if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
411
432
|
yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
|
|
412
433
|
}
|
|
413
434
|
if (schema.pattern !== undefined) {
|
|
@@ -428,12 +449,12 @@ var ValueErrors;
|
|
|
428
449
|
}
|
|
429
450
|
}
|
|
430
451
|
}
|
|
431
|
-
function* Symbol(schema, path, value) {
|
|
452
|
+
function* Symbol(schema, references, path, value) {
|
|
432
453
|
if (!(typeof value === 'symbol')) {
|
|
433
454
|
return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
|
|
434
455
|
}
|
|
435
456
|
}
|
|
436
|
-
function* Tuple(schema, path, value) {
|
|
457
|
+
function* Tuple(schema, references, path, value) {
|
|
437
458
|
if (!globalThis.Array.isArray(value)) {
|
|
438
459
|
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
439
460
|
}
|
|
@@ -447,18 +468,18 @@ var ValueErrors;
|
|
|
447
468
|
return;
|
|
448
469
|
}
|
|
449
470
|
for (let i = 0; i < schema.items.length; i++) {
|
|
450
|
-
yield* Visit(schema.items[i], `${path}/${i}`, value[i]);
|
|
471
|
+
yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
|
|
451
472
|
}
|
|
452
473
|
}
|
|
453
|
-
function* Undefined(schema, path, value) {
|
|
474
|
+
function* Undefined(schema, references, path, value) {
|
|
454
475
|
if (!(value === undefined)) {
|
|
455
476
|
yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
|
|
456
477
|
}
|
|
457
478
|
}
|
|
458
|
-
function* Union(schema, path, value) {
|
|
479
|
+
function* Union(schema, references, path, value) {
|
|
459
480
|
const errors = [];
|
|
460
481
|
for (const inner of schema.anyOf) {
|
|
461
|
-
const variantErrors = [...Visit(inner, path, value)];
|
|
482
|
+
const variantErrors = [...Visit(inner, references, path, value)];
|
|
462
483
|
if (variantErrors.length === 0)
|
|
463
484
|
return;
|
|
464
485
|
errors.push(...variantErrors);
|
|
@@ -470,101 +491,96 @@ var ValueErrors;
|
|
|
470
491
|
yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
|
|
471
492
|
}
|
|
472
493
|
}
|
|
473
|
-
function* Uint8Array(schema, path, value) {
|
|
494
|
+
function* Uint8Array(schema, references, path, value) {
|
|
474
495
|
if (!(value instanceof globalThis.Uint8Array)) {
|
|
475
496
|
return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
|
|
476
497
|
}
|
|
477
|
-
if (
|
|
498
|
+
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
478
499
|
yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
|
|
479
500
|
}
|
|
480
|
-
if (
|
|
501
|
+
if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
|
|
481
502
|
yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
|
|
482
503
|
}
|
|
483
504
|
}
|
|
484
|
-
function* Unknown(schema, path, value) { }
|
|
485
|
-
function* Void(schema, path, value) {
|
|
486
|
-
if (
|
|
487
|
-
|
|
488
|
-
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null or undefined` };
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
else {
|
|
492
|
-
if (!(value === undefined)) {
|
|
493
|
-
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected undefined` };
|
|
494
|
-
}
|
|
505
|
+
function* Unknown(schema, references, path, value) { }
|
|
506
|
+
function* Void(schema, references, path, value) {
|
|
507
|
+
if (!IsVoid(value)) {
|
|
508
|
+
return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` };
|
|
495
509
|
}
|
|
496
510
|
}
|
|
497
|
-
function* UserDefined(schema, path, value) {
|
|
511
|
+
function* UserDefined(schema, references, path, value) {
|
|
498
512
|
const check = Types.TypeRegistry.Get(schema[Types.Kind]);
|
|
499
513
|
if (!check(schema, value)) {
|
|
500
514
|
return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
|
|
501
515
|
}
|
|
502
516
|
}
|
|
503
|
-
function* Visit(schema, path, value) {
|
|
504
|
-
const
|
|
505
|
-
|
|
517
|
+
function* Visit(schema, references, path, value) {
|
|
518
|
+
const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
|
|
519
|
+
const schema_ = schema;
|
|
520
|
+
switch (schema_[Types.Kind]) {
|
|
506
521
|
case 'Any':
|
|
507
|
-
return yield* Any(
|
|
522
|
+
return yield* Any(schema_, references_, path, value);
|
|
508
523
|
case 'Array':
|
|
509
|
-
return yield* Array(
|
|
524
|
+
return yield* Array(schema_, references_, path, value);
|
|
510
525
|
case 'BigInt':
|
|
511
|
-
return yield* BigInt(
|
|
526
|
+
return yield* BigInt(schema_, references_, path, value);
|
|
512
527
|
case 'Boolean':
|
|
513
|
-
return yield* Boolean(
|
|
528
|
+
return yield* Boolean(schema_, references_, path, value);
|
|
514
529
|
case 'Constructor':
|
|
515
|
-
return yield* Constructor(
|
|
530
|
+
return yield* Constructor(schema_, references_, path, value);
|
|
516
531
|
case 'Date':
|
|
517
|
-
return yield* Date(
|
|
532
|
+
return yield* Date(schema_, references_, path, value);
|
|
518
533
|
case 'Function':
|
|
519
|
-
return yield* Function(
|
|
534
|
+
return yield* Function(schema_, references_, path, value);
|
|
520
535
|
case 'Integer':
|
|
521
|
-
return yield* Integer(
|
|
536
|
+
return yield* Integer(schema_, references_, path, value);
|
|
522
537
|
case 'Intersect':
|
|
523
|
-
return yield* Intersect(
|
|
538
|
+
return yield* Intersect(schema_, references_, path, value);
|
|
524
539
|
case 'Literal':
|
|
525
|
-
return yield* Literal(
|
|
540
|
+
return yield* Literal(schema_, references_, path, value);
|
|
526
541
|
case 'Never':
|
|
527
|
-
return yield* Never(
|
|
542
|
+
return yield* Never(schema_, references_, path, value);
|
|
528
543
|
case 'Not':
|
|
529
|
-
return yield* Not(
|
|
544
|
+
return yield* Not(schema_, references_, path, value);
|
|
530
545
|
case 'Null':
|
|
531
|
-
return yield* Null(
|
|
546
|
+
return yield* Null(schema_, references_, path, value);
|
|
532
547
|
case 'Number':
|
|
533
|
-
return yield* Number(
|
|
548
|
+
return yield* Number(schema_, references_, path, value);
|
|
534
549
|
case 'Object':
|
|
535
|
-
return yield* Object(
|
|
550
|
+
return yield* Object(schema_, references_, path, value);
|
|
536
551
|
case 'Promise':
|
|
537
|
-
return yield* Promise(
|
|
552
|
+
return yield* Promise(schema_, references_, path, value);
|
|
538
553
|
case 'Record':
|
|
539
|
-
return yield* Record(
|
|
554
|
+
return yield* Record(schema_, references_, path, value);
|
|
540
555
|
case 'Ref':
|
|
541
|
-
return yield* Ref(
|
|
556
|
+
return yield* Ref(schema_, references_, path, value);
|
|
542
557
|
case 'Self':
|
|
543
|
-
return yield* Self(
|
|
558
|
+
return yield* Self(schema_, references_, path, value);
|
|
544
559
|
case 'String':
|
|
545
|
-
return yield* String(
|
|
560
|
+
return yield* String(schema_, references_, path, value);
|
|
546
561
|
case 'Symbol':
|
|
547
|
-
return yield* Symbol(
|
|
562
|
+
return yield* Symbol(schema_, references_, path, value);
|
|
548
563
|
case 'Tuple':
|
|
549
|
-
return yield* Tuple(
|
|
564
|
+
return yield* Tuple(schema_, references_, path, value);
|
|
550
565
|
case 'Undefined':
|
|
551
|
-
return yield* Undefined(
|
|
566
|
+
return yield* Undefined(schema_, references_, path, value);
|
|
552
567
|
case 'Union':
|
|
553
|
-
return yield* Union(
|
|
568
|
+
return yield* Union(schema_, references_, path, value);
|
|
554
569
|
case 'Uint8Array':
|
|
555
|
-
return yield* Uint8Array(
|
|
570
|
+
return yield* Uint8Array(schema_, references_, path, value);
|
|
556
571
|
case 'Unknown':
|
|
557
|
-
return yield* Unknown(
|
|
572
|
+
return yield* Unknown(schema_, references_, path, value);
|
|
558
573
|
case 'Void':
|
|
559
|
-
return yield* Void(
|
|
574
|
+
return yield* Void(schema_, references_, path, value);
|
|
560
575
|
default:
|
|
561
|
-
if (!Types.TypeRegistry.Has(
|
|
576
|
+
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
562
577
|
throw new ValueErrorsUnknownTypeError(schema);
|
|
563
|
-
return yield* UserDefined(
|
|
578
|
+
return yield* UserDefined(schema_, references_, path, value);
|
|
564
579
|
}
|
|
565
580
|
}
|
|
566
|
-
function Errors(schema, value) {
|
|
567
|
-
|
|
581
|
+
function Errors(schema, references, value) {
|
|
582
|
+
const iterator = Visit(schema, references, '', value);
|
|
583
|
+
return new ValueErrorIterator(iterator);
|
|
568
584
|
}
|
|
569
585
|
ValueErrors.Errors = Errors;
|
|
570
586
|
})(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
|