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

Commit

Permalink
Support granular diagnosis exclusion
Browse files Browse the repository at this point in the history
Fixes #180. See also #162.
  • Loading branch information
skeggse committed Oct 7, 2018
1 parent 32397f3 commit 774ed58
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ internals.checkIpV6 = function (items) {
};


internals.isIterable = function (value) {

/* $lab:coverage:off$ */
if (!value) {
return false;
}
/* $lab:coverage:on$ */

if (typeof value !== 'object') {
return false;
}

// Return early in case we're on a version that doesn't support iterators.
if (Array.isArray(value)) {
return true;
}

/* $lab:coverage:off$ */
if (typeof Symbol === 'undefined') {
return false;
}
/* $lab:coverage:on$ */

return typeof value[Symbol.iterator] === 'function';
};


// Node 10 introduced isSet and isMap, which are useful for cross-context type
// checking.
// $lab:coverage:off$
Expand Down Expand Up @@ -368,10 +395,23 @@ exports.validate = internals.validate = function (email, options, callback) {
throw new TypeError('expected positive integer minDomainAtoms');
}

// Normalize the set of excluded diagnoses.
if (options.excludeDiagnoses) {
if (!internals.isIterable(options.excludeDiagnoses)) {
throw new TypeError('expected iterable excludeDiagnoses');
}

// This won't catch cross-realm Sets pre-Node 10, but it will cast the
// value to an in-realm Set representation.
if (!internals.isSet(options.excludeDiagnoses)) {
options.excludeDiagnoses = new Set(options.excludeDiagnoses);
}
}

let maxResult = internals.diagnoses.valid;
const updateResult = (value) => {

if (value > maxResult) {
if (value > maxResult && (!options.excludeDiagnoses || !options.excludeDiagnoses.has(value))) {
maxResult = value;
}
};
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,43 @@ describe('validate()', () => {
})).to.equal(diag.rfc5321TLDNumeric);
});

it('should ignore diagnoses in excludeDiagnoses', () => {

expect(() => {

Isemail.validate('person@123', { excludeDiagnoses: true });
}).to.throw(TypeError, /excludeDiagnoses/);

expect(() => {

Isemail.validate('person@123', {
excludeDiagnoses: {
[diag.rfc5321TLDNumeric]: true
}
});
}).to.throw(TypeError, /excludeDiagnoses/);

expect(Isemail.validate('[email protected]', {
excludeDiagnoses: [diag.rfc5321AddressLiteral],
errorLevel: diag.rfc5321TLDNumeric
})).to.equal(diag.rfc5321TLDNumeric);

expect(Isemail.validate('person@[127.0.0.1]', {
excludeDiagnoses: [diag.rfc5321AddressLiteral],
errorLevel: diag.rfc5321TLDNumeric
})).to.equal(diag.valid);

expect(Isemail.validate('person@[127.0.0.1]', {
excludeDiagnoses: new Set([diag.rfc5321AddressLiteral]),
errorLevel: diag.rfc5321TLDNumeric
})).to.equal(diag.valid);

expect(Isemail.validate('person@[127.0.0.1]', {
excludeDiagnoses: [],
errorLevel: diag.rfc5321TLDNumeric
})).to.equal(diag.rfc5321AddressLiteral);
});

it('should handle omitted options', () => {

expect(Isemail.validate(expectations[0][0])).to.equal(expectations[0][1] < internals.defaultThreshold);
Expand Down

0 comments on commit 774ed58

Please sign in to comment.