https://github.com/konsumer/mongoose-type-email
An email field-type for Mongoose schemas
https://github.com/konsumer/mongoose-type-email
Last synced: over 1 year ago
JSON representation
An email field-type for Mongoose schemas
- Host: GitHub
- URL: https://github.com/konsumer/mongoose-type-email
- Owner: konsumer
- Created: 2015-01-27T11:06:55.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T03:52:51.000Z (over 5 years ago)
- Last Synced: 2025-03-17T02:51:23.931Z (over 1 year ago)
- Language: JavaScript
- Size: 861 KB
- Stars: 26
- Watchers: 5
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongoose-type-email
An email field-type for Mongoose schemas
[](https://codeclimate.com/github/konsumer/mongoose-type-email)
[](https://www.npmjs.com/package/mongoose-type-email)
## usage
This will validate email, correctly:
```js
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:
```js
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:
```js
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
```js
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`
```js
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`](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address) 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:
```js
var UserSchema = new mongoose.Schema({
email: {
work: {type: mongoose.SchemaTypes.Email, correctTld: true},
home: {type: mongoose.SchemaTypes.Email, correctTld: true},
}
});
```