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

Example 3: mongoose model enum

enum Role {
  ADMIN = 'ADMIN',
  USER = 'USER'
}

{
    type: String,
    enum: Role,
    default: Role.USER,
}