ya_dimon ya_dimon - 6 months ago 43
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:



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
should be enough.


Do you use the express validator module?

If yes, is this something that works for you?

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


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

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