Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose enum validation for embedded documents

I have two enum validators in my mongoose schema:

var UserSchema = new Schema({
    facebookId: {
        type: Number,
        required: true,
        unique: true
    },
    fullName: {
        type: String,
        required: true
    },
    firstName: String,
    lastName: String,
    locale: String,
    gender: {
        type: String,
        uppercase: true,
        enum: ['M', 'F']
    },
    matchPreferences: {
        gender: {
            type: String,
            uppercase: true,
            enum: ['M', 'F']
        },
        minAge: Number,
        maxAge: Number,
        maxRadiusMiles: Number
    }
});

The top level gender attribute gets enforced, but I was able to save anything for the matchPreferences.gender without any validation errors. A validation error does occur upon reading the document though.

Does enum validation work on embedded documents? And if not, what are the alternatives to enforce only enum value?

like image 207
Anish Basu Avatar asked Aug 04 '26 22:08

Anish Basu


1 Answers

The fix for enum validations on sub-documents should be available on 4.2.5 version.

Refer this issue page

like image 188
notionquest Avatar answered Aug 06 '26 16:08

notionquest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!