File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed
Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,7 @@ describe("misc.ts", () => {
236236 name : z . string ( ) ,
237237 age : z . number ( ) . positive ( ) ,
238238 tags : z . array ( z . string ( ) ) ,
239+ enumArray : z . array ( z . enum ( [ "one" , "two" ] ) ) . min ( 2 ) ,
239240 } )
240241 . partial ( )
241242 . strip ( ) ;
@@ -306,5 +307,15 @@ describe("misc.ts", () => {
306307 const stripped = sanitize ( schema . strip ( ) , obj ) ;
307308 expect ( stripped ) . not . toHaveProperty ( "powerLevel" ) ;
308309 } ) ;
310+ it ( "should provide a readable error message" , ( ) => {
311+ const obj = {
312+ arrayOneTwo : [ "one" , "nonexistent" ] ,
313+ } as any ;
314+ expect ( ( ) => {
315+ sanitize ( schema . strip ( ) , obj ) ;
316+ } ) . toThrowError (
317+ "unable to sanitize: arrayOneTwo: Array must contain at least 2 element(s)"
318+ ) ;
319+ } ) ;
309320 } ) ;
310321} ) ;
Original file line number Diff line number Diff line change @@ -770,9 +770,11 @@ export function sanitize<T extends z.ZodTypeAny>(
770770 // eslint-disable-next-line @typescript-eslint/no-unsafe-return
771771 return cleanValidate . data ;
772772 }
773- throw new Error (
774- "unable to sanitize: " + cleanValidate . error . errors . join ( "," )
775- ) ;
773+
774+ const errorsString = cleanValidate . error . errors
775+ . map ( ( e ) => e . path . join ( "." ) + ": " + e . message )
776+ . join ( ", " ) ;
777+ throw new Error ( "unable to sanitize: " + errorsString ) ;
776778}
777779
778780export function triggerResize ( ) : void {
You can’t perform that action at this time.
0 commit comments