I created a REST api and want to validate the body and the params before calling the controller logic. For the validation I use Joi (https://www.npmjs.com/package/joi).
Let's say I have a route with one url parameter and some body variables. The params object contains this url parameter but Joi still returns a 400. The detail message is
"userId" is required
I tried to create a minimalistic example showing my code. To reproduce the error create the app.js file
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const cors = require('cors');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use('/users', require('./routes/users.js'));
app.listen(3000);
Due to the fact each validation fails there is only one route required to test it. Create users.js with the following content
const express = require('express');
const router = express.Router();
const usersController = require('../controllers/users.js');
const usersControllerPolicy = require('../policies/users.js');
router.get('/:userId', usersControllerPolicy.getUserById, usersController.getUserById);
module.exports = router;
And this users.js controller file
exports.getUserById = async (req, res, next) => {
const { userId } = req.params;
return res.status(200).json("everything is fine");
};
When it comes to the policy I created the users.js policy which adds the required schema to the middleware
const joi = require('joi');
const schemaValidation = require('../middleware/schemaValidation.js');
module.exports = {
getUserById: (req, res, next) => {
schemaValidation({
params: {
userId: joi.string().guid().required()
},
body: {}
}, req, res, next);
}
}
and then the schema gets validated by my schemaValidation.js
const joi = require('joi');
module.exports = (schema, req, res, next) => {
const { error } = joi.validate(req, schema);
if (error)
return res.status(400).json("something went wrong");
next(); // execute the controller logic
}
As you can see I pass in the whole req object. I do this because sometimes I have to validate the body and the params. The url parameter userId is not found by Joi so I get returned a status code of 400.
How can I fix my middleware validation to validate both objects within the req object?