Mongoose - validate email syntax

I have a mongoose schema for users (UserSchema) and I'd like to validate whether the email has the right syntax. The validation that I currently use is the following:

UserSchema.path('email').validate(function (email) {
  return email.length
}, 'The e-mail field cannot be empty.')

However, this only checks if the field is empty or not, and not for the syntax.

Does something already exist that I could re-use or would I have to come up with my own method and call that inside the validate function?

you could also use the match or the validate property for validation in the schema

example

var validateEmail = function(email) {
    var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    return re.test(email)
};

var EmailSchema = new Schema({
    email: {
        type: String,
        trim: true,
        unique: true,
        required: 'Email address is required',
        validate: [validateEmail, 'Please fill a valid email address'],
        match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address']
    }
});

You can use a regex. Take a look at this question: Validate email address in Javascript?

I've used this in the past.

UserSchema.path('email').validate(function (email) {
   var emailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   return emailRegex.test(email.text); // Assuming email has a text attribute
}, 'The e-mail field cannot be empty.')

I use validator for my input sanitation, and it can be used in a pretty cool way.

Install it, and then use it like so:

var validator = require('validator');
// ... 

var EmailSchema = new Schema({
    email: { 
        //... other setup
        validate: [ validator.isEmail, 'invalid email' ]
    }
});

works a treat, and reads nicely.

(edit: fixed typo. Thanks!)

Kris Selbekk's answer is quite useful and easy to get, but just one detail:

use 'validate' instead of 'validator' when defined Schema, e.g:

var EmailSchema = new Schema({
  email: { 
    //... other setup
    validate: [ validator.isEmail, 'invalid email' ] // use 'validate' here
  }
}