leepowelldev / mongoose-validator

Validators for mongoose models utilising validator.js
MIT License
378 stars 43 forks source link

Custom Validator ignoring false returnable? #36

Closed ryancmoon closed 7 years ago

ryancmoon commented 7 years ago

My custom validator is returning true/false, which seems to follow the documentation, but it is being ignored. I suspect this might be an async issue, but the default-return is false on this function:

  validate({ validator: function(value) {
      User.findOne({_id: value}, function(err, user) {
        if (user && user._id) {
          return true
        } else {
          return false
        }
      });
    }, message: 'A real user _id must be provided as the user.' })
  ]

user is correctly "null" when an invalid _id is passed as value, and a user object when a valid one is passed. However, mongoose-validator does not seem to respect the returned boolean.

leepowelldev commented 7 years ago

Hi. You're correct, this is an async issue. Your validator isn't actually returning any value, the callback is. All validators should be synchronous. If I get time in the future I may look at providing some kind of async support with promises.

ryancmoon commented 7 years ago

Ahh. Thanks for the quick reply. Cheers.