Skip to content

Commit

Permalink
Switch from isemail package to smtp-address-parser for more correct R…
Browse files Browse the repository at this point in the history
…FC-5321 / RFC-6531 Mailbox address syntax check.
  • Loading branch information
Gene Hightower committed Mar 17, 2021
1 parent c56ec9a commit 8956e4c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ when writing the unit tests for IRI-references. Consider:

### idn-email

[`isemail`](https://www.npmjs.com/package/isemail) is used to check the validity
of the email.
[`smtp-address-parser`](https://www.npmjs.com/package/smtp-address-parser) is used to check the validity of the email.

### idn-hostname

Expand Down
10 changes: 8 additions & 2 deletions formats/idn-email.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const { validate } = require('isemail');
const { parse } = require('smtp-address-parser');

module.exports = value => {
return validate(value);
try {
parse(value);
return true;
}
catch {
return false;
}
};
12 changes: 11 additions & 1 deletion formats/iri.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const { parse } = require('uri-js');
const { validate } = require('isemail');
const { addressParser } = require('smtp-address-parser');
const schemes = require('schemes');

function validate (address) {
try {
addressParser(address);
return true;
}
catch {
return false;
}
}

module.exports = value => {
const iri = parse(value);
if (iri.scheme === 'mailto' && iri.to.every(validate)) {
Expand Down
1 change: 1 addition & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('load types', function() {
assert.ok(validate('квіточка@пошта.укр'));
assert.ok(validate('Dörte@Sörensen.example.com'));
assert.ok(validate('[email protected]'));
assert.ok(validate('"John Doe"@example.com'));
});

it('reject invalid idn-emails', function() {
Expand Down
67 changes: 59 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"license": "MIT",
"repository": "https://github.com/luzlab/ajv-formats-draft2019.git",
"dependencies": {
"isemail": "^3.2.0",
"punycode": "^2.1.1",
"schemes": "^1.1.1",
"smtp-address-parser": "^1.0.2",
"uri-js": "^4.2.2"
},
"peerDependencies": {
Expand Down

0 comments on commit 8956e4c

Please sign in to comment.