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

regression: 5.4.11 rejects subdocument required:false if type is not explicitly Object #7516

Closed
autopulated opened this issue Feb 12, 2019 · 1 comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@autopulated
Copy link

autopulated commented Feb 12, 2019

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

What is the current behavior?
in 5.4.10 and below, this is accepted:

var testschema = new mongoose.Schema({
    subdoc:{
        required:false
    }
});

If the current behavior is a bug, please provide the steps to reproduce.
In 5.4.11, the above schema produces the error:
(stack:TypeError: Invalid value for schema path `subdoc.required` ...

Unless the subdocument type is explicitly set to Object:

var testschema = new mongoose.Schema({
    subdoc:{
       type: Object,
        required:false
    }
});

What is the expected behavior?
5.4.11 should continue to accept schemas which were accepted under 5.4.10

Please mention your node.js, mongoose and MongoDB version.
tested with node v10.15.0 amongst other v10 versions.

@vkarpov15 vkarpov15 added this to the 5.4.12 milestone Feb 13, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Feb 13, 2019
@vkarpov15
Copy link
Collaborator

The fact that this worked in Mongoose 5.4.10 is an accident more than anything, the testschema you defined actually creates subdoc.required as a path of type mixed as opposed to a property subdoc of type object that isn't required:

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh7512`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);
  await mongoose.connection.dropDatabase();

  
  var testschema = new mongoose.Schema({
    subdoc:{
        required:false
    }
  });

  console.log(testschema.path('subdoc.required')); // This is a path in the schema!

  var testschema2 = new mongoose.Schema({
    subdoc:{
        type: Object,
        required:false
    }
  });

  console.log(testschema2.path('subdoc.required')); // undefined
}

So your schema needs a little work.

But since this check has caused so much confusion, it was probably too big a change for a patch release. Just make sure you structure your schemas correctly going forward, because the way you wrote testschema will likely break in 6.0 (see #7181, etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

2 participants