Skip to content

.tip

· one of the small things

Return every validation error, not just the first

A form posts eight fields, the response names one of them, and the person fixes it only to learn about the next one. The information was there: a ZodError holds one issue per failure, and the handler flattened it to one line.

const parsed = CreateOrder.safeParse(await req.json());
if (!parsed.success) {
  const errors = parsed.error.issues.map((i) => ({
    field: i.path.join("."),
    message: i.message,
  }));
  return Response.json({ errors }, { status: 400 });
}

path is an array, so joining it gives items.0.quantity, which the client matches to the input it rendered.

validationapi-design