Remove keys from unrecognised set as soon as found

This commit is contained in:
Michael B. Gale
2026-07-10 13:10:25 +01:00
parent cee0056fc0
commit e4752e74e1
2 changed files with 4 additions and 2 deletions

2
lib/entry-points.js generated
View File

@@ -144598,6 +144598,7 @@ function checkSchema(schema, obj, options = {}, path29 = "") {
const inputKeys = new Set(Object.keys(obj));
for (const [key, validator] of Object.entries(schema)) {
const hasKey = key in obj;
inputKeys.delete(key);
if (validator.required && !hasKey) {
result.valid = false;
if (options.failFast) {
@@ -144623,7 +144624,6 @@ function checkSchema(schema, obj, options = {}, path29 = "") {
continue;
}
}
inputKeys.delete(key);
}
for (const remainingKey of inputKeys) {
result.unknownKeys.push(`${path29}${remainingKey}`);

View File

@@ -226,6 +226,9 @@ export function checkSchema<S extends Schema>(
for (const [key, validator] of Object.entries(schema)) {
const hasKey = key in obj;
// Remove key from set of unrecognised keys.
inputKeys.delete(key);
// If the property is required, but absent, fail.
if (validator.required && !hasKey) {
result.valid = false;
@@ -263,7 +266,6 @@ export function checkSchema<S extends Schema>(
}
// If we reach this point, the key has been successfully validated.
inputKeys.delete(key);
}
// If there are any remaining keys in `inputKeys`, add them to `unknownKeys`.