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

Commit 089504fe by Gabe Gorelick Committed by Sushant

feat(instance-validator): add model name to allowNull messages (#8500)

1 parent e40f6f45
...@@ -322,7 +322,7 @@ class InstanceValidator { ...@@ -322,7 +322,7 @@ class InstanceValidator {
if (rawAttribute.allowNull === false && (value === null || value === undefined)) { if (rawAttribute.allowNull === false && (value === null || value === undefined)) {
const validators = this.modelInstance.validators[field]; const validators = this.modelInstance.validators[field];
const errMsg = _.get(validators, 'notNull.msg', `${field} cannot be null`); const errMsg = _.get(validators, 'notNull.msg', `${this.modelInstance.constructor.name}.${field} cannot be null`);
error = new sequelizeError.ValidationErrorItem(errMsg, 'notNull Violation', field, value); error = new sequelizeError.ValidationErrorItem(errMsg, 'notNull Violation', field, value);
this.errors.push(error); this.errors.push(error);
} }
......
...@@ -299,7 +299,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => { ...@@ -299,7 +299,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
}); });
}); });
}); });
describe('pass all paths when validating', () => { describe('pass all paths when validating', () => {
beforeEach(function() { beforeEach(function() {
const self = this; const self = this;
...@@ -345,7 +345,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => { ...@@ -345,7 +345,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
}); });
}); });
}); });
describe('not null schema validation', () => { describe('not null schema validation', () => {
beforeEach(function() { beforeEach(function() {
const Project = this.sequelize.define('Project', { const Project = this.sequelize.define('Project', {
...@@ -375,9 +375,9 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => { ...@@ -375,9 +375,9 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
it('correctly throws an error using create method with default generated messages', function() { it('correctly throws an error using create method with default generated messages', function() {
return this.Project.create({}).catch(err => { return this.Project.create({}).catch(err => {
expect(err).to.have.property('name', 'SequelizeValidationError'); expect(err).to.have.property('name', 'SequelizeValidationError');
expect(err.message).equal('notNull Violation: name cannot be null'); expect(err.message).equal('notNull Violation: Project.name cannot be null');
expect(err.errors).to.be.an('array').and.have.length(1); expect(err.errors).to.be.an('array').and.have.length(1);
expect(err.errors[0]).to.have.property('message', 'name cannot be null'); expect(err.errors[0]).to.have.property('message', 'Project.name cannot be null');
}); });
}); });
}); });
......
...@@ -65,6 +65,20 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => { ...@@ -65,6 +65,20 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
return expect(result).to.be.rejectedWith(SequelizeValidationError); return expect(result).to.be.rejectedWith(SequelizeValidationError);
}); });
it('has a useful default error message for not null validation failures', () => {
const User = Support.sequelize.define('user', {
name: {
type: Support.Sequelize.STRING,
allowNull: false
}
});
const instanceValidator = new InstanceValidator(User.build());
const result = instanceValidator.validate();
return expect(result).to.be.rejectedWith(SequelizeValidationError, /user\.name cannot be null/);
});
}); });
describe('_validateAndRunHooks', () => { describe('_validateAndRunHooks', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!