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

Fix(isEmail): Added support for internationalized domain names #2473

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const default_email_options = {
allow_underscores: false,
require_display_name: false,
allow_utf8_local_part: true,
allow_idn: true,
require_tld: true,
blacklisted_chars: '',
ignore_max_length: false,
Expand Down Expand Up @@ -144,6 +145,7 @@ export default function isEmail(str, options) {
require_tld: options.require_tld,
ignore_max_length: options.ignore_max_length,
allow_underscores: options.allow_underscores,
allow_idn: options.allow_idn,
})) {
if (!options.allow_ip_domain) {
return false;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/isFQDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const default_fqdn_options = {
allow_trailing_dot: false,
allow_numeric_tld: false,
allow_wildcard: false,
allow_idn: true,
ignore_max_length: false,
};

Expand Down Expand Up @@ -71,6 +72,11 @@ export default function isFQDN(str, options) {
return false;
}

// verify if domain is IDN
if (!options.allow_idn && !/^[a-z0-9-_]+$/i.test(part)) {
return false;
}

return true;
});
}
46 changes: 46 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ describe('Validators', () => {
});
});

it('should not validate email addresses with non-ASCII domain names if allow_idn is set to false', () => {
test({
validator: 'isEmail',
args: [{ allow_idn: false }],
valid: [],
invalid: [
'ka25maj@gˇmail.com',
'i18n@exampلe.com',
'i18n@EXАМПЛЕ.com',
'i18n@exамple.com',
'i18n@éxample.com',
'i18n@eßample.com',
'i18n@EXΑΜΠΛΕ.com',
'i18n@exαmple.com',
'i18n@exampłe.com',
'i18n@eซample.com',
'i18n@例題.com',
'i18n@例え.com',
'i18n@사례.com',
],
});
});

it('should validate email addresses with display names', () => {
test({
validator: 'isEmail',
Expand Down Expand Up @@ -828,6 +851,29 @@ describe('Validators', () => {
});
});

it('should not validate URLs with non-ASCII domain names if allow_idn is set to false', () => {
test({
validator: 'isURL',
args: [{ allow_idn: false }],
valid: [],
invalid: [
'gˇmail.com',
'exampلe.com',
'EXАМПЛЕ.com',
'exамple.com',
'éxample.com',
'eßample.com',
'EXΑΜΠΛΕ.com',
'exαmple.com',
'exampłe.com',
'eซample.com',
'例題.com',
'例え.com',
'사례.com',
],
});
});

it('should validate MAC addresses', () => {
test({
validator: 'isMACAddress',
Expand Down
Loading