in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

Related questions

0 votes
    I am running my nodejs app by npm start I just installed nodemon by sudo npm install -g nodemon ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Here is my function. If you pass MemoryStream to XmlReader it does not validate proper xml files sometimes. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I am using the electron-quick-start app with a windows machine. I have added logging statements in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    here is my code. var http=require("http"); var fs = require("fs"); var express = require(" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I am using react FineUploaderS3, with params: this.myUploader = new FineUploaderS3({ options: { request: { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am trying to create a simple node.js app on heroku. Here is app.js: console.log("Starting App" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a particular context in which one data are transformed a lot to get transferred across network. At ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    When I run a new page, I must specify size of the viewport using the setViewport function: await page ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a problem, I want to move value of Object[0] nazwa, which is a result of query (let's ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I want to build my js/css code, write it on disk and serve it using webpack-dev-server in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is it possible to add a property (with get and set method) to the scope of a file without making ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
...