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

Commit 921f6955 by Jan Aagaard Meier

Merge pull request #1374 from tonytamps:feature/immutable-validation

1 parent f449abc9
......@@ -78,6 +78,13 @@ Validator.extend('is', function(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers);
})
function extendModelValidations(modelInstance){
Validator.extend('isImmutable', function(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
})
}
/**
* The Main DAO Validator.
*
......@@ -112,6 +119,8 @@ var DaoValidator = module.exports = function(modelInstance, options) {
/** @type {boolean} Indicates if validations are in progress */
this.inProgress = false;
extendModelValidations(modelInstance);
}
/** @define {string} The error key for arguments as passed by custom validators */
......@@ -259,7 +268,7 @@ DaoValidator.prototype._builtinAttrValidate = function(value, field) {
true, value, field))
}
var validatorPromise = self._invokeBuiltinValidator(value, test, validatorType);
var validatorPromise = self._invokeBuiltinValidator(value, test, validatorType, field);
// errors are handled in settling, stub this
validatorPromise.catch(noop)
validators.push(validatorPromise)
......@@ -320,11 +329,12 @@ DaoValidator.prototype._invokeCustomValidator = Promise.method(function(validato
* @param {*} value Anything.
* @param {*} test The test case.
* @param {string} validatorType One of known to Sequelize validators.
* @param {string} field The field that is being validated
* @return {Object} An object with specific keys to invoke the validator.
* @private
*/
DaoValidator.prototype._invokeBuiltinValidator = Promise.method(function(value,
test, validatorType) {
test, validatorType, field) {
// check if Validator knows that kind of validation test
if (typeof Validator[validatorType] !== 'function') {
......@@ -343,6 +353,7 @@ DaoValidator.prototype._invokeBuiltinValidator = Promise.method(function(value,
} else {
validatorArgs = validatorArgs.slice(0);
}
validatorArgs.push(field)
if (!Validator[validatorType].apply(Validator, [value].concat(validatorArgs))) {
throw errorMessage
}
......
......@@ -718,5 +718,50 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
done()
})
})
it("raises an error if saving a different value into an immutable field", function(done) {
var User = this.sequelize.define('User', {
name: {
type: Sequelize.STRING,
validate: {
isImmutable: true
}
}
})
User.sync({force: true}).success(function() {
User.create({ name: "RedCat" }).success(function(user){
expect(user.getDataValue('name')).to.equal('RedCat')
user.setDataValue('name','YellowCat')
user.save()
.done(function(errors){
expect(errors).to.not.be.null
expect(errors).to.be.an.instanceOf(Error)
expect(errors.name[0].message).to.eql('Validation isImmutable failed')
done()
})
})
})
})
it("allows setting an immutable field if the record is unsaved", function(done) {
var User = this.sequelize.define('User', {
name: {
type: Sequelize.STRING,
validate: {
isImmutable: true
}
}
})
var user = User.build({ name: "RedCat" })
expect(user.getDataValue('name')).to.equal('RedCat')
user.setDataValue('name','YellowCat')
user.validate().done(function(errors){
expect(errors).to.be.null
done()
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!