Skip to content

Commit

Permalink
Merge pull request #832 from tux-tn/master
Browse files Browse the repository at this point in the history
Add gmail validation to isEmail
  • Loading branch information
chriso authored May 16, 2018
2 parents 69bdc8b + 9ad2034 commit 447981f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 24 deletions.
20 changes: 18 additions & 2 deletions lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var default_email_options = {
/* eslint-disable no-control-regex */
var displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
var gmailUserPart = /^[a-z\d]+$/;
var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
Expand Down Expand Up @@ -68,6 +69,21 @@ function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
var username = user.split('+')[0];

// Dots are not included in gmail length restriction
if (!(0, _isByteLength2.default)(username.replace('.', ''), { min: 6, max: 30 })) {
return false;
}

var _user_parts = username.split('.');
for (var i = 0; i < _user_parts.length; i++) {
if (!gmailUserPart.test(_user_parts[i])) {
return false;
}
}
}

if (!(0, _isByteLength2.default)(user, { max: 64 }) || !(0, _isByteLength2.default)(domain, { max: 254 })) {
Expand All @@ -86,8 +102,8 @@ function isEmail(str, options) {
var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;

var user_parts = user.split('.');
for (var i = 0; i < user_parts.length; i++) {
if (!pattern.test(user_parts[i])) {
for (var _i = 0; _i < user_parts.length; _i++) {
if (!pattern.test(user_parts[_i])) {
return false;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const default_email_options = {
/* eslint-disable no-control-regex */
const displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
const emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
const gmailUserPart = /^[a-z\d]+$/;
const quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
const emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
const quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
Expand Down Expand Up @@ -49,6 +50,21 @@ export default function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
const username = user.split('+')[0];

// Dots are not included in gmail length restriction
if (!isByteLength(username.replace('.', ''), { min: 6, max: 30 })) {
return false;
}

const user_parts = username.split('.');
for (let i = 0; i < user_parts.length; i++) {
if (!gmailUserPart.test(user_parts[i])) {
return false;
}
}
}

if (!isByteLength(user, { max: 64 }) ||
Expand Down
44 changes: 25 additions & 19 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('Validators', function () {
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'"foobar"@example.com',
'" foo m端ller "@example.com',
'"foo\\@bar"@example.com',
Expand All @@ -73,6 +73,7 @@ describe('Validators', function () {
`${repeat('a', 64)}@${repeat('a', 251)}.com`,
`${repeat('a', 65)}@${repeat('a', 250)}.com`,
`${repeat('a', 64)}@${repeat('a', 64)}.com`,
`${repeat('a', 31)}@gmail.com`,
'[email protected] m',
'[email protected] m',
'[email protected] m',
Expand All @@ -87,8 +88,12 @@ describe('Validators', function () {
'[email protected] m',
'[email protected] m',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'test123+invalid! [email protected]',
],
});
});
Expand All @@ -104,8 +109,8 @@ describe('Validators', function () {
'[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'"foobar"@example.com',
'"foo\\@bar"@example.com',
'" foo bar "@example.com',
Expand Down Expand Up @@ -136,21 +141,21 @@ describe('Validators', function () {
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <hans.m端[email protected]>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test[email protected]>',
'Some Name <test123[email protected]>',
'\'Foo Bar, Esq\'<[email protected]>',
'Some Name <some.name.midd.leNa.me.[email protected]>',
'Some Middle Name <some.name.midd.leNa.me.[email protected]>',
'Name <some.name.midd.leNa.me.[email protected]>',
'Name<some.name.midd.leNa.me.[email protected]>',
'Some Name <[email protected]>',
'Some Middle Name <[email protected]>',
'Name <[email protected]>',
'Name<[email protected]>',
],
invalid: [
'invalidemail@',
Expand All @@ -168,6 +173,7 @@ describe('Validators', function () {
'Some Name < [email protected] >',
'Name [email protected]',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
],
});
});
Expand All @@ -184,22 +190,22 @@ describe('Validators', function () {
'Some Name <hans.m端[email protected]>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test[email protected]>',
'Some Name <some.name.midd.leNa.me.[email protected]>',
'Some Middle Name <some.name.midd.leNa.me.[email protected]>',
'Name <some.name.midd.leNa.me.[email protected]>',
'Name<some.name.midd.leNa.me.[email protected]>',
'Some Name <test123[email protected]>',
'Some Name <[email protected]>',
'Some Middle Name <[email protected]>',
'Name <[email protected]>',
'Name<[email protected]>',
],
invalid: [
'some.name.midd.leNa.me.[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'test123[email protected]',
'invalidemail@',
'invalid.com',
'@invalid.com',
Expand Down Expand Up @@ -3016,7 +3022,7 @@ describe('Validators', function () {
});
for (var i = 0, str = '', encoded; i < 1000; i++) {
str += String.fromCharCode(Math.random() * 26 | 97);
encoded = new Buffer(str).toString('base64');
encoded = Buffer.from(str).toString('base64');
if (!validator.isBase64(encoded)) {
var msg = format('validator.isBase64() failed with "%s"', encoded);
throw new Error(msg);
Expand Down
20 changes: 18 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var default_email_options = {
/* eslint-disable no-control-regex */
var displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
var gmailUserPart = /^[a-z\d]+$/;
var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
Expand Down Expand Up @@ -218,6 +219,21 @@ function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
var username = user.split('+')[0];

// Dots are not included in gmail length restriction
if (!isByteLength(username.replace('.', ''), { min: 6, max: 30 })) {
return false;
}

var _user_parts = username.split('.');
for (var i = 0; i < _user_parts.length; i++) {
if (!gmailUserPart.test(_user_parts[i])) {
return false;
}
}
}

if (!isByteLength(user, { max: 64 }) || !isByteLength(domain, { max: 254 })) {
Expand All @@ -236,8 +252,8 @@ function isEmail(str, options) {
var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;

var user_parts = user.split('.');
for (var i = 0; i < user_parts.length; i++) {
if (!pattern.test(user_parts[i])) {
for (var _i = 0; _i < user_parts.length; _i++) {
if (!pattern.test(user_parts[_i])) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 447981f

Please sign in to comment.