diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 000000000..1d346dec1 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,70 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '38 10 * * 4' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'javascript' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://git.io/codeql-language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/README.md b/README.md index a8d793ae2..782ee2f81 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ [![Downloads][downloads-image]][npm-url] [![Backers on Open Collective](https://opencollective.com/validatorjs/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/validatorjs/sponsors/badge.svg)](#sponsors) -[![Gitter](https://badges.gitter.im/validatorjs/community.svg)](https://gitter.im/validatorjs/community) +[![Gitter][gitter-image]][gitter-url] +[![Disclose a vulnerability][huntr-image]][huntr-url] A library of string validators and sanitizers. @@ -91,10 +92,10 @@ 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 [, date])** | check if the string is a date that's after the specified date (defaults to 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', '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', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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', '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', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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', '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', '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', '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', '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', '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', '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)** | check if a string is base32 encoded. +**isBase32(str [, options])** | check if a 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](http://www.crockford.com/base32.html). **isBase58(str)** | check if a string is base58 encoded. **isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) **isBefore(str [, date])** | check if the string is a date that's before the specified date. @@ -109,7 +110,7 @@ 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` 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', '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's divisible by another. **isEAN(str)** | check if the string is an EAN (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, 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. 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. +**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, 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. 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. **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](https://ethereum.org/) address using basic regex. 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` 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-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`. @@ -129,6 +130,7 @@ Validator | Description **isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6). **isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13). **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). +**isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. **isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. @@ -139,21 +141,22 @@ Validator | Description **isJWT(str)** | check if the string is valid JWT token. **isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.

`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format. **isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. -**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', pt-PT', 'sq-AL', 'pt-BR']` or `any`) +**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE', 'en-IN', 'hi-IN', 'gu-IN', 'as-IN', 'bn-IN', 'kn-IN', 'ml-IN', 'mr-IN', 'or-IN', 'pa-IN', 'sa-IN', 'ta-IN', 'te-IN', 'kok-IN']` or `any`) **isLocale(str)** | check if the string is a locale **isLowercase(str)** | check if the string is lowercase. +**isLuhnValid(str)** | check if the string passes the luhn check. **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](https://en.wikipedia.org/wiki/Magnet_URI_scheme). **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](https://en.wikipedia.org/wiki/Media_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-IQ', ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'en-AU', 'en-BM', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-KE', 'en-KI', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', 'my-MM', 'mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', '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', 'dz-BT', 'ar-YE', 'ar-EH', 'fa-AF']` 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-SY', 'ar-TN', 'ar-YE' , 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'en-AU', 'en-AG', 'en-BM', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-LS', 'en-KE', 'en-KI', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fa-AF', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF','fr-BJ', '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', 'ms-MY', 'mn-MN', 'my-MM' , 'mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL','nl-AW' 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', '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', 'dz-BT']` 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` 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', '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']`. **isOctal(str)** | check if the string is a valid octal number. -**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'ID', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`. +**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'ID', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`. **isPort(str)** | check if the string is a valid port number. -**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). +**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). **isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. **isRgbColor(str [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). @@ -161,7 +164,7 @@ Validator | Description **isUppercase(str)** | check if the string is uppercase. **isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`] **isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.
Default options:
`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }` -**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.

More info about exact TIN support can be found in `src/lib/isTaxID.js`

Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]` +**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.

More info about exact TIN support can be found in `src/lib/isTaxID.js`

Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]` **isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
require_port - if set as true isURL will check if port is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
allow_fragments - if set as false isURL will return false if fragments are present.
allow_query_components - if set as false isURL will return false if query components are present.
validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length). **isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5). **isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars. @@ -226,6 +229,8 @@ $ npm test - [chriso](https://github.com/chriso) - **Chris O'Hara** (author) - [profnandaa](https://github.com/profnandaa) - **Anthony Nandaa** +- [ezkemboi](https://github.com/ezkemboi) - **Ezrqn Kemboi** +- [tux-tn](https://github.com/tux-tn) - **Sarhan Aissi** ## Reading @@ -267,6 +272,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [ci-url]: https://github.com/validatorjs/validator.js/actions?query=workflow%3ACI [ci-image]: https://github.com/validatorjs/validator.js/workflows/CI/badge.svg?branch=master +[gitter-url]: https://gitter.im/validatorjs/community +[gitter-image]: https://badges.gitter.im/validatorjs/community.svg + +[huntr-url]: https://huntr.dev/bounties/disclose/?target=https://github.com/validatorjs/validator.js +[huntr-image]: https://cdn.huntr.dev/huntr_security_badge_mono.svg + [amd]: http://requirejs.org/docs/whyamd.html [bower]: http://bower.io/ diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..72592f135 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,11 @@ +# Security Policy + +## Supported Versions + +In the case of a confirmed security issue, only the current version of validator is guaranteed to be patched. + +## Reporting a Vulnerability + +**Please don't disclose security-related issues publicly.** + +If you discover a vulnerability within validator, please use [huntr.dev disclosure form](https://huntr.dev/bounties/disclose/?target=https://github.com/validatorjs/validator.js). We will try to validate and respond to reports in a reasonable time. if the issue is confirmed, we will create a security advisory and a patch as soon as possible. \ No newline at end of file diff --git a/src/index.js b/src/index.js index b8ad651ee..42e1e8b69 100644 --- a/src/index.js +++ b/src/index.js @@ -69,6 +69,7 @@ import isBefore from './lib/isBefore'; import isIn from './lib/isIn'; +import isLuhnValid from './lib/isLuhnValid'; import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; @@ -86,6 +87,7 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; +import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; import isISO31661Alpha2 from './lib/isISO31661Alpha2'; @@ -182,6 +184,7 @@ const validator = { isAfter, isBefore, isIn, + isLuhnValid, isCreditCard, isIdentityCard, isEAN, @@ -195,6 +198,7 @@ const validator = { isEthereumAddress, isCurrency, isBtcAddress, + isISO6391, isISO8601, isRFC3339, isISO31661Alpha2, diff --git a/src/lib/alpha.js b/src/lib/alpha.js index d663eded0..445ef85b0 100644 --- a/src/lib/alpha.js +++ b/src/lib/alpha.js @@ -32,6 +32,7 @@ export const alpha = { he: /^[א-ת]+$/, fa: /^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$/i, 'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i, + 'si-LK': /^[\u0D80-\u0DFF]+$/, }; export const alphanumeric = { @@ -67,6 +68,7 @@ export const alphanumeric = { he: /^[0-9א-ת]+$/, fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i, 'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i, + 'si-LK': /^[0-9\u0D80-\u0DFF]+$/, }; export const decimal = { @@ -112,7 +114,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', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', + 'ru-RU', '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/isBase32.js b/src/lib/isBase32.js index b2e9c8a28..5e2969cbc 100644 --- a/src/lib/isBase32.js +++ b/src/lib/isBase32.js @@ -1,9 +1,21 @@ import assertString from './util/assertString'; +import merge from './util/merge'; const base32 = /^[A-Z2-7]+=*$/; +const crockfordBase32 = /^[A-HJKMNP-TV-Z0-9]+$/; -export default function isBase32(str) { +const defaultBase32Options = { + crockford: false, +}; + +export default function isBase32(str, options) { assertString(str); + options = merge(options, defaultBase32Options); + + if (options.crockford) { + return crockfordBase32.test(str); + } + const len = str.length; if (len % 8 === 0 && base32.test(str)) { return true; diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index b11c4cc90..53c80eb80 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import isLuhnValid from './isLuhnValid'; /* eslint-disable max-len */ const creditCard = /^(?: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}))$/; @@ -10,24 +11,5 @@ export default function isCreditCard(str) { if (!creditCard.test(sanitized)) { return false; } - let sum = 0; - let digit; - let tmpNum; - let shouldDouble; - for (let i = sanitized.length - 1; i >= 0; i--) { - digit = sanitized.substring(i, (i + 1)); - tmpNum = parseInt(digit, 10); - if (shouldDouble) { - tmpNum *= 2; - if (tmpNum >= 10) { - sum += ((tmpNum % 10) + 1); - } else { - sum += tmpNum; - } - } else { - sum += tmpNum; - } - shouldDouble = !shouldDouble; - } - return !!((sum % 10) === 0 ? sanitized : false); + return isLuhnValid(str); } diff --git a/src/lib/isDataURI.js b/src/lib/isDataURI.js index 01e43f70c..0275d2cc8 100644 --- a/src/lib/isDataURI.js +++ b/src/lib/isDataURI.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const validMediaType = /^[a-z]+\/[a-z0-9\-\+]+$/i; +const validMediaType = /^[a-z]+\/[a-z0-9\-\+\.]+$/i; const validAttribute = /^[a-z\-]+=[a-z0-9\-]+$/i; @@ -14,10 +14,10 @@ export default function isDataURI(str) { } const attributes = data.shift().trim().split(';'); const schemeAndMediaType = attributes.shift(); - if (schemeAndMediaType.substr(0, 5) !== 'data:') { + if (schemeAndMediaType.slice(0, 5) !== 'data:') { return false; } - const mediaType = schemeAndMediaType.substr(5); + const mediaType = schemeAndMediaType.slice(5); if (mediaType !== '' && !validMediaType.test(mediaType)) { return false; } diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index f24f2dad2..c7b7d6e95 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -77,7 +77,7 @@ export default function isEmail(str, options) { // eg. myname // the display name is `myname` instead of `myname `, so need to trim the last space if (display_name.endsWith(' ')) { - display_name = display_name.substr(0, display_name.length - 1); + display_name = display_name.slice(0, -1); } if (!validateDisplayName(display_name)) { @@ -144,7 +144,7 @@ export default function isEmail(str, options) { return false; } - let noBracketdomain = domain.substr(1, domain.length - 2); + let noBracketdomain = domain.slice(1, -1); if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) { return false; diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index 4a04cf34e..884d1dd6f 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -32,7 +32,7 @@ export default function isFQDN(str, options) { return false; } - if (!/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) { + if (!options.allow_numeric_tld && !/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) { return false; } diff --git a/src/lib/isIP.js b/src/lib/isIP.js index 25856cae0..40ca19aec 100644 --- a/src/lib/isIP.js +++ b/src/lib/isIP.js @@ -51,14 +51,10 @@ export default function isIP(str, version = '') { return isIP(str, 4) || isIP(str, 6); } if (version === '4') { - if (!IPv4AddressRegExp.test(str)) { - return false; - } - const parts = str.split('.').sort((a, b) => a - b); - return parts[3] <= 255; + return IPv4AddressRegExp.test(str); } if (version === '6') { - return !!IPv6AddressRegExp.test(str); + return IPv6AddressRegExp.test(str); } return false; } diff --git a/src/lib/isISO6391.js b/src/lib/isISO6391.js new file mode 100644 index 000000000..eaa01c5b4 --- /dev/null +++ b/src/lib/isISO6391.js @@ -0,0 +1,35 @@ +import assertString from './util/assertString'; + +const isISO6391Set = new Set([ + 'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av', 'ay', 'az', 'az', + 'ba', 'be', 'bg', 'bh', 'bi', 'bm', 'bn', 'bo', 'br', 'bs', + 'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy', + 'da', 'de', 'dv', 'dz', + 'ee', 'el', 'en', 'eo', 'es', 'et', 'eu', + 'fa', 'ff', 'fi', 'fj', 'fo', 'fr', 'fy', + 'ga', 'gd', 'gl', 'gn', 'gu', 'gv', + 'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz', + 'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'io', 'is', 'it', 'iu', + 'ja', 'jv', + 'ka', 'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku', 'kv', 'kw', 'ky', + 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv', + 'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my', + 'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny', + 'oc', 'oj', 'om', 'or', 'os', + 'pa', 'pi', 'pl', 'ps', 'pt', + 'qu', + 'rm', 'rn', 'ro', 'ru', 'rw', + 'sa', 'sc', 'sd', 'se', 'sg', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw', + 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty', + 'ug', 'uk', 'ur', 'uz', + 've', 'vi', 'vo', + 'wa', 'wo', + 'xh', + 'yi', 'yo', + 'za', 'zh', 'zu', +]); + +export default function isISO6391(str) { + assertString(str); + return isISO6391Set.has(str); +} diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 9dc1302ad..d34ddae26 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -130,15 +130,15 @@ const validators = { }, IR: (str) => { if (!str.match(/^\d{10}$/)) return false; - str = (`0000${str}`).substr(str.length - 6); + str = (`0000${str}`).slice(str.length - 6); - if (parseInt(str.substr(3, 6), 10) === 0) return false; + if (parseInt(str.slice(3, 9), 10) === 0) return false; - const lastNumber = parseInt(str.substr(9, 1), 10); + const lastNumber = parseInt(str.slice(9, 10), 10); let sum = 0; for (let i = 0; i < 9; i++) { - sum += parseInt(str.substr(i, 1), 10) * (10 - i); + sum += parseInt(str.slice(i, i + 1), 10) * (10 - i); } sum %= 11; diff --git a/src/lib/isLength.js b/src/lib/isLength.js index 2e7c75c51..4ef8b83eb 100644 --- a/src/lib/isLength.js +++ b/src/lib/isLength.js @@ -12,7 +12,8 @@ export default function isLength(str, options) { min = arguments[1] || 0; max = arguments[2]; } + const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || []; const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || []; - const len = str.length - surrogatePairs.length; + const len = str.length - presentationSequences.length - surrogatePairs.length; return len >= min && (typeof max === 'undefined' || len <= max); } diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index d6b27a5ad..e370e5029 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -4,7 +4,7 @@ const validators = { 'cs-CZ': str => /^(([ABCDEFHKIJKLMNPRSTUVXYZ]|[0-9])-?){5,8}$/.test(str), 'de-DE': str => - /^((AW|UL|AK|GA|AÖ|LF|AZ|AM|AS|ZE|AN|AB|A|KG|KH|BA|EW|BZ|HY|KM|BT|HP|B|BC|BI|BO|FN|TT|ÜB|BN|AH|BS|FR|HB|ZZ|BB|BK|BÖ|OC|OK|CW|CE|C|CO|LH|CB|KW|LC|LN|DA|DI|DE|DH|SY|NÖ|DO|DD|DU|DN|D|EI|EA|EE|FI|EM|EL|EN|PF|ED|EF|ER|AU|ZP|E|ES|NT|EU|FL|FO|FT|F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A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str), 'pt-BR': str => /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), + 'sv-SE': str => + /^[A-HJ-PR-UW-Z]{3} ?[\d]{2}[A-HJ-PR-UW-Z1-9]$|(^[A-ZÅÄÖ ]{2,7}$)/.test(str.trim()), + 'en-IN': str => /^[A-Z]{2}[ -]?[0-9]{1,2}(?:[ -]?[A-Z])(?:[ -]?[A-Z]*)?[ -]?[0-9]{4}$/.test(str), }; +validators['hi-IN'] = validators['en-IN']; +validators['gu-IN'] = validators['en-IN']; +validators['as-IN'] = validators['en-IN']; +validators['bn-IN'] = validators['en-IN']; +validators['kn-IN'] = validators['en-IN']; +validators['ml-IN'] = validators['en-IN']; +validators['mr-IN'] = validators['en-IN']; +validators['or-IN'] = validators['en-IN']; +validators['pa-IN'] = validators['en-IN']; +validators['sa-IN'] = validators['en-IN']; +validators['ta-IN'] = validators['en-IN']; +validators['te-IN'] = validators['en-IN']; +validators['kok-IN'] = validators['en-IN']; + export default function isLicensePlate(str, locale) { assertString(str); if (locale in validators) { diff --git a/src/lib/isLuhnValid.js b/src/lib/isLuhnValid.js new file mode 100644 index 000000000..da205271f --- /dev/null +++ b/src/lib/isLuhnValid.js @@ -0,0 +1,26 @@ +import assertString from './util/assertString'; + +export default function isLuhnValid(str) { + assertString(str); + const sanitized = str.replace(/[- ]+/g, ''); + let sum = 0; + let digit; + let tmpNum; + let shouldDouble; + for (let i = sanitized.length - 1; i >= 0; i--) { + digit = sanitized.substring(i, (i + 1)); + tmpNum = parseInt(digit, 10); + if (shouldDouble) { + tmpNum *= 2; + if (tmpNum >= 10) { + sum += ((tmpNum % 10) + 1); + } else { + sum += tmpNum; + } + } else { + sum += tmpNum; + } + shouldDouble = !shouldDouble; + } + return !!((sum % 10) === 0 ? sanitized : false); +} diff --git a/src/lib/isMagnetURI.js b/src/lib/isMagnetURI.js index 45b5c8ebf..e00ee3c32 100644 --- a/src/lib/isMagnetURI.js +++ b/src/lib/isMagnetURI.js @@ -1,8 +1,13 @@ import assertString from './util/assertString'; -const magnetURI = /^magnet:\?xt(?:\.1)?=urn:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?($|&)/i; +const magnetURIComponent = /(?:^magnet:\?|[^?&]&)xt(?:\.1)?=urn:(?:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})(?:$|&)/i; export default function isMagnetURI(url) { assertString(url); - return magnetURI.test(url.trim()); + + if (url.indexOf('magnet:?') !== 0) { + return false; + } + + return magnetURIComponent.test(url); } diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index 574d2e970..c46e792c8 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -18,7 +18,7 @@ const phones = { 'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, 'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, 'ar-TN': /^(\+?216)?[2459]\d{7}$/, - 'az-AZ': /^(\+994|0)(5[015]|7[07]|99)\d{7}$/, + 'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, 'bs-BA': /^((((\+|00)3876)|06))((([0-3]|[5-6])\d{6})|(4\d{7}))$/, 'be-BY': /^(\+?375)?(24|25|29|33|44)\d{7}$/, 'bg-BG': /^(\+?359|0)?8[789]\d{7}$/, @@ -33,6 +33,7 @@ const phones = { 'dv-MV': /^(\+?960)?(7[2-9]|91|9[3-9])\d{7}$/, 'el-GR': /^(\+?30|0)?(69\d{8})$/, 'en-AU': /^(\+?61|0)4\d{8}$/, + 'en-AG': /^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/, 'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}))/, 'en-GB': /^(\+?44|0)7\d{9}$/, 'en-GG': /^(\+?44|0)1481\d{6}$/, @@ -44,11 +45,13 @@ const phones = { 'en-IN': /^(\+?91|0)?[6789]\d{9}$/, 'en-KE': /^(\+?254|0)(7|1)\d{8}$/, 'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, + 'en-LS': /^(\+?266)(22|28|57|58|59|27|52)\d{6}$/, 'en-MT': /^(\+?356|0)?(99|79|77|21|27|22|25)[0-9]{6}$/, 'en-MU': /^(\+?230|0)?\d{8}$/, 'en-NA': /^(\+?264|0)(6|8)\d{7}$/, 'en-NG': /^(\+?234|0)?[789]\d{9}$/, 'en-NZ': /^(\+?64|0)[28]\d{7,9}$/, + 'en-PG': /^(\+?675|0)?(7\d|8[18])\d{6}$/, 'en-PK': /^((00|\+)?92|0)3[0-6]\d{8}$/, 'en-PH': /^(09|\+639)\d{9}$/, 'en-RW': /^(\+?250|0)?[7]\d{8}$/, @@ -68,7 +71,7 @@ const phones = { 'es-CR': /^(\+506)?[2-8]\d{7}$/, 'es-CU': /^(\+53|0053)?5\d{7}/, 'es-DO': /^(\+?1)?8[024]9\d{7}$/, - 'es-HN': /^(\+?504)?[9|8]\d{7}$/, + 'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, 'es-ES': /^(\+?34)?[6|7]\d{8}$/, 'es-PE': /^(\+?51)?9\d{8}$/, @@ -80,10 +83,11 @@ const phones = { 'es-VE': /^(\+?58)?(2|4)\d{9}$/, 'et-EE': /^(\+?372)?\s?(5|8[1-4])\s?([0-9]\s?){6,7}$/, 'fa-IR': /^(\+?98[\-\s]?|0)9[0-39]\d[\-\s]?\d{3}[\-\s]?\d{4}$/, - 'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5|6)?|50)\s?(\d\s?){4,8}\d$/, + 'fi-FI': /^(\+?358|0)\s?(4[0-6]|50)\s?(\d\s?){4,8}$/, 'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/, 'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/, 'fr-BF': /^(\+226|0)[67]\d{7}$/, + 'fr-BJ': /^(\+229)\d{8}$/, 'fr-CM': /^(\+?237)6[0-9]{8}$/, 'fr-FR': /^(\+?33|0)[67]\d{8}$/, 'fr-GF': /^(\+?594|0|00594)[67]\d{8}$/, @@ -94,15 +98,19 @@ const phones = { '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})$/, + 'ir-IR': /^(\+98|0)?9\d{9}$/, 'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/, 'it-SM': /^((\+378)|(0549)|(\+390549)|(\+3780549))?6\d{5,9}$/, 'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/, - 'ka-GE': /^(\+?995)?(5|79)\d{7}$/, + 'ka-GE': /^(\+?995)?(79\d{7}|5\d{8})$/, 'kk-KZ': /^(\+?7|8)?7\d{9}$/, 'kl-GL': /^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$/, 'ko-KR': /^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$/, + 'ky-KG': /^(\+?7\s?\+?7|0)\s?\d{2}\s?\d{3}\s?\d{4}$/, 'lt-LT': /^(\+370|8)\d{8}$/, 'lv-LV': /^(\+?371)2\d{7}$/, + 'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, + 'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, 'my-MM': /^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, 'ms-MY': /^(\+?6?01){1}(([0145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/, 'mz-MZ': /^(\+?258)?8[234567]\d{7}$/, @@ -110,9 +118,10 @@ const phones = { 'ne-NP': /^(\+?977)?9[78]\d{8}$/, 'nl-BE': /^(\+?32|0)4\d{8}$/, '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}$/, - '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[2-9]{1}\d{3}\-?\d{4}))$/, + '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}$/, 'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/, diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js index 4c38bfbbe..c434a7078 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -46,6 +46,7 @@ const passportRegexByCountryCode = { MT: /^\d{7}$/, // MALTA MZ: /^([A-Z]{2}\d{7})|(\d{2}[A-Z]{2}\d{5})$/, // MOZAMBIQUE MY: /^[AHK]\d{8}$/, // MALAYSIA + MX: /^\d{10,11}$/, // MEXICO NL: /^[A-Z]{2}[A-Z0-9]{6}\d$/, // NETHERLANDS PL: /^[A-Z]{2}\d{7}$/, // POLAND PT: /^[A-Z]\d{6}$/, // PORTUGAL diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index 7ef17abcf..cf5b50d25 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -11,6 +11,7 @@ const patterns = { AT: fourDigit, AU: fourDigit, AZ: /^AZ\d{4}$/, + BA: /^([7-8]\d{4}$)/, BE: fourDigit, BG: fourDigit, BR: /^\d{5}-\d{3}$/, @@ -47,6 +48,7 @@ const patterns = { LU: fourDigit, LV: /^LV\-\d{4}$/, LK: fiveDigit, + MG: threeDigit, MX: fiveDigit, MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, MY: fiveDigit, diff --git a/src/lib/isTaxID.js b/src/lib/isTaxID.js index ee66ca326..933783f44 100644 --- a/src/lib/isTaxID.js +++ b/src/lib/isTaxID.js @@ -60,6 +60,36 @@ function bgBgCheck(tin) { return checksum === digits[9]; } +/** + * Check if an input is a valid Canadian SIN (Social Insurance Number) + * + * The Social Insurance Number (SIN) is a 9 digit number that + * you need to work in Canada or to have access to government programs and benefits. + * + * https://en.wikipedia.org/wiki/Social_Insurance_Number + * https://www.canada.ca/en/employment-social-development/services/sin.html + * https://www.codercrunch.com/challenge/819302488/sin-validator + * + * @param {string} input + * @return {boolean} + */ +function isCanadianSIN(input) { + const digitsArray = input.split(''); + const even = digitsArray + .filter((_, idx) => idx % 2) + .map(i => Number(i) * 2) + .join('') + .split(''); + + const total = digitsArray + .filter((_, idx) => !(idx % 2)) + .concat(even) + .map(i => Number(i)) + .reduce((acc, cur) => acc + cur); + + return (total % 10 === 0); +} + /* * cs-CZ validation function * (Rodné číslo (RČ), persons only) @@ -343,7 +373,7 @@ function enUsGetPrefixes() { * Verify that the TIN starts with a valid IRS campus prefix */ function enUsCheck(tin) { - return enUsGetPrefixes().indexOf(tin.substr(0, 2)) !== -1; + return enUsGetPrefixes().indexOf(tin.slice(0, 2)) !== -1; } /* @@ -1096,7 +1126,6 @@ function svSeCheck(tin) { * uppercase and lowercase letters are acceptable. */ const taxIdFormat = { - 'bg-BG': /^\d{10}$/, 'cs-CZ': /^\d{6}\/{0,1}\d{3,4}$/, 'de-AT': /^\d{9}$/, @@ -1104,6 +1133,7 @@ const taxIdFormat = { 'dk-DK': /^\d{6}-{0,1}\d{4}$/, 'el-CY': /^[09]\d{7}[A-Z]$/, 'el-GR': /^([0-4]|[7-9])\d{8}$/, + 'en-CA': /^\d{9}$/, 'en-GB': /^\d{10}$|^(?!GB|NK|TN|ZZ)(?![DFIQUV])[A-Z](?![DFIQUVO])[A-Z]\d{6}[ABCD ]$/i, 'en-IE': /^\d{7}[A-W][A-IW]{0,1}$/i, 'en-US': /^\d{2}[- ]{0,1}\d{7}$/, @@ -1126,16 +1156,15 @@ const taxIdFormat = { 'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/, 'sl-SI': /^[1-9]\d{7}$/, 'sv-SE': /^(\d{6}[-+]{0,1}\d{4}|(18|19|20)\d{6}[-+]{0,1}\d{4})$/, - }; // taxIdFormat locale aliases taxIdFormat['lb-LU'] = taxIdFormat['fr-LU']; taxIdFormat['lt-LT'] = taxIdFormat['et-EE']; taxIdFormat['nl-BE'] = taxIdFormat['fr-BE']; +taxIdFormat['fr-CA'] = taxIdFormat['en-CA']; // Algorithmic tax id check functions for various locales const taxIdCheck = { - 'bg-BG': bgBgCheck, 'cs-CZ': csCzCheck, 'de-AT': deAtCheck, @@ -1143,6 +1172,7 @@ const taxIdCheck = { 'dk-DK': dkDkCheck, 'el-CY': elCyCheck, 'el-GR': elGrCheck, + 'en-CA': isCanadianSIN, 'en-IE': enIeCheck, 'en-US': enUsCheck, 'es-ES': esEsCheck, @@ -1164,12 +1194,12 @@ const taxIdCheck = { 'sk-SK': skSkCheck, 'sl-SI': slSiCheck, 'sv-SE': svSeCheck, - }; // taxIdCheck locale aliases taxIdCheck['lb-LU'] = taxIdCheck['fr-LU']; taxIdCheck['lt-LT'] = taxIdCheck['et-EE']; taxIdCheck['nl-BE'] = taxIdCheck['fr-BE']; +taxIdCheck['fr-CA'] = taxIdCheck['en-CA']; // Regexes for locales where characters should be omitted before checking format const allsymbols = /[-\\\/!@#$%\^&\*\(\)\+\=\[\]]+/g; diff --git a/src/lib/isURL.js b/src/lib/isURL.js index aa6222a90..3d2b1df3e 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -87,11 +87,11 @@ export default function isURL(url, options) { } } else if (options.require_protocol) { return false; - } else if (url.substr(0, 2) === '//') { + } else if (url.slice(0, 2) === '//') { if (!options.allow_protocol_relative_urls) { return false; } - split[0] = url.substr(2); + split[0] = url.slice(2); } url = split.join('://'); @@ -152,6 +152,11 @@ export default function isURL(url, options) { if (options.host_whitelist) { return checkHost(host, options.host_whitelist); } + + if (host === '' && !options.require_host) { + return true; + } + if (!isIP(host) && !isFQDN(host, options) && (!ipv6 || !isIP(ipv6, 6))) { return false; } diff --git a/src/lib/matches.js b/src/lib/matches.js index 5b435e2af..9e23c2e46 100644 --- a/src/lib/matches.js +++ b/src/lib/matches.js @@ -5,5 +5,5 @@ export default function matches(str, pattern, modifiers) { if (Object.prototype.toString.call(pattern) !== '[object RegExp]') { pattern = new RegExp(pattern, modifiers); } - return pattern.test(str); + return !!str.match(pattern); } diff --git a/test/validators.js b/test/validators.js index 8b959c25f..0020612f5 100644 --- a/test/validators.js +++ b/test/validators.js @@ -472,6 +472,24 @@ describe('Validators', () => { }); }); + it('should validate postgres URLs without a host', () => { + test({ + validator: 'isURL', + args: [{ + protocols: ['postgres'], + require_host: false, + }], + valid: [ + 'postgres://user:pw@/test', + ], + invalid: [ + 'http://foobar.com', + 'postgres://', + ], + }); + }); + + it('should validate URLs with any protocol', () => { test({ validator: 'isURL', @@ -1291,7 +1309,18 @@ describe('Validators', () => { ], }); }); - + it('should validate FQDN with required allow_trailing_dot, allow_underscores and allow_numeric_tld options', () => { + test({ + validator: 'isFQDN', + args: [ + { allow_trailing_dot: true, allow_underscores: true, allow_numeric_tld: true }, + ], + valid: [ + 'abc.efg.g1h.', + 'as1s.sad3s.ssa2d.', + ], + }); + }); it('should validate alpha strings', () => { test({ validator: 'isAlpha', @@ -1920,6 +1949,26 @@ describe('Validators', () => { }); }); + it('should validate Sinhala alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['si-LK'], + valid: [ + 'චතුර', + 'කචටදබ', + 'ඎඏදාෛපසුගො', + ], + invalid: [ + 'ஆஐअतක', + 'කචට 12', + ' ඎ ', + 'abc1', + 'abc', + '', + ], + }); + }); + it('should error on invalid locale', () => { test({ validator: 'isAlpha', @@ -2507,6 +2556,27 @@ describe('Validators', () => { }); }); + it('should validate Sinhala alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['si-LK'], + valid: [ + 'චතුර', + 'කචට12', + 'ඎඏදාෛපසුගො2', + '1234', + ], + invalid: [ + 'ஆஐअतක', + 'කචට 12', + ' ඎ ', + 'a1234', + 'abc', + '', + ], + }); + }); + it('should error on invalid locale', () => { test({ validator: 'isAlphanumeric', @@ -3145,6 +3215,19 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['MX'], + valid: [ + '43986369222', + '01234567890', + ], + invalid: [ + 'ABC34567890', + '34567890', + ], + }); + test({ validator: 'isPassportNumber', args: ['NL'], @@ -4586,6 +4669,11 @@ describe('Validators', () => { validator: 'isLength', valid: ['a', '', 'asds'], }); + test({ + validator: 'isLength', + args: [{ max: 8 }], + valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], + }); }); it('should validate strings by byte length', () => { @@ -4931,6 +5019,34 @@ describe('Validators', () => { }); }); + it('should validate luhn numbers', () => { + test({ + validator: 'isLuhnValid', + valid: [ + '0', + '5421', + '01234567897', + '0123456789012345678906', + '0123456789012345678901234567891', + '123456789012345678906', + '375556917985515', + '36050234196908', + '4716461583322103', + '4716-2210-5188-5662', + '4929 7226 5379 7141', + ], + invalid: [ + '', + '1', + '5422', + 'foo', + 'prefix6234917882863855', + '623491788middle2863855', + '6234917882863855suffix', + ], + }); + }); + it('should validate credit cards', () => { test({ validator: 'isCreditCard', @@ -5753,6 +5869,25 @@ describe('Validators', () => { }); }); + it('should validate base32 strings with crockford alternative', () => { + test({ + validator: 'isBase32', + args: [{ crockford: true }], + valid: [ + '91JPRV3F41BPYWKCCGGG', + '60', + '64', + 'B5QQA833C5Q20S3F41MQ8', + ], + invalid: [ + '91JPRV3F41BUPYWKCCGGG', + 'B5QQA833C5Q20S3F41MQ8L', + '60I', + 'B5QQA833OULIC5Q20S3F41MQ8', + ], + }); + }); + it('should validate base58 strings', () => { test({ validator: 'isBase58', @@ -6457,6 +6592,12 @@ describe('Validators', () => { '(22) 999567894', '(22) 99956-7894', '(11) 94123-4567', + '(11) 91431-4567', + '+55 (11) 91431-4567', + '+55 11 91431-4567', + '+551191431-4567', + '5511914314567', + '5511912345678', ], invalid: [ '0819876543', @@ -6465,12 +6606,12 @@ describe('Validators', () => { '5501599623874', '+55012962308', '+55 015 1234-3214', - '+55 11 91431-4567', - '+55 (11) 91431-4567', - '+551191431-4567', - '5511914314567', - '5511912345678', - '(11) 91431-4567', + '+55 11 90431-4567', + '+55 (11) 90431-4567', + '+551190431-4567', + '5511904314567', + '5511902345678', + '(11) 90431-4567', ], }, { @@ -6550,6 +6691,25 @@ describe('Validators', () => { '0-987123456', ], }, + { + local: 'en-LS', + valid: [ + '+26622123456', + '+26628123456', + '+26657123456', + '+26658123456', + '+26659123456', + '+26627123456', + '+26652123456', + ], + invalid: [ + '+26612345678', + '', + '2664512-21', + '+2662212345678', + 'someString', + ], + }, { locale: 'en-BM', valid: [ @@ -6933,6 +7093,24 @@ describe('Validators', () => { '+22634523', ], }, + { + locale: 'fr-BJ', + valid: [ + '+22920215789', + '+22920293092', + '+22921307898', + '+22921736346', + '+22922416346', + '+22923836346', + ], + invalid: [ + '0612457892', + '01122921737346', + '+22762457898', + '+226724578980', + '+22634523', + ], + }, { locale: 'fr-CA', valid: ['19876543210', '8005552222', '+15673628910'], @@ -7069,18 +7247,19 @@ describe('Validators', () => { { locale: 'ka-GE', valid: [ - '+99550001111', - '+99551535213', + '+995500011111', + '+995515352134', '+995798526662', '798526662', - '50001111', + '500011119', '798526662', '+995799766525', ], invalid: [ - '+995500011118', + '+99550001111', '+9957997665250', - '+995999766525', + '+9959997665251', + '+995780011111', '20000000000', '68129485729', '6589394827', @@ -7585,6 +7764,10 @@ describe('Validators', () => { '+50489234567', '+50488987896', '+50497567389', + '+50427367389', + '+50422357389', + '+50431257389', + '+50430157389', ], invalid: [ '12345', @@ -7835,6 +8018,9 @@ describe('Validators', () => { '0457 123 45 67', '+358457 123 45 67', '+358 50 555 7171', + '0501234', + '+358501234', + '050 1234', ], invalid: [ '12345', @@ -7946,6 +8132,20 @@ describe('Validators', () => { '+820 11 7766 1234', ], }, + { + locale: 'ky-KG', + valid: [ + '+7 727 123 4567', + '+7 714 2396102', + '77271234567', + '0271234567', + ], + invalid: [ + '02188565377', + '09386932778', + '0938693277vadggjdsaasdgj8', + ], + }, { locale: 'ja-JP', valid: [ @@ -7985,6 +8185,22 @@ describe('Validators', () => { '90 1234 5678', ], }, + { + locale: 'ir-IR', + valid: [ + '09023818688', + '09123809999', + '+989023818688', + '+989103923523', + ], + invalid: [ + '19023818688', + '323254', + '+903232323257', + '++3567868', + '0902381888832', + ], + }, { locale: 'it-IT', valid: [ @@ -8078,6 +8294,35 @@ describe('Validators', () => { '310212345678', ], }, + { + locale: 'nl-AW', + valid: [ + '2975612345', + '2976412345', + '+2975612345', + '+2975912345', + '+2976412345', + '+2977312345', + '+2977412345', + '+2979912345', + ], + invalid: [ + '12345', + '+2972345', + '2972345', + '06701234567', + '012345678', + '+2974701234567', + '2974701234567', + '0297345678', + '029734567', + '+2971234567', + '2971234567', + '+297212345678', + '297212345678', + 'number', + ], + }, { locale: 'ro-RO', valid: [ @@ -8461,8 +8706,10 @@ describe('Validators', () => { '+994502111111', '0505436743', '0554328772', + '0104328772', '0993301022', '+994776007139', + '+994106007139', ], invalid: [ 'wrong-number', @@ -8564,6 +8811,46 @@ describe('Validators', () => { 'NotANumber', ], }, + { + locale: 'mg-MG', + valid: [ + '+261204269174', + '261204269174', + '0204269174', + '0209269174', + '0374269174', + '4269174', + ], + invalid: [ + '0261204269174', + '+261 20 4 269174', + '+261 20 4269174', + '020 4269174', + '204269174', + '0404269174', + 'NotANumber', + ], + }, + { + locale: 'mn-MN', + valid: [ + '+97699112222', + '97696112222', + '97695112222', + '01197691112222', + '0097688112222', + '+97677112222', + '+97694112222', + '+97681112222', + ], + invalid: [ + '+97888112222', + '+97977112222', + '+97094112222', + '+97281112222', + '02297681112222', + ], + }, { locale: 'my-MM', valid: [ @@ -8594,6 +8881,55 @@ describe('Validators', () => { '09650000234', ], }, + { + locale: 'en-PG', + valid: [ + '+67570123456', + '67570123456', + '+67571123456', + '+67572123456', + '+67573123456', + '+67574123456', + '+67575123456', + '+67576123456', + '+67577123456', + '+67578123456', + '+67579123456', + '+67581123456', + '+67588123456', + ], + invalid: [ + '', + 'not a number', + '12345', + '+675123456789', + '+67580123456', + '+67569123456', + '+67582123456', + '+6757012345', + ], + }, + { + locale: 'en-AG', + valid: [ + '12687151234', + '+12687151234', + '+12684641234', + '12684641234', + '+12687211234', + '+12687302468', + '+12687642456', + '+12687763333', + ], + invalid: [ + '2687151234', + '+12687773333', + '+126846412333', + '+12684641', + '+12687123456', + '+12687633456', + ], + }, { locale: 'en-PK', valid: [ @@ -9833,6 +10169,14 @@ describe('Validators', () => { }); }); + it('should validate ISO 639-1 language codes', () => { + test({ + validator: 'isISO6391', + valid: ['ay', 'az', 'ba', 'be', 'bg'], + invalid: ['aj', 'al', 'pe', 'pf', 'abc', '123', ''], + }); + }); + const validISO8601 = [ '2009-12T12:34', '2009', @@ -10211,6 +10555,7 @@ describe('Validators', () => { ' data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E', 'data:,A%20brief%20note', 'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E', + 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,dGVzdC5kb2N4', ], invalid: [ 'dataxbase64', @@ -10241,6 +10586,8 @@ describe('Validators', () => { 'magnet:?xt=urn:md5:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', 'magnet:?xt=urn:tree:tiger:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', 'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', + 'magnet:?tr=udp://helloworld:1337/announce&xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', + 'magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', ], invalid: [ ':?xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', @@ -10253,6 +10600,8 @@ describe('Validators', () => { 'magnet:?xt:urn:nonexisting:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', 'magnet:?xt.2=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', 'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234567890123456789ABCD', + 'magnet:?xt=urn:btmh:1120caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', + 'magnet:?ttxt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', ], }); /* eslint-enable max-len */ @@ -10372,6 +10721,23 @@ describe('Validators', () => { 'A1A 1A1', 'X0A-0H0', 'V5K 0A1', + 'A1C 3S4', + 'A1C3S4', + 'a1c 3s4', + 'V9A 7N2', + 'B3K 5X5', + 'K8N 5W6', + 'K1A 0B1', + 'B1Z 0B9', + ], + invalid: [ + ' ', + 'invalid value', + 'a1a1a', + 'A1A 1A1', + 'K1A 0D1', + 'W1A 0B1', + 'Z1A 0B1', ], }, { @@ -10577,6 +10943,8 @@ describe('Validators', () => { '78-399', '39-490', '38-483', + '05-800', + '54-060', ], }, { @@ -10635,6 +11003,9 @@ describe('Validators', () => { '65000', '65080', '01000', + '51901', + '51909', + '49125', ], }, { @@ -10663,6 +11034,15 @@ describe('Validators', () => { '4144', ], }, + { + locale: 'MG', + valid: [ + '101', + '303', + '407', + '512', + ], + }, { locale: 'MT', valid: [ @@ -10784,6 +11164,23 @@ describe('Validators', () => { '982', ], }, + { + locale: 'BA', + valid: [ + '76300', + '71000', + '75412', + '76100', + '88202', + '88313', + ], + invalid: [ + '1234', + '789389', + '98212', + '11000', + ], + }, ]; let allValid = []; @@ -11010,6 +11407,43 @@ describe('Validators', () => { '658426713', '558426713'], }); + test({ + validator: 'isTaxID', + args: ['en-CA'], + valid: [ + '000000000', + '521719666', + '469317481', + '120217450', + '480534858', + '325268597', + '336475660', + '744797853', + '130692544', + '046454286', + ], + invalid: [ + ' ', + 'any value', + '012345678', + '111111111', + '999999999', + '657449110', + '74 47 978 53', + '744 797 853', + '744-797-853', + '981062432', + '267500713', + '2675o0713', + '70597312', + '7058973122', + '069437151', + '046454281', + '146452286', + '30x92544', + '30692544', + ], + }); test({ validator: 'isTaxID', args: ['en-GB'], @@ -11702,6 +12136,16 @@ describe('Validators', () => { 'FS AB 1234 E', 'FSAB1234E', 'FS-AB-1234-E', + 'FS AB-1234-E', + 'FSAB1234 E', + 'FS AB1234E', + 'LRO AB 123', + 'LRO-AB-123-E', + 'LRO-AB-123E', + 'LRO-AB-123 E', + 'LRO-AB-123-H', + 'LRO-AB-123H', + 'LRO-AB-123 H', ], invalid: [ 'YY AB 123', @@ -11709,6 +12153,14 @@ describe('Validators', () => { 'M ABC 123', 'M AB 12345', 'FS AB 1234 A', + 'LRO-AB-1234', + 'HRO ABC 123', + 'HRO ABC 1234', + 'LDK-AB-1234-E', + 'ÖHR FA 123D', + 'MZG-AB-123X', + 'OBG-ABD-123', + 'PAF-AB2-123', ], }); test({ @@ -11827,6 +12279,58 @@ describe('Validators', () => { 'FS AB 1234 A', ], }); + test({ + validator: 'isLicensePlate', + args: ['sv-SE'], + valid: [ + 'ABC 123', + 'ABC 12A', + 'ABC123', + 'ABC12A', + 'A WORD', + 'WORD', + 'ÅSNA', + 'EN VARG', + 'CERISE', + 'AA', + 'ABCDEFG', + 'ÅÄÖ', + 'ÅÄÖ ÅÄÖ', + ], + invalid: [ + '', + ' ', + 'IQV 123', + 'IQV123', + 'ABI 12Q', + 'ÅÄÖ 123', + 'ÅÄÖ 12A', + 'AB1 A23', + 'AB1 12A', + 'lower', + 'abc 123', + 'abc 12A', + 'abc 12a', + 'AbC 12a', + 'WORDLONGERTHANSEVENCHARACTERS', + 'A', + 'ABC-123', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['en-IN'], + valid: [ + 'MH 04 AD 0001', + 'HR26DQ0001', + 'WB-04-ZU-2001', + 'KL 18 X 5800', + 'DL 4 CAF 4856', + 'KA-41CE-5289', + 'GJ 04-AD 5822', + ], + invalid: ['mh04ad0045', 'invalidlicenseplate', '4578', '', 'GJ054GH4785'], + }); }); it('should validate VAT numbers', () => { test({