@@ -48,7 +48,11 @@ export function isStringOrUndefined(
4848 */
4949export type Validator < T > = {
5050 validate : ( val : unknown ) => val is T ;
51- check : ( val : unknown , path : string ) => CheckSchemaResult ;
51+ check : (
52+ val : unknown ,
53+ opts : CheckSchemaOptions ,
54+ path : string ,
55+ ) => CheckSchemaResult ;
5256 required : boolean ;
5357} ;
5458
@@ -85,7 +89,7 @@ export function array<T>(validator: Validator<T>) {
8589 } ;
8690 return {
8791 validate,
88- check : ( val : unknown , path : string ) => {
92+ check : ( val : unknown , opts : CheckSchemaOptions , path : string ) => {
8993 const result : CheckSchemaResult = successfulCheckSchema ( ) ;
9094
9195 // The value must be an array.
@@ -98,14 +102,19 @@ export function array<T>(validator: Validator<T>) {
98102 let index = 0 ;
99103 for ( const e of val ) {
100104 const elementPath = `${ path } [${ index } ]` ;
101- const eResult = validator . check ( e , `${ elementPath } ` ) ;
105+ const eResult = validator . check ( e , opts , `${ elementPath } ` ) ;
102106
103107 result . unknownKeys . push ( ...eResult . unknownKeys ) ;
104108 index ++ ;
105109
106110 if ( ! eResult . valid ) {
107111 result . valid = false ;
108112 result . invalidKeys . push ( elementPath ) ;
113+
114+ if ( opts . failFast ) {
115+ return result ;
116+ }
117+
109118 continue ;
110119 }
111120 }
@@ -125,11 +134,11 @@ export function object<
125134 validate : ( val : unknown ) => {
126135 return isObject ( val ) && validateSchema < S , T > ( schema , val ) ;
127136 } ,
128- check : ( val , path ) => {
137+ check : ( val , opts , path ) => {
129138 if ( ! isObject ( val ) ) {
130139 return invalidCheckSchema ( ) ;
131140 }
132- return checkSchema ( schema , val , { } , path ) ;
141+ return checkSchema ( schema , val , opts , path ) ;
133142 } ,
134143 required : true ,
135144 } as const satisfies Validator < T > ;
@@ -144,11 +153,11 @@ export function optionalOrNull<T>(validator: Validator<T>) {
144153 validate : ( val : unknown ) => {
145154 return val === undefined || val === null || validator . validate ( val ) ;
146155 } ,
147- check : ( val , path ) => {
156+ check : ( val , opts , path ) => {
148157 if ( val === undefined || val === null ) {
149158 return successfulCheckSchema ( ) ;
150159 }
151- return validator . check ( val , path ) ;
160+ return validator . check ( val , opts , path ) ;
152161 } ,
153162 required : false ,
154163 } as const satisfies Validator < T | undefined | null > ;
@@ -163,11 +172,11 @@ export function optional<T>(validator: Validator<T>) {
163172 validate : ( val : unknown ) : val is T | undefined => {
164173 return val === undefined || validator . validate ( val ) ;
165174 } ,
166- check : ( val , path ) => {
175+ check : ( val , opts , path ) => {
167176 if ( val === undefined ) {
168177 return successfulCheckSchema ( ) ;
169178 }
170- return validator . check ( val , path ) ;
179+ return validator . check ( val , opts , path ) ;
171180 } ,
172181 required : false ,
173182 } as const satisfies Validator < T | undefined > ;
@@ -284,7 +293,7 @@ export function checkSchema<S extends Schema>(
284293
285294 // If the property is present, validate it.
286295 if ( hasKey ) {
287- const checkResult = validator . check ( obj [ key ] , `${ path } .${ key } ` ) ;
296+ const checkResult = validator . check ( obj [ key ] , options , `${ path } .${ key } ` ) ;
288297
289298 result . unknownKeys . push ( ...checkResult . unknownKeys ) ;
290299 result . invalidKeys . push ( ...checkResult . invalidKeys ) ;
0 commit comments