Mongoose: Enum support on array of array

Created on 25 Jun 2019  路  4Comments  路  Source: Automattic/mongoose

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

What is the current behavior?

  people: {
    type: [[String]],
    enum: [
      'manager',
      'student',
      'account',
    ],
  },

Throw an Error:
Error: enum can only be set on an array of strings, not Array

What is the expected behavior?
check each string like array of string

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

enhancement

Most helpful comment

As a workaround, you can do:

  people: {
    type: [[{ type: String, enum: ['manager', 'student'] }]]
  },

All 4 comments

As a workaround, you can do:

  people: {
    type: [[{ type: String, enum: ['manager', 'student'] }]]
  },

This ended up being much trickier than I thought because my workaround from my original comment doesn't actually work right now. Looks like validators on nested arrays were broken. We'll need to prioritize some refactoring work on nested arrays like #6405

This ended up being much trickier than I thought because my workaround from my original comment doesn't actually work right now. Looks like validators on nested arrays were broken. We'll need to prioritize some refactoring work on nested arrays like #6405

Thanks for updating. I will write a pre middleware to check enum before fix/release.
I guess I can use type: [[String]] on 5.6.7 right?

Yep, once 5.6.7 is released :+1:

Was this page helpful?
0 / 5 - 0 ratings