From 3b537ead98e5f83d19ae467c9e187c3dc8377a48 Mon Sep 17 00:00:00 2001 From: amralaa10 Date: Wed, 27 Mar 2019 15:31:00 +0200 Subject: [PATCH 1/4] check for standard email max length --- src/lib/isEmail.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index f07b1be22..1258b110b 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -20,6 +20,7 @@ const gmailUserPart = /^[a-z\d]+$/; const quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i; const emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; const quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; +const defaultMaxEmailLength = 254; /* eslint-enable max-len */ /* eslint-enable no-control-regex */ @@ -35,6 +36,9 @@ export default function isEmail(str, options) { return false; } } + if(!options.ignore_max_length && str.length > defaultMaxEmailLength){ + return false; + } const parts = str.split('@'); const domain = parts.pop(); From d61e70b9aecb242145d364efde3c780d2da168db Mon Sep 17 00:00:00 2001 From: amralaa10 Date: Wed, 27 Mar 2019 15:35:24 +0200 Subject: [PATCH 2/4] update readme file with the new email option --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 261b5e57a..542f2e955 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Validator | Description **isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`locale` determine 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-FR', 'hu-HU', 'it-IT', 'ku-IQ', 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']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. -**isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. +**isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. **isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace:false }`. **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 }`. **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` determine 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-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`. From 8c6729e265b59ba4576f5dc9aa3bfda8a116b669 Mon Sep 17 00:00:00 2001 From: amralaa10 Date: Wed, 27 Mar 2019 17:38:31 +0200 Subject: [PATCH 3/4] fix code styling --- src/lib/isEmail.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index 1258b110b..7eb018fb1 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -36,7 +36,7 @@ export default function isEmail(str, options) { return false; } } - if(!options.ignore_max_length && str.length > defaultMaxEmailLength){ + if (!options.ignore_max_length && str.length > defaultMaxEmailLength) { return false; } From 5d57d1b6966270f823a2e541dde5523dbe3e9d4e Mon Sep 17 00:00:00 2001 From: amralaa10 Date: Thu, 28 Mar 2019 06:17:24 +0200 Subject: [PATCH 4/4] fix test failing case, emails with length more than 254 should be invalid --- lib/isEmail.js | 5 +++++ test/validators.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/isEmail.js b/lib/isEmail.js index ab897f773..826991527 100644 --- a/lib/isEmail.js +++ b/lib/isEmail.js @@ -33,6 +33,7 @@ var gmailUserPart = /^[a-z\d]+$/; var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i; var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; +var defaultMaxEmailLength = 254; /* eslint-enable max-len */ /* eslint-enable no-control-regex */ @@ -51,6 +52,10 @@ function isEmail(str, options) { } } + if (!options.ignore_max_length && str.length > defaultMaxEmailLength) { + return false; + } + var parts = str.split('@'); var domain = parts.pop(); var user = parts.join('@'); diff --git a/test/validators.js b/test/validators.js index 61ffbe479..04bded083 100644 --- a/test/validators.js +++ b/test/validators.js @@ -61,7 +61,6 @@ describe('Validators', () => { '" foo m端ller "@example.com', '"foo\\@bar"@example.com', `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, `${repeat('a', 64)}@${repeat('a', 63)}.com`, `${repeat('a', 31)}@gmail.com`, 'test@gmail.com', @@ -79,6 +78,7 @@ describe('Validators', () => { `${repeat('a', 64)}@${repeat('a', 251)}.com`, `${repeat('a', 65)}@${repeat('a', 250)}.com`, `${repeat('a', 64)}@${repeat('a', 64)}.com`, + `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, 'test1@invalid.co m', 'test2@invalid.co m', 'test3@invalid.co m',