diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index bf7c05a0e..ba6041d49 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -5,8 +5,11 @@ feat(validatorName): brief title of what has been done + + ## Checklist - [ ] PR contains only changes related; no stray files, etc. - [ ] README updated (where applicable) - [ ] Tests written (where applicable) +- [ ] References provided in PR (where applicable) diff --git a/CHANGELOG.md b/CHANGELOG.md index 023aeac3f..8d27cf979 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,33 @@ +# 13.11.0 + +### New Features / Validators + +- [#2144](https://github.com/validatorjs/validator.js/pull/2144) `isFreightContainerID`: for shipping containers IDs @songyuew +- [#2188](https://github.com/validatorjs/validator.js/pull/2188) `isMailtoURI` @uksarkar + +### Fixes, New Locales and Enhancements + +- [#2025](https://github.com/validatorjs/validator.js/pull/2025) `isIBAN` add `MA` locale @lroudge +- [#2117](https://github.com/validatorjs/validator.js/pull/2117) `isCreditCard` refactor @pano9000 +- [#2189](https://github.com/validatorjs/validator.js/pull/2189) `isLocale` add support for more language tags @kwahome +- [#2203](https://github.com/validatorjs/validator.js/pull/2203) `isVAT` for `CU` @jimmyorpheus +- [#2217](https://github.com/validatorjs/validator.js/pull/2217) `isJWT` @Prathamesh061 +- [#2222](https://github.com/validatorjs/validator.js/pull/2222) `IsFQDN` test enhancements @aalekhpatel07 +- [#2226](https://github.com/validatorjs/validator.js/pull/2226) `isAlpha`, `isAlphanumeric` for `kk-KZ` @BekStar7 +- [#2229](https://github.com/validatorjs/validator.js/pull/2229) `isEmail` support `allow_underscores` @guspower +- [#2231](https://github.com/validatorjs/validator.js/pull/2231) `isDate` enhance Date declaration compatibility across multiple environments @CiprianS +- [#2235](https://github.com/validatorjs/validator.js/pull/2235) `isIBAN` add white and blacklist options to the isIBAN validator @edilson +- [#2237](https://github.com/validatorjs/validator.js/pull/2237) `isEmail` do not allow non-breaking space in user part @jeremy21212121 +- `isMobilePhone`: + - [#2175](https://github.com/validatorjs/validator.js/pull/2175) `so-SO` @ohersi + - [#2176](https://github.com/validatorjs/validator.js/pull/2176) `fr-CF` @cheboi + - [#2197](https://github.com/validatorjs/validator.js/pull/2197) `es-CU` @klaframboise + - [#2202](https://github.com/validatorjs/validator.js/pull/2202) `pl-PL` @czerwony03 + - [#2209](https://github.com/validatorjs/validator.js/pull/2209) `fr-WF` @aidos42 + - [#2246](https://github.com/validatorjs/validator.js/pull/2246) `ar-SD` @Hussienma + + + # 13.9.0 ### New Features / Validators diff --git a/README.md b/README.md index e4d1d7f4e..3e56bd51d 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,8 @@ Validator | Description **contains(str, seed [, options])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false.
`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. **equals(str, comparison)** | check if the string matches the comparison. **isAfter(str [, options])** | check if the string is a date that is after the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.
**Options:**
`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now). -**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

`locale` 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. -**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

`locale` 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', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

`locale` 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

`locale` 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', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. **isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32]. **isBase58(str)** | check if the string is base58 encoded. @@ -109,18 +109,19 @@ Validator | Description **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` 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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**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 is divisible by another. **isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number]. -**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, blacklisted_chars: '', host_blacklist: [] }`. 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, email addresses without a 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. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. +**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, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. 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, email addresses without a 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. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. **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, 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`). +**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **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']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. **isHexColor(str)** | check if the string is a hexadecimal color. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). -**isIBAN(str)** | check if the string is an IBAN (International Bank Account Number). +**isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).

`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`. **isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.

Defaults to 'any'. **isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. IMEI should be of format `###############` or `##-######-######-#`.

`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format. If `allow_hyphens` is set to true, the validator will validate the second format. **isIn(str, values)** | check if the string is in an array of allowed values. @@ -129,6 +130,7 @@ Validator | Description **isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6). **isISBN(str [, options])** | check if the string is an [ISBN][ISBN].

`options` is an object that has no default.
**Options:**
`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested. **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). +**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601][ISO 8601] date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code. @@ -146,9 +148,10 @@ Validator | Description **isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm). **isMACAddress(str [, options])** | check if the string is a MAC address.

`options` is an object which defaults to `{ no_separators: false }`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g. '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64. **isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format]. +**isMailtoURI(str, [, options])** | check if the string is a [Magnet URI format][Mailto URI Format].

`options` is an object of validating emails inside the URI (check `isEmail`s options for details). **isMD5(str)** | check if the string is a MD5 hash.

Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA). **isMimeType(str)** | check if the string matches to a valid [MIME type][MIME Type] format. -**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to `'any'`. If 'any' or a falsey value is used, function will check if any of the locales match).

`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`. +**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to `'any'`. If 'any' or a falsey value is used, function will check if any of the locales match).

`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`. **isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid]. **isMultibyte(str)** | check if the string contains one or more multibyte chars. **isNumeric(str [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

`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-FR', 'fr-CA', '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']`. @@ -299,7 +302,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number [Luhn Check]: https://en.wikipedia.org/wiki/Luhn_algorithm [Magnet URI Format]: https://en.wikipedia.org/wiki/Magnet_URI_scheme +[Mailto URI Format]: https://en.wikipedia.org/wiki/Mailto [MIME Type]: https://en.wikipedia.org/wiki/Media_type [mongoid]: http://docs.mongodb.org/manual/reference/object-id/ [RFC 3339]: https://tools.ietf.org/html/rfc3339 -[VAT Number]: https://en.wikipedia.org/wiki/VAT_identification_number \ No newline at end of file +[VAT Number]: https://en.wikipedia.org/wiki/VAT_identification_number diff --git a/package.json b/package.json index 4a1034945..3f088d3ce 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.9.0", + "version": "13.11.0", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ diff --git a/src/index.js b/src/index.js index 906fd7d1d..bef4cfff4 100644 --- a/src/index.js +++ b/src/index.js @@ -88,6 +88,7 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; +import { isISO6346, isFreightContainerID } from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; @@ -100,6 +101,7 @@ import isBase58 from './lib/isBase58'; import isBase64 from './lib/isBase64'; import isDataURI from './lib/isDataURI'; import isMagnetURI from './lib/isMagnetURI'; +import isMailtoURI from './lib/isMailtoURI'; import isMimeType from './lib/isMimeType'; @@ -124,7 +126,7 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; -const version = '13.9.0'; +const version = '13.11.0'; const validator = { version, @@ -199,6 +201,8 @@ const validator = { isEthereumAddress, isCurrency, isBtcAddress, + isISO6346, + isFreightContainerID, isISO6391, isISO8601, isRFC3339, @@ -210,6 +214,7 @@ const validator = { isBase64, isDataURI, isMagnetURI, + isMailtoURI, isMimeType, isLatLong, ltrim, diff --git a/src/lib/alpha.js b/src/lib/alpha.js index 0535e50d1..d540ed1cf 100644 --- a/src/lib/alpha.js +++ b/src/lib/alpha.js @@ -19,6 +19,7 @@ export const alpha = { 'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i, 'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i, 'ru-RU': /^[А-ЯЁ]+$/i, + 'kk-KZ': /^[А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$/i, 'sl-SI': /^[A-ZČĆĐŠŽ]+$/i, 'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i, 'sr-RS@latin': /^[A-ZČĆŽŠĐ]+$/i, @@ -58,6 +59,7 @@ export const alphanumeric = { 'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i, 'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i, 'ru-RU': /^[0-9А-ЯЁ]+$/i, + 'kk-KZ': /^[0-9А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$/i, 'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i, 'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i, 'sr-RS@latin': /^[0-9A-ZČĆŽŠĐ]+$/i, @@ -125,7 +127,7 @@ export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY']; export const commaDecimal = [ 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'id-ID', 'it-IT', 'ku-IQ', 'hi-IN', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', - 'ru-RU', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', + 'ru-RU', 'kk-KZ', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', ]; for (let i = 0; i < dotDecimal.length; i++) { diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index b7b24e968..938679d39 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -10,9 +10,17 @@ const cards = { unionpay: /^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/, visa: /^(?:4[0-9]{12})(?:[0-9]{3,6})?$/, }; -/* eslint-disable max-len */ -const allCards = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/; -/* eslint-enable max-len */ + +const allCards = (() => { + const tmpCardsArray = []; + for (const cardProvider in cards) { + // istanbul ignore else + if (cards.hasOwnProperty(cardProvider)) { + tmpCardsArray.push(cards[cardProvider]); + } + } + return tmpCardsArray; +})(); export default function isCreditCard(card, options = {}) { assertString(card); @@ -26,7 +34,7 @@ export default function isCreditCard(card, options = {}) { } else if (provider && !(provider.toLowerCase() in cards)) { /* specific provider not in the list */ throw new Error(`${provider} is not a valid credit card provider.`); - } else if (!(allCards.test(sanitized))) { + } else if (!allCards.some(cardProvider => cardProvider.test(sanitized))) { // no specific provider return false; } diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 8b7862e8b..9f1c6926b 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -47,7 +47,25 @@ export default function isDate(input, options) { dateObj[formatWord.charAt(0)] = dateWord; } - return new Date(`${dateObj.m}/${dateObj.d}/${dateObj.y}`).getDate() === +dateObj.d; + let fullYear = dateObj.y; + + if (dateObj.y.length === 2) { + const parsedYear = parseInt(dateObj.y, 10); + + if (isNaN(parsedYear)) { + return false; + } + + const currentYearLastTwoDigits = new Date().getFullYear() % 100; + + if (parsedYear < currentYearLastTwoDigits) { + fullYear = `20${dateObj.y}`; + } else { + fullYear = `19${dateObj.y}`; + } + } + + return new Date(`${fullYear}-${dateObj.m}-${dateObj.d}`).getDate() === +dateObj.d; } if (!options.strictMode) { diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index d1c35bd46..12938765e 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -1,12 +1,13 @@ import assertString from './util/assertString'; -import merge from './util/merge'; import isByteLength from './isByteLength'; import isFQDN from './isFQDN'; import isIP from './isIP'; +import merge from './util/merge'; const default_email_options = { allow_display_name: false, + allow_underscores: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, @@ -22,7 +23,7 @@ const splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+) !(countryCode in ibanRegexThroughCountryCode)); + + if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) { + return false; + } + + return true; +} + /** * Check whether string has correct universal IBAN format * The IBAN consists of up to 34 alphanumeric characters, as follows: @@ -95,14 +115,37 @@ const ibanRegexThroughCountryCode = { * NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z] * * @param {string} str - string under validation + * @param {object} options - object to pass the countries to be either whitelisted or blacklisted * @return {boolean} */ -function hasValidIbanFormat(str) { +function hasValidIbanFormat(str, options) { // Strip white spaces and hyphens const strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase(); const isoCountryCode = strippedStr.slice(0, 2).toUpperCase(); - return (isoCountryCode in ibanRegexThroughCountryCode) && + const isoCountryCodeInIbanRegexCodeObject = isoCountryCode in ibanRegexThroughCountryCode; + + if (options.whitelist) { + if (!hasOnlyValidCountryCodes(options.whitelist)) { + return false; + } + + const isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode); + + if (!isoCountryCodeInWhiteList) { + return false; + } + } + + if (options.blacklist) { + const isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode); + + if (isoCountryCodeInBlackList) { + return false; + } + } + + return (isoCountryCodeInIbanRegexCodeObject) && ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr); } @@ -130,10 +173,10 @@ function hasValidIbanChecksum(str) { return remainder === 1; } -export default function isIBAN(str) { +export default function isIBAN(str, options = {}) { assertString(str); - return hasValidIbanFormat(str) && hasValidIbanChecksum(str); + return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str); } export const locales = Object.keys(ibanRegexThroughCountryCode); diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js new file mode 100644 index 000000000..0cb657e7c --- /dev/null +++ b/src/lib/isISO6346.js @@ -0,0 +1,37 @@ +import assertString from './util/assertString'; + +// https://en.wikipedia.org/wiki/ISO_6346 +// according to ISO6346 standard, checksum digit is mandatory for freight container but recommended +// for other container types (J and Z) +const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/; +const isDigit = /^[0-9]$/; + +export function isISO6346(str) { + assertString(str); + + str = str.toUpperCase(); + + if (!isISO6346Str.test(str)) return false; + + if (str.length === 11) { + let sum = 0; + for (let i = 0; i < str.length - 1; i++) { + if (!isDigit.test(str[i])) { + let convertedCode; + const letterCode = str.charCodeAt(i) - 55; + if (letterCode < 11) convertedCode = letterCode; + else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); + else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); + else convertedCode = 34 + (letterCode % 31); + sum += convertedCode * (2 ** i); + } else sum += str[i] * (2 ** i); + } + + const checkSumDigit = sum % 11; + return Number(str[str.length - 1]) === checkSumDigit; + } + + return true; +} + +export const isFreightContainerID = isISO6346; diff --git a/src/lib/isJWT.js b/src/lib/isJWT.js index 1a8896f98..1d0ade5ee 100644 --- a/src/lib/isJWT.js +++ b/src/lib/isJWT.js @@ -7,7 +7,7 @@ export default function isJWT(str) { const dotSplit = str.split('.'); const len = dotSplit.length; - if (len > 3 || len < 2) { + if (len !== 3) { return false; } diff --git a/src/lib/isLocale.js b/src/lib/isLocale.js index cacac8aec..ec84c8fce 100644 --- a/src/lib/isLocale.js +++ b/src/lib/isLocale.js @@ -1,11 +1,111 @@ import assertString from './util/assertString'; -const localeReg = /^[A-Za-z]{2,4}([_-]([A-Za-z]{4}|[\d]{3}))?([_-]([A-Za-z]{2}|[\d]{3}))?$/; +/* + = 3ALPHA ; selected ISO 639 codes + *2("-" 3ALPHA) ; permanently reserved + */ +const extlang = '([A-Za-z]{3}(-[A-Za-z]{3}){0,2})'; + +/* + = 2*3ALPHA ; shortest ISO 639 code + ["-" extlang] ; sometimes followed by + ; extended language subtags + / 4ALPHA ; or reserved for future use + / 5*8ALPHA ; or registered language subtag + */ +const language = `(([a-zA-Z]{2,3}(-${extlang})?)|([a-zA-Z]{5,8}))`; + +/* + = 4ALPHA ; ISO 15924 code + */ +const script = '([A-Za-z]{4})'; + +/* + = 2ALPHA ; ISO 3166-1 code + / 3DIGIT ; UN M.49 code + */ +const region = '([A-Za-z]{2}|\\d{3})'; + +/* + = 5*8alphanum ; registered variants + / (DIGIT 3alphanum) + */ +const variant = '([A-Za-z0-9]{5,8}|(\\d[A-Z-a-z0-9]{3}))'; + +/* + = DIGIT ; 0 - 9 + / %x41-57 ; A - W + / %x59-5A ; Y - Z + / %x61-77 ; a - w + / %x79-7A ; y - z + */ +const singleton = '(\\d|[A-W]|[Y-Z]|[a-w]|[y-z])'; + +/* + = singleton 1*("-" (2*8alphanum)) + ; Single alphanumerics + ; "x" reserved for private use + */ +const extension = `(${singleton}(-[A-Za-z0-9]{2,8})+)`; + +/* + = "x" 1*("-" (1*8alphanum)) + */ +const privateuse = '(x(-[A-Za-z0-9]{1,8})+)'; + +// irregular tags do not match the 'langtag' production and would not +// otherwise be considered 'well-formed'. These tags are all valid, but +// most are deprecated in favor of more modern subtags or subtag combination + +const irregular = '((en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|' + + '(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|' + + '(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE))'; + +// regular tags match the 'langtag' production, but their subtags are not +// extended language or variant subtags: their meaning is defined by +// their registration and all of these are deprecated in favor of a more +// modern subtag or sequence of subtags + +const regular = '((art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|' + + '(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang))'; + +/* + = irregular ; non-redundant tags registered + / regular ; during the RFC 3066 era + + */ +const grandfathered = `(${irregular}|${regular})`; + +/* + RFC 5646 defines delimitation of subtags via a hyphen: + + "Subtag" refers to a specific section of a tag, delimited by a + hyphen, such as the subtags 'zh', 'Hant', and 'CN' in the tag "zh- + Hant-CN". Examples of subtags in this document are enclosed in + single quotes ('Hant') + + However, we need to add "_" to maintain the existing behaviour. + */ +const delimiter = '(-|_)'; + +/* + = language + ["-" script] + ["-" region] + *("-" variant) + *("-" extension) + ["-" privateuse] + */ +const langtag = `${language}(${delimiter}${script})?(${delimiter}${region})?(${delimiter}${variant})*(${delimiter}${extension})*(${delimiter}${privateuse})?`; + +/* + Regex implementation based on BCP RFC 5646 + Tags for Identifying Languages + https://www.rfc-editor.org/rfc/rfc5646.html + */ +const languageTagRegex = new RegExp(`(^${privateuse}$)|(^${grandfathered}$)|(^${langtag}$)`); export default function isLocale(str) { assertString(str); - if (str === 'en_US_POSIX' || str === 'ca_ES_VALENCIA') { - return true; - } - return localeReg.test(str); + return languageTagRegex.test(str); } diff --git a/src/lib/isMailtoURI.js b/src/lib/isMailtoURI.js new file mode 100644 index 000000000..0dd95b6a9 --- /dev/null +++ b/src/lib/isMailtoURI.js @@ -0,0 +1,67 @@ +import trim from './trim'; +import isEmail from './isEmail'; +import assertString from './util/assertString'; + +function parseMailtoQueryString(queryString) { + const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']), + query = { cc: '', bcc: '' }; + let isParseFailed = false; + + const queryParams = queryString.split('&'); + + if (queryParams.length > 4) { + return false; + } + + for (const q of queryParams) { + const [key, value] = q.split('='); + + // checked for invalid and duplicated query params + if (key && !allowedParams.has(key)) { + isParseFailed = true; + break; + } + + if (value && (key === 'cc' || key === 'bcc')) { + query[key] = value; + } + + if (key) { + allowedParams.delete(key); + } + } + + return isParseFailed ? false : query; +} + +export default function isMailtoURI(url, options) { + assertString(url); + + if (url.indexOf('mailto:') !== 0) { + return false; + } + + const [to = '', queryString = ''] = url.replace('mailto:', '').split('?'); + + if (!to && !queryString) { + return true; + } + + const query = parseMailtoQueryString(queryString); + + if (!query) { + return false; + } + + return `${to},${query.cc},${query.bcc}` + .split(',') + .every((email) => { + email = trim(email, ' '); + + if (email) { + return isEmail(email, options); + } + + return true; + }); +} diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index 5c37d42bb..1da97ce88 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -16,6 +16,7 @@ const phones = { 'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, 'ar-PS': /^(\+?970|0)5[6|9](\d{7})$/, 'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, + 'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/, 'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, 'ar-TN': /^(\+?216)?[2459]\d{7}$/, 'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, @@ -48,6 +49,7 @@ const phones = { 'en-IN': /^(\+?91|0)?[6789]\d{9}$/, 'en-JM': /^(\+?876)?\d{7}$/, 'en-KE': /^(\+?254|0)(7|1)\d{8}$/, + 'fr-CF': /^(\+?236| ?)(70|75|77|72|21|22)\d{6}$/, 'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, 'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, 'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, @@ -75,7 +77,7 @@ const phones = { 'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, 'es-CR': /^(\+506)?[2-8]\d{7}$/, - 'es-CU': /^(\+53|0053)?5\d{7}/, + 'es-CU': /^(\+53|0053)?5\d{7}$/, 'es-DO': /^(\+?1)?8[024]9\d{7}$/, 'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, @@ -103,6 +105,7 @@ const phones = { 'fr-MQ': /^(\+?596|0|00596)[67]\d{8}$/, 'fr-PF': /^(\+?689)?8[789]\d{6}$/, 'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/, + 'fr-WF': /^(\+681)?\d{6}$/, 'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/, 'hu-HU': /^(\+?36|06)(20|30|31|50|70)\d{7}$/, 'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/, @@ -128,7 +131,7 @@ const phones = { 'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/, 'nl-AW': /^(\+)?297(56|59|64|73|74|99)\d{5}$/, 'nn-NO': /^(\+?47)?[49]\d{7}$/, - 'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/, + 'pl-PL': /^(\+?48)? ?([5-8]\d|45) ?\d{3} ?\d{2} ?\d{2}$/, 'pt-BR': /^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, 'pt-PT': /^(\+?351)?9[1236]\d{7}$/, 'pt-AO': /^(\+244)\d{9}$/, @@ -138,6 +141,7 @@ const phones = { 'si-LK': /^(?:0|94|\+94)?(7(0|1|2|4|5|6|7|8)( |-)?)\d{7}$/, 'sl-SI': /^(\+386\s?|0)(\d{1}\s?\d{3}\s?\d{2}\s?\d{2}|\d{2}\s?\d{3}\s?\d{3})$/, 'sk-SK': /^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/, + 'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, 'sq-AL': /^(\+355|0)6[789]\d{6}$/, 'sr-RS': /^(\+3816|06)[- \d]{5,9}$/, 'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/, diff --git a/src/lib/isVAT.js b/src/lib/isVAT.js index 95593569d..ece7d8560 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -1,6 +1,21 @@ import assertString from './util/assertString'; import * as algorithms from './util/algorithms'; +const CH = (str) => { + // @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0} + const hasValidCheckNumber = (digits) => { + const lastDigit = digits.pop(); // used as check number + const weights = [5, 4, 3, 2, 7, 6, 5, 4]; + const calculatedCheckNumber = (11 - (digits.reduce((acc, el, idx) => + acc + (el * weights[idx]), 0) % 11)) % 11; + + return lastDigit === calculatedCheckNumber; + }; + + // @see {@link https://www.estv.admin.ch/estv/de/home/mehrwertsteuer/uid/mwst-uid-nummer.html} + return /^(CHE[- ]?)?(\d{9}|(\d{3}\.\d{3}\.\d{3})|(\d{3} \d{3} \d{3})) ?(TVA|MWST|IVA)?$/.test(str) && hasValidCheckNumber((str.match(/\d/g).map(el => +el))); +}; + const PT = (str) => { const match = str.match(/^(PT)?(\d{9})$/); if (!match) { @@ -69,7 +84,7 @@ export const vatMatchers = { SM: str => /^(SM)?\d{5}$/.test(str), SA: str => /^(SA)?\d{15}$/.test(str), RS: str => /^(RS)?\d{9}$/.test(str), - CH: str => /^(CH)?(\d{6}|\d{9}|(\d{3}.\d{3})|(\d{3}.\d{3}.\d{3}))(TVA|MWST|IVA)$/.test(str), + CH, TR: str => /^(TR)?\d{10}$/.test(str), UA: str => /^(UA)?\d{12}$/.test(str), GB: str => /^GB((\d{3} \d{4} ([0-8][0-9]|9[0-6]))|(\d{9} \d{3})|(((GD[0-4])|(HA[5-9]))[0-9]{2}))$/.test(str), diff --git a/test/validators.test.js b/test/validators.test.js index a1079b34f..6bf812d15 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -36,6 +36,7 @@ describe('Validators', () => { 'invalid.com', '@invalid.com', 'foo@bar.com.', + 'foo@_bar.com', 'somename@gmail.com', 'foo@bar.co.uk.', 'z@co.c', @@ -66,6 +67,9 @@ describe('Validators', () => { '"wrong()[]",:;<>@@gmail.com', 'username@domain.com�', 'username@domain.com©', + 'nbsp test@test.com', + 'nbsp_test@te st.com', + 'nbsp_test@test.co m', ], }); }); @@ -89,6 +93,16 @@ describe('Validators', () => { }); }); + it('should validate email addresses with underscores in the domain', () => { + test({ + validator: 'isEmail', + args: [{ allow_underscores: true }], + valid: [ + 'foobar@my_sarisari_store.typepad.com', + ], + invalid: [], + }); + }); it('should validate email addresses without UTF8 characters in local part', () => { test({ @@ -117,6 +131,7 @@ describe('Validators', () => { 'hans.m端ller@test.com', 'z@co.c', 'tüst@invalid.com', + 'nbsp test@test.com', ], }); }); @@ -1604,6 +1619,27 @@ describe('Validators', () => { }); }); + it('should validate kazakh alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['kk-KZ'], + valid: [ + 'Сәлем', + 'қанағаттандырылмағандықтарыңыздан', + 'Кешіріңіз', + 'Өкінішке', + 'Қайталаңызшы', + 'ағылшынша', + 'түсінбедім', + ], + invalid: [ + 'Кешіріңіз1', + ' Кет бар ', + 'مرحبا العا', + ], + }); + }); + it('should validate Vietnamese alpha strings', () => { test({ validator: 'isAlpha', @@ -2433,6 +2469,27 @@ describe('Validators', () => { }); }); + it('should validate kazakh alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['kk-KZ'], + valid: [ + 'Сәлем777', + '123Бәсе', + 'солай', + 'Жиенсу', + '90тоқсан', + 'жалғыз', + '570бердім', + ], + invalid: [ + ' кешіріңіз ', + 'abcағылшынша', + 'мүмкін!!', + ], + }); + }); + it('should validate kurdish alphanumeric strings', () => { test({ validator: 'isAlphanumeric', @@ -4674,10 +4731,11 @@ describe('Validators', () => { 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E', - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ', // No signature ], invalid: [ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9', + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTUxNjIzOTAyMn0', + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTYxNjY1Mzg3Mn0.eyJpc3MiOiJodHRwczovL2V4YW1wbGUuY29tIiwiaWF0IjoxNjE2NjUzODcyLCJleHAiOjE2MTY2NTM4ODJ9.a1jLRQkO5TV5y5ERcaPAiM9Xm2gBdRjKrrCpHkGr_8M', '$Zs.ewu.su84', 'ks64$S/9.dy$§kz.3sd73b', ], @@ -4816,16 +4874,51 @@ describe('Validators', () => { 'uz_Latn_UZ', 'en', 'gsw', + 'en-US', 'es_ES', + 'es-419', 'sw_KE', 'am_ET', + 'zh-CHS', 'ca_ES_VALENCIA', 'en_US_POSIX', + 'hak-CN', + 'zh-Hant', + 'zh-Hans', + 'sr-Cyrl', + 'sr-Latn', + 'zh-cmn-Hans-CN', + 'cmn-Hans-CN', + 'zh-yue-HK', + 'yue-HK', + 'zh-Hans-CN', + 'sr-Latn-RS', + 'sl-rozaj', + 'sl-rozaj-biske', + 'sl-nedis', + 'de-CH-1901', + 'sl-IT-nedis', + 'hy-Latn-IT-arevela', + 'i-enochian', + 'en-scotland-fonipa', + 'sl-IT-rozaj-biske-1994', + 'de-CH-x-phonebk', + 'az-Arab-x-AZE-derbend', + 'x-whatever', + 'qaa-Qaaa-QM-x-southern', + 'de-Qaaa', + 'sr-Latn-QM', + 'sr-Qaaa-RS', + 'en-US-u-islamcal', + 'zh-CN-a-myext-x-private', + 'en-a-myext-b-another', ], invalid: [ 'lo_POP', '12', '12_DD', + 'de-419-DE', + 'a-DE', ], }); }); @@ -5164,6 +5257,83 @@ describe('Validators', () => { 'LB92000700000000123123456123', 'IR200170000000339545727003', 'MZ97123412341234123412341', + 'MA64011519000001205000534921', + ], + invalid: [ + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + ], + }); + test({ + validator: 'isIBAN', + args: [{ whitelist: ['DK', 'GB'] }], + valid: [ + 'DK5000400440116243', + 'GB29NWBK60161331926819', + ], + invalid: [ + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + ], + }); + test({ + validator: 'isIBAN', + args: [{ whitelist: ['XX', 'AA'] }], + invalid: [ + 'DK5000400440116243', + 'GB29NWBK60161331926819', + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + ], + }); + test({ + validator: 'isIBAN', + args: [{ blacklist: ['IT'] }], + valid: [ + 'SC52BAHL01031234567890123456USD', + 'LC14BOSL123456789012345678901234', + 'MT31MALT01100000000000000000123', + 'SV43ACAT00000000000000123123', + 'EG800002000156789012345180002', + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'CH56 0483 5012 3456 7800 9', + 'GB98 MIDL 0700 9312 3456 78', + 'IL170108000000012612345', + 'JO71CBJO0000000000001234567890', + 'TR320010009999901234567890', + 'BR1500000000000010932840814P2', + 'LB92000700000000123123456123', + 'IR200170000000339545727003', + 'MZ97123412341234123412341', ], invalid: [ 'XX22YYY1234567890123', @@ -5171,6 +5341,7 @@ describe('Validators', () => { 'FR7630006000011234567890189@', 'FR7630006000011234567890189😅', 'FR763000600001123456!!🤨7890189@', + 'IT60X0542811101000000123456', ], }); }); @@ -6777,6 +6948,24 @@ describe('Validators', () => { '0114152198', ], }, + { + locale: 'ar-SD', + valid: [ + '0128652312', + '+249919425113', + '249123212345', + '0993212345', + ], + invalid: [ + '12345', + '', + '+249972662622', + '+24946266262', + '+24933221097', + '0614152198', + '096554', + ], + }, { locale: 'ar-TN', valid: [ @@ -7404,6 +7593,31 @@ describe('Validators', () => { '+254800723845', ], }, + { + locale: 'fr-CF', + valid: [ + '+23670850000', + '+23675038756', + '+23677859002', + '+23672854202', + '+23621854052', + '+23622854072', + '72234650', + '70045902', + '77934567', + '21456794', + '22452389', + ], + invalid: [ + '+23689032', + '123456789', + '+236723845987', + '022452389', + '+236772345678', + '+236700456794', + + ], + }, { locale: 'en-KI', valid: [ @@ -7739,6 +7953,32 @@ describe('Validators', () => { '6898912345', ], }, + { + locale: 'fr-WF', + valid: [ + '+681408500', + '+681499387', + '+681728590', + '+681808542', + '+681828540', + '+681832014', + '408500', + '499387', + '728590', + '808542', + '828540', + '832014', + ], + invalid: [ + '+68189032', + '123456789', + '+681723845987', + '022452389', + '+681772345678', + '+681700456794', + + ], + }, { locale: 'ka-GE', valid: [ @@ -8271,6 +8511,7 @@ describe('Validators', () => { '', 'abc', '+535123457', + '56043029304', ], }, { @@ -8531,6 +8772,7 @@ describe('Validators', () => { '+48 56 6572724', '+48 67 621 5461', '48 67 621 5461', + '+48 45 621 5461', ], invalid: [ '+48 67 621 5461', @@ -8541,6 +8783,7 @@ describe('Validators', () => { '1800-88-8687', '+6019-5830837', '357562855', + '+48 44 621 5461', ], }, { @@ -9321,6 +9564,27 @@ describe('Validators', () => { 'NotANumber', ], }, + { + locale: 'so-SO', + valid: [ + '+252601234567', + '+252650101010', + '+252794567120', + '252650647388', + '252751234567', + '0601234567', + '0609876543', + ], + invalid: [ + '', + 'not a number', + '+2526012345678', + '25260123456', + '+252705555555', + '+0601234567', + '06945454545', + ], + }, { locale: 'sq-AL', valid: [ @@ -11946,6 +12210,58 @@ describe('Validators', () => { }); }); + + it('should validate ISO6346 shipping containerID', () => { + test({ + validator: 'isISO6346', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + 'QJRZ123456', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + 'AFFU5962593', + ], + }); + }); + it('should validate ISO6346 shipping containerID', () => { + test({ + validator: 'isFreightContainerID', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + 'QJRZ123456', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + 'AFFU5962593', + ], + }); + }); + // EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas. it('should validate taxID', () => { test({ @@ -12643,6 +12959,7 @@ describe('Validators', () => { '15/7/2002', '15-7-2002', '15/07-02', + '30/04/--', ], }); test({ @@ -12657,6 +12974,7 @@ describe('Validators', () => { '15/7/02', '15-7-02', '5/7-02', + '3/4/aa', ], }); test({ @@ -13766,18 +14084,30 @@ describe('Validators', () => { validator: 'isVAT', args: ['CH'], valid: [ - 'CH123456TVA', - '123456TVA', - 'CH123456789MWST', - '123456789MWST', - 'CH123.456IVA', - '123.456IVA', - 'CH123.456.789TVA', - '123.456.789TVA', - ], - invalid: [ - 'CH 123456', - '12345', + // strictly valid + 'CHE-116.281.710 MWST', + 'CHE-116.281.710 IVA', + 'CHE-116.281.710 TVA', + // loosely valid presentation variants + 'CHE 116 281 710 IVA', // all separators are spaces + 'CHE-191.398.369MWST', // no space before suffix + 'CHE-116281710 MWST', // no number separators + 'CHE-116281710MWST', // no number separators and no space before suffix + 'CHE105854263MWST', // no separators + 'CHE-116.285.524', // no suffix (vat abbreviation) + 'CHE116281710', // no suffix and separators + '116.281.710 TVA', // no prefix (CHE, ISO-3166-1 Alpha-3) + '116281710MWST', // no prefix and separators + '100.218.485', // no prefix and suffix + '123456788', // no prefix, separators and suffix + ], + invalid: [ + 'CH-116.281.710 MWST', // invalid prefix (should be CHE) + 'CHE-116.281 MWST', // invalid number of digits (should be 9) + 'CHE-123.456.789 MWST', // invalid last digit (should match the calculated check-number 8) + 'CHE-123.356.780 MWST', // invalid check-number (there are no swiss UIDs with the calculated check number 10) + 'CH-116.281.710 VAT', // invalid suffix (should be MWST, IVA or TVA) + 'CHE-116/281/710 IVA', // invalid number separators (should be all dots or all spaces) ], }); test({ @@ -14081,4 +14411,55 @@ describe('Validators', () => { ], }); }); + it('should validate mailto URI', () => { + test({ + validator: 'isMailtoURI', + valid: [ + 'mailto:?subject=something&cc=valid@mail.com', + 'mailto:?subject=something&cc=valid@mail.com,another@mail.com,', + 'mailto:?subject=something&bcc=valid@mail.com', + 'mailto:?subject=something&bcc=valid@mail.com,another@mail.com', + 'mailto:?bcc=valid@mail.com,another@mail.com', + 'mailto:?cc=valid@mail.com,another@mail.com', + 'mailto:?cc=valid@mail.com', + 'mailto:?bcc=valid@mail.com', + 'mailto:?subject=something&body=something else', + 'mailto:?subject=something&body=something else&cc=hello@mail.com,another@mail.com', + 'mailto:?subject=something&body=something else&bcc=hello@mail.com,another@mail.com', + 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com', + 'mailto:hello@mail.com', + 'mailto:info@mail.com?', + 'mailto:hey@mail.com?subject=something', + 'mailto:info@mail.com?subject=something&cc=valid@mail.com', + 'mailto:info@mail.com?subject=something&cc=valid@mail.com,another@mail.com,', + 'mailto:info@mail.com?subject=something&bcc=valid@mail.com', + 'mailto:info@mail.com?subject=something&bcc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?bcc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?cc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?cc=valid@mail.com', + 'mailto:info@mail.com?bcc=valid@mail.com&', + 'mailto:info@mail.com?subject=something&body=something else', + 'mailto:info@mail.com?subject=something&body=something else&cc=hello@mail.com,another@mail.com', + 'mailto:info@mail.com?subject=something&body=something else&bcc=hello@mail.com,another@mail.com', + 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com', + 'mailto:', + ], + invalid: [ + '', + 'somthing', + 'valid@gmail.com', + 'mailto:?subject=okay&subject=444', + 'mailto:?subject=something&wrong=888', + 'mailto:somename@gmail.com', + 'mailto:hello@world.com?cc=somename@gmail.com', + 'mailto:hello@world.com?bcc=somename@gmail.com', + 'mailto:hello@world.com?bcc=somename@gmail.com&bcc', + 'mailto:valid@gmail.com?subject=anything&body=nothing&cc=&bcc=&key=', + 'mailto:hello@world.com?cc=somename', + 'mailto:somename', + 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&', + 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&', + ], + }); + }); }); diff --git a/test/validators/isFQDN.test.js b/test/validators/isFQDN.test.js new file mode 100644 index 000000000..134bab005 --- /dev/null +++ b/test/validators/isFQDN.test.js @@ -0,0 +1,26 @@ +import test from '../testFunctions'; + +describe('isFQDN', () => { + it('should validate domain names.', () => { + test({ + validator: 'isFQDN', + args: [], + valid: [ + 'google.com', + ], + invalid: [ + 'google.l33t', + ], + }); + test({ + validator: 'isFQDN', + args: [{ allow_numeric_tld: true }], + valid: [ + 'google.com', + 'google.l33t', + ], + invalid: [ + ], + }); + }); +});