ya_dimon ya_dimon - 1 year ago 99
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.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download