Aquila169 / zod-express-middleware

Express middleware to validate requests using zod schema's.
MIT License
88 stars 13 forks source link

Unable to catch errors in express middleware #18

Open nicoandreoli2000 opened 6 months ago

nicoandreoli2000 commented 6 months ago

Description

When a validation fails, the library does this:

res.status(400).send(...)

When it should actually do this:

throw new ZodError(...)

If not, there's no way of parsing the errors to match different needs.

nicoandreoli2000 commented 6 months ago

@Aquila169 I can help you with the implementation, we just need to change the functions sendError and sendErrors:

export const sendErrors: (errors: Array<ErrorListItem>, res: Response) => void = (errors, res) => {
  throw new CustomError(...)
};
export const sendError: (error: ErrorListItem, res: Response) => void = (error, res) => {
  throw new CustomError(...)
};
JovanJevtic commented 5 months ago

I was also having troubles with this approach, my errorHandler was not catching errors and I was constantly getting can not set headers after they are sent to the client errors