Custom Error Messages with Mongoose

前端 未结 8 569
旧巷少年郎
旧巷少年郎 2021-02-01 04:06

So according to the mongoose docs, you are supposed to be able to set a custom error message in the schema like so:

 var breakfastSchema = new Schema({
  eggs: {         


        
8条回答
  •  自闭症患者
    2021-02-01 04:46

    It is not directly possible as you tried it but you may want to have a look at mongoose-unique-validator which allows for custom error message if uniqueness would be violated.

    In particular the section about custom errors should be of interest to you.

    To get your desired

    "email must be unique"

    it would look similar to this

    var uniqueValidator = require('mongoose-unique-validator');
    ...
    emailVerificationTokenSchema.plugin(uniqueValidator, { message: '{PATH} must be unique' });
    

提交回复
热议问题