diff --git a/lib/index.js b/lib/index.js index 430096d6ec3..d6e1b28c490 100644 --- a/lib/index.js +++ b/lib/index.js @@ -86,6 +86,7 @@ Mongoose.prototype.STATES = STATES; * - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model. * - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema. * - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter. + * - 'runValidators': false by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default. * * @param {String} key * @param {String|Function|Boolean} value diff --git a/lib/query.js b/lib/query.js index c7f880d9d96..a45d7cda506 100644 --- a/lib/query.js +++ b/lib/query.js @@ -2417,6 +2417,22 @@ Query.prototype._findOneAndRemove = function(callback) { this._findAndModify('remove', callback); }; +/*! + * Get options from query opts, falling back to the base mongoose object. + */ + +function _getOption(query, option, def) { + const opts = query._optionsForExec(query.model); + + if (option in opts) { + return opts[option]; + } + if ('option' in query.model.base.options) { + return query.model.base.options[option]; + } + return def; +} + /*! * Override mquery.prototype._findAndModify to provide casting etc. * @@ -2525,6 +2541,7 @@ Query.prototype._findAndModify = function(type, callback) { var _callback; var useFindAndModify = true; + var runValidators = _getOption(this, 'runValidators', false); var base = _this.model && _this.model.base; if ('useFindAndModify' in base.options) { useFindAndModify = base.get('useFindAndModify'); @@ -2552,7 +2569,7 @@ Query.prototype._findAndModify = function(type, callback) { return this; } - if (opts.runValidators && doValidate) { + if (runValidators && doValidate) { _callback = function(error) { if (error) { return callback(error); @@ -2582,7 +2599,7 @@ Query.prototype._findAndModify = function(type, callback) { return this; } - if (opts.runValidators && doValidate) { + if (runValidators && doValidate) { _callback = function(error) { if (error) { return callback(error); @@ -2707,7 +2724,8 @@ function _updateThunk(op, callback) { castedDoc, options); } - if (this.options.runValidators) { + var runValidators = _getOption(this, 'runValidators', false); + if (runValidators) { if (isOverwriting) { doValidate = function(callback) { castedDoc.validate(callback);