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

Commit 30d24ba9 by Faris Masad Committed by Sushant

Include field name for built-in validation error messages (#6618)

* Include field name in built in validation error message

* Use template string for error message
1 parent f07b58a7
...@@ -262,7 +262,7 @@ class InstanceValidator { ...@@ -262,7 +262,7 @@ class InstanceValidator {
const validatorArgs = this._extractValidatorArgs(test, validatorType, field); const validatorArgs = this._extractValidatorArgs(test, validatorType, field);
if (!validator[validatorType].apply(validator, [valueString].concat(validatorArgs))) { if (!validator[validatorType].apply(validator, [valueString].concat(validatorArgs))) {
// extract the error msg // extract the error msg
throw new Error(test.msg || 'Validation ' + validatorType + ' failed'); throw new Error(test.msg || `Validation ${validatorType} on ${field} failed`);
} }
}); });
} }
......
...@@ -1275,7 +1275,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1275,7 +1275,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect(err).to.be.instanceof(Object); expect(err).to.be.instanceof(Object);
expect(err.get('validateTest')).to.be.instanceof(Array); expect(err.get('validateTest')).to.be.instanceof(Array);
expect(err.get('validateTest')[0]).to.exist; expect(err.get('validateTest')[0]).to.exist;
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed'); expect(err.get('validateTest')[0].message).to.equal('Validation isInt on validateTest failed');
}); });
}); });
...@@ -1285,7 +1285,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1285,7 +1285,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect(err).to.be.instanceof(Object); expect(err).to.be.instanceof(Object);
expect(err.get('validateTest')).to.be.instanceof(Array); expect(err.get('validateTest')).to.be.instanceof(Array);
expect(err.get('validateTest')[0]).to.exist; expect(err.get('validateTest')[0]).to.exist;
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed'); expect(err.get('validateTest')[0].message).to.equal('Validation isInt on validateTest failed');
}); });
}); });
...@@ -1309,7 +1309,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1309,7 +1309,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect(err.get('validateTest')).to.exist; expect(err.get('validateTest')).to.exist;
expect(err.get('validateTest')).to.be.instanceof(Array); expect(err.get('validateTest')).to.be.instanceof(Array);
expect(err.get('validateTest')[0]).to.exist; expect(err.get('validateTest')[0]).to.exist;
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed'); expect(err.get('validateTest')[0].message).to.equal('Validation isInt on validateTest failed');
}); });
}); });
}); });
......
...@@ -52,7 +52,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -52,7 +52,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
return Model.create({name: 'World'}).then(function(model) { return Model.create({name: 'World'}).then(function(model) {
return model.updateAttributes({name: ''}).catch(function(err) { return model.updateAttributes({name: ''}).catch(function(err) {
expect(err).to.be.an.instanceOf(Error); expect(err).to.be.an.instanceOf(Error);
expect(err.get('name')[0].message).to.equal('Validation notEmpty failed'); expect(err.get('name')[0].message).to.equal('Validation notEmpty on name failed');
}); });
}); });
}); });
...@@ -73,7 +73,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -73,7 +73,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
return Model.create({name: 'World'}).then(function() { return Model.create({name: 'World'}).then(function() {
return Model.update({name: ''}, {where: {id: 1}}).catch(function(err) { return Model.update({name: ''}, {where: {id: 1}}).catch(function(err) {
expect(err).to.be.an.instanceOf(Error); expect(err).to.be.an.instanceOf(Error);
expect(err.get('name')[0].message).to.equal('Validation notEmpty failed'); expect(err.get('name')[0].message).to.equal('Validation notEmpty on name failed');
}); });
}); });
}); });
...@@ -236,7 +236,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -236,7 +236,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
return User.sync({ force: true }).then(function() { return User.sync({ force: true }).then(function() {
return User.create({id: 'helloworld'}).catch(function(err) { return User.create({id: 'helloworld'}).catch(function(err) {
expect(err).to.be.an.instanceOf(Error); expect(err).to.be.an.instanceOf(Error);
expect(err.get('id')[0].message).to.equal('Validation isInt failed'); expect(err.get('id')[0].message).to.equal('Validation isInt on id failed');
}); });
}); });
}); });
...@@ -490,7 +490,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -490,7 +490,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
return expect(failingBar.validate()).to.be.rejected.then(function(errors) { return expect(failingBar.validate()).to.be.rejected.then(function(errors) {
expect(errors.get('field')).to.have.length(1); expect(errors.get('field')).to.have.length(1);
expect(errors.get('field')[0].message).to.equal('Validation isIn failed'); expect(errors.get('field')[0].message).to.equal('Validation isIn on field failed');
}); });
}); });
...@@ -527,7 +527,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -527,7 +527,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
expect(user.getDataValue('name')).to.equal('RedCat'); expect(user.getDataValue('name')).to.equal('RedCat');
user.setDataValue('name', 'YellowCat'); user.setDataValue('name', 'YellowCat');
return expect(user.save()).to.be.rejected.then(function(errors) { return expect(user.save()).to.be.rejected.then(function(errors) {
expect(errors.get('name')[0].message).to.eql('Validation isImmutable failed'); expect(errors.get('name')[0].message).to.eql('Validation isImmutable on name failed');
}); });
}); });
}); });
...@@ -689,7 +689,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -689,7 +689,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
name: 'a' name: 'a'
}).validate()).to.be.rejected; }).validate()).to.be.rejected;
}).then(function(errors) { }).then(function(errors) {
expect(errors.get('name')[0].message).to.equal('Validation isExactly7Characters failed'); expect(errors.get('name')[0].message).to.equal('Validation isExactly7Characters on name failed');
}); });
}); });
}); });
...@@ -1004,7 +1004,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1004,7 +1004,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(str2.str).to.equal('http://sequelizejs.org'); expect(str2.str).to.equal('http://sequelizejs.org');
return StringIsNullOrUrl.create({ str: '' }).catch(function(err) { return StringIsNullOrUrl.create({ str: '' }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(err.get('str')[0].message).to.match(/Validation isURL failed/); expect(err.get('str')[0].message).to.match(/Validation isURL on str failed/);
}); });
}); });
}); });
...@@ -1619,7 +1619,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1619,7 +1619,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(errors[0].errors.get('name')[0].type).to.equal('notNull Violation'); expect(errors[0].errors.get('name')[0].type).to.equal('notNull Violation');
expect(errors[1].record.name).to.equal('bar'); expect(errors[1].record.name).to.equal('bar');
expect(errors[1].record.code).to.equal('1'); expect(errors[1].record.code).to.equal('1');
expect(errors[1].errors.get('code')[0].message).to.equal('Validation len failed'); expect(errors[1].errors.get('code')[0].message).to.equal('Validation len on code failed');
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!