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

Commit e234a683 by thanpolas

re-enable synchronous model validators

1 parent 518baccc
...@@ -73,6 +73,7 @@ var validateModel = function() { ...@@ -73,6 +73,7 @@ var validateModel = function() {
this.chainer.add(new Utils.CustomEventEmitter(function(emitter) { this.chainer.add(new Utils.CustomEventEmitter(function(emitter) {
var next = function(err) { var next = function(err) {
if (err) { if (err) {
var error = {} var error = {}
error[validatorType] = [err] error[validatorType] = [err]
...@@ -137,8 +138,33 @@ var prepareValidationOfAttribute = function(value, details, validatorType, optio ...@@ -137,8 +138,33 @@ var prepareValidationOfAttribute = function(value, details, validatorType, optio
if (typeof details === 'function') { if (typeof details === 'function') {
// it is a custom validator function? // it is a custom validator function?
isCustomValidator = true isCustomValidator = true
var callArgs = []
var validatorArity = details.length
var omitValue = !!(options || {}).omitValue
if (!omitValue) {
callArgs.push(value)
}
// check if validator is async and requires a callback
var isAsync = omitValue && validatorArity === 1 ||
!omitValue && validatorArity === 2
validatorFunction = function(next) { validatorFunction = function(next) {
details.apply(this.model, ((options || {}).omitValue) ? [next] : [value, next]) if (isAsync) {
callArgs.push(next)
}
try {
details.apply(this.model, callArgs)
} catch(ex) {
return next(ex.message)
}
if (!isAsync) {
next()
}
}.bind(this) }.bind(this)
} else { } else {
// it is a validator module function? // it is a validator module function?
......
/* jshint expr:true */
var chai = require('chai') var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Sequelize = require(__dirname + '/../index') , Sequelize = require(__dirname + '/../index')
...@@ -571,6 +572,43 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -571,6 +572,43 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
}) })
it('validates a model with no async callback', function(done) {
var Foo = this.sequelize.define('Foo' + config.rand(), {
field1: {
type: Sequelize.INTEGER,
allowNull: true
},
field2: {
type: Sequelize.INTEGER,
allowNull: true
}
}, {
validate: {
xnor: function() {
if ((this.field1 === null) === (this.field2 === null)) {
throw new Error('xnor failed')
}
}
}
})
Foo
.build({ field1: null, field2: null })
.validate()
.success(function(errors) {
expect(errors).not.to.be.null
expect(errors).to.deep.equal({ 'xnor': ['xnor failed'] })
Foo
.build({ field1: 33, field2: null })
.validate()
.success(function(errors) {
expect(errors).not.exist
done()
})
})
})
it('validates model with a validator whose arg is an Array successfully twice in a row', function(done){ it('validates model with a validator whose arg is an Array successfully twice in a row', function(done){
var Foo = this.sequelize.define('Foo' + config.rand(), { var Foo = this.sequelize.define('Foo' + config.rand(), {
bar: { bar: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!