Sequelize.js – asynchronousvalidation器

有没有可能有一个asynchronousvalidation使用Sequelize.js? 我想在保存模型之前检查一个关联的存在。 像这样的东西:

User = db.define("user", { name: Sequelize.STRING }, { validate: hasDevice: -> @getDevices().success (devices) -> throw Exception if (devices.length < 1) return }) # .... Device is just another model User.hasMany(Device) 

还是有办法强制检查同步运行? (不理想)

您可以在v2.0.0中使用asynchronousvalidation。 它是这样工作的:

 var Model = sequelize.define('Model', { attr: Sequelize.STRING }, { validate: { hasAssociation: function(next) { functionThatChecksTheAssociation(function(ok) { if (ok) { next() } else { next('Ooops. Something is wrong!') } }) } } })