Skip to content

Commit eb2c337

Browse files
committed
fix(document): run validate hooks on array subdocs even if not directly modified
Fix #5861 Future Work: #5885
1 parent 5025a4e commit eb2c337

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/document.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,9 +1413,13 @@ function _getPathsToValidate(doc) {
14131413
len = subdocs.length;
14141414
for (i = 0; i < len; ++i) {
14151415
subdoc = subdocs[i];
1416-
if (subdoc.$isSingleNested &&
1417-
doc.isModified(subdoc.$basePath) &&
1416+
if (doc.isModified(subdoc.$basePath) &&
14181417
!doc.isDirectModified(subdoc.$basePath)) {
1418+
// Remove child paths for now, because we'll be validating the whole
1419+
// subdoc
1420+
paths = paths.filter(function(p) {
1421+
return p != null && p.indexOf(subdoc.$basePath + '.') !== 0;
1422+
});
14191423
paths.push(subdoc.$basePath);
14201424
}
14211425
}
@@ -1499,8 +1503,8 @@ Document.prototype.$__validate = function(callback) {
14991503
});
15001504
}
15011505

1502-
var validating = {},
1503-
total = 0;
1506+
var validated = {};
1507+
var total = 0;
15041508

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

15151519
var validatePath = function(path) {
1516-
if (validating[path]) {
1520+
if (path == null || validated[path]) {
15171521
return;
15181522
}
15191523

1520-
validating[path] = true;
1524+
validated[path] = true;
15211525
total++;
15221526

15231527
process.nextTick(function() {

lib/schema/documentarray.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var utils = require('../utils');
2727

2828
function DocumentArray(key, schema, options) {
2929
var EmbeddedDocument = _createConstructor(schema, options);
30+
EmbeddedDocument.prototype.$basePath = key;
3031

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

0 commit comments

Comments
 (0)