-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
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(); |
@IslandRhythms the example you are sharing is testing for simple array, not array of array. Try with await |
But your example helped me go in the right direction though and I found a way |
The best workaround for this right now is to make 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 |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce.
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
The text was updated successfully, but these errors were encountered: