Is it possible to do the validation in a separate file and not inline in the route?
See original GitHub issueI am using express-validator to validate the Request body. I wanted to abstract validation in a separate JS partial/import and not have it inline in my routes.
Here’s what I created:
// routes
const controller = require('../controllers/auth.controller');
const validate = require('../validations/auth.validation');
router.route('/register')
.post(validate.register, controller.register);
/**** auth.validation.js *****/
const { check, validationResult } = require('express-validator/check');
exports.register = (req, res, next) => {
check('email').isEmail().withMessage('must be a valid email');
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.mapped() });
}
return next();
};
I get
RangeError: Invalid status code: undefined
at ServerResponse.writeHead (_http_server.js:188:11)
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
How to implement validation in a separate file using express ...
I have a route file, controller file and I want to carryout validation in a file called validate.js. Meanwhile, I have installed express- ......
Read more >Client-side form validation - Learn web development | MDN
However, client-side validation should not be considered an exhaustive security measure! Your apps should always perform security checks on ...
Read more >Validation - Laravel - The PHP Framework For Web Artisans
The GET route will display a form for the user to create a new blog post, while the POST route will store the...
Read more >Configuring Inline File Validation in the GUI - IBM
Inline file validation is a feature that enables file content to be validated while the file is in transit, as well as when...
Read more >Dynamic type validation in TypeScript - LogRocket Blog
Do you need to validate everything? ... Simply put, no! ... Checking all the variables in our application is time-consuming from both a ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @gkatsanos, The correct way to use express-validator would be to use it as a middleware when defining your express routes, eg:
…otherwise, the validations will never run.
Also, you may be doing something wrong inside your routes, as Node’s http module is complaining about an undefined status code. This is unrelated to express-validator, so I recommend you to use StackOverflow in case you need more help finding the issue.
Thank you 😃 indeed the array then looks much much better!: