Error helper for mongoose error complex messages. Transform field validators into human readable arrays.
Switch branches/tags
Nothing to show
Latest commit eb1a098 Jul 17, 2017 Sebastian Bromberg Sebastian Bromberg New revision version

Mongoose Error Helper

Error helper for Mongoose schemas utilising utils to transform complex messages into human language.

Mongoose Validator simply returns Mongoose style validation objects inside an array of messages.


npm install mongoose-error-helper


var errorHelper = require('mongoose-error-helper').errorHelper;

function (req, res, next) {
    //generate `user` here (err) {
        //If we have an error, call the helper, return, and pass it `next`
        //to pass the "user-friendly" errors to
        if (err) return errorHelper(err, next);

##With callback##

var errorHelper = require('mongoose-error-helper').errorHelper;

	errorHelper(err, function(errors){

##Error Messages Before##

{ message: 'Validation failed',
  name: 'ValidationError',
   { username:
      { message: 'Validator "required" failed for path username',
        name: 'ValidatorError',
        path: 'username',
        type: 'required' },
      { message: 'Validator "enum" failed for path state',
        name: 'ValidatorError',
        path: 'state',
        type: 'enum' },
      { message: 'Validator "custom validator here" failed for path email',
        name: 'ValidatorError',
        path: 'email',
        type: 'custom validator here' },
      { message: 'Validator "min" failed for path age',
        name: 'ValidatorError',
        path: 'age',
        type: 'min' } } }

Error Messages After

[ 'username is required.',
  'state not an allowed value.',
  'custom validator here',
  'age below minimum.' ]


Special thanks to "clarkf" on stackoverflow for for this solution. We made it public for everybody.