Mongoose require a field based on some enum values of another field

Viewed 4209

I have a Mongoose schema Employee. In that I want to store a field (phone number) related to office for the employee, only if he/she is eligible for office, which is only for two levels "senior" and "c-level".

The schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var EmployeeSchema = new Schema({
    name: String,
    designation: String,
    level: {
        type: String,
        enum: ["intern", "junior", "mid-level", "senior", "c-level"],
        required: true,
    },
    phoneNo: { type: String, required: true },
    officePhoneNo: { type: String, required: true } // How to require only if the level is senior or c-level?,
});

Appreciate your help.

Thanks

1 Answers
Related