Skip to content

Commit

Permalink
fix(document): run validate hooks on array subdocs even if not direct…
Browse files Browse the repository at this point in the history
…ly modified

Fix #5861
Future Work: #5885
  • Loading branch information
vkarpov15 committed Dec 9, 2017
1 parent 5025a4e commit eb2c337
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1413,9 +1413,13 @@ function _getPathsToValidate(doc) {
len = subdocs.length;
for (i = 0; i < len; ++i) {
subdoc = subdocs[i];
if (subdoc.$isSingleNested &&
doc.isModified(subdoc.$basePath) &&
if (doc.isModified(subdoc.$basePath) &&
!doc.isDirectModified(subdoc.$basePath)) {
// Remove child paths for now, because we'll be validating the whole
// subdoc
paths = paths.filter(function(p) {
return p != null && p.indexOf(subdoc.$basePath + '.') !== 0;
});
paths.push(subdoc.$basePath);
}
}
Expand Down Expand Up @@ -1499,8 +1503,8 @@ Document.prototype.$__validate = function(callback) {
});
}

var validating = {},
total = 0;
var validated = {};
var total = 0;

var complete = function() {
var error = _complete();
Expand All @@ -1513,11 +1517,11 @@ Document.prototype.$__validate = function(callback) {
};

var validatePath = function(path) {
if (validating[path]) {
if (path == null || validated[path]) {
return;
}

validating[path] = true;
validated[path] = true;
total++;

process.nextTick(function() {
Expand Down
1 change: 1 addition & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var utils = require('../utils');

function DocumentArray(key, schema, options) {
var EmbeddedDocument = _createConstructor(schema, options);
EmbeddedDocument.prototype.$basePath = key;

ArrayType.call(this, key, EmbeddedDocument, options);

Expand Down

0 comments on commit eb2c337

Please sign in to comment.