Skip to content

Commit

Permalink
Merge pull request #14685 from Automattic/vkarpov15/gh-14677
Browse files Browse the repository at this point in the history
fix(document): avoid passing validateModifiedOnly to subdocs so subdocs get fully validating if they're directly modified
  • Loading branch information
vkarpov15 authored Jun 25, 2024
2 parents 3ad5b4f + 8b05872 commit c062b1f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,6 @@ Document.prototype.$__validate = function(pathsToValidate, options, callback) {
const doValidateOptions = {
...doValidateOptionsByPath[path],
path: path,
validateModifiedOnly: shouldValidateModifiedOnly,
validateAllPaths
};

Expand Down
82 changes: 82 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,88 @@ describe('document', function() {
// Does not throw
await Model.create({ name: 'test' });
});

it('fully validates modified subdocs (gh-14677)', async function() {
const embedSchema = new mongoose.Schema({
field1: {
type: String,
required: true
},
field2: String
});
const testSchema = new mongoose.Schema({
testField: {
type: String,
required: true
},
testArray: [embedSchema]
});
const TestModel = db.model('Test', testSchema);

let doc = new TestModel({ testArray: [{ field2: 'test' }] });
let err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['testArray.0.field1']);
assert.equal(err.errors['testArray.0.field1'].kind, 'required');

await TestModel.collection.insertOne(doc.toObject());
doc = await TestModel.findById(doc._id).orFail();
doc.testArray[0].field2 = 'test modified';
err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ifError(err);

err = await doc.validate().then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['testArray.0.field1']);
assert.equal(err.errors['testArray.0.field1'].kind, 'required');

doc.testArray[0] = { field2: 'test modified 3' };
err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['testArray.0.field1']);
assert.equal(err.errors['testArray.0.field1'].kind, 'required');
});

it('fully validates modified single nested subdocs (gh-14677)', async function() {
const embedSchema = new mongoose.Schema({
field1: {
type: String,
required: true
},
field2: String
});
const testSchema = new mongoose.Schema({
testField: {
type: String,
required: true
},
subdoc: embedSchema
});
const TestModel = db.model('Test', testSchema);

let doc = new TestModel({ subdoc: { field2: 'test' } });
let err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['subdoc.field1']);
assert.equal(err.errors['subdoc.field1'].kind, 'required');

await TestModel.collection.insertOne(doc.toObject());
doc = await TestModel.findById(doc._id).orFail();
doc.subdoc.field2 = 'test modified';
err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ifError(err);

err = await doc.validate().then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['subdoc.field1']);
assert.equal(err.errors['subdoc.field1'].kind, 'required');

doc.subdoc = { field2: 'test modified 3' };
err = await doc.validate({ validateModifiedOnly: true }).then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['subdoc.field1']);
assert.equal(err.errors['subdoc.field1'].kind, 'required');
});
});

describe('bug fixes', function() {
Expand Down

0 comments on commit c062b1f

Please sign in to comment.