I have a basic mongoose Schema:
var userSchema = new Schema({
userName: {type: String, required: 'Please enter a username'},
email: {type: String, required: 'Please enter email'},
password: {type: String, required: 'Please create a password'},
created: {type: Date, default: Date.now}
});
And I perform a custom validator that calls a small service (in another file) to find if the email address is already in use:
userSchema.path('email').validate(function(value, next) {
userService.findUser(value, function(err, user) {
if (err) {
console.log(err);
return next(false);
}
next(!user);
});
}, 'Already exists');
Here is the findUser method:
exports.findUser = function(email, next) {
User.findOne({email: email.toLowerCase()}, function(err, user) {
next(err, user);
});
};
My question is: if I would like to use a module like validator to validate whether the input is an email or not, how would I go about doing that? I know I can require the module and do something like validator.isEmail(email)
and get a boolean in return, but how can I integrate this with my current setup?