bobbyz bobbyz - 9 days ago 6
Node.js Question

Mongoose Schema enum validation on array of strings

The enum validation works when it's a single String:

var songSchema = new Schema({
vocalRange: {
type: String,
enum: ["Soprano", "Mezzo-soprano", "Contralto", "Alto", "Tenor", "Baritone", "Bass"],
}
})


Trying to POST anything but what's in the enum returns a validation error through mongoose.

However, the following:

var songSchema = new Schema({
vocalRange: {
type: [String], // this is what changed
enum: ["Soprano", "Mezzo-soprano", "Contralto", "Alto", "Tenor", "Baritone", "Bass"],
}
})


Allows me to POST anything for
vocalRange
without any kind of validation. What's going on? And why doesn't the validation work for an array of strings like it does for a single string?

Answer

Answering from my comment:

vocalRange: [{ type: String }]

should work.