@@ -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