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端ller@test.com',
'hans@m端ller.com',
'test|123@m端ller.com',
'test+ext@gmail.com',
'some.name.midd.leNa.me.+extension@GoogleMail.com',
'test123+ext@gmail.com',
'some.name.midd.leNa.me+extension@GoogleMail.com',
'"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`,
'test1@invalid.co m',
'test2@invalid.co m',
'test3@invalid.co m',
Expand All @@ -87,8 +88,12 @@ describe('Validators', function () {
'test12@invalid.co m',
'test13@invalid.co m',
'gmail...ignores...dots...@gmail.com',
'test@gmail.com',
'test.1@gmail.com',
'ends.with.dot.@gmail.com',
'multiple..dots@gmail.com',
'multiple..dots@stillinvalid.com',
'test123+invalid! sub_address@gmail.com',
],
});
});
Expand All @@ -104,8 +109,8 @@ describe('Validators', function () {
'foo+bar@bar.com',
'hans@m端ller.com',
'test|123@m端ller.com',
'test+ext@gmail.com',
'some.name.midd.leNa.me.+extension@GoogleMail.com',
'test123+ext@gmail.com',
'some.name.midd.leNa.me+extension@GoogleMail.com',
'"foobar"@example.com',
'"foo\\@bar"@example.com',
'" foo bar "@example.com',
Expand Down Expand Up @@ -136,21 +141,21 @@ describe('Validators', function () {
'hans.m端ller@test.com',
'hans@m端ller.com',
'test|123@m端ller.com',
'test+ext@gmail.com',
'some.name.midd.leNa.me.+extension@GoogleMail.com',
'test123+ext@gmail.com',
'some.name.midd.leNa.me+extension@GoogleMail.com',
'Some Name <foo@bar.com>',
'Some Name <x@x.au>',
'Some Name <foo@bar.com.au>',
'Some Name <foo+bar@bar.com>',
'Some Name <hans.m端ller@test.com>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test+ext@gmail.com>',
'Some Name <test123+ext@gmail.com>',
'\'Foo Bar, Esq\'<foo@bar.com>',
'Some Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Some Middle Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Name<some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Some Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Some Middle Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Name<some.name.midd.leNa.me+extension@GoogleMail.com>',
],
invalid: [
'invalidemail@',
Expand All @@ -168,6 +173,7 @@ describe('Validators', function () {
'Some Name < foo@bar.co.uk >',
'Name foo@bar.co.uk',
'Some Name <some..name@gmail.com>',
'Some Name <foo@gmail.com>',
],
});
});
Expand All @@ -184,22 +190,22 @@ describe('Validators', function () {
'Some Name <hans.m端ller@test.com>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test+ext@gmail.com>',
'Some Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Some Middle Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Name <some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Name<some.name.midd.leNa.me.+extension@GoogleMail.com>',
'Some Name <test123+ext@gmail.com>',
'Some Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Some Middle Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Name <some.name.midd.leNa.me+extension@GoogleMail.com>',
'Name<some.name.midd.leNa.me+extension@GoogleMail.com>',
],
invalid: [
'some.name.midd.leNa.me.+extension@GoogleMail.com',
'some.name.midd.leNa.me+extension@GoogleMail.com',
'foo@bar.com',
'x@x.au',
'foo@bar.com.au',
'foo+bar@bar.com',
'hans.m端ller@test.com',
'hans@m端ller.com',
'test|123@m端ller.com',
'test+ext@gmail.com',
'test123+ext@gmail.com',
'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.