colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

RFC: New error map API #3618

Open colinhacks opened 2 months ago

colinhacks commented 2 months ago

RFC: Error maps

General comments can go below. Leave specific comments in the "Files changed" interface.

View the formatted RFC here: https://github.com/colinhacks/zod/blob/v4-rfc-error-maps/rfcs/v4-error-maps.md

  export type ZodErrorMap = (
    issue: ZodIssueOptionalMessage,  // includes `input` field to replace `ctx.data`
+   /** @deprecated */
    ctx: { defaultError: string; data: any }
- ) => { message: string };
+ ) => { message: string } | string | undefined;

My full-time work on Zod 4, including the design & implementation of this proposal, is supported by Clerk. Don't roll your own auth. 🫶

clerk logo

donaldpipowitch commented 2 months ago

As you touch this area in zod 4 (thanks for working on this), could this maybe be officially added as well? https://github.com/colinhacks/zod/issues/3212 It works right now, it just needs a type cast. It would be nice to know that the lib itself treats the actual error message as a black box.