Skip to content

Commit 3f1e4da

Browse files
committed
Propagate and prefer invalidKeys from element validator in array validator
1 parent 89343ee commit 3f1e4da

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/json/index.ts

Lines changed: 7 additions & 1 deletion
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;

0 commit comments

Comments
 (0)