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?
The fix for enum validations on sub-documents should be available on 4.2.5 version.
Refer this issue page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With