Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(document): avoid passing validateModifiedOnly to subdocs so subdo…
…cs get fully validating if they're directly modified

Fix #14677
  • Loading branch information
vkarpov15 committed Jun 24, 2024
commit 758eca3a7763ec8434f74ec19d121c975b34612b
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