question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to do the validation in a separate file and not inline in the route?

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
gustavohenkecommented, Oct 9, 2017

Hey @gkatsanos, The correct way to use express-validator would be to use it as a middleware when defining your express routes, eg:

// auth.validation.js
exports.register = [
  check('email').isEmail(),
  (req, res, next) => { /* the rest of the existing function */ }
];

…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.

5reactions
gkatsanoscommented, Oct 10, 2017

Thank you 😃 indeed the array then looks much much better!:

exports.register = [
  check('email').isEmail().withMessage('must be a valid email'),
  check('password').isLength({ min: 4 }).withMessage('passwd 4 chars long!'),
];
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found