Skip to content

Commit

Permalink
fix(documentarray): report validation errors that occur in an array s…
Browse files Browse the repository at this point in the history
…ubdoc created using `create()` and then `set()`

Fix #7504
  • Loading branch information
vkarpov15 committed Feb 18, 2019
1 parent 3fec456 commit 764735b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/schema/documentarray.js
Expand Up @@ -369,7 +369,11 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
value[i].schema !== Constructor.schema) {
value[i] = value[i].toObject({ transform: false, virtuals: false });
}
if (!(value[i] instanceof Subdocument) && value[i]) {

if (value[i] instanceof Subdocument) {
// Might not have the correct index yet, so ensure it does.
value[i].$setIndex(i);
} else if (value[i] != null) {
if (init) {
if (doc) {
selected || (selected = scopePaths(this, doc.$__.selected, init));
Expand Down
2 changes: 1 addition & 1 deletion lib/types/documentarray.js
Expand Up @@ -310,7 +310,7 @@ MongooseDocumentArray.mixin = {
}
}

return new Constructor(obj);
return new Constructor(obj, this);
},

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/types/embedded.js
Expand Up @@ -277,19 +277,17 @@ if (util.inspect.custom) {
EmbeddedDocument.prototype.invalidate = function(path, err, val) {
Document.prototype.invalidate.call(this, path, err, val);

if (!this[documentArrayParent]) {
if (!this[documentArrayParent] || this.__index == null) {
if (err[validatorErrorSymbol] || err.name === 'ValidationError') {
return true;
}
throw err;
}

const index = this.__index;
if (typeof index !== 'undefined') {
const parentPath = this.__parentArray._path;
const fullPath = [parentPath, index, path].join('.');
this[documentArrayParent].invalidate(fullPath, err, val);
}
const parentPath = this.__parentArray._path;
const fullPath = [parentPath, index, path].join('.');
this[documentArrayParent].invalidate(fullPath, err, val);

return true;
};
Expand Down

0 comments on commit 764735b

Please sign in to comment.