IdoPesok / zsa

https://zsa.vercel.app
MIT License
436 stars 13 forks source link

Returning fieldErrors flattened is not very useful #146

Open thomasheeney opened 1 week ago

thomasheeney commented 1 week ago

Using the errors object, the key "fieldErrors" is returned flattened (here).

If these could be the keyed fieldErrors zod returns before flattening, it would be more possible to display each error on the correct place.

IdoPesok commented 1 week ago

Hi, per the zod docs after you do .flatten() the field errors is an "object that groups all issues by key." Can you maybe provide a code snippet of what you are getting with ZSA in field errors vs what you are expecting to receive.

Screen Shot 2024-06-24 at 5 57 17 AM
thomasheeney commented 1 week ago

My input schema is as follows:

z.object({
    form: z.object({
        email: emailSchema, //..email('You must provide a valid email'),
        password: z.string().min(1, 'Your password is required'),
        twoFactorCode: z.string()
    }),
     turnstile: z.string().nullish(),
     callbackUrl: z.string().optional()
})

Because I am passing the form as another zod object, the flattened response comes back as

"fieldErrors": {
    "form": [
      "A valid email address is required",
      "Your password is required"
    ]
  }

The deep nesting is removed by flattening.

IdoPesok commented 1 week ago

Gotcha, thanks for sharing this. Is it possible to use the formatted errors in your case?

{isError && error.code === "INPUT_PARSE_ERROR" && (
  <div>{error.formattedErrors.form?.email?._errors}</div>
)}

If these could be the keyed fieldErrors zod returns before flattening

I am not sure what you mean by this, can you reference the zod docs? Happy to see what we can do here but not seeing how to get field errors before flattening.

andresgutgon commented 1 week ago

What about using shapeError? Would that help?