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

Leaf nodes validation not working correctly after upgrade mongoose >= 6.3.1 #12021

Closed
2 tasks done
skrtheboss opened this issue Jun 30, 2022 · 1 comment
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@skrtheboss
Copy link
Contributor

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.

@skrtheboss
Copy link
Contributor Author

I think this is the commit that caused the regression.

@vkarpov15 vkarpov15 added this to the 6.4.3 milestone Jul 5, 2022
vkarpov15 added a commit that referenced this issue Jul 5, 2022
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants