Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 776
Jaruzel Avatar asked Dec 11 '22 06:12

Jaruzel


1 Answers

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
       }
}
like image 134
feiiiiii Avatar answered Dec 27 '22 03:12

feiiiiii