ya_dimon ya_dimon - 1 month ago 17
Node.js Question

express-validator/node-validator: how to validate that property exists, and should be present

is there a validation like:

req.checkBody('property', 'should be present').isPresent();


The value itself, does not matter.

for now i use this solution:

.isLength()


update

-
.isLength()
is a bad solution, because it tests if the value is a string.

- you can not send value as 'undefined' because of JSON.stringify method, that ignores such properties. So the validation against
undefined
should be enough.

Answer

Do you use the express validator module?

If yes, is this something that works for you?

req.checkBody('postparam', 'Invalid postparam').notEmpty()

UPDATE

According to the documentation does not exist the validator that you want. Creating your custom one can be the solution:

expressValidator({
  customValidators: {
    isPresent: function(value) {
        return (typeof value !== 'undefined');
    }
  }
})