Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh9885 #9895

Merged
merged 5 commits into from
Feb 9, 2021
Merged

Gh9885 #9895

Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,6 @@ function _getPathsToValidate(doc) {
const skipSchemaValidators = {};

_evaluateRequiredFunctions(doc);

// only validate required fields when necessary
let paths = new Set(Object.keys(doc.$__.activePaths.states.require).filter(function(path) {
if (!doc.$__isSelected(path) && !doc.isModified(path)) {
Expand Down Expand Up @@ -2340,7 +2339,6 @@ function _getPathsToValidate(doc) {
}
}


for (const path of paths) {
// Single nested paths (paths embedded under single nested subdocs) will
// be validated on their own when we call `validate()` on the subdoc itself.
Expand Down Expand Up @@ -2434,11 +2432,9 @@ Document.prototype.$__validate = function(pathsToValidate, options, callback) {
pathDetails[0].filter((path) => this.isModified(path)) :
pathDetails[0];
const skipSchemaValidators = pathDetails[1];

if (Array.isArray(pathsToValidate)) {
paths = _handlePathsToValidate(paths, pathsToValidate);
}

if (paths.length === 0) {
return process.nextTick(function() {
const error = _complete();
Expand Down Expand Up @@ -2606,7 +2602,6 @@ Document.prototype.validateSync = function(pathsToValidate, options) {
if (Array.isArray(pathsToValidate)) {
paths = _handlePathsToValidate(paths, pathsToValidate);
}

const validating = {};

paths.forEach(function(path) {
Expand Down
9 changes: 5 additions & 4 deletions lib/types/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ function registerRemoveListener(sub) {
*/

EmbeddedDocument.prototype.$__remove = function(cb) {
if (cb == null) {
return;
}
return cb(null, this);
};

Expand All @@ -218,7 +221,7 @@ EmbeddedDocument.prototype.remove = function(options, fn) {
options = undefined;
}
if (!this.__parentArray || (options && options.noop)) {
fn && fn(null);
this.$__remove(fn);
return this;
}

Expand All @@ -234,9 +237,7 @@ EmbeddedDocument.prototype.remove = function(options, fn) {
registerRemoveListener(this);
}

if (fn) {
fn(null);
}
this.$__remove(fn);

return this;
};
Expand Down
1 change: 0 additions & 1 deletion lib/types/subdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ Subdocument.prototype.remove = function(options, callback) {
callback = options;
options = null;
}

registerRemoveListener(this);

// If removing entire doc, no need to remove subdoc
Expand Down
38 changes: 38 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9931,4 +9931,42 @@ describe('document', function() {
assert.ok(doc);
});
});
it('Makes sure pre remove hook is executed gh-9885', function() {
const SubSchema = new Schema({
myValue: {
type: String
}
}, {});
let count = 0;
SubSchema.pre('remove', function(next) {
count++;
next();
});
const thisSchema = new Schema({
foo: {
type: String,
required: true
},
mySubdoc: {
type: [SubSchema],
required: true
}
}, { minimize: false, collection: 'test' });

const Model = db.model('TestModel', thisSchema);

return co(function*() {
yield Model.deleteMany({}); // remove all existing documents
const newModel = {
foo: 'bar',
mySubdoc: [{ myValue: 'some value' }]
};
const document = yield Model.create(newModel);
document.mySubdoc[0].remove();
yield document.save().catch((error) => {
console.error(error);
});
assert.equal(count, 1);
});
});
});