Sequelize.js-异步验证器

Jes*_*ton 3 node.js sequelize.js

使用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)
Run Code Online (Sandbox Code Playgroud)

还是有一种方法可以强制该检查同步运行?(不理想)

sde*_*old 7

您可以在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!')
        }
      })
    }
  }
})
Run Code Online (Sandbox Code Playgroud)