handleValidationError.js 427 B

1234567891011121314151617
  1. const { isZodErrorLike, fromError } = require('zod-validation-error')
  2. /**
  3. * @typedef {import('express').ErrorRequestHandler} ErrorRequestHandler
  4. */
  5. const handleValidationError = [
  6. /** @type {ErrorRequestHandler} */
  7. (err, req, res, next) => {
  8. if (!isZodErrorLike(err)) {
  9. return next(err)
  10. }
  11. res.status(400).json({ ...fromError(err), statusCode: 400 })
  12. },
  13. ]
  14. module.exports = { handleValidationError }