From b4893552fda407a18bc5c6932d63347457c95b67 Mon Sep 17 00:00:00 2001 From: Anthony Nandaa Date: Wed, 1 Feb 2023 20:49:38 +0300 Subject: [PATCH] fix(isEmail): fixed `isFQDN`'s `ignore_max_length` check (#2170) fix(isEmail): fixed `isFQDN` still checking email length when `ignore_max_length` is `true` profnandaa: clean-up #2128 --------- Co-authored-by: Said Akhmedbayev Co-authored-by: Said Akhmedbayev --- README.md | 2 +- src/lib/isEmail.js | 5 ++++- src/lib/isFQDN.js | 3 ++- test/validators.test.js | 9 +++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c843bc456..257a61509 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ Validator | Description **isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace: false }`. **isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums. **isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`. -**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). +**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). **isFullWidth(str)** | check if the string contains any full-width chars. **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index 6db00195c..d1c35bd46 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -139,7 +139,10 @@ export default function isEmail(str, options) { return false; } - if (!isFQDN(domain, { require_tld: options.require_tld })) { + if (!isFQDN(domain, { + require_tld: options.require_tld, + ignore_max_length: options.ignore_max_length, + })) { if (!options.allow_ip_domain) { return false; } diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index 884d1dd6f..eb6928fda 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -7,6 +7,7 @@ const default_fqdn_options = { allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, + ignore_max_length: false, }; export default function isFQDN(str, options) { @@ -48,7 +49,7 @@ export default function isFQDN(str, options) { } return parts.every((part) => { - if (part.length > 63) { + if (part.length > 63 && !options.ignore_max_length) { return false; } diff --git a/test/validators.test.js b/test/validators.test.js index 0ca5dfa0a..45c955892 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -278,6 +278,15 @@ describe('Validators', () => { ], invalid: [], }); + + test({ + validator: 'isEmail', + args: [{ ignore_max_length: true }], + valid: [ + 'Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX@Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX.com', + ], + invalid: [], + }); }); it('should not validate email addresses with denylisted domains', () => {