Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validator.normalizeEmail improvements #320

Merged
merged 6 commits into from
Oct 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ $ bower install validator-js
- **stripLow(input [, keep_new_lines])** - remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript.
- **whitelist(input, chars)** - remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. whitelist(input, '\\[\\]').
- **blacklist(input, chars)** - remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. blacklist(input, '\\[\\]').
- **normalizeEmail(email)** - canonicalize a gmail address.

### Strings only
- **normalizeEmail(email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.

This library validates and sanitizes **strings** only. All input will be coerced to a string using the following rules

Expand Down
36 changes: 28 additions & 8 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,37 @@ describe('Sanitizers', function () {
test({
sanitizer: 'normalizeEmail'
, expect: {
'[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
'[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': 'somename@googlemail.com'
, '[email protected]': 'somename@gmail.com'
, '[email protected]': '[email protected]'
, '[email protected]': 'somenamemiddlename@googlemail.com'
, 'some.name.midd..leNa...me..[email protected]': '[email protected]'
, 'some.name.midd..leNa...me...[email protected]': 'somenamemiddlename@googlemail.com'
, '[email protected]': 'somenamemiddlename@gmail.com'
, 'some.name.midd.leNa.me[email protected]': '[email protected]'
, '[email protected]': 'somenamemiddlename@gmail.com'
, '[email protected]': '[email protected]'
, 'an invalid email address': 'an invalid email address'
, '': ''
, 'hans@m端ller.com': 'hans@m端ller.com'
, 'an invalid email address': false
, '': false
// [email protected] was removed from test cases because of a bug with validator.isEmail. See issue #258
}
});
test({
sanitizer: 'normalizeEmail'
, args: [{lowercase: false}]
, expect: {
'[email protected]': '[email protected]'
, 'hans@m端ller.com': 'hans@m端ller.com'
, '[email protected]': '[email protected]' // Hostname is always lowercased
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'

// Domains that are known for being case-insensitive are always lowercased
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
}
});
});
Expand Down
2 changes: 2 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('Validators', function () {
, 'hans.m端[email protected]'
, 'hans@m端ller.com'
, 'test|123@m端ller.com'
, '[email protected]'
, '[email protected]'
]
, invalid: [
'invalidemail@'
Expand Down
32 changes: 28 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,38 @@
validator.blacklist = function (str, chars) {
return str.replace(new RegExp('[' + chars + ']+', 'g'), '');
};

var default_normalize_email_options = {
// Lowercase the local part for all domains (domains that are known for having case-insensitive local parts are always lowercased, such as gmail.com)
lowercase: true
};

validator.normalizeEmail = function (email) {
var parts = email.toLowerCase().split('@', 2);
validator.normalizeEmail = function (email, options) {
options = merge(options, default_normalize_email_options);

// Fail if the email address is invalid
if (!validator.isEmail(email)) {
return false;
}

var parts = email.split('@', 2);

// Always lowercase the domain, but the local part only if requested
parts[1] = parts[1].toLowerCase();
if (options.lowercase) {
parts[0] = parts[0].toLowerCase();
}

// gmail.com and googlemail.com
if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
if (!options.lowercase) { // case-insensitive
parts[0] = parts[0].toLowerCase();
}
parts[0] = parts[0].replace(/\./g, '').split('+')[0];
email = parts.join('@');
parts[1] = 'gmail.com'; // Always replace googlemail.com to gmail.com
}
return email;

return parts.join('@');
};

function merge(obj, defaults) {
Expand Down