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

Commit e967a2eb by Jan Aagaard Meier

Apply type validation to all dialects. Thanks to @fixe

1 parent 7073e03b
# NEXT
- [FIXED] Fix findOrCreate regression trying to add a transaction even if there is none
- [FEATURE] Added default validation based on attribute types. [#3472](https://github.com/sequelize/sequelize/pull/3472). The validation _cannot_ be disabled. If you really want to completely disable it, you can remove the `validate` function from the corresponding datatype, but know that this permanently disables the validation.
# 3.4.1
- [FIXED] Fix belongs-to-many `countAssociations` - ambigious id when through model has id
......
......@@ -84,6 +84,11 @@ STRING.prototype.toSql = function() {
};
STRING.prototype.validate = function(value) {
if (typeof value !== 'string') {
if (this.options.binary) {
if (Buffer.isBuffer(value)) {
return true;
}
}
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
}
......
......@@ -268,7 +268,6 @@ var QueryGenerator = {
}
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull);
for (key in valueHash) {
if (valueHash.hasOwnProperty(key)) {
value = valueHash[key];
......@@ -894,6 +893,11 @@ var QueryGenerator = {
if (value && value._isSequelizeMethod) {
return this.handleSequelizeMethod(value);
} else {
if (field && field.type && value) {
if (field.type.validate) {
field.type.validate(value);
}
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
}
},
......
......@@ -358,6 +358,11 @@ var QueryGenerator = {
} else if (value && field && field.type instanceof DataTypes.GEOMETRY) {
return 'GeomFromText(\'' + Wkt.stringify(value) + '\')';
} else {
if (field && field.type && value) {
if (field.type.validate) {
field.type.validate(value);
}
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
}
},
......@@ -387,7 +392,7 @@ var QueryGenerator = {
geometrySelect: function(column) {
return 'AsText(' + this.quoteIdentifiers(column) + ') AS ' + column;
}
}
};
module.exports = Utils._.extend(Utils._.clone(require('../abstract/query-generator')), QueryGenerator);
......@@ -131,6 +131,7 @@ var QueryGenerator = {
upsertQuery: function (tableName, insertValues, updateValues, where, rawAttributes, options) {
options.ignore = true;
var sql = this.insertQuery(tableName, insertValues, rawAttributes, options) + ' ' + this.updateQuery(tableName, updateValues, where, options, rawAttributes);
return sql;
......
......@@ -410,7 +410,7 @@ QueryInterface.prototype.renameColumn = function(tableName, attrNameBefore, attr
_options[attrNameAfter] = {
attribute: attrNameAfter,
type: DataTypes[data.type],
type: data.type,
allowNull: data.allowNull,
defaultValue: data.defaultValue
};
......
......@@ -495,25 +495,6 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
});
});
it('skips validation when asked', function() {
var values = ['value1', 'value2'];
var Bar = this.sequelize.define('Bar' + config.rand(), {
field: {
type: Sequelize.ENUM,
values: values,
validate: {
isIn: [values]
}
}
});
return Bar.sync({force: true}).then(function() {
return Bar.create({ field: 'value3' }, {validate: false})
.catch(Sequelize.DatabaseError, function() {
});
});
});
it('raises an error if saving a different value into an immutable field', function() {
var User = this.sequelize.define('User', {
name: {
......
......@@ -6,6 +6,7 @@ var chai = require('chai')
, expect = chai.expect
, Sequelize = require(__dirname + '/../../../index')
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, config = require(__dirname + '/../../config/config');
describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
......@@ -263,4 +264,41 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
describe('datatype validations', function () {
describe.only('should throw validationerror', function () {
var User = current.define('user', {
age: Sequelize.INTEGER
});
describe('create', function () {
it('should throw when passing string', function () {
return expect(User.create({
age: 'jan'
})).to.be.rejectedWith(current.ValidationError);
});
it('should throw when passing decimal', function () {
return expect(User.create({
age: 4.5
})).to.be.rejectedWith(current.ValidationError);
});
});
describe('update', function () {
it('should throw when passing string', function () {
return expect(User.update({
age: 'jan'
}, { where : {}})).to.be.rejectedWith(current.ValidationError);
});
it('should throw when passing decimal', function () {
return expect(User.update({
age: 4.5
}, { where : {}})).to.be.rejectedWith(current.ValidationError);
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!