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

Schema Map not properly validating/casting value type #7859

Closed
franktzheng opened this issue May 31, 2019 · 2 comments
Closed

Schema Map not properly validating/casting value type #7859

franktzheng opened this issue May 31, 2019 · 2 comments
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@franktzheng
Copy link

franktzheng commented May 31, 2019

What is the current behavior?

If I create a schema containing a Map of another schema, the type casting and validation does not seem to be working correctly.

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

const userSchema = new mongoose.Schema({
  modules: {
    type: Map,
    of: {
      isRequired: {
        type: String,
        required: true
      },
      hasDefault: {
        type: String,
        default: "test"
      }
    }
  }
})

const User = model('User', userSchema)

// Successfully creates user
User.create({ modules: { example: { /* missing isRequired and hasDefault property */ } } })

What is the expected behavior?

Throw an error as a result of missing isRequired and fill in hasDefault to be "test".

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

Node: 12.3.1
Mongoose: 5.5.12
MongoDB: Atlas

@franktzheng franktzheng reopened this May 31, 2019
@franktzheng franktzheng changed the title Map not properly validating/casting value type Schema Map not properly validating/casting value type May 31, 2019
@franktzheng
Copy link
Author

The solution is to nest schemas:

const userSchema = new mongoose.Schema({
  modules: {
    type: Map,
    of: {
      new mongoose.Schema({
        isRequired: {
          type: String,
          required: true
        },
        hasDefault: {
          type: String,
          default: "test"
        }
      })
    }
  }
})

@vkarpov15 vkarpov15 reopened this Jun 3, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.6, 5.5.15 Jun 3, 2019
@vkarpov15 vkarpov15 added the developer-experience This issue improves error messages, debugging, or reporting label Jun 3, 2019
@Fonger
Copy link
Contributor

Fonger commented Jun 6, 2019

@vkarpov15 In Mongoose Map schema, of should be one of the valid value of type.

Like in normal schema, type: { xxx: x } is actually interpreted as Mixed type so it makes sense that of: { xxx: x } is interpreted as Mixed type too.

If we really want to support this, I think #7181 will cover this issue and need to be discussed.

vkarpov15 added a commit that referenced this issue Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

No branches or pull requests

3 participants