Return a formatted JSON error when validation fails

GitOrigin-RevId: 0f2d3d73a6e371aa332dee245be8802250adfb2b
This commit is contained in:
Andrew Rumble
2025-09-23 08:07:37 +00:00
committed by Copybot
parent 3cd9ded7c8
commit b1cf8d4e88
3 changed files with 30 additions and 7 deletions
@@ -9,6 +9,11 @@ const {
zz,
ParamsError,
} = require('@overleaf/validation-tools')
const { isZodErrorLike, fromError } = require('zod-validation-error')
/**
* @typedef {import('express').ErrorRequestHandler} ErrorRequestHandler
*/
const objectIdValidator = {
type: 'objectId',
@@ -31,10 +36,21 @@ const objectIdValidator = {
}
const Joi = CelebrateJoi.extend(objectIdValidator)
const errorMiddleware = errors()
const errorMiddleware = [
errors(),
/** @type {ErrorRequestHandler} */
(err, req, res, next) => {
if (!isZodErrorLike(err)) {
return next(err)
}
res.status(400).json({ ...fromError(err), statusCode: 400 })
},
]
/**
* Validation middleware
* @deprecated Please use Zod schemas and `validateReq` instead
*/
function validate(schema) {
return celebrate(schema, { allowUnknown: true })