handleValidationError.js 457 B

1234567891011121314151617
  1. const { isZodErrorLike, fromError } = require('zod-validation-error')
  2. function createHandleValidationError(statusCode = 400) {
  3. return [
  4. (err, req, res, next) => {
  5. if (!isZodErrorLike(err)) {
  6. return next(err)
  7. }
  8. res.status(statusCode).json({ ...fromError(err), statusCode })
  9. },
  10. ]
  11. }
  12. const handleValidationError = createHandleValidationError(400)
  13. module.exports = { handleValidationError, createHandleValidationError }