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.
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:
And then, the error checking procedure:
If you want to shorten this, you have various options. If you only have one type of validation you can do it like this:
The minimal expression of the error check procedure would be similar to what you’ve wrote in your post:
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.