Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Custom error messages are ignored in zod.date() after transform or coerce #3907

Open
ZeRiix opened this issue Dec 12, 2024 · 0 comments

Comments

@ZeRiix
Copy link

ZeRiix commented Dec 12, 2024

Description

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 * as z from "zod";

const zod = z;

const dateInString = "im not a date";

// Case with transform + pipe
console.log(
  "test pipe",
  zod
    .string()
    .transform((date) => new Date(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 coerce
console.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant