Git Product home page Git Product logo

mongoose-type-email's Introduction

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 [email protected]) 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},
    }
});

mongoose-type-email's People

Contributors

astik avatar esclapes avatar greenkeeper[bot] avatar igormucsicska-planorama avatar konsumer avatar samermurad avatar tamirl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mongoose-type-email's Issues

allowBlank with null value?

Hey, is it possible to add support for allowBlank so that not only '', but null also counts as blank? The use case is that I have

  email: {
    allowBlank: true,
    default: '', // want it to default to null
    type: mongoose.SchemaTypes.Email,
    unique: true,
  },

and the problem is that 2 emails with '' as values don't pass the unique validation. Having those as nulls will make the uniqueness validation pass.

Seems that the fix would be checking val not only for '', but for null on this line https://github.com/konsumer/mongoose-type-email/blob/master/index.js#L7

Not checking proper format for email

test6@12
If I just pass this it accepts email. Even though it doesn't have any subdomain like i.e. .com, .co.in etc.. please let me if you are planning to fix this. But it works perfectly.

An in-range update of mongoose is breaking the build 🚨

The devDependency mongoose was updated from 5.2.17 to 5.2.18.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mongoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 16 commits.

  • 3084fcb chore: release 5.2.18
  • b844bca style: fix lint re: #5704
  • 53c39fa fix(populate): handle multiple localFields + foreignFields using localField: function() {} syntax
  • 0e2d638 test(populate): repro #5704
  • 55ad233 docs(aggregate): fix syntax highlighting on allowDiskUse() docs
  • 7c2eb93 docs(migrating_to_5): add note about overwriting filter properties
  • 9975182 fix(document): retain user-defined key order on initial set with nested docs
  • fd8e227 test(document): repro #6944
  • 8fea4f8 fix(query): correctly handle select('+c') if c is not in schema
  • 5ec10b6 test(query): repro #7017
  • 477e8ca test(model): add coverage for #6972
  • 05aa04d fix(document): check path exists before checking for required
  • 2221d72 chore: hide home page ad on mobile
  • b54ce42 style fix some more lint warnings
  • eedfc03 chore: now working on 5.2.18

There are 16 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of mongoose is breaking the build 🚨

The devDependency mongoose was updated from 5.5.10 to 5.5.11.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mongoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 7 commits.

  • 09bc1a9 chore: release 5.5.11
  • 4d09963 refactor(discriminator): use one consolidated getConstructor() function to get the correct discriminator
  • ec01f4f fix(discriminator): allow numeric discriminator keys for embedded discriminators
  • e91fca1 test(discriminator): repro #7808
  • dc56c43 Merge pull request #7784 from Automattic/chore-node-12
  • c4fd03a chore: now working on 5.5.11
  • 10e8961 chore: add node 12 to travis

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of mocha is breaking the build 🚨

Version 5.2.0 of mocha was just published.

Branch Build failing 🚨
Dependency mocha
Current Version 5.1.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

mocha is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v5.2.0

5.2.0 / 2018-05-18

πŸŽ‰ Enhancements

πŸ› Fixes

πŸ“– Documentation

πŸ”© Other

Commits

The new version differs by 30 commits.

  • 5bd33a0 Release v5.2.0
  • 0a5604f reformat missed files
  • 7c8f551 ensure scripts/*.js gets prettiered
  • d8ea2ba update CHANGELOG.md for v5.2.0 [ci skip]
  • 7203ed7 update all dependencies
  • fb5393b migrate Mocha's tests to Unexpected assertion library (#3343)
  • fae9af2 docs(docs/index.md): Update "mocha.opts" documentation
  • 9d9e6c6 feat(bin/options.js): Add support for comment lines in "mocha.opts"
  • e0306ff fix busted lint-staged config
  • f2be6d4 Annotate when exceptions are caught but ignored; closes #3354 (#3356)
  • 889e681 remove dead code in bin/_mocha
  • 8712b95 fix(ocd): re-order Node.js tests in .travis.yml (descending)
  • 3ab0e7e fix to exit correctly when using bail flag
  • d87b12e add Node.js v10 to build; fix win32 issues (#3350)
  • b392af5 update package-lock.json for npm@6 [ci skip]

There are 30 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

this.constructor._checkRequired is not a function

I having get this error: _UserModel validation failed: email: this.constructor.checkRequired is not a function

this is my schema:

const mongoose = require('../db');
require('mongoose-type-email');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: mongoose.SchemaTypes.Email,
    required: true,
    index: {
      unique: true
    }
  },
  password: {
    type: String,
    required: true
  },
  phoneNumber: String
});

module.exports = mongoose.model('UserModel', userSchema);

Email Regex

While working with this package, I noticed that the email validation regex allows for something like 'email@email' to pass.
Doing a little searching I found this regex string catches pretty well, may want to consider using it instead.

(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])

An in-range update of mockingoose is breaking the build 🚨

The devDependency mockingoose was updated from 2.12.0 to 2.13.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mockingoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 4 commits.

  • 5ccda64 :bookmark: 2.13.0
  • 7158b0a Convert to TypeScript (#51)
  • dda1e44 feat: mocking toReturn via function (#50)
  • 95c1efe :wrench: add .circleci to .npmignore

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of release-it is breaking the build 🚨

The devDependency release-it was updated from 8.3.1 to 8.4.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

release-it is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for Release 8.4.0
  • Try to coerce provided increment into valid semver version (3abbc73)
  • Fall back to "0.0.0" if no latest version is found (ab4c1e7)
Commits

The new version differs by 3 commits.

  • 56ee11f Release 8.4.0
  • 3abbc73 Try to coerce provided increment into valid semver version
  • ab4c1e7 Fall back to "0.0.0" if no latest version is found

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of release-it is breaking the build 🚨

Version 7.4.4 of release-it was just published.

Branch Build failing 🚨
Dependency release-it
Current Version 7.4.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

release-it is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes Release 7.4.4
  • Update dependencies (support for Node v10) (50c6d5f)
Commits

The new version differs by 2 commits.

  • 396e734 Release 7.4.4
  • 50c6d5f Update dependencies (support for Node v10)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Update cryptiles to @hapi/cryptiles

This is to avoid this
npm WARN deprecated [email protected]: This module has moved and is now available at @hapi/cryptiles. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
npm WARN deprecated [email protected]: This module has moved and is now available at @hapi/boom. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
npm WARN deprecated [email protected]: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.

checkRequired overrides email validation message.

Hi,
Following 2bd35bb and its origin issue #24, there might be some change of behavior in the way error message are defined.

Considering this schema:

const userSchema = mongoose.Schema({
	email: {
		type: mongoose.SchemaTypes.Email,
		required: [true, "error.common.required"],
		unique: true,
		message: "error.common.email",
	},
});

If an email is invalid for example foobar i'm expecting to get error.common.email as error message but i get error.common.required.
This is due to the fact that checkRequired does not only check for the mandatory value but also for the email validation:

Email.prototype.checkRequired = function (val) {
return typeof val === 'string' && validateEmail(val, this.options)
}

From my POV, checkRequired should focus only on requirement, leaving the email validation to the plugin later lifecycle.

Current code for checkRequired is :

mongoose.SchemaTypes.Email.prototype.checkRequired = function (val) {
	return typeof val === 'string' && validateEmail(val, this.options);
};

My current alternative is to override this function by setting back the default mongoose behavior:

mongoose.SchemaTypes.Email.prototype.checkRequired = function (val) {
	return val != null;
};

That way, requirement is handled as before and will throw error.common.required only if value is missing. In case of incorrect email, checkRequired will be true, passing the requirement validation and will fail later in the plugin lifecycle providing error.common.email error as expected.

Beware that my alternative only care about non null value for requirement. It might be interesting to take care of allowBlank options here as well.

npm ERR! peer dep missing: mongoose@, required by mongoose-type-email

Found this little error. I guess you have to update this line: https://github.com/konsumer/mongoose-type-email/blob/master/package.json#L32

[23:13:53] sobolev :: MacBook-Pro-Nikita ➜ Documents/Frontends/razgrebaem β€Ήmaster*β€Ί Β» npm list mongoose
[email protected] /Users/sobolev/Documents/Frontends/razgrebaem
└── UNMET PEER DEPENDENCY [email protected]

npm ERR! peer dep missing: mongoose@, required by [email protected]
npm ERR! peer dep missing: mongoose@, required by [email protected]
npm ERR! code 1

When field's required is a function, allowBlank doesn't work.

Hey, nice plugin!
However, I am having an issue with allowBlank when field's required is set to a function.

To reproduce have this schema:

require('mongoose-type-email')
const { Schema } = mongoose

const userSchema = new Schema({
  email: {
    allowBlank: true,
    default: '',
    required: function() {
      return false // returning false here for the sake of the display
    },
    type: mongoose.SchemaTypes.Email,
  }
})

Most likely this is happening because on this line https://github.com/konsumer/mongoose-type-email/blob/master/index.js#L6 it doesn't check if the required is a functon and that it doesn't return false.

An in-range update of mongoose is breaking the build 🚨

The devDependency mongoose was updated from 5.3.16 to 5.4.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mongoose is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 60 commits.

  • f50eaca chore: release 5.4.0
  • bd5c04d Merge pull request #7333 from Automattic/5.4
  • 95d55f8 Merge pull request #7335 from zcfan/patch-1
  • de48c16 Fix mistake in populate documentation
  • 3a18679 Merge pull request #7256 from Automattic/gh7045
  • 1a0d204 feat: add custom caster support for Decimal128
  • aede9d1 test: add coverage for inheritance re: #7045
  • 1d12a53 feat(schema): add custom caster support for dates
  • fbd1547 Merge branch '5.4' into gh7045
  • 0c38458 fix: quick fix for #7268 #7125
  • 0b3eb72 Merge pull request #7268 from Automattic/gh7125
  • 7af371e docs(model): add clone option to docs
  • 31bdd14 feat(populate): add clone option to ensure each result gets a separate copy of the populated doc
  • 455a052 Merge pull request #7327 from danielkesselberg/add-find-one-and-delete-to-docs
  • 3c686b4 test: rename test

There are 60 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.