问题
Imagine I have defined the following custom validator function:
isUnique: function () { // This works as expected
  throw new Error({error:[{message:'Email address already in use!'}]});
}
However, when I attempt to query the DB I run into problems:
isUnique: function (email) { // This doesn't work
  var User = seqeulize.import('/path/to/user/model');
  User.find({where:{email: email}})
    .success(function () { // This gets called
      throw new Error({error:[{message:'Email address already in use!'}]});  // But this isn't triggering a validation error.
    });
}
How can I query the ORM in a custom validator and trigger a validation error based on the response from the ORM?
回答1:
You can verify if the email already exists like that:
email: {
  type: Sequelize.STRING,
  allowNull: false,
  validate: {
    isEmail:true
  },
  unique: {
      args: true,
      msg: 'Email address already in use!'
  }
}
回答2:
Here's a simplified sample of a functioning isUnique validation callback (works as of SequelizeJS v2.0.0). I added comments to explain the important bits:
var UserModel = sequelize.define('User', {
    id: {
        type: Sequelize.INTEGER(11).UNSIGNED,
        autoIncrement: true,
        primaryKey: true
    },
    email: {
        type: Sequelize.STRING,
        validate: {
            isUnique: function(value, next) {
                UserModel.find({
                    where: {email: value},
                    attributes: ['id']
                })
                    .done(function(error, user) {
                        if (error)
                            // Some unexpected error occured with the find method.
                            return next(error);
                        if (user)
                            // We found a user with this email address.
                            // Pass the error to the next method.
                            return next('Email address already in use!');
                        // If we got this far, the email address hasn't been used yet.
                        // Call next with no arguments when validation is successful.
                        next();
                    });
            }
        }
    }
});
module.exports = UserModel;
回答3:
With Sequelize 2.0, you need to catch Validation Errors.
First, define the User Model with a custom validator:
var User = sequelize.define('User',
    {
        email: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: true,
            validate: {
                isUnique: function (value, next) {
                    var self = this;
                    User.find({where: {email: value}})
                        .then(function (user) {
                            // reject if a different user wants to use the same email
                            if (user && self.id !== user.id) {
                                return next('Email already in use!');
                            }
                            return next();
                        })
                        .catch(function (err) {
                            return next(err);
                        });
                }
            }
        },
        other_field: Sequelize.STRING
    });
module.exports = User;
Then, in the controller, catch any Validation Errors:
var Sequelize = require('sequelize'),
    _ = require('lodash'),
    User = require('./path/to/User.model');
exports.create = function (req, res) {
    var allowedKeys = ['email', 'other_field'];
    var attributes = _.pick(req.body, allowedKeys);
    User.create(attributes)
        .then(function (user) {
            res.json(user);
        })
        .catch(Sequelize.ValidationError, function (err) {
            // respond with validation errors
            return res.status(422).send(err.errors);
        })
        .catch(function (err) {
            // every other error
            return res.status(400).send({
                message: err.message
            });
        });
回答4:
Success callback is called even if no user is found. You have to check if the function passes a user as an argument:
isUnique: function (email) { // This doesn't work
  var User = seqeulize.import('/path/to/user/model');
  User.find({where:{email: email}})
    .success(function (u) { // This gets called
      if(u){
        throw new Error({error:[{message:'Email address already in use!'}]});  // But this isn't triggering a validation error.
      }
    });
}
来源:https://stackoverflow.com/questions/16356856/sequelize-js-custom-validator-check-for-unique-username-password