-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.4.1
Node.js version
16.15.1
MongoDB server version
5.0.0
Description
After upgrading mongoose ta a version grater than 6.3.0, the validation stopped working in some conditions.
If a schema is created with leaf nodes, then the validation is only performed if the property is required.
Steps to Reproduce
const mongoose = require('mongoose');
const SubSubSchema = new mongoose.Schema(
{
from: {
type: mongoose.Schema.Types.String,
required: true,
},
},
{ _id: false }
);
const SubSchema = new mongoose.Schema(
{
nested: {
type: SubSubSchema,
required: false,
},
},
{ _id: false }
);
// DOES NOT FAIL - WRONG BEHAVIOR
async function runLeafTest() {
const TestLeafSchema = new mongoose.Schema({
testProp: {
testSubProp: {
type: SubSchema,
required: true,
},
},
});
const TestLeafModel = mongoose.model('test-leaf-model', TestLeafSchema);
const testModelInstance = new TestLeafModel({ testProp: { testSubProp: { nested: { from: null } } } });
await testModelInstance.validate();
}
// DOES FAIL - CORRECT BEHAVIOR
async function runTest() {
const TestLeafSchema = new mongoose.Schema({
testSubProp: {
type: SubSchema,
required: true,
},
});
const TestModel = mongoose.model('test-model', TestLeafSchema);
const testModelInstance = new TestModel({ testSubProp: { nested: { from: null } } });
await testModelInstance.validate();
}
await Promise.allSettled([runLeafTest(), runTest()]).then(([leafResult, nonLeafResult]) => {
console.log({
leafResult,
nonLeafResult,
});
});Runkit 6.3.0 https://runkit.com/embed/r0jhjsjztl99
Runkit 6.3.1 https://runkit.com/embed/ylkawc9mlm1s
Runkit latest https://runkit.com/embed/s553idqmvs9x
Expected Behavior
Both leaf and non leaf nodes should be validated the same way.
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.