Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including mongoose-type-email with all npm packages installed. Try it out:

require("mongoose/package.json"); // mongoose is a peer dependency. var mongooseTypeEmail = require("mongoose-type-email")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mongoose-type-email v1.0.12

An email field-type for Mongoose schemas

mongoose-type-email

An email field-type for Mongoose schemas

npm

Greenkeeper badge Build Status Code Climate

usage

This will validate email, correctly:

var mongoose = require('mongoose');
require('mongoose-type-email');

var UserSchema = new mongoose.Schema({
    email: {
        work: mongoose.SchemaTypes.Email,
        home: mongoose.SchemaTypes.Email
    }
});

You can also use the stuff in String type:

var UserSchema = new mongoose.Schema({
    email: {
        work: {type: mongoose.SchemaTypes.Email, required: true},
        home: {type: mongoose.SchemaTypes.Email, required: true},
    }
});

You can also use it as an array:

var UserSchema = new mongoose.Schema({
    emails: [{type: mongoose.SchemaTypes.Email}]
});

You can add 'allowBlank: true' in order to allow empty string ('') when the field is not required

var mongoose = require('mongoose');
require('mongoose-type-email');

var UserSchema = new mongoose.Schema({
    email: {
        work: { type: mongoose.SchemaTypes.Email, allowBlank: true }, // allows '' as a value
        home: mongoose.SchemaTypes.Email // throws when the value is ''
    }
});

You can specify a default custom error message by overriding mongoose.SchemaTypes.Email.defaults.message

var mongoose = require('mongoose');
require('mongoose-type-email');
mongoose.SchemaTypes.Email.defaults.message = 'Email address is invalid'

var UserSchema = new mongoose.Schema({
    email: {
        work: mongoose.SchemaTypes.Email,
        home: mongoose.SchemaTypes.Email
    }
});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free