Search code examples
node.jsmongodbmongoosemongoose-schema

Ignore case in enum values


I have created one mongoose schema as below:

const quesSchema = new mongoose.Schema({
acRate: {
    type: Number,
},
difficulty: {
    type: String,
    enum: {
        values: ['EASY', 'MEDIUM', 'HARD'],
        message: 'Difficulty should be either EASY, MEDIUM or HARD',
    },
},
title: {
    type: String,
},
titleSlug: {
    type: String,
},
topicTags: [
    {
        name: {
            type: String,
        },
    },
],
});

In the data, the difficulty may occur in any case. I could not figure out how to ignore the case for the enum easily. I can make a custom validator that will make the input value lowercase/uppercase. But is there any other solution to it?


Solution

  • Yes, you need to add lowercase: true in your schema.

    Reference