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

Add "required" flag to discriminator model's nested property #10157

Closed
fardolieri opened this issue Apr 19, 2021 · 3 comments
Closed

Add "required" flag to discriminator model's nested property #10157

fardolieri opened this issue Apr 19, 2021 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fardolieri
Copy link

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

Potential bug

My Problem

I want my discriminated model to have a required flag on name.firstName, but my base model should not.

What is the current behavior?

Given the following code snippet:

const nameSchema = new Schema({
  firstName: { type: 'String' },
  lastName: { type: 'String' },
});

const personSchema = new Schema(
  {
    name: { type: nameSchema, default: {} },
    kind: { type: 'String', enum: ['normal', 'vip'], required: true },
  },
  { discriminatorKey: 'kind' },
);

const Person = mongoose.model('Person', personSchema);

const vipSchema = new Schema({
  name: {
    type: new Schema({
      firstName: { type: 'String', required: true },
      title: { type: 'String', required: true },
    }),
  },
});

const Vip = Person.discriminator('vip', vipSchema);


new Vip({ name: { firstName: 'John', title: 'Dr' } }).validateSync(); // does not throw, as expected
new Vip({ name: { firstName: 'John' } }).validateSync(); // does throw, as expected ('name.title' is required and missing)
new Vip({ name: { title: 'Dr' } }).validateSync(); // !? does NOT throw, but I would expect it to ('name.firstName' should be required and is missing)

What are the versions of Node.js, Mongoose and MongoDB you are using?
Node 15.14.0, mongoose 5.12.3, MongoDB 4.4.4 Community


It looks like the title's required flag in my vipSchema is working just fine, but on firstName it is somehow ignored. Im wondering if this is the expected behaviour of mongoose and if so, how else am I supposed to add the required flag to my discriminator model?

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const {Schema} = mongoose;

const nameSchema = new Schema({
    firstName: { type: 'String' },
    lastName: { type: 'String' },
  });
  
  const personSchema = new Schema(
    {
      name: { type: nameSchema, default: {} },
      kind: { type: 'String', enum: ['normal', 'vip'], required: true },
    },
    { discriminatorKey: 'kind' },
  );
  
  const Person = mongoose.model('Person', personSchema);
  
  const vipSchema = new Schema({
    name: {
      type: new Schema({
        firstName: { type: 'String', required: true },
        title: { type: 'String', required: true },
      }),
    },
  });
  
  async function test() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true
      });
    
      await mongoose.connection.dropDatabase();

      const entry = await Person.create({name: {firstName: 'John', lastName: 'Does'}, kind: 'vip'})
      await entry.save();
      const Vip = Person.discriminator('vip', vipSchema);
  
  
      console.log('1', new Vip({ name: { firstName: 'John', title: 'Dr' } }).validateSync()); // does not throw, as expected
      console.log('2', new Vip({ name: { firstName: 'John' } }).validateSync()); // does throw, as expected ('name.title' is required and missing)
      console.log('3', new Vip({ name: { title: 'Dr' } }).validateSync()); // !? does NOT throw, but I would expect it to ('name.firstName' should be required and is missing)
  }
test();

@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 Apr 19, 2021
@vkarpov15 vkarpov15 added this to the 5.12.7 milestone Apr 26, 2021
@vkarpov15
Copy link
Collaborator

I've been digging into this and confirmed this is a bug. It looks like it is due to the discriminator() function overwriting vipSchema's name.firstName with personSchema's name.firstName for some reason. Will look into this more tomorrow.

vkarpov15 added a commit that referenced this issue Apr 28, 2021
@vkarpov15 vkarpov15 removed the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Apr 29, 2021
@fardolieri
Copy link
Author

Wow I didn't expect a fix so soon. Thank you for your hard work! 🥇

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

3 participants