diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index e58c7503d..3dff7d2fd 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -17,39 +17,52 @@ export default function isFQDN(str, options) { str = str.substring(0, str.length - 1); } const parts = str.split('.'); - for (let i = 0; i < parts.length; i++) { - if (parts[i].length > 63) { + const tld = parts[parts.length - 1]; + + if (options.require_tld) { + // disallow fqdns without tld + if (parts.length < 2) { return false; } - } - if (options.require_tld) { - const tld = parts.pop(); - if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) { + + if (!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) { return false; } + // disallow spaces && special characers if (/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20\u00A9\uFFFD]/.test(tld)) { return false; } } - for (let part, i = 0; i < parts.length; i++) { - part = parts[i]; - if (!options.allow_numeric_tld && i === parts.length - 1 && /^\d+$/.test(part)) { - return false; // reject numeric TLDs + + // reject numeric TLDs + if (!options.allow_numeric_tld && /^\d+$/.test(tld)) { + return false; + } + + return parts.every((part) => { + if (part.length > 63) { + return false; } + if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) { return false; } + // disallow full-width chars if (/[\uff01-\uff5e]/.test(part)) { return false; } - if (part[0] === '-' || part[part.length - 1] === '-') { + + // disallow parts starting or ending with hyphen + if (/^-|-$/.test(part)) { return false; } + if (!options.allow_underscores && /_/.test(part)) { return false; } - } - return true; + + return true; + }); } diff --git a/test/validators.js b/test/validators.js index bfb53c0e1..4bb00d3f5 100644 --- a/test/validators.js +++ b/test/validators.js @@ -79,6 +79,7 @@ describe('Validators', () => { `${repeat('a', 31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', + 'test@1337.com', ], invalid: [ 'invalidemail@', @@ -374,6 +375,7 @@ describe('Validators', () => { 'http://[2010:836B:4179::836B:4179]', 'http://example.com/example.json#/foo/bar', 'http://user:@www.foobar.com', + 'http://1337.com', ], invalid: [ 'http://localhost:3000/', @@ -894,6 +896,7 @@ describe('Validators', () => { 'foo--bar.com', 'xn--froschgrn-x9a.com', 'rebecca.blackfriday', + '1337.com', ], invalid: [ 'abc',