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

Validation for array of array #11252

Closed
gilles-yvetot opened this issue Jan 20, 2022 · 4 comments
Closed

Validation for array of array #11252

gilles-yvetot opened this issue Jan 20, 2022 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@gilles-yvetot
Copy link

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

What is the current behavior?

TypeError: Invalid schema configuration: Could not determine the embedded type for array `paths`

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

const mongoose = require(`mongoose`)
const VariableSchema = require(`./VariableSchema.js`)

const ElementPathSchema = new mongoose.Schema({
  paths: {
    // each path is an array of variables, and we can store multiple paths
    type: [
      {
        type: [VariableSchema],
        validate: {
          validator: function validateTemplate(variables) {
            return VariableSchema.statics.validateTemplate(
              variables,
              `elementQuery`,
            )
          },
          message: `elementPath.paths not valid`,
        },
      },
    ],
  },
})

What is the expected behavior?

Being able to have an array of array with validation

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

Node.js 16.13.0
mongoose 6.0.12
mongo 5.0.5

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jan 21, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    paths: { type: [], validate: { validator: function (v) {
        return Array.isArray(v);
    } } }
})


const Test = mongoose.model('Test', testSchema);

async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();

await Test.create({
    paths: [[1,2,3,4,5], [6,7,8,9,10]]
});

console.log(await Test.findOne());
}

run();

@gilles-yvetot
Copy link
Author

@IslandRhythms the example you are sharing is testing for simple array, not array of array. Try with await Test.create({paths: [1,2,3,4,5]}) and it will still be valid even though it should not

@gilles-yvetot
Copy link
Author

But your example helped me go in the right direction though and I found a way

@vkarpov15 vkarpov15 added this to the 6.1.12 milestone Jan 30, 2022
@vkarpov15
Copy link
Collaborator

The best workaround for this right now is to make paths an array of arrays as shown below:

const ElementPathSchema = new mongoose.Schema({
  paths: {
    // each path is an array of variables, and we can store multiple paths
    type: [[VariableSchema]],
    validate: {
      validator: function validateTemplate(variables) {
        return VariableSchema.statics.validateTemplate(
          variables,
          `elementQuery`,
        )
      },
      message: `elementPath.paths not valid`,
    }
  }
})

We pushed a fix to allow declaring arrays of arrays using [{ type: [VariableSchema] }]

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Feb 21, 2022
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