# mongoose-type-email

> An email field-type for Mongoose schemas

Latest version **1.1.2** (published 2020-11-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-type-email
pnpm add mongoose-type-email
yarn add mongoose-type-email
bun add mongoose-type-email
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2020-11-13 |
| First published | 2015-01-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | David Konsumer |
| Maintainers | konsumer |
| Keywords | mongoose, types, email, schema |

## Links

- npm: https://www.npmjs.com/package/mongoose-type-email
- Repository: https://github.com/konsumer/mongoose-type-email
- Issues: https://github.com/konsumer/mongoose-type-email/issues
- npm.io page: https://npm.io/package/mongoose-type-email

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) ^13.1.17

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2020-11-13
- 1.1.1 — 2020-11-13
- 1.1.0 — 2020-11-13
- 1.0.13 — 2020-10-05
- 1.0.12 — 2020-01-06
- 1.0.11 — 2019-04-23
- 1.0.10 — 2018-11-29
- 1.0.9 — 2018-05-20
- 1.0.8 — 2018-05-20
- 1.0.7 — 2018-05-20
- 1.0.6 — 2018-05-20
- 1.0.5 — 2017-04-05
- 1.0.4 — 2017-04-05
- 1.0.3 — 2017-02-09
- 1.0.2 — 2016-03-15
- … 1 more at https://npm.io/package/mongoose-type-email/versions

## README

# mongoose-type-email

An email field-type for Mongoose schemas

[![Code Climate](https://codeclimate.com/github/konsumer/mongoose-type-email/badges/gpa.svg)](https://codeclimate.com/github/konsumer/mongoose-type-email)

[![npm](https://nodei.co/npm/mongoose-type-email.png)](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},
    }
});
```

---
_Source: https://npm.io/package/mongoose-type-email · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
