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

Commit f3736c59 by Daniel Durante

You can now skip validations on a as-needed basis by adding {skip: ['array', 'of…

…', 'fields']} to .validate() which is useful for bulkCreate().
1 parent 8f572eb2
...@@ -437,6 +437,7 @@ module.exports = (function() { ...@@ -437,6 +437,7 @@ module.exports = (function() {
DAOFactory.prototype.bulkCreate = function(records, fields, options) { DAOFactory.prototype.bulkCreate = function(records, fields, options) {
options = options || {} options = options || {}
options.validate = options.validate || false options.validate = options.validate || false
fields = fields || []
var self = this var self = this
, updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt' , updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt'
...@@ -445,7 +446,7 @@ module.exports = (function() { ...@@ -445,7 +446,7 @@ module.exports = (function() {
, daos = records.map(function(v) { , daos = records.map(function(v) {
var build = self.build(v) var build = self.build(v)
if (options.validate === true) { if (options.validate === true) {
var valid = build.validate({type: 'insert'}) var valid = build.validate({skip: fields})
if (valid !== null) { if (valid !== null) {
errors[errors.length] = {record: v, errors: valid} errors[errors.length] = {record: v, errors: valid}
} }
...@@ -459,8 +460,6 @@ module.exports = (function() { ...@@ -459,8 +460,6 @@ module.exports = (function() {
}).run() }).run()
} }
fields = fields || []
// we will re-create from DAOs, which may have set up default attributes // we will re-create from DAOs, which may have set up default attributes
records = [] records = []
var found = false var found = false
...@@ -477,7 +476,7 @@ module.exports = (function() { ...@@ -477,7 +476,7 @@ module.exports = (function() {
values[updatedAtAttr] = Utils.now() values[updatedAtAttr] = Utils.now()
} }
records.push(values); records.push(values)
}) })
// Validate enums // Validate enums
......
var Validator = require("validator") var Validator = require("validator")
, Utils = require("./utils") , 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.model = model
this.options = options
} }
DaoValidator.prototype.validate = function() { DaoValidator.prototype.validate = function() {
...@@ -32,17 +36,19 @@ var validateModel = function() { ...@@ -32,17 +36,19 @@ var validateModel = function() {
} }
var validateAttributes = function() { var validateAttributes = function() {
var errors = {} var self = this
, errors = {}
// for each field and value // for each field and value
Utils._.each(this.model.dataValues, function(value, field) { 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))) , 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) { if (self.model.validators.hasOwnProperty(field) && !hasAllowedNull && !isSkipped) {
errors = Utils._.merge(errors, validateAttribute.call(this, value, field)) errors = Utils._.merge(errors, validateAttribute.call(self, value, field))
} }
}.bind(this)) // for each field })
return errors return errors
} }
......
...@@ -201,8 +201,8 @@ module.exports = (function() { ...@@ -201,8 +201,8 @@ module.exports = (function() {
* *
* @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries. * @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
*/ */
DAO.prototype.validate = function() { DAO.prototype.validate = function(object) {
var validator = new DaoValidator(this) var validator = new DaoValidator(this, object)
, errors = validator.validate() , errors = validator.validate()
return (Utils._.isEmpty(errors) ? null : errors) return (Utils._.isEmpty(errors) ? null : errors)
......
...@@ -750,6 +750,33 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -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() { describe('enums', function() {
it('correctly restores enum values', function(done) { it('correctly restores enum values', function(done) {
var self = this var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!