How to customize the error message of z.coerce.date()
?
#1851
-
When I use z.date({
required_error: "Date is required.",
invalid_type_error: "Wrong date format.",
}), However, once I use For example, Uncaught ZodError: [
{
"code": "invalid_date",
"path": [],
"message": "Invalid date"
}
] I assume that ZodError gets this message from However, since I use react-hook-form with a zod schema, I "have to" provide the error messages via zod (or start doing some hacky workarounds). As far as I see, right now there is no way to change this behavior (eg. Is this considered a Bug or a feature in Zod? And how would you recommend working around it? A few thoughts on how to improve the DX:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Is this what you are looking for? const schema = z.coerce.date( {
errorMap: ( { code }, { defaultError } ) => {
if ( code == 'invalid_date' ) return { message: 'Wrong date format.' }
return { message: defaultError }
}
} )
const result = schema.safeParse( '' )
!result.success && console.log( result.error.issues[ 0 ] )
// { code: 'invalid_date', path: [], message: 'Wrong date format.' } |
Beta Was this translation helpful? Give feedback.
Is this what you are looking for?