Mongoose enum Validation on String Arrays? Mongoose enum Validation on String Arrays? arrays arrays

Mongoose enum Validation on String Arrays?


This is working fine for me (mongoose@4.1.8)

var schema = new mongoose.Schema({  factors: [{type: String, enum: ['1', '2', '3'], required: ...}]   ...})

Note I'm using an Array of Objects


As of mongoose version 5.0.6 and higher, the OP issue now works!

 factors: {            type: [String],            enum: permitted,            required: "Please specify at least one factor."    }

Reference

https://github.com/Automattic/mongoose/issues/6204#issuecomment-374690551


TRY THIS

let inventory_type_enum = ["goods", "services"]; inventory_type: {    type: String,    enum: inventory_type_enum,    validate: {      //  validator: (inventory_type) => !inventory_type.enum.includes(inventory_type),        validator: (inventory_type) => inventory_type_enum.includes(inventory_type),        message: languages('general_merchandise_model','inventory_type')    },      required : [true, languages('general_merchandise_model','inventory_type_required')],},