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

Cannot override a non-required field from a base schema as of 5.12.7 #10203

Closed
sean-daley opened this issue May 4, 2021 · 1 comment
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@sean-daley
Copy link

sean-daley commented May 4, 2021

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

If I have a base schema that includes a field with required set to false and I add an additional schema to it which overrides that field to required set to true, this override stops working as of 5.12.7

If the current behavior is a bug, please provide the steps to reproduce.
Here is a script which reproduces the issue

const mongoose = require("mongoose");

const { Schema } = mongoose;

const baseSchema = new Schema({
    username: {
        type: Schema.Types.String,
        required: false
    },
});

const userSchema = new Schema({
    email: {
      type: Schema.Types.String,
      required: true
    },
    username: {
        type: Schema.Types.String,
        required: true
    },
});

const realSchema = baseSchema.clone()
realSchema.add(userSchema)


const User = mongoose.model("User", realSchema);

const main = async () => {
   await mongoose.connect('mongodb://localhost:27017/test', {
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: true,
    useNewUrlParser: true,
    connectTimeoutMS: 3000
});
    const user = new User({
        email: "user@example.com",
    });

    const dbUser = await user.save();
    console.log(dbUser.toObject());
}

main();

What is the expected behavior?
Because we are NOT setting a username here, this should throw an error.
In versions before 5.12.7 this will error out with:

$ node test.js
(node:32903) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: username: Path `username` is required.
    at model.Document.invalidate (/Users/sean/node/mongoose-test/node_modules/mongoose/lib/document.js:2704:32)
    at /Users/sean/node/mongoose-test/node_modules/mongoose/lib/document.js:2524:17
    at /Users/sean/node/mongoose-test/node_modules/mongoose/lib/schematype.js:1256:9
    at process._tickCallback (internal/process/next_tick.js:61:11)

Starting with 5.12.7 though this now succeeds:

$ node test.js
{ _id: 6091342ecfb5858155bdc13e,
  email: 'user@example.com',
  __v: 0 }

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose: 5.12.7
Node: v10.23.0
MongoDB: 4.2.13

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 4, 2021
@vkarpov15 vkarpov15 added this to the 5.12.9 milestone May 10, 2021
vkarpov15 added a commit that referenced this issue May 11, 2021
@vkarpov15
Copy link
Collaborator

Thanks for reporting, fix will be in 5.12.9 👍

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. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

3 participants