Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Implement allowUnicode option (defaults to true) #160

Merged
merged 6 commits into from
Jan 18, 2018
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ The `tldBlacklist` option can be either an object lookup table or an array of in

The `tldWhitelist` option can be either an object lookup table or an array of valid top-level domains. If the email address has a top-level domain that is not in the whitelist, the email will be marked as invalid.

The `allowUnicode` option governs whether non-ASCII characters are allowed. Defaults to `true` per RFC 6530.

Only one of `tldBlacklist` and `tldWhitelist` will be consulted for TLD validity.

The `minDomainAtoms` option is an optional positive integer that specifies the minimum number of domain atoms that must be included for the email address to be considered valid. Be careful with the option, as some top-level domains, like `io`, directly support email addresses.
Expand Down
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const internals = {
cfwsComment: 17,
cfwsFWS: 18,

// Address contains non-ASCII when the allowUnicode option is false
// Has to be > internals.defaultThreshold so that it's rejected
// without an explicit errorLevel:
undesiredNonAscii: 25,

// Address contains deprecated elements, but may still be valid in some contexts

deprecatedLocalPart: 33,
Expand Down Expand Up @@ -214,6 +219,7 @@ internals.validDomain = function (tldAtom, options) {
* addresses.
* {*} tldBlacklist The set of domains to consider invalid.
* {*} tldWhitelist The set of domains to consider valid.
* {*} allowUnicode Whether to allow non-ASCII characters, defaults to true.
* {*} minDomainAtoms The minimum number of domain atoms which must be present
* for the address to be valid.
* @param {function(number|boolean)} callback The (optional) callback handler.
Expand Down Expand Up @@ -276,6 +282,11 @@ exports.validate = internals.validate = function (email, options, callback) {
}
};

const allowUnicode = options.allowUnicode === undefined || !!options.allowUnicode;
if (!allowUnicode && /[^\x00-\x7f]/.test(email)) {
updateResult(internals.diagnoses.undesiredNonAscii);
}

const context = {
now: internals.components.localpart,
prev: internals.components.localpart,
Expand Down
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ describe('validate()', () => {
done();
});

describe('with options.allowUnicode', () => {

it('should accept a pure ASCII email address when false', (done) => {

expect(Isemail.validate('[email protected]', {
allowUnicode: false
})).to.equal(true);
done();
});

it('should reject email addresses containing unicode when false', (done) => {

expect(Isemail.validate('üñïçø∂é@example.com', {
allowUnicode: false
})).to.equal(false);

expect(Isemail.validate('unicode@exãmple.com', {
allowUnicode: false
})).to.equal(false);
done();
});

describe('in combination with errorLevel', () => {

it('should return the right diagnosis when allowUnicode is false', (done) => {

expect(Isemail.validate('üñïçø∂é@example.com', {
allowUnicode: false,
errorLevel: 8
})).to.equal(25);
done();
});
});
});

it('should check options.minDomainAtoms', (done) => {

expect(() => {
Expand Down