@sinclair/typebox 0.26.5 → 0.26.7
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.js +4 -0
- package/errors/errors.js +6 -0
- package/package.json +1 -1
- package/value/check.js +6 -0
- package/value/convert.js +14 -1
package/compiler/compiler.js
CHANGED
|
@@ -341,6 +341,10 @@ var TypeCompiler;
|
|
|
341
341
|
}
|
|
342
342
|
function* Record(schema, references, value) {
|
|
343
343
|
yield IsRecordCheck(value);
|
|
344
|
+
if (IsNumber(schema.minProperties))
|
|
345
|
+
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
346
|
+
if (IsNumber(schema.maxProperties))
|
|
347
|
+
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
344
348
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
345
349
|
const local = PushLocal(`new RegExp(/${keyPattern}/)`);
|
|
346
350
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
|
package/errors/errors.js
CHANGED
|
@@ -401,6 +401,12 @@ var ValueErrors;
|
|
|
401
401
|
if (!IsRecordObject(value)) {
|
|
402
402
|
return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` };
|
|
403
403
|
}
|
|
404
|
+
if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
405
|
+
yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
|
|
406
|
+
}
|
|
407
|
+
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
408
|
+
yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
|
|
409
|
+
}
|
|
404
410
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
405
411
|
const regex = new RegExp(keyPattern);
|
|
406
412
|
if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
|
package/package.json
CHANGED
package/value/check.js
CHANGED
|
@@ -291,6 +291,12 @@ var ValueCheck;
|
|
|
291
291
|
if (!IsRecordObject(value)) {
|
|
292
292
|
return false;
|
|
293
293
|
}
|
|
294
|
+
if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
294
300
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
295
301
|
const regex = new RegExp(keyPattern);
|
|
296
302
|
if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
|
package/value/convert.js
CHANGED
|
@@ -30,6 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
30
|
exports.ValueConvert = exports.ValueConvertDereferenceError = exports.ValueConvertUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const clone_1 = require("./clone");
|
|
33
|
+
const check_1 = require("./check");
|
|
33
34
|
// ----------------------------------------------------------------------------------------------
|
|
34
35
|
// Errors
|
|
35
36
|
// ----------------------------------------------------------------------------------------------
|
|
@@ -236,7 +237,13 @@ var ValueConvert;
|
|
|
236
237
|
return value;
|
|
237
238
|
}
|
|
238
239
|
function Record(schema, references, value) {
|
|
239
|
-
|
|
240
|
+
const propertyKey = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
241
|
+
const property = schema.patternProperties[propertyKey];
|
|
242
|
+
const result = {};
|
|
243
|
+
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
244
|
+
result[propKey] = Visit(property, references, propValue);
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
240
247
|
}
|
|
241
248
|
function Ref(schema, references, value) {
|
|
242
249
|
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
@@ -270,6 +277,12 @@ var ValueConvert;
|
|
|
270
277
|
return TryConvertUndefined(value);
|
|
271
278
|
}
|
|
272
279
|
function Union(schema, references, value) {
|
|
280
|
+
for (const subschema of schema.anyOf) {
|
|
281
|
+
const converted = Visit(subschema, references, value);
|
|
282
|
+
if (check_1.ValueCheck.Check(subschema, references, converted)) {
|
|
283
|
+
return converted;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
273
286
|
return value;
|
|
274
287
|
}
|
|
275
288
|
function Uint8Array(schema, references, value) {
|