-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Document.prototype.validate() does not validate subdocument required fields with validateModifiedOnly option #14677
Closed
2 tasks done
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
labels
Jun 20, 2024
'use strict';
const mongoose = require('mongoose');
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 = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const m = new TestModel({testArray: [{field2: 'test'}]});
await m.validate(null, {validateModifiedOnly: true});
console.log('done');
}
run().catch(err => {
console.log(err);
process.exit(-1)
}); |
vkarpov15
added a commit
that referenced
this issue
Jun 25, 2024
fix(document): avoid passing validateModifiedOnly to subdocs so subdocs get fully validating if they're directly modified
This was referenced Jul 16, 2024
Closed
This was referenced Jul 17, 2024
This was referenced Aug 21, 2024
This was referenced Aug 23, 2024
This was referenced Aug 24, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
8.4.3
Node.js version
20.14.0
MongoDB server version
7.0.11
Typescript version (if applicable)
No response
Description
In versions 8.2.4 and prior, a call to
Document.prototype.validate(null, {validateModifiedOnly: true})
would throw an error if required fields in a subdocument were missing. In 8.3.0 and subsequent versions,validate()
no longer checks for required fields in subdocuments if thevalidateModifiedOnly
option is set to true.If subdocuments are added, the required fields should be validated as was done in 8.2.4 and prior.
Steps to Reproduce
The following code will throw an error in mongoose versions 8.2.4 and prior, and succeed in 8.3.0 and subsequent:
With the
validateModifiedOnly
option,testField
is not required, however it also skips the check forfield1
in the subdocument (which is missing) starting in version 8.3.0.Expected Behavior
If a subdocument is added,
validate()
should check that all required fields are present when thevalidateModifiedOnly
is set to true.The text was updated successfully, but these errors were encountered: