1602 / jugglingdb

Multi-database ORM for nodejs: redis, mongodb, mysql, sqlite3, postgresql, arango, in-memory...
http://1602.github.io/jugglingdb/
2.04k stars 241 forks source link

{ if: 'fieldName } doesn't play nice with async validations #342

Open absynce opened 10 years ago

absynce commented 10 years ago

When using the if option on an async validation, the async isValid does not call the callback.

Example User model:

User.validateAsync({ 
    if      : 'createdByScript',
    message : 'group is not valid' 
}, validateGroup);

function validateGroup(err, done) {
  Group.find(this.groupId, function (error, group) {
    if (error || !group) { err(); }
    done();
  });
}

The following code does not call the isValid callback:

var u = new User();
u.isValid(function (valid) {
  // This is never executed.
});

One could argue it's unnecessary to have this attribute on a custom async validation because the properties could be checked within the validation method (which is what I ended up doing to get around the issue), but it should then be ignored or at least documented so people are aware.

yanickrochon commented 10 years ago

It seems that the next versions will always handle model validation with a callback, therefore will fix your problem. The issue you're having is because if the model's async validation is skipped, the "async" mode is never set and the validation result is returned instead of the callback being called.

Follow up with issues #378 and #362.