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

When asigning a value to a Schema property with a nested array type the schema appears to mutate the input incorrectly. #8544

Closed
hally9k opened this issue Jan 28, 2020 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@hally9k
Copy link

hally9k commented Jan 28, 2020

Do you want to request a feature or report a bug?
Report a Bug.

What is the current behavior?
If a variable with the incorrect array nesting is assigned to a schema instance property it mutates the input instead of throwing a validation error.

It feels unhygienic to mutate the schema instance in this way but the resulting behaviour here is unexpected...

If the current behavior is a bug, please provide the steps to reproduce.
See the reproduction here.
https://codesandbox.io/s/sparkling-rain-dbv7v?fontsize=14&hidenavigation=1&theme=dark

The assignment on line 16:

position.coordinates = [1, 2];

Results in the output:

"coordinates": [
    [
      1
    ],
    [
      2
    ]
  ]

What is the expected behavior?
An error should be thrown when the incorrect schema object is assigned?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node@11.7.0
mongoose@5.8.10
mongodb@4.2

@jloveridge
Copy link

@hally9k mongoose has fairly complex typecasting logic. The current behavior of taking a single value and casting it into an array when the schema defines the property to be an array of values was explicitly requested, thus the current behavior that you are seeing. Any change to this behavior would be a backwards breaking change. I definitely understand you concern and frustration with the current behavior. However, changing how typecasting works should not be undertaken lightly. Better to ensure your code sets the appropriate values, casted however is appropriate, than to rely on typecasting behaviors inside of mongoose.

@vkarpov15 vkarpov15 added this to the 5.8.12 milestone Feb 5, 2020
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Feb 5, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.8.12, 5.8.13, 5.9.1, 5.9.2 Feb 12, 2020
@vkarpov15
Copy link
Collaborator

We fixed it so that Mongoose will cast [1, 2] -> [[1, 2]] rather than [[1], [2]] going forward.

If you want that to throw a CastError, you can set the castNonArrays option to false:

const mongoose = require('mongoose');

mongoose.Schema.Types.Array.options.castNonArrays = false;

const positionSchema = mongoose.Schema({
  coordinates: {
    type: [[Number]],
    required: true
  }
});

const Position = mongoose.model("Position", positionSchema);

const position = new Position();

position.coordinates = [1, 2];

// CastError
console.log(position.validateSync());

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Feb 16, 2020
vkarpov15 added a commit that referenced this issue Feb 20, 2020
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