Thanks for a cool validation module.
One thing that bugs me is how validate.async() rejects with the errors. This is not analogous to how validate() works. It returns no matter the validation result, either with undefined for success or a hash of errors if not successful. It does not throw the errors. So validate.async should also do the same: Resolve with either undefined if successful or a hash of errors if not successful.
Rejecting the promise corresponds to throwing in validate(). It indicates an application error, not a validation error.
This goes for both validate.async and the promise custom async validators return.
First of all nothing should reject with random objects or strings. You should reject with a proper Error instance.
Secondly, it becomes a problem when the validation rejection gets mixed up with application errors. Worst case scenario is that an application error containing sensitive information is leaked to a user. Here's an example of that:
validate.validators.userExists = function(userId) {
return db.query("SELECT id FROM users WHERE ?", [userId])
.then(function(rows) {
if (rows.length === 0) {
throw 'User does not exist';
}
})
}
If db.query rejects, the db error object will be included in the final validation errors hash.
I would gladly submit a PR to fix this, if you want to take the library this way (I definitely think you should).