Skip to content

Commit

Permalink
You can now skip validations on a as-needed basis by adding {skip: ['…
Browse files Browse the repository at this point in the history
…array', 'of', 'fields']} to .validate() which is useful for bulkCreate().
  • Loading branch information
durango committed Jul 31, 2013
1 parent 8f572eb commit f3736c5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ module.exports = (function() {
DAOFactory.prototype.bulkCreate = function(records, fields, options) {
options = options || {}
options.validate = options.validate || false
fields = fields || []

var self = this
, updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt'
Expand All @@ -445,7 +446,7 @@ module.exports = (function() {
, daos = records.map(function(v) {
var build = self.build(v)
if (options.validate === true) {
var valid = build.validate({type: 'insert'})
var valid = build.validate({skip: fields})
if (valid !== null) {
errors[errors.length] = {record: v, errors: valid}
}
Expand All @@ -459,8 +460,6 @@ module.exports = (function() {
}).run()
}

fields = fields || []

// we will re-create from DAOs, which may have set up default attributes
records = []
var found = false
Expand All @@ -477,7 +476,7 @@ module.exports = (function() {
values[updatedAtAttr] = Utils.now()
}

records.push(values);
records.push(values)
})

// Validate enums
Expand Down
18 changes: 12 additions & 6 deletions lib/dao-validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var Validator = require("validator")
, Utils = require("./utils")

var DaoValidator = module.exports = function(model) {
var DaoValidator = module.exports = function(model, options) {
options = options || {}
options.skip = options.skip || []

this.model = model
this.options = options
}

DaoValidator.prototype.validate = function() {
Expand Down Expand Up @@ -32,17 +36,19 @@ var validateModel = function() {
}

var validateAttributes = function() {
var errors = {}
var self = this
, errors = {}

// for each field and value
Utils._.each(this.model.dataValues, function(value, field) {
var rawAttribute = this.model.rawAttributes[field]
var rawAttribute = self.model.rawAttributes[field]
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1

if (this.model.validators.hasOwnProperty(field) && !hasAllowedNull) {
errors = Utils._.merge(errors, validateAttribute.call(this, value, field))
if (self.model.validators.hasOwnProperty(field) && !hasAllowedNull && !isSkipped) {
errors = Utils._.merge(errors, validateAttribute.call(self, value, field))
}
}.bind(this)) // for each field
})

return errors
}
Expand Down
4 changes: 2 additions & 2 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ module.exports = (function() {
*
* @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
*/
DAO.prototype.validate = function() {
var validator = new DaoValidator(this)
DAO.prototype.validate = function(object) {
var validator = new DaoValidator(this, object)
, errors = validator.validate()

return (Utils._.isEmpty(errors) ? null : errors)
Expand Down
27 changes: 27 additions & 0 deletions test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,33 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})

it("doesn't emit an error when validate is set to true but our selectedValues are fine", function(done) {
var Tasks = this.sequelize.define('Task', {
name: {
type: Sequelize.STRING,
validate: {
notNull: { args: true, msg: 'name cannot be null' }
}
},
code: {
type: Sequelize.STRING,
validate: {
len: [3, 10]
}
}
})

Tasks.sync({ force: true }).success(function() {
Tasks.bulkCreate([
{name: 'foo', code: '123'},
{code: '1234'}
], ['code'], {validate: true}).success(function() {
// we passed!
done()
})
})
})

describe('enums', function() {
it('correctly restores enum values', function(done) {
var self = this
Expand Down

0 comments on commit f3736c5

Please sign in to comment.