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

Single nested sub-document field is going wrong in a schema syntax #9205

Closed
lutherman opened this issue Jul 5, 2020 · 2 comments
Closed
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@lutherman
Copy link

Hi,

I find that the result is wrong if define a single nested sub-document field by syntax as shown below.

  1. Unknown fields are defined in the sub-document.
  2. Wrong type value can be input to the field of sub-document.

Below is the test code to reproduce the problem:

const chai = require("chai");
const expect = chai.expect;

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const testModel = mongoose.model('test', new Schema({
  singleNested: {
    type: {
      field1: Boolean,
      field2: Boolean
    },
    default: {
      field1: false,
      field2: false
    }
  },
}));

describe('test', () => {
  it('Should not define fields that not exist', () => {
    expect(testModel.schema.path('singleNested.fgfggsdfgdf')).to.undefined;
    expect(testModel.schema.path('singleNested.sfdfsfds')).to.undefined;
    expect(testModel.schema.path('singleNested.dsfsfdsf')).to.undefined;
  });
  it('Should not allow set the wrong type value', () => {
    const testDoc = new testModel();

    testDoc.singleNested.field1 = 'String value to boolean field';
    testDoc.validateSync('singleNested');

    expect(testDoc.errors['singleNested.field1']).to.be.an('Error');
  });
});

And the test result is:

$ mocha index.js


  test
    1) Should not define fields that not exist
    2) Should not allow set the wrong type value


  0 passing (30ms)
  2 failing

       Should not define fields that not exist:
     AssertionError: expected { Object (path, instance, ...) } to be undefined
      at Context.<anonymous> (index.js:24:65)
      at processImmediate (internal/timers.js:456:21)

  2) test
       Should not allow set the wrong type value:
     TypeError: Cannot read property 'singleNested.field1' of undefined
      at Context.<anonymous> (index.js:34:26)
      at processImmediate (internal/timers.js:456:21)

nodejs: 12.18.2
mongoose: 5.9.21
chai: 4.2.0
mocha: 8.0.1

@vkarpov15
Copy link
Collaborator

That isn't how you define a single nested subdocument. You're actually defining singleNested as a mixed path: https://mongoosejs.com/docs/schematypes.html#mixed

We're changing this behavior in 6.0 with #7181 because this is likely at least the 100th time someone has reported this behavior as a bug, and I agree this behavior is exceedingly confusing.

As a workaround, you can do:

const testModel = mongoose.model('test', new Schema({
  singleNested: {
    type: new Schema({
      field1: Boolean,
      field2: Boolean
    }),
    default: {
      field1: false,
      field2: false
    }
  },
}));

Or, set the typePojoToMixed option.

const testModel = mongoose.model('test', new Schema({
  singleNested: {
    type: {
      field1: Boolean,
      field2: Boolean
    },
    default: {
      field1: false,
      field2: false
    }
  },
}, { typePojoToMixed: false }));

Sorry for the inconvenience!

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jul 10, 2020
@lutherman
Copy link
Author

Oh, I see. Thank you for your explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants