You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a Zod schema uses a transformation (transform) or coercion (coerce) to validate a date, the custom error message defined in the configuration of zod.date() is ignored. Instead, the schema returns Zod's default error message: "Invalid date".
Reproduction example
Here is a minimal code snippet to reproduce the issue:
import*aszfrom"zod";constzod=z;constdateInString="im not a date";// Case with transform + pipeconsole.log("test pipe",zod.string().transform((date)=>newDate(date))// Transforms the string into a Date.pipe(zod.date({message: "test pipe"}))// Defines a custom error message.safeParse(dateInString).error?.errors.map((error)=>error.message));// Case with coerceconsole.log("test coerce",zod.coerce.date({message: "test coerce"})// Defines a custom error message.safeParse(dateInString).error?.errors.map((error)=>error.message));
Expected result
In both cases, when the input dateInString is not a valid date, the schema should return the custom error messages:
"test pipe" for the first case.
"test coerce" for the second case.
Observed result
However, in both cases, the schema returns the default error message:
test pipe > ["Invalid date"]
test coerce > ["Invalid date"]
Versions
Node: v20.11.1
Zod: v3.24.0
Additional notes
This issue may be problematic for users who want to handle errors with context-specific messages. Thank you for the fantastic work on Zod, and I hope this issue can be resolved! See
The text was updated successfully, but these errors were encountered:
Description
When a Zod schema uses a transformation (
transform
) or coercion (coerce
) to validate a date, the custom error message defined in the configuration ofzod.date()
is ignored. Instead, the schema returns Zod's default error message: "Invalid date".Reproduction example
Here is a minimal code snippet to reproduce the issue:
Expected result
In both cases, when the input dateInString is not a valid date, the schema should return the custom error messages:
Observed result
However, in both cases, the schema returns the default error message:
Versions
Node: v20.11.1
Zod: v3.24.0
Additional notes
This issue may be problematic for users who want to handle errors with context-specific messages. Thank you for the fantastic work on Zod, and I hope this issue can be resolved! See
The text was updated successfully, but these errors were encountered: