Skip to content

Commit 2a8c532

Browse files
authored
Merge pull request #14604 from Automattic/vkarpov15/gh-14591
fix(document): fire pre validate hooks on 5 level deep single nested subdoc when modifying after save()
2 parents c7315fb + 65c40b5 commit 2a8c532

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/document.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,9 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
17161716
} else {
17171717
obj._doc[parts[i]] = val;
17181718
}
1719+
if (shouldModify) {
1720+
obj.markModified(parts[i]);
1721+
}
17191722
} else {
17201723
obj[parts[i]] = val;
17211724
}
@@ -2712,6 +2715,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
27122715
!doc.isDirectModified(fullPathToSubdoc) &&
27132716
!doc.$isDefault(fullPathToSubdoc)) {
27142717
paths.add(fullPathToSubdoc);
2718+
27152719
if (doc.$__.pathsToScopes == null) {
27162720
doc.$__.pathsToScopes = {};
27172721
}

lib/types/subdocument.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Subdocument.prototype.$__fullPath = function(path) {
113113

114114
/**
115115
* Given a path relative to this document, return the path relative
116-
* to the top-level document.
116+
* to the parent document.
117117
* @param {String} p
118118
* @returns {String}
119119
* @method $__pathRelativeToParent

test/document.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12627,6 +12627,41 @@ describe('document', function() {
1262712627
doc.set('branding.logo.attachment', { name: 'coolLogo' });
1262812628
await doc.save();
1262912629
assert.strictEqual(attachmentSchemaPreValidateCalls, 1);
12630+
12631+
instance.set('branding.logo.attachment', { name: 'coolOtherLogo' });
12632+
await instance.save();
12633+
assert.strictEqual(attachmentSchemaPreValidateCalls, 2);
12634+
});
12635+
12636+
it('fires pre validate hooks on 5 level deep single nested subdoc when modifying after save() (gh-14591)', async function() {
12637+
let preValidate = [];
12638+
12639+
const createSchema = (path, subSchema) => {
12640+
const schema = new Schema({ [path]: subSchema });
12641+
schema.pre('validate', function() {
12642+
preValidate.push(path);
12643+
});
12644+
return schema;
12645+
};
12646+
12647+
const e = createSchema('e', { type: String });
12648+
const d = createSchema('d', { type: e });
12649+
const c = createSchema('c', { type: d });
12650+
const b = createSchema('b', { type: c });
12651+
const a = createSchema('a', { type: b });
12652+
b.add({ otherProp: String });
12653+
const NestedModelHooksTestModel = db.model('Test', a);
12654+
12655+
const newData = { a: { b: { c: { d: { e: new Date().toString() } } } } };
12656+
newData.a.otherProp = 'test';
12657+
const doc = await new NestedModelHooksTestModel(newData).save();
12658+
preValidate = [];
12659+
doc.set({ 'a.b.c.d.e': 'updated nested value' });
12660+
await doc.save();
12661+
assert.deepStrictEqual(preValidate, ['a', 'b', 'c', 'd', 'e']);
12662+
12663+
const fromDb = await NestedModelHooksTestModel.findById(doc._id);
12664+
assert.strictEqual(fromDb.a.otherProp, 'test');
1263012665
});
1263112666

1263212667
it('returns constructor if using $model() with no args (gh-13878)', async function() {

0 commit comments

Comments
 (0)