Jaruzel
Jaruzel

Reputation: 129

express-validator doesn't do anything

I'm trying to use express-validator, but none of it's functions work. I'm not getting any errors either.

Example:

const check = require('express-validator/check').check;

router.post('/register', check('someRandomName').exists(), (req, res) => userController.register(req, res));

express-validator always allows it, even if I don't have someRandomName in the req.

I also tried using body instead of check, but the result was the same.

Upvotes: 8

Views: 8866

Answers (1)

feiiiiii
feiiiiii

Reputation: 1583

The check returns a validation chain which you suppose to validate result in your req after it using validationResult, so something like this:

const {check , validationResult}  = require('express-validator/check');

router.post('/register', check('someRandomName').exists(), (req, res) => {
       var err = validationResult(req);
       if (!err.isEmpty()) {
           console.log(err.mapped())
           // you stop here 
       } else {
           // you pass req and res on to your controller
       }
}

Upvotes: 20

Related Questions