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

fix(document): avoid passing validateModifiedOnly to subdocs so subdocs get fully validating if they're directly modified #14685

Merged
merged 2 commits into from
Jun 25, 2024
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
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 @@
// 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],

Check failure on line 2573 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 2573 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
});
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
Loading