Skip to content

Commit 3d2713f

Browse files
committed
Merge remote-tracking branch 'origin/main' into mbg/config/merge
2 parents 7577328 + f133996 commit 3d2713f

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

lib/entry-points.js

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/json/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const checkSchemaTestSchema = {
107107
rootKey: json.object(objectSchema),
108108
};
109109

110-
test("validateSchema - checkSchema reports unknown keys", async (t) => {
110+
test("checkSchema - reports unknown keys", async (t) => {
111111
const result = json.checkSchema(checkSchemaTestSchema, {
112112
rootKey: {
113113
objectKey: {
@@ -125,7 +125,7 @@ test("validateSchema - checkSchema reports unknown keys", async (t) => {
125125
);
126126
});
127127

128-
test("validateSchema - checkSchema reports invalid keys", async (t) => {
128+
test("checkSchema - reports invalid keys", async (t) => {
129129
const result = json.checkSchema(checkSchemaTestSchema, {
130130
rootKey: {
131131
objectKey: {

src/json/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,18 @@ export function array<T>(validator: Validator<T>) {
104104
const elementPath = `${path}[${index}]`;
105105
const eResult = validator.check(e, opts, `${elementPath}`);
106106

107+
result.invalidKeys.push(...eResult.invalidKeys);
107108
result.unknownKeys.push(...eResult.unknownKeys);
108109
index++;
109110

110111
if (!eResult.valid) {
111112
result.valid = false;
112-
result.invalidKeys.push(elementPath);
113+
114+
// Add the element path to `invalidKeys` if we didn't get
115+
// any more specific ones from the element validator.
116+
if (eResult.invalidKeys.length === 0) {
117+
result.invalidKeys.push(elementPath);
118+
}
113119

114120
if (opts.failFast) {
115121
return result;
@@ -258,9 +264,16 @@ export function checkSchema<S extends Schema>(
258264
path: string = "",
259265
): CheckSchemaResult {
260266
const result: CheckSchemaResult = successfulCheckSchema();
267+
268+
// Track the set of input keys. We remove keys from this set as we recognise them
269+
// during validation.
261270
const inputKeys = new Set(Object.keys(obj));
271+
272+
// Track keys that have failed validation, starting with the empty set.
262273
const invalidKeys = new Set();
263274

275+
// Loop through all keys in the object schema and validate that the given object
276+
// satisfies the schema key.
264277
for (const [key, validator] of Object.entries(schema)) {
265278
const hasKey = key in obj;
266279

@@ -276,7 +289,7 @@ export function checkSchema<S extends Schema>(
276289
result.valid = false;
277290

278291
if (options.failFast) {
279-
return result;
292+
break;
280293
}
281294
continue;
282295
}
@@ -286,7 +299,7 @@ export function checkSchema<S extends Schema>(
286299
result.valid = false;
287300

288301
if (options.failFast) {
289-
return result;
302+
break;
290303
}
291304
continue;
292305
}
@@ -308,7 +321,7 @@ export function checkSchema<S extends Schema>(
308321
result.valid = false;
309322

310323
if (options.failFast) {
311-
return result;
324+
break;
312325
}
313326
continue;
314327
}

0 commit comments

Comments
 (0)