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

Nested Map validation (Map of Map of String) allows any nested value #10644

Closed
headlessme opened this issue Aug 30, 2021 · 2 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@headlessme
Copy link

Do you want to request a feature or report a bug?
Bug (possibly Feature)

What is the current behavior?
When inserting values into a Map of Map of String, any values is accepted at the innermost level. I'm not sure if this should already be supported or if this actually a feature request.

If the current behavior is a bug, please provide the steps to reproduce.

const MySchema = new mongoose.Schema({
  mapOfMapOfString: {
    type: Map,
    of: {
      type: Map,
      of: String
    }
  }
})

const MyModel = mongoose.model('MyModel', MySchema)

const doc = new MyModel()
doc.mapOfMapOfString = { outer: { inner: 123 } }
doc.save()

What is the expected behavior?
Only string values are accepted as values of the nested Map.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 14.14.0
Mongoose 6.0.2
MongoDB 4.0.20

@IslandRhythms
Copy link
Collaborator

the inner:123 is where an error should be thrown since it is declared to be only strings.

const mongoose = require('mongoose');

const MySchema = new mongoose.Schema({
    mapOfMapOfString: {
      type: Map,
      of: {
        type: Map,
        of: String
      }
    }
  })
  
  const MyModel = mongoose.model('MyModel', MySchema)
  async function test() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
      await mongoose.connection.dropDatabase();
    const doc = new MyModel()
    console.log(doc);
    doc.mapOfMapOfString = { outer: { inner: 123 } }
    await doc.save()
    let test = await MyModel.find();
    console.log(test);
    console.log('done');
  }

  test();

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 30, 2021
@vkarpov15 vkarpov15 added this to the 6.0.4 milestone Sep 1, 2021
vkarpov15 added a commit that referenced this issue Sep 1, 2021
@vkarpov15
Copy link
Collaborator

The issue here is that Mongoose is interpretting { type: Map, of: { type: Map, of: String } } as { type: Map, of: Map }, so it's losing the nested schema type.

FYI, the correct behavior here is that Mongoose automatically converts number 123 into string '123', which works with d21d2b1

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