Anish Basu
Anish Basu

Reputation: 145

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?

Upvotes: 3

Views: 3184

Answers (1)

notionquest
notionquest

Reputation: 39226

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

Refer this issue page

Upvotes: 1

Related Questions