zod icon indicating copy to clipboard operation
zod copied to clipboard

Error Message docs inconsistent with actual behaviour for string().date() validation method

Open alex-delia opened this issue 1 year ago • 1 comments

The string().date() validation message takes a string as a parameter, date(message?: string): ZodString; whereas all the rest of the methods take in an errorUtil.ErrMessage, ex: email(message?: errorUtil.ErrMessage): ZodString;

This is not a huge issue, though it is inconsistent with the docs which show error messages for the string().date() validator can be set in this way z.string().date({ message: "Invalid date string!" }); when they actually are set like this z.string().date("Invalid date string!");

alex-delia avatar Oct 18 '24 20:10 alex-delia

Hi, @alex-delia. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • Discrepancy in Zod documentation regarding string().date() validation method.
  • string().date() uses a plain string for error messages instead of errorUtil.ErrMessage.
  • The issue is considered minor but important for documentation consistency.
  • No comments or activity have been made on this issue yet.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the Zod repository by commenting here.
  • If there is no further activity, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jun 19 '25 16:06 dosubot[bot]

Hi @alex-delia! I'm Dosu and I’m helping the zod team.

You're correct—z.string().date() only accepts a plain string for its error message, while other validators like email accept an errorUtil.ErrMessage (which can be a string or an object with a message property). This makes the date method less flexible and inconsistent with the rest of the API. The docs currently don't clarify this difference, and the implementation confirms that only a string is accepted for date's error message parameter, not an object {source}.

If you want to set a custom error message for z.string().date(), you should use the string form:
z.string().date("Invalid date string!")

This inconsistency may be due to legacy reasons or oversight. If your question is answered, please close the issue!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Jul 21 '25 22:07 dosubot[bot]

Both forms are supported universally in Zod 4

colinhacks avatar Jul 24 '25 19:07 colinhacks