Best way to check for mongoose validation error

Technically you must check first the error name because not all errors are handled the same way. Then, based on the error name you must check for particular properties, as the errors property that comes with a ValidationError.

Also you put the field name in the error type and this is redundant, it's better to use the same error type because in the error checking procedure you will get the field name also.

So your code can be something like:

User.schema.path('email').validate(function(value, respond) {
  User.findOne({email: value}, function(err, user) {
    if(err) throw err;
    if(user) return respond(false);
    respond(true);
  });
}, 'exists');

User.schema.path('username').validate(function(value, respond) {
  User.findOne({username: value}, function(err, user) {
    if(err) throw err;
    if(user) return respond(false);
    respond(true);
  });
}, 'exists');

And then, the error checking procedure:

if (err) {
  switch (err) {
    case err instanceof mongoose.Error.ValidationError:
      for (field in err.errors) {
        switch (err.errors[field].type) {
          case 'exists':
            ...
            break;
          case 'invalid':
            ...
            break;
          ...
        }
      }
      break;
    default:
      ...
  }
}

If you want to shorten this, you have various options. If you only have one type of validation you can do it like this:

if (err) {
  if (err instanceof mongoose.Error.ValidationError) {
    for (field in err.errors) {
      ...
    }
  } else {
    // A general error (db, crypto, etc…)
    ...
  }
}

The minimal expression of the error check procedure would be similar to what you've wrote in your post:

if (err) {
  for (field in err.errors) {
    ...
  }
}

This will work because if errors is not defined it will just ignore the for. But you're ignoring all other error types here.

I also think that these error layouts are a bit messing, but don't expect for this to change in a near future.


Just write the following code and enjoy.

if (err) {
    console.log('Error Inserting New Data');
    if (err.name == 'ValidationError') {
        for (field in err.errors) {
            console.log(err.errors[field].message); 
        }
    }
}