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

Commit 25a4f4b3 by Sascha Depold

Merge branch 'update-validations' of https://github.com/durango/sequelize into d…

…urango-update-validations
2 parents abf50093 b17b1e08
Showing with 97 additions and 18 deletions
...@@ -558,10 +558,23 @@ module.exports = (function() { ...@@ -558,10 +558,23 @@ module.exports = (function() {
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`. * @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/ */
DAOFactory.prototype.update = function(attrValueHash, where, options) { DAOFactory.prototype.update = function(attrValueHash, where, options) {
options = options || {}
options.validate = options.validate || true
if(this.options.timestamps) { if(this.options.timestamps) {
var attr = this.options.underscored ? 'updated_at' : 'updatedAt' var attr = this.options.underscored ? 'updated_at' : 'updatedAt'
attrValueHash[attr] = Utils.now() attrValueHash[attr] = Utils.now()
} }
if (options.validate === true) {
var validate = this.build(attrValueHash).validate()
if (validate !== null && Object.keys(validate).length > 0) {
return new Utils.CustomEventEmitter(function(emitter) {
emitter.emit('error', validate)
}).run()
}
}
return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where, options) return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where, options)
} }
......
...@@ -253,6 +253,50 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -253,6 +253,50 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
} }
} }
describe('#update', function() {
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', {
name: {
type: Sequelize.STRING,
validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings
}
}
})
Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) {
model.updateAttributes({name: ''}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'String is empty: name', 'String is empty: name' ] })
done()
})
})
})
})
it('should be able to emit an error upon updating when a validation has failed from the factory', function(done) {
var Model = this.sequelize.define('model', {
name: {
type: Sequelize.STRING,
validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings
}
}
})
Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) {
Model.update({name: ''}, {id: 1}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'String is empty: name', 'String is empty: name' ] })
done()
})
})
})
})
})
describe('#create', function() { describe('#create', function() {
describe('generic', function() { describe('generic', function() {
beforeEach(function(done) { beforeEach(function(done) {
...@@ -301,7 +345,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -301,7 +345,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
}) })
describe('explicitly validating id/primary/auto incremented columns', function() { describe('explicitly validating primary/auto incremented columns', function() {
it('should emit an error when we try to enter in a string for the id key without validation arguments', function(done) { it('should emit an error when we try to enter in a string for the id key without validation arguments', function(done) {
var User = this.sequelize.define('UserId', { var User = this.sequelize.define('UserId', {
id: { id: {
...@@ -322,41 +366,63 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -322,41 +366,63 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
}) })
it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) { it('should emit an error when we try to enter in a string for an auto increment key (not named id)', function(done) {
var User = this.sequelize.define('UserId', { var User = this.sequelize.define('UserId', {
id: { username: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
autoIncrement: true, autoIncrement: true,
primaryKey: true, primaryKey: true,
validate: { validate: {
isInt: { args: true, msg: 'ID must be an integer!' } isInt: { args: true, msg: 'Username must be an integer!' }
} }
} }
}) })
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
User.create({id: 'helloworld'}).error(function(err) { User.create({username: 'helloworldhelloworld'}).error(function(err) {
expect(err).to.deep.equal({id: ['ID must be an integer!']}) expect(err).to.deep.equal({username: ['Username must be an integer!']})
done() done()
}) })
}) })
}) })
it('should emit an error when we try to enter in a string for an auto increment key (not named id)', function(done) { describe("primaryKey with the name as id with arguments for it's validation", function() {
var User = this.sequelize.define('UserId', { beforeEach(function(done) {
username: { this.User = this.sequelize.define('UserId', {
type: Sequelize.INTEGER, id: {
autoIncrement: true, type: Sequelize.INTEGER,
primaryKey: true, autoIncrement: true,
validate: { primaryKey: true,
isInt: { args: true, msg: 'Username must be an integer!' } validate: {
isInt: { args: true, msg: 'ID must be an integer!' }
}
} }
} })
this.User.sync({ force: true }).success(function() {
done()
})
}) })
User.sync({ force: true }).success(function() { it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) {
User.create({username: 'helloworldhelloworld'}).error(function(err) { this.User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.deep.equal({username: ['Username must be an integer!']}) expect(err).to.deep.equal({id: ['ID must be an integer!']})
done()
})
})
it('should emit an error when we try to enter in a string for an auto increment key through .build().validate()', function(done) {
var user = this.User.build({id: 'helloworld'})
, errors = user.validate()
expect(errors).to.deep.equal({ id: [ 'ID must be an integer!' ] })
done()
})
it('should emit an error when we try to .save()', function(done) {
var user = this.User.build({id: 'helloworld'})
user.save().error(function(err) {
expect(err).to.deep.equal({ id: [ 'ID must be an integer!' ] })
done() done()
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!