-
|
I'm currently building a schema for a form where you can set some notification settings on a set of error types. You can enter a comma separated list of emails in a text field. I used #1869 to accomplish this. It works! However, I need the email errors to show on the text field which is |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi @mwkcoding! I'm Dosu and I’m helping the zod team. Zod always includes the array index in the error path for array items (e.g., To show errors on the parent field (like import { z } from "zod";
const result = schema.safeParse(data);
if (!result.success) {
const emailsErrors = result.error.issues
.filter(issue =>
issue.path.slice(-2).join('.') === 'emails.0' // adjust as needed for your path
)
.map(issue => issue.message);
// Or, more generally, aggregate all errors under 'emails'
const parentFieldErrors = result.error.issues
.filter(issue => issue.path.includes('emails'))
.map(issue => issue.message);
// Now you can assign parentFieldErrors to your form field for 'emails'
}You can also use utilities like If you want to see more discussion and example workarounds, see this issue and this one. If this answers your question, feel free to close the issue! If you need a more specific code example for your form library, let me know. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
I ended up doing it with a superRefine: z.object({
emails: z.string()
}).superRefine((data, ctx) => {
const emails = emails.trim().toLowerCase().split(",");
...
ctx.issues.push({
code: "too_small",
minimum: 1,
origin: "array",
message: "Please enter at least one email",
path: ["emails"],
inclusive: true,
input: emails,
});
...
}); |
Beta Was this translation helpful? Give feedback.
I ended up doing it with a superRefine: