November: Procrastination Month
Miss any of our Open RFC calls?Watch the recordings here! »

mongoose-type-email

1.1.2 • Public • Published

mongoose-type-email

An email field-type for Mongoose schemas

Code Climate

npm

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

By default, this library follows the same validation you see in the html spec for type=email which allows local email addresses, and other non-standard email types. If you want more complete TLD validation (eg user@host.com) you can use the correctTld options:

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

Install

npm i mongoose-type-email

DownloadsWeekly Downloads

1,632

Version

1.1.2

License

MIT

Unpacked Size

8.25 kB

Total Files

5

Last publish

Collaborators

  • avatar