Skip to content

Commit

Permalink
fix: skip findOneAndReplace() validation if runValidators = false
Browse files Browse the repository at this point in the history
Fix #11559
  • Loading branch information
vkarpov15 committed Apr 10, 2022
1 parent e8b05e4 commit 6611203
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
34 changes: 29 additions & 5 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const MongooseError = require('./error/mongooseError');
const ObjectParameterError = require('./error/objectParameter');
const QueryCursor = require('./cursor/QueryCursor');
const ReadPreference = require('./driver').get().ReadPreference;
const ValidationError = require('./error/validation');
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const cast = require('./cast');
Expand Down Expand Up @@ -3713,7 +3714,6 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options, callb
*/
Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
this._castConditions();

if (this.error() != null) {
callback(this.error());
return null;
Expand All @@ -3724,9 +3724,6 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
convertNewToReturnDocument(options);
let fields = null;

let castedDoc = new this.model(this._update, null, true);
this._update = castedDoc;

this._applyPaths();
if (this._fields != null) {
options.projection = this._castFields(utils.clone(this._fields));
Expand All @@ -3737,7 +3734,34 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
}
}

castedDoc.$validate(err => {
const runValidators = _getOption(this, 'runValidators', false);
if (runValidators === false) {
try {
this._castUpdate(this._update, true);
} catch (err) {
const validationError = new ValidationError();
validationError.errors[err.path] = err;
callback(validationError);
return null;
}

this._collection.collection.findOneAndReplace(filter, this._update, options, _wrapThunkCallback(this, (err, res) => {
if (err) {
return callback(err);
}

const doc = res.value;

return this._completeOne(doc, res, callback);
}));

return;
}


let castedDoc = new this.model(this._update, null, true);
this._update = castedDoc;
castedDoc.validate(err => {
if (err != null) {
return callback(err);
}
Expand Down
23 changes: 22 additions & 1 deletion test/model.findOneAndReplace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ describe('model: findOneAndReplace:', function() {
const schema = new Schema({ name: String, age: Number });
const Model = db.model('Test', schema);


await Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, { upsert: true });

const doc = await Model.findOne();
Expand Down Expand Up @@ -412,4 +411,26 @@ describe('model: findOneAndReplace:', function() {

assert.ifError(err);
});

it('skips validation if `runValidators` === false (gh-11559)', async function() {
const testSchema = new Schema({
name: {
type: String,
required: true // you had a typo here
}
});
const Test = db.model('Test', testSchema);
const entry = await Test.create({
name: 'Test'
});

await Test.findOneAndReplace(
{ name: 'Test' },
{}, // this part is key, I am trying to replace without required fields
{ runValidators: false }
);

const doc = await Test.findById(entry);
assert.strictEqual(doc.name, undefined);
});
});

0 comments on commit 6611203

Please sign in to comment.