回送模型validation失败(asynchronous行为)

我试图validation一个模型,它的内容。 但是,由于回送自定义validation函数的结构,编程比简单stringvalidation更高级的逻辑相当困难。

Job.validate('job_definition, function(err){ //err(); //this will succeed in throwing error Job.app.models.anotherModel.findOne({where:{name:this.job_definition.toolName}}, function(error, tool){ if(tool.aProperty === this.job_definition.aProperty){ //err(); //this will not succeed, validation script will exit before err() is thrown } }); }, {message: 'this is malformed'}); 

我怎样才能得到这个validationfunction,在退出之前等待?

这里是一个使用validateAsync的例子( https://apidocs.strongloop.com/loopback-datasource-juggler/#validatable-validateasync )。 请注意,如果您想validation失败,则必须运行err()。

 module.exports = function(Person) { function myCustom(err, done) { console.log('async validate'); var name = this.name; setTimeout(function() { if(name == 'Ray') { err(); done(); } else { done(); } }, 1000); } Person.validateAsync('name', myCustom, {message:'Dont like'}); }; 

这有道理吗? 仅供参考,如果更好一点,我可以重写。