I'm using Express-validation to validate request fields. everything is working as it supposed to be, except for the response error code.
I have the following code:
//
// ValidationLeaner.js
const Joi = require('joi');
module.exports = {
body: {
name: Joi.string().min(3).max(20).required(),
classifierId: Joi.number().integer().positive().required()
}
}
//Route.js
const { Router } = require('express')
const Validate = require('express-validation')
const ValidationLearner = require('./ValidationLeaner')
const router = Router()
router.route('/').post(Validate(ValidationLearner), ...someFunction)
module.exports = router
When sending a POST
request with body: {name: "Leaner01", classifierId: false}
.
I receive the following response, which is correct:
{
"status": 400,
"statusText": "Bad Request",
"errors": [
{
"field": [ "classifierId" ],
"location": "body",
"messages": [ "\"classifierId\" must be a number" ],
"types": [ "number.base" ]
}
]
}
Unfortunately the response code is 500 Internal Server Error and not 400 as pointed in the response body.
How can we make Express-validation send the same response code stated in the body (status: 400)?