Best way to check for mongoose validation error


Question

I've got two validation functions for my usermodel

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);
  });
}, 'EMAIL_EXISTS');

and the same for username

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);
  });
}, 'USERNAME_TAKEN');

They return errors in the following format

{ message: 'Validation failed',
  name: 'ValidationError',
  errors: 
    { username: 
      { message: 'Validator "USERNAME_TAKEN" failed for path username',
        name: 'ValidatorError',
        path: 'username',
        type: 'USERNAME_TAKEN' } } }

The error for the email path is similar. Is there a smarter way to check for those errors than the following?

if (err && err.errors && err.errors.username) { ... }

This is kind of ugly.

1
25
1/18/2013 8:20:43 PM

Accepted Answer

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.name) {
    case '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.name == '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.

29
2/1/2013 8:23:30 AM

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); 
        }
    }
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon