how to define enum value in mongoose schema code example

Example 1: enum in mongoose

var userSchema = new mongooseSchema({
   userType: {
        type: String,
        enum : ['user','admin'],
        default: 'user'
    },
})

Example 2: mongoose schema enum example

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')],
},

Tags:

Misc Example