Setting Custom Error Messages on Zod Unions
Hello,
I'd like to set a custom error message when a union of literals does not pass parsing in zod. With other constraints, such as
.min()
or .max()
you can set a error message like this:
But I am not sure how to do so on this union:
Could anyone help me out? Thanks!1 Reply
I'm not really sure how to do it. I usually just safeParse the union and if it was unsuccessful, I just write my own error message. But, if this was meant to be used in multiple places then coupling the error message with the union would be a better solution.