Erik
Erik

Reputation: 14770

express-validator throw exception

In my nodejs app to filter all input data I use express-validator. Something like this:

app.post('/', function (req, res) {
   req.sanitize('login').xss();
   req.sanitize('password').xss();

   //etc.
});

So my view is as:

form(action='/', method='POST')
  input(name='login', type='text')
  input(name='password', type='password')
  input(type='submit')

All works fine but if I omit login field for example:

form(action='/', method='POST')
  input(name='login', type='text')
  input(name='password', type='password')
  input(type='submit')

then I get exception. Why doesn't express-validator check param existing?

Upvotes: 3

Views: 2830

Answers (1)

Nathan C. Tresch
Nathan C. Tresch

Reputation: 948

According to the documentation you're using it wrong.

Validation errors

You have two choices on how to get the validation errors:

req.assert('email', 'required').notEmpty();
req.assert('email', 'valid email required').isEmail();
req.assert('password', '6 to 20 characters required').len(6, 20);

var errors = req.validationErrors();
var mappedErrors = req.validationErrors(true);

You'll need to use req.assert to validate. req.sanitize is to sanitize input, like so:

req.sanitize('postparam').toBoolean();

Upvotes: 8

Related Questions