addamsson - Is there a way to fine-tune error m...
Is there a way to fine-tune error messages in Zod to include the path (or the last element of the path)? I'm looking at my errors and the only thing I'd change is to have the field name instead of
String
/Number
:
vs
Solution:Jump to solution
ok, I just realized I can do this:
```ts
const Address = z.object({
street: z
.string({...
2 Replies
Solution
ok, I just realized I can do this:
There's no way to "automate" it as far as I know, no.
In our project we've worked around it by writing all error messages in a way that the error message component (which does know the field label) can just put the error message after the label and have it make sense.
For example
must be greater than zero
, can easily be appended to a label and make a string like "Age" must be greater than zero
. Definitely not optimal, but works OK for now 🙂