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

Mongoose 8.x: Required array schema validation breaks in static validation #15164

Closed
2 tasks done
skrtheboss opened this issue Jan 7, 2025 · 1 comment · Fixed by #15169
Closed
2 tasks done

Mongoose 8.x: Required array schema validation breaks in static validation #15164

skrtheboss opened this issue Jan 7, 2025 · 1 comment · Fixed by #15169
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

8.9.3

Node.js version

20.17.0

MongoDB server version

7.0.14

Typescript version (if applicable)

5.5.4

Description

When using a schema with a required array field, static validation using Model.validate() fails when the array is empty, even though instance validation using new Model().validate() passes.

Steps to Reproduce

Run the following code with any 8.x.x version and it will fail the static validation:

const mongoose = require('mongoose');

async function runValidationTest() {
    const barSchema = new mongoose.Schema({
        foo: {
            type: mongoose.Schema.Types.String,
            required: true,
        },
    });

    const fooSchema = new mongoose.Schema({
        bars: {
            type: [barSchema],
            required: true,
        },
    });

    const Model = mongoose.model('FooTest', fooSchema);

    const fooInstance = {
        bars: [],
    };

    console.log('--- Instance Validation (new Model().validate()) ---');
    try {
        await new Model(fooInstance).validate();
        console.log('Instance validation: PASSED ✓');
    } catch (error) {
        console.error('Instance validation: FAILED ✗', error);
    }

    console.log('\n--- Static Validation (Model.validate()) ---');
    try {
        await Model.validate(fooInstance);
        console.log('Static validation: PASSED ✓');
    } catch (error) {
        console.error('Static validation: FAILED ✗', error);
    }
}

// Run the test
runValidationTest().catch(console.error);

Expected Behavior

Both instance validation (new Model().validate()) and static validation (Model.validate()) should pass when the bars array is empty because the array exists and satisfies the required constraint.

@skrtheboss
Copy link
Contributor Author

Maybe has something in common with this issue: #15075

@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 8, 2025
@vkarpov15 vkarpov15 added this to the 8.9.4 milestone Jan 9, 2025
vkarpov15 added a commit that referenced this issue Jan 9, 2025
fix(model): make Model.validate() static correctly cast document arrays
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

Successfully merging a pull request may close this issue.

2 participants