是否可以使用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)
或者有没有办法强制该检查同步运行? (不理想)
答案 0 :(得分:6)
您可以在v2.0.0中使用异步验证。 它的工作原理如下:
var Model = sequelize.define('Model', {
attr: Sequelize.STRING
}, {
validate: {
hasAssociation: function(next) {
functionThatChecksTheAssociation(function(ok) {
if (ok) {
next()
} else {
next('Ooops. Something is wrong!')
}
})
}
}
})