不要怂,就是干,撸起袖子干!

Commit 4b4549d5 by Daniel Durante

DAOFactory.update() will now skip validations for columns that we're not updating. Closes #876

1 parent 65ee4e9d
...@@ -657,14 +657,17 @@ module.exports = (function() { ...@@ -657,14 +657,17 @@ module.exports = (function() {
DAOFactory.prototype.update = function(attrValueHash, where, options) { DAOFactory.prototype.update = function(attrValueHash, where, options) {
options = options || {} options = options || {}
options.validate = options.validate === undefined ? true : Boolean(options.validate) options.validate = options.validate === undefined ? true : Boolean(options.validate)
if(this.options.timestamps) { if(this.options.timestamps) {
var attr = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored) var attr = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)
attrValueHash[attr] = Utils.now() attrValueHash[attr] = Utils.now()
} }
if (options.validate === true) { if (options.validate === true) {
var validate = this.build(attrValueHash).validate() var build = this.build(attrValueHash)
, attrKeys = Object.keys(attrValueHash)
, validate = build.validate({skip: Object.keys(build.dataValues).filter(function(val) { return attrKeys.indexOf(val) !== -1 })})
if (validate !== null && Object.keys(validate).length > 0) { if (validate !== null && Object.keys(validate).length > 0) {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
emitter.emit('error', validate) emitter.emit('error', validate)
......
...@@ -36,7 +36,7 @@ var validateModel = function() { ...@@ -36,7 +36,7 @@ var validateModel = function() {
} }
var validateAttributes = function() { var validateAttributes = function() {
var self = this var self = this
, errors = {} , errors = {}
// for each field and value // for each field and value
......
...@@ -254,6 +254,34 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -254,6 +254,34 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
} }
describe('#update', function() { describe('#update', function() {
it('should allow us to update specific columns without tripping the validations', function(done) {
var User = this.sequelize.define('model', {
username: Sequelize.STRING,
email: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isEmail: {
msg: 'You must enter a valid email address'
}
}
}
})
User.sync({ force: true }).success(function() {
User.create({username: 'bob', email: 'hello@world.com'}).success(function(user) {
User.update({username: 'toni'}, {id: user.id})
.error(function(err) { console.log(err) })
.success(function() {
User.find(1).success(function(user) {
expect(user.username).to.equal('toni')
done()
})
})
})
})
})
it('should be able to emit an error upon updating when a validation has failed from an instance', function(done) { it('should be able to emit an error upon updating when a validation has failed from an instance', function(done) {
var Model = this.sequelize.define('model', { var Model = this.sequelize.define('model', {
name: { name: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!