Handling Mongoose validation errors – where and how?

后端 未结 9 1668
孤城傲影
孤城傲影 2020-12-23 19:45

I\'m trying to decide how I want to handle validation errors in Mongoose.

Custom error messages using node-validator

I have defined my own validation rules

9条回答
  •  囚心锁ツ
    2020-12-23 19:50

    Warning: As of Mongoose 4.1.3 the signature for function ValidatorError has completely changed and the information below is no more applicable:

    As of Mongoose 3.8.12 the signature for function ValidatorError is:

    function ValidatorError (path, msg, type, val) 
    

    Where type can be either "notvalid" or "required"

    For example if your "email" field validation raises a validation error, you can simply do:

    var error = new ValidationError(this);
    error.errors.email = 
          new ValidatorError('email', "Your err message.", 'notvalid', this.email);
    

提交回复
热议问题