Skip to content

Commit

Permalink
fix(update): copy exact errors from array subdoc validation into top-…
Browse files Browse the repository at this point in the history
…level update validator error

Fix tests re: #7135
  • Loading branch information
vkarpov15 committed Oct 25, 2018
1 parent b321e81 commit a611926
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 15 additions & 2 deletions lib/helpers/updateValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ module.exports = function(query, schema, castedDoc, options) {
const validatorsToExecute = [];
const validationErrors = [];

const alreadyValidated = [];

const context = options && options.context === 'query' ? query : null;
function iter(i, v) {
const schemaPath = schema._getSchema(updates[i]);
Expand Down Expand Up @@ -123,6 +125,7 @@ module.exports = function(query, schema, castedDoc, options) {
}

if (schemaPath.$isMongooseDocumentArrayElement && v != null && v.$__ != null) {
alreadyValidated.push(updates[i]);
validatorsToExecute.push(function(callback) {
schemaPath.doValidate(v, function(err) {
if (err) {
Expand All @@ -133,15 +136,25 @@ module.exports = function(query, schema, castedDoc, options) {

v.validate(function(err) {
if (err) {
err.path = updates[i];
validationErrors.push(err);
if (err.errors) {
for (const key of Object.keys(err.errors)) {
const _err = err.errors[key];
_err.path = updates[i] + '.' + key;
validationErrors.push(_err);
}
}
}
callback(null);
});
}, context, { updateValidator: true });
});
} else {
validatorsToExecute.push(function(callback) {
for (const path of alreadyValidated) {
if (updates[i].startsWith(path + '.')) {
return callback(null);
}
}
schemaPath.doValidate(v, function(err) {
if (err) {
err.path = updates[i];
Expand Down
3 changes: 2 additions & 1 deletion lib/types/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function EmbeddedDocument(obj, parentArr, skipId, fields, index) {
this.__parent = undefined;
}
this.$setIndex(index);
this.$isDocumentArrayElement = true;

Document.call(this, obj, fields, skipId);

Expand Down Expand Up @@ -275,7 +276,7 @@ EmbeddedDocument.prototype.invalidate = function(path, err, val) {
Document.prototype.invalidate.call(this, path, err, val);

if (!this.__parent) {
if (err[validatorErrorSymbol]) {
if (err[validatorErrorSymbol] || err.name === 'ValidationError') {
return true;
}
throw err;
Expand Down
6 changes: 3 additions & 3 deletions test/model.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,7 @@ describe('model: update:', function() {

Group.update({}, update, opts, function(error) {
assert.ok(error);
assert.ok(error.errors['users.0.permission']);
assert.ok(error.errors['users.0.permission'], Object.keys(error.errors));
done();
});
});
Expand Down Expand Up @@ -3133,13 +3133,13 @@ describe('model: updateOne: ', function() {
then(() => null, err => err);

assert.ok(err);
assert.ok(err.errors['anArray.0']);
assert.ok(err.errors['anArray.0.a']);

err = yield TestModel.
updateOne({}, { $set: { 'anArray.0': {} } }, { runValidators: true }).
then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['anArray.0']);
assert.ok(err.errors['anArray.0.a']);
});
});
});

0 comments on commit a611926

Please sign in to comment.