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

Commit f3b924d5 by Mick Hansen

fix(validations): add basic schema validation to STRING(N) and TEXT aswell, closes #2631

1 parent b987ae4a
...@@ -364,7 +364,7 @@ InstanceValidator.prototype._validateSchema = function(rawAttribute, field, valu ...@@ -364,7 +364,7 @@ InstanceValidator.prototype._validateSchema = function(rawAttribute, field, valu
this.errors.push(error); this.errors.push(error);
} }
if (rawAttribute.type === DataTypes.STRING) { if (rawAttribute.type === DataTypes.STRING || rawAttribute.type instanceof DataTypes.STRING || rawAttribute.type === DataTypes.TEXT) {
if (Array.isArray(value) || (_.isObject(value) && !value._isSequelizeMethod)) { if (Array.isArray(value) || (_.isObject(value) && !value._isSequelizeMethod)) {
error = new sequelizeError.ValidationErrorItem(field + ' cannot be an array or an object', 'string violation', field, value); error = new sequelizeError.ValidationErrorItem(field + ' cannot be an array or an object', 'string violation', field, value);
this.errors.push(error); this.errors.push(error);
......
...@@ -825,6 +825,36 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -825,6 +825,36 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
}) })
it('raises an error for array on a STRING(20)', function (done) {
var User = this.sequelize.define('User', {
'email': {
type: Sequelize.STRING(20)
}
})
User.build({
email: ['iama', 'dummy.com']
}).validate().success(function (errors) {
expect(errors).to.be.an.instanceof(Sequelize.ValidationError)
done()
})
})
it('raises an error for array on a TEXT', function (done) {
var User = this.sequelize.define('User', {
'email': {
type: Sequelize.TEXT
}
})
User.build({
email: ['iama', 'dummy.com']
}).validate().success(function (errors) {
expect(errors).to.be.an.instanceof(Sequelize.ValidationError)
done()
})
})
it('raises an error for {} on a STRING', function (done) { it('raises an error for {} on a STRING', function (done) {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
'email': { 'email': {
...@@ -840,6 +870,36 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -840,6 +870,36 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
}) })
it('raises an error for {} on a STRING(20)', function (done) {
var User = this.sequelize.define('User', {
'email': {
type: Sequelize.STRING(20)
}
})
User.build({
email: {lol: true}
}).validate().success(function (errors) {
expect(errors).to.be.an.instanceof(Sequelize.ValidationError)
done()
})
})
it('raises an error for {} on a TEXT', function (done) {
var User = this.sequelize.define('User', {
'email': {
type: Sequelize.TEXT
}
})
User.build({
email: {lol: true}
}).validate().success(function (errors) {
expect(errors).to.be.an.instanceof(Sequelize.ValidationError)
done()
})
})
it('does not raise an error for null on a STRING (where null is allowed)', function (done) { it('does not raise an error for null on a STRING (where null is allowed)', function (done) {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
'email': { 'email': {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!