Skip to content

Commit

Permalink
Merge pull request #8651 from dskrvk/master
Browse files Browse the repository at this point in the history
Correctly set message for ValidationError; npm audit fixes
  • Loading branch information
vkarpov15 committed Mar 7, 2020
2 parents 7b0bd36 + c213424 commit 5b19b31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const specialProperties = utils.specialProperties;
* @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
* @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
* @event `init`: Emitted on a document after it has was retreived from the db and fully hydrated by Mongoose.
* @event `init`: Emitted on a document after it has been retrieved from the db and fully hydrated by Mongoose.
* @event `save`: Emitted when the document is successfully saved
* @api private
*/
Expand Down Expand Up @@ -1162,10 +1162,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
return false;
}
const modelName = val.get(refPath);
if (modelName === model.modelName || modelName === model.baseModelName) {
return true;
}
return false;
return modelName === model.modelName || modelName === model.baseModelName;
})();

let didPopulate = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/error/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ function ValidationError(instance) {
this.errors = {};
this._message = '';

MongooseError.call(this, this._message);
if (instance && instance.constructor.name === 'model') {
this._message = instance.constructor.modelName + ' validation failed';
} else {
this._message = 'Validation failed';
}
MongooseError.call(this, this._message);
this.name = 'ValidationError';

if (Error.captureStackTrace) {
Expand Down
18 changes: 13 additions & 5 deletions test/errors.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('ValidationError', function() {
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'min Date validation failed.');
assert.ok(err.message.startsWith('MinSchema validation failed'));
model.appointmentDate = new Date(Date.now().valueOf() + 10000);

// should pass validation
Expand All @@ -85,6 +86,7 @@ describe('ValidationError', function() {
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'max Date validation failed');
assert.ok(err.message.startsWith('MaxSchema validation failed'));
model.birthdate = Date.now();

// should pass validation
Expand All @@ -111,6 +113,7 @@ describe('ValidationError', function() {
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String minlegth validation failed.');
assert.ok(err.message.startsWith('MinLengthAddress validation failed'));
model.postalCode = '95125';

// should pass validation
Expand Down Expand Up @@ -142,6 +145,7 @@ describe('ValidationError', function() {
// should fail validation
model.validate(function(err) {
assert.equal(err.errors['postalCode'].message, 'woops!');
assert.ok(err.message.startsWith('gh4207 validation failed'));
mongoose.Error.messages = old;
done();
});
Expand All @@ -163,6 +167,7 @@ describe('ValidationError', function() {
// should fail validation
model.validate(function(err) {
assert.notEqual(err, null, 'String maxlegth validation failed.');
assert.ok(err.message.startsWith('MaxLengthAddress validation failed'));
model.postalCode = '95125';

// should pass validation
Expand Down Expand Up @@ -198,17 +203,16 @@ describe('ValidationError', function() {
});

describe('formatMessage', function() {
it('replaces properties in a message', function(done) {
it('replaces properties in a message', function() {
const props = {base: 'eggs', topping: 'bacon'};
const message = 'I had {BASE} and {TOPPING} for breakfast';

const result = ValidatorError.prototype.formatMessage(message, props);
assert.equal(result, 'I had eggs and bacon for breakfast');
done();
});
});

it('JSON.stringify() with message (gh-5309)', function(done) {
it('JSON.stringify() with message (gh-5309)', function() {
model.modelName = 'TestClass';
const err = new ValidationError(new model());

Expand All @@ -220,8 +224,12 @@ describe('ValidationError', function() {
assert.ok(obj.message.indexOf('test: Fail') !== -1,
obj.message);

done();

function model() {}
});

it('default error message', function() {
const err = new ValidationError();

assert.equal(err.message, 'Validation failed');
});
});

0 comments on commit 5b19b31

Please sign in to comment.