when creating a mongoose schema, and using enum in order to validate the values, is it case sensitive?
As in:
const mySchema = new Schema({
animal: {
type: String,
enum: ['cat', 'dog']
}
});
const myModel = db.model('animal', mySchema);
const animal1 = new myModel({
animal: 'CAT'
});
Will animal1 be saved or rejected?
Thanks in advance!
CodePudding user response:
Mongoose has several inbuilt validators. Strings have enum as one of the validators. So enum creates a validator and checks if the value is given in an array & these are case sensitive so it will throw validation error, so you can use .toLowerCase() while saving the doc.
CodePudding user response:
If only Mongoose had a way of manually validating a document. Oh wait, there is, and it's even documented.
console.log( animal1.validateSync() );
…
Error: animal validation failed: animal: `CAT` is not a valid enum value for path `animal`.