diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 000000000..2c6af7763 --- /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@v2 + 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@v2 + + # ℹ️ 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@v2 diff --git a/.gitignore b/.gitignore index 9c8fa47bf..86aaedee1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ yarn.lock /index.js validator.js validator.min.js + diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b063ff2a..023aeac3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,116 @@ -## 13.7.0 +# 13.9.0 -### New Features +### New Features / Validators -- [#1706](https://github.com/validatorjs/validator.js/pull/1706) `isISO4217`, currency code validator @jpaya17 +- [#1892](https://github.com/validatorjs/validator.js/pull/1892) `isISO6391`: add ISO 639-1 validator @braaar +- [#1974](https://github.com/validatorjs/validator.js/pull/1974) `isLuhnNumber` @ST-DDT + +### Fixes and Enhancements + +- [#1865](https://github.com/validatorjs/validator.js/pull/1865) `isMACAddress`: add EUI-validation @WikiRik @tux-tn +- [#1888](https://github.com/validatorjs/validator.js/pull/1888) `isBase32`: add option for Crockford's base32 alternative @BigOsvaap +- [#1916](https://github.com/validatorjs/validator.js/pull/1916) `isDataURI`: fix mediaType format @temoffey +- [#1920](https://github.com/validatorjs/validator.js/pull/1920) `isEmail`: add `host_whitelist` option @poor-coder +- [#1939](https://github.com/validatorjs/validator.js/pull/1939) `isFQDN`: fix `allow_numeric_tld` option @BigOsvaap +- [#1962](https://github.com/validatorjs/validator.js/pull/1962) `isIP`: refactor @UnKnoWn-Consortium +- [#1967](https://github.com/validatorjs/validator.js/pull/1967) `isLength` @ikkyu-3 +- [#1992](https://github.com/validatorjs/validator.js/pull/1992) `isMagnetURI` @Rhilip @tux-tn +- [#1995](https://github.com/validatorjs/validator.js/pull/1995) `isURL`: fix check for host @mortbauer +- [#2008](https://github.com/validatorjs/validator.js/pull/2008) `isCreditCard` @brianwhaley +- [#2075](https://github.com/validatorjs/validator.js/pull/2075) `isAfter`: allow usage of option object @WikiRik +- [#2114](https://github.com/validatorjs/validator.js/pull/2114) `isRgbColor` @pano9000 +- [#2122](https://github.com/validatorjs/validator.js/pull/2122) `isDataURI`: fix MIME types with underscores @pano9000 +- [#2148](https://github.com/validatorjs/validator.js/pull/2148) `isStrongPassword` @sandmule +- [#2157](https://github.com/validatorjs/validator.js/pull/2157) `isISBN`: allow usage of option object @WikiRik +- [#2170](https://github.com/validatorjs/validator.js/pull/2170) `isEmail`: fix `ignore_max_length` for FQDN @sakhmedbayev +- [#2020](https://github.com/validatorjs/validator.js/pull/2170) `isFloat`: fix comma(,) passing as float @frederike-ramin + +- Documentation fixes: + - [#1860](https://github.com/validatorjs/validator.js/pull/1860) @leonardovillela + - [#1861](https://github.com/validatorjs/validator.js/pull/1860) @tux-tn + - [#1957](https://github.com/validatorjs/validator.js/pull/1957) @tfilo + - [#2010](https://github.com/validatorjs/validator.js/pull/2010) @marcelozarate + - [#2107](https://github.com/validatorjs/validator.js/pull/2107) @pano9000 + - [#2160](https://github.com/validatorjs/validator.js/pull/2160) @WikiRik + +- Code Refactors: + - [#1942](https://github.com/validatorjs/validator.js/pull/1942) @CommanderRoot + - [#1975](https://github.com/validatorjs/validator.js/pull/1975) @fedeci + - [#2137](https://github.com/validatorjs/validator.js/pull/2137) [#2132](https://github.com/validatorjs/validator.js/pull/2132) @pano9000 + +### New and Improved Locales + +- `isAlpha`, `isAlphanumeric`: + - [#1678](https://github.com/validatorjs/validator.js/pull/1678) `bn-BD` @rak810 + - [#1996](https://github.com/validatorjs/validator.js/pull/1996) `si-LK` @melkorCBA + - [#2014](https://github.com/validatorjs/validator.js/pull/2014) `ja-JP` @starcharles + - [#1995](https://github.com/validatorjs/validator.js/pull/1995) `ko-KR` @Dongkyuuuu + +- `isBIC`: + - [#2046](https://github.com/validatorjs/validator.js/pull/2046) `XK` @import-brain + +- `isIdentityCard`: + - [#2142](https://github.com/validatorjs/validator.js/pull/2142) `hk-HK` @Dongkyuuuu + +- `isMobilePhone`: + - [#1813](https://github.com/validatorjs/validator.js/pull/1813) `my-MM`, @ferdousulhaque + - [#1868](https://github.com/validatorjs/validator.js/pull/1868) `de-DE`, @thomaschaaf + - [#1896](https://github.com/validatorjs/validator.js/pull/1896) `en-LS`, @DevilsAutumn + - [#1897](https://github.com/validatorjs/validator.js/pull/1897) `el-CY`, @ikerasiotis + - [#1909](https://github.com/validatorjs/validator.js/pull/1909) `es-NI`, @ajGingrich + - [#1910](https://github.com/validatorjs/validator.js/pull/1910) `az-AZ`, @shaanaliyev + - [#1922](https://github.com/validatorjs/validator.js/pull/1922) `ir-IR`, @ArashST79 + - [#1924](https://github.com/validatorjs/validator.js/pull/1924) `ky-KG`, @arsalanfiroozi + - [#1925](https://github.com/validatorjs/validator.js/pull/1925) `ar-YE`, `ar-EH`, `fa-AF`, @Mustafiz04 + - [#1932](https://github.com/validatorjs/validator.js/pull/1932) `ro-MD`, @mik7up + - [#1940](https://github.com/validatorjs/validator.js/pull/1940) `ar-YE`, `en-BS`, @savannahvaith + - [#1952](https://github.com/validatorjs/validator.js/pull/1952) `ka-GE`, @avkvak + - [#1964](https://github.com/validatorjs/validator.js/pull/1964) [#1951](https://github.com/validatorjs/validator.js/pull/1951) `pt-BR`, @jhcaiafa @matheusnascgomes + - [#1983](https://github.com/validatorjs/validator.js/pull/1983) `es-HN`, @ademyan05 + - [#1985](https://github.com/validatorjs/validator.js/pull/1985) `nl-AW`, @adida948 + - [#1986](https://github.com/validatorjs/validator.js/pull/1986) `en-JM`, @ademyan05 + - [#1993](https://github.com/validatorjs/validator.js/pull/1993) `mn-MN`, @rksp25 + - [#1997](https://github.com/validatorjs/validator.js/pull/1997) `fr-BJ`, @rkuma552 @rksp25 + - [#2001](https://github.com/validatorjs/validator.js/pull/2001) `mg-MG`, @ShivangiRai1310 + - [#2002](https://github.com/validatorjs/validator.js/pull/2002) `en-PG`, @kai2128 + - [#2004](https://github.com/validatorjs/validator.js/pull/2004) `en-AG`, @jiaweilow + - [#2007](https://github.com/validatorjs/validator.js/pull/2007) `en-AI`, @elaine1129 + - [#2011](https://github.com/validatorjs/validator.js/pull/2011) `en-KN`, @Eelyneee + - [#2041](https://github.com/validatorjs/validator.js/pull/2041) `fr-CD`, @coolbeatz71 + - [#2084](https://github.com/validatorjs/validator.js/pull/2084) `en-SS`, @cheboi + - [#2109](https://github.com/validatorjs/validator.js/pull/2109) `dv-MV`, @pano9000 + - [#2129](https://github.com/validatorjs/validator.js/pull/2129) `en-HN`, @WikiRik + - [#2148](https://github.com/validatorjs/validator.js/pull/2148) `ar-KW`, @Yazan-KE @WikiRik + - [#2112](https://github.com/validatorjs/validator.js/pull/2112) `el-GR`, @pano9000 + - [#2116](https://github.com/validatorjs/validator.js/pull/2116) `en-BM`, @pano9000 + - [#2155](https://github.com/validatorjs/validator.js/pull/2155) `ms-MY`, @pano9000 + - [#2156](https://github.com/validatorjs/validator.js/pull/2156) `ro-RO`, @pano9000 + +- `isLicensePlate`: + - [#1665](https://github.com/validatorjs/validator.js/pull/1665) `sv-SE`, @elmaxe + - [#1895](https://github.com/validatorjs/validator.js/pull/1895) `hu-HU`, @szabolcstarnai + - [#1944](https://github.com/validatorjs/validator.js/pull/1944) `en-NI`, @NishantJS + - [#1945](https://github.com/validatorjs/validator.js/pull/1945) `de-DE`, @bennetfabian + - [#1945](https://github.com/validatorjs/validator.js/pull/1945) `de-DE`, @bennetfabian + - [#2103](https://github.com/validatorjs/validator.js/pull/2103) `es-AR`, @alvarocastro + +- `isPassportNumber`: + - [#1515](https://github.com/validatorjs/validator.js/pull/1515) `JM`,`KZ`,`LI`,`NZ` @JuanFML + - [#1814](https://github.com/validatorjs/validator.js/pull/1814) `TH` @TonPC64 @braaar + - [#2061](https://github.com/validatorjs/validator.js/pull/2061) `AZ` @djeks922 + - [#2073](https://github.com/validatorjs/validator.js/pull/2073) `PH`,`PK` @digambar-t7 + +- `isPostalCode`: + - [#1951](https://github.com/validatorjs/validator.js/pull/1951) `BA`, @matheusnascgomes + - [#2134](https://github.com/validatorjs/validator.js/pull/2134) `BY`, @pano9000 + - [#2136](https://github.com/validatorjs/validator.js/pull/2136) `IR`, @pano9000 + + +- `isTaxID`: + - [#1867](https://github.com/validatorjs/validator.js/pull/1867) `en-CA`, @boonya + - [#1989](https://github.com/validatorjs/validator.js/pull/1989) `'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HU', 'IE', 'LV', 'LT', 'LU', 'MT', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'AL', 'MK', 'AU', 'BY', 'CA', 'IS', 'IN', 'ID', 'IL', 'KZ', 'NZ', 'NG', 'NO', 'PH', 'RU', 'SM', 'SA', 'RS', 'CH', 'TR', 'UA', 'UZ', 'AR', 'BO', 'BR', 'CL', 'CO', 'CR', 'EC', 'SV', 'GT', 'HN', 'MX', 'NI', 'PA', 'PY', 'PE', 'DO', 'UY', 'VE'` @Dev1lDragon + +## 13.7.0 ### New Features diff --git a/README.md b/README.md index 2cf6505be..e4d1d7f4e 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # validator.js - [![NPM version][npm-image]][npm-url] [![CI][ci-image]][ci-url] [![Coverage][codecov-image]][codecov-url] [![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. @@ -88,86 +88,89 @@ Here is a list of the validators currently available. 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. +**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. +**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. **isAscii(str)** | check if the string contains ASCII chars only. -**isBase32(str)** | check if a string is base32 encoded. -**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. -**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. -**isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). +**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. +**isBase64(str [, options])** | check if the 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][Base64 URL Safe]. +**isBefore(str [, date])** | check if the string is a date that is before the specified date. +**isBIC(str)** | check if the string is a BIC (Bank Identification Code) or SWIFT code. +**isBoolean(str [, options])** | check if the string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If `loose` is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If `loose` is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (e.g.: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. -**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. -**isCreditCard(str)** | check if the string is a credit card. -**isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. -**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). -**isDate(input [, options])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. -**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. -**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`. -**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). +**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{ min: 0, max: undefined }`. +**isCreditCard(str [, options])** | check if the string is a credit card number.

`options` is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `['amex', 'dinersclub', 'discover', 'jcb', 'mastercard', 'unionpay', 'visa']` or blank will check for any provider. +**isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{ symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false }`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. +**isDataURI(str)** | check if the string is a [data uri format][Data URI Format]. +**isDate(str [, options])** | check if the string is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`.

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject strings different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. +**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. +**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`). **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 `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']` +**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](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).

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 a string is a IBAN (International Bank Account Number). -**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']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. -**isIMEI(str [, options]))** | check if the string is a valid IMEI number. 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 a array of allowed values. +**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). +**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. **isInt(str [, options])** | check if the string is an integer.

`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). **isIP(str [, version])** | check if the string is an IP (version 4 or 6). **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). +**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). -**isISO8601(str)** | 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. -**isISO4217(str)** | check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code. -**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code). -**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. +**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. +**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3][ISO 3166-1 alpha-3] officially assigned country code. +**isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code. +**isISRC(str)** | check if the string is an [ISRC][ISRC]. +**isISSN(str [, options])** | check if the string is an [ISSN][ISSN].

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. **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`) -**isLocale(str)** | check if the string is a locale +**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 the string matches the format of a country's license plate.

`locale` is one of `['cs-CZ', 'de-DE', 'de-LI', 'en-IN', 'es-AR', 'hu-HU', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `'any'`. +**isLocale(str)** | check if the string is a locale. **isLowercase(str)** | check if the string is lowercase. -**isMACAddress(str)** | 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'. -**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). +**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]. **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', ''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']` 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`. +**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`. **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']`. +**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']`. **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', 'AZ', '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', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', '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`.). -**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. +**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 locales match. Locale list is `validator.isPostalCodeLocales`. +**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] 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). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **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' ]` -**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). +**isSlug(str)** | check if the string is of type slug. +**isStrongPassword(str [, options])** | check if the string can be considered a strong password 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 }` +**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].

`options` is an object which can contain the keys `hourFormat` or `mode`.

`hourFormat` is a key and defaults to `'hour24'`.

`mode` is a key and defaults to `'default'`.

`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format.

`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format. +**isTaxID(str, locale)** | check if the string 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 a 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 to 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 to false isURL will not check if host is present in the URL.
`require_port` - if set to true isURL will check if port is present in the URL.
`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.
`allow_fragments` - if set to false isURL will return false if fragments are present.
`allow_query_components` - if set to false isURL will return false if query components are present.
`validate_length` - if set to 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. -**isVAT(str, countryCode)** | checks that the string is a [valid VAT number](https://en.wikipedia.org/wiki/VAT_identification_number) if validation is available for the given country code matching [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).

Available country codes: `[ 'GB', 'IT','NL' ]`. -**isWhitelisted(str, chars)** | checks characters if they appear in the whitelist. -**matches(str, pattern [, modifiers])** | check if string matches the pattern.

Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. +**isVAT(str, countryCode)** | check if the string is a [valid VAT number][VAT Number] if validation is available for the given country code matching [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2].

`countryCode` is one of `['AL', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GT', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'KZ', 'LT', 'LU', 'LV', 'MK', 'MT', 'MX', 'NG', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'RS', 'RU', 'SA', 'SE', 'SI', 'SK', 'SM', 'SV', 'TR', 'UA', 'UY', 'UZ', 'VE']`. +**isWhitelisted(str, chars)** | check if the string consists only of characters that appear in the whitelist `chars`. +**matches(str, pattern [, modifiers])** | check if the string matches the pattern.

Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. ## Sanitizers @@ -178,7 +181,7 @@ Sanitizer | Description **blacklist(input, chars)** | remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`. **escape(input)** | replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities. **ltrim(input [, chars])** | trim characters from the left-side of the input. -**normalizeEmail(email [, options])** | canonicalizes an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand)

`options` is an object with the following keys and default values:
  • *all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it's case insensitive per RFC 1035.
  • *gmail_lowercase: true* - GMail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, GMail addresses are lowercased regardless of the value of this setting.
  • *gmail_remove_dots: true*: Removes dots from the local part of the email address, as GMail ignores them (e.g. "john.doe" and "johndoe" are considered equal).
  • *gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@gmail.com" becomes "foo@gmail.com").
  • *gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.
  • *outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.
  • *outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@outlook.com" becomes "foo@outlook.com").
  • *yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.
  • *yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "foo-bar@yahoo.com" becomes "foo@yahoo.com").
  • *icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.
  • *icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@icloud.com" becomes "foo@icloud.com").
+**normalizeEmail(email [, options])** | canonicalize an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand).

`options` is an object with the following keys and default values:
  • *all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it is case insensitive per RFC 1035.
  • *gmail_lowercase: true* - Gmail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Gmail addresses are lowercased regardless of the value of this setting.
  • *gmail_remove_dots: true*: Removes dots from the local part of the email address, as Gmail ignores them (e.g. "john.doe" and "johndoe" are considered equal).
  • *gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@gmail.com" becomes "foo@gmail.com").
  • *gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.
  • *outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.
  • *outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@outlook.com" becomes "foo@outlook.com").
  • *yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.
  • *yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "foo-bar@yahoo.com" becomes "foo@yahoo.com").
  • *icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.
  • *icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@icloud.com" becomes "foo@icloud.com").
**rtrim(input [, chars])** | trim characters from the right-side of the input. **stripLow(input [, keep_new_lines])** | remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript. **toBoolean(input [, strict])** | convert the input string to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`. @@ -186,7 +189,7 @@ Sanitizer | Description **toFloat(input)** | convert the input string to a float, or `NaN` if the input is not a float. **toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer. **trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input. -**unescape(input)** | replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. +**unescape(input)** | replace HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. **whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`. ### XSS Sanitization @@ -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,8 +272,34 @@ 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/ -[mongoid]: http://docs.mongodb.org/manual/reference/object-id/ +[Crockford Base32]: http://www.crockford.com/base32.html +[Base64 URL Safe]: https://base64.guru/standards/base64url +[Data URI Format]: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs +[European Article Number]: https://en.wikipedia.org/wiki/International_Article_Number +[Ethereum]: https://ethereum.org/ +[CSS Colors Level 4 Specification]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value +[IMEI]: https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity +[ISBN]: https://en.wikipedia.org/wiki/ISBN [ISIN]: https://en.wikipedia.org/wiki/International_Securities_Identification_Number +[ISO 639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes +[ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 +[ISO 3166-1 alpha-2]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 +[ISO 3166-1 alpha-3]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 +[ISO 4217]: https://en.wikipedia.org/wiki/ISO_4217 +[ISRC]: https://en.wikipedia.org/wiki/International_Standard_Recording_Code +[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 +[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 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/package.json b/package.json index 7d505205e..4a1034945 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.7.0", + "version": "13.9.0", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ @@ -66,7 +66,7 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" }, "engines": { "node": ">= 0.10" diff --git a/src/index.js b/src/index.js index b8ad651ee..906fd7d1d 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ import isIP from './lib/isIP'; import isIPRange from './lib/isIPRange'; import isFQDN from './lib/isFQDN'; import isDate from './lib/isDate'; +import isTime from './lib/isTime'; import isBoolean from './lib/isBoolean'; import isLocale from './lib/isLocale'; @@ -69,6 +70,7 @@ import isBefore from './lib/isBefore'; import isIn from './lib/isIn'; +import isLuhnNumber from './lib/isLuhnNumber'; import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; @@ -86,6 +88,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'; @@ -121,7 +124,7 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; -const version = '13.7.0'; +const version = '13.9.0'; const validator = { version, @@ -182,6 +185,7 @@ const validator = { isAfter, isBefore, isIn, + isLuhnNumber, isCreditCard, isIdentityCard, isEAN, @@ -195,6 +199,7 @@ const validator = { isEthereumAddress, isCurrency, isBtcAddress, + isISO6391, isISO8601, isRFC3339, isISO31661Alpha2, @@ -222,6 +227,7 @@ const validator = { isStrongPassword, isTaxID, isDate, + isTime, isLicensePlate, isVAT, ibanLocales, diff --git a/src/lib/alpha.js b/src/lib/alpha.js index d663eded0..0535e50d1 100644 --- a/src/lib/alpha.js +++ b/src/lib/alpha.js @@ -11,6 +11,7 @@ export const alpha = { 'fi-FI': /^[A-ZÅÄÖ]+$/i, 'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i, 'it-IT': /^[A-ZÀÉÈÌÎÓÒÙ]+$/i, + 'ja-JP': /^[ぁ-んァ-ヶヲ-゚一-龠ー・。、]+$/i, 'nb-NO': /^[A-ZÆØÅ]+$/i, 'nl-NL': /^[A-ZÁÉËÏÓÖÜÚ]+$/i, 'nn-NO': /^[A-ZÆØÅ]+$/i, @@ -27,11 +28,14 @@ export const alpha = { 'tr-TR': /^[A-ZÇĞİıÖŞÜ]+$/i, 'uk-UA': /^[А-ЩЬЮЯЄIЇҐі]+$/i, 'vi-VN': /^[A-ZÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$/i, + 'ko-KR': /^[ㄱ-ㅎㅏ-ㅣ가-힣]*$/, 'ku-IQ': /^[ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$/i, ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/, he: /^[א-ת]+$/, fa: /^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$/i, + bn: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣৰৱ৲৳৴৵৶৷৸৹৺৻']+$/, 'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i, + 'si-LK': /^[\u0D80-\u0DFF]+$/, }; export const alphanumeric = { @@ -46,6 +50,7 @@ export const alphanumeric = { 'fi-FI': /^[0-9A-ZÅÄÖ]+$/i, 'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i, 'it-IT': /^[0-9A-ZÀÉÈÌÎÓÒÙ]+$/i, + 'ja-JP': /^[0-90-9ぁ-んァ-ヶヲ-゚一-龠ー・。、]+$/i, 'hu-HU': /^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i, 'nb-NO': /^[0-9A-ZÆØÅ]+$/i, 'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i, @@ -61,12 +66,15 @@ export const alphanumeric = { 'th-TH': /^[ก-๙\s]+$/i, 'tr-TR': /^[0-9A-ZÇĞİıÖŞÜ]+$/i, 'uk-UA': /^[0-9А-ЩЬЮЯЄIЇҐі]+$/i, + 'ko-KR': /^[0-9ㄱ-ㅎㅏ-ㅣ가-힣]*$/, 'ku-IQ': /^[٠١٢٣٤٥٦٧٨٩0-9ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$/i, 'vi-VN': /^[0-9A-ZÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$/i, ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/, he: /^[0-9א-ת]+$/, fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i, + bn: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ০১২৩৪৫৬৭৮৯ৰৱ৲৳৴৵৶৷৸৹৺৻']+$/, 'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i, + 'si-LK': /^[0-9\u0D80-\u0DFF]+$/, }; export const decimal = { @@ -85,10 +93,8 @@ for (let locale, i = 0; i < englishLocales.length; i++) { } // Source: http://www.localeplanet.com/java/ -export const arabicLocales = [ - 'AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY', - 'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE', -]; +export const arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY', + 'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE']; for (let locale, i = 0; i < arabicLocales.length; i++) { locale = `ar-${arabicLocales[i]}`; @@ -97,9 +103,7 @@ for (let locale, i = 0; i < arabicLocales.length; i++) { decimal[locale] = decimal.ar; } -export const farsiLocales = [ - 'IR', 'AF', -]; +export const farsiLocales = ['IR', 'AF']; for (let locale, i = 0; i < farsiLocales.length; i++) { locale = `fa-${farsiLocales[i]}`; @@ -107,12 +111,21 @@ for (let locale, i = 0; i < farsiLocales.length; i++) { decimal[locale] = decimal.ar; } +export const bengaliLocales = ['BD', 'IN']; + +for (let locale, i = 0; i < bengaliLocales.length; i++) { + locale = `bn-${bengaliLocales[i]}`; + alpha[locale] = alpha.bn; + alphanumeric[locale] = alphanumeric.bn; + decimal[locale] = decimal['en-US']; +} + // Source: https://en.wikipedia.org/wiki/Decimal_mark 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/isAfter.js b/src/lib/isAfter.js index 47bfb537f..e116e77ce 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -1,9 +1,11 @@ -import assertString from './util/assertString'; import toDate from './toDate'; -export default function isAfter(str, date = String(new Date())) { - assertString(str); - const comparison = toDate(date); - const original = toDate(str); +export default function isAfter(date, options) { + // For backwards compatibility: + // isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date` + const comparisonDate = options?.comparisonDate || options || Date().toString(); + + const comparison = toDate(comparisonDate); + const original = toDate(date); return !!(original && comparison && original > comparison); } diff --git a/src/lib/isBIC.js b/src/lib/isBIC.js index b5576b24e..b0f586728 100644 --- a/src/lib/isBIC.js +++ b/src/lib/isBIC.js @@ -9,7 +9,9 @@ export default function isBIC(str) { // toUpperCase() should be removed when a new major version goes out that changes // the regex to [A-Z] (per the spec). - if (!CountryCodes.has(str.slice(4, 6).toUpperCase())) { + const countryCode = str.slice(4, 6).toUpperCase(); + + if (!CountryCodes.has(countryCode) && countryCode !== 'XK') { return false; } 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/isBtcAddress.js b/src/lib/isBtcAddress.js index 2dfd04651..08f12f4ca 100644 --- a/src/lib/isBtcAddress.js +++ b/src/lib/isBtcAddress.js @@ -1,14 +1,9 @@ import assertString from './util/assertString'; -// supports Bech32 addresses const bech32 = /^(bc1)[a-z0-9]{25,39}$/; const base58 = /^(1|3)[A-HJ-NP-Za-km-z1-9]{25,39}$/; export default function isBtcAddress(str) { assertString(str); - // check for bech32 - if (str.startsWith('bc1')) { - return bech32.test(str); - } - return base58.test(str); + return bech32.test(str) || base58.test(str); } diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index b11c4cc90..b7b24e968 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -1,33 +1,34 @@ import assertString from './util/assertString'; +import isLuhnValid from './isLuhnNumber'; +const cards = { + amex: /^3[47][0-9]{13}$/, + dinersclub: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, + discover: /^6(?:011|5[0-9][0-9])[0-9]{12,15}$/, + jcb: /^(?:2131|1800|35\d{3})\d{11}$/, + mastercard: /^5[1-5][0-9]{2}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/, // /^[25][1-7][0-9]{14}$/; + 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 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}))$/; +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 */ -export default function isCreditCard(str) { - assertString(str); - const sanitized = str.replace(/[- ]+/g, ''); - 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; +export default function isCreditCard(card, options = {}) { + assertString(card); + const { provider } = options; + const sanitized = card.replace(/[- ]+/g, ''); + if (provider && provider.toLowerCase() in cards) { + // specific provider in the list + if (!(cards[provider.toLowerCase()].test(sanitized))) { + return false; } - shouldDouble = !shouldDouble; + } 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))) { + // no specific provider + return false; } - return !!((sum % 10) === 0 ? sanitized : false); + return isLuhnValid(card); } diff --git a/src/lib/isDataURI.js b/src/lib/isDataURI.js index 01e43f70c..506544807 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..d1c35bd46 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -13,6 +13,7 @@ const default_email_options = { blacklisted_chars: '', ignore_max_length: false, host_blacklist: [], + host_whitelist: [], }; /* eslint-disable max-len */ @@ -77,7 +78,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)) { @@ -99,6 +100,10 @@ export default function isEmail(str, options) { return false; } + if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) { + return false; + } + let user = parts.join('@'); if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) { @@ -134,7 +139,10 @@ export default function isEmail(str, options) { return false; } - if (!isFQDN(domain, { require_tld: options.require_tld })) { + if (!isFQDN(domain, { + require_tld: options.require_tld, + ignore_max_length: options.ignore_max_length, + })) { if (!options.allow_ip_domain) { return false; } @@ -144,7 +152,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..eb6928fda 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -7,6 +7,7 @@ const default_fqdn_options = { allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, + ignore_max_length: false, }; export default function isFQDN(str, options) { @@ -32,7 +33,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; } @@ -48,7 +49,7 @@ export default function isFQDN(str, options) { } return parts.every((part) => { - if (part.length > 63) { + if (part.length > 63 && !options.ignore_max_length) { return false; } diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index e6cced044..643f9729f 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -5,7 +5,7 @@ export default function isFloat(str, options) { assertString(str); options = options || {}; const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`); - if (str === '' || str === '.' || str === '-' || str === '+') { + if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') { return false; } const value = parseFloat(str.replace(',', '.')); 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/isISBN.js b/src/lib/isISBN.js index c66c4c991..4499c59a0 100644 --- a/src/lib/isISBN.js +++ b/src/lib/isISBN.js @@ -1,43 +1,55 @@ import assertString from './util/assertString'; -const isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/; -const isbn13Maybe = /^(?:[0-9]{13})$/; +const possibleIsbn10 = /^(?:[0-9]{9}X|[0-9]{10})$/; +const possibleIsbn13 = /^(?:[0-9]{13})$/; const factor = [1, 3]; -export default function isISBN(str, version = '') { - assertString(str); - version = String(version); - if (!version) { - return isISBN(str, 10) || isISBN(str, 13); +export default function isISBN(isbn, options) { + assertString(isbn); + + // For backwards compatibility: + // isISBN(str [, version]), i.e. `options` could be used as argument for the legacy `version` + const version = String(options?.version || options); + + if (!(options?.version || options)) { + return isISBN(isbn, { version: 10 }) || isISBN(isbn, { version: 13 }); } - const sanitized = str.replace(/[\s-]+/g, ''); + + const sanitizedIsbn = isbn.replace(/[\s-]+/g, ''); + let checksum = 0; - let i; + if (version === '10') { - if (!isbn10Maybe.test(sanitized)) { + if (!possibleIsbn10.test(sanitizedIsbn)) { return false; } - for (i = 0; i < 9; i++) { - checksum += (i + 1) * sanitized.charAt(i); + + for (let i = 0; i < version - 1; i++) { + checksum += (i + 1) * sanitizedIsbn.charAt(i); } - if (sanitized.charAt(9) === 'X') { + + if (sanitizedIsbn.charAt(9) === 'X') { checksum += 10 * 10; } else { - checksum += 10 * sanitized.charAt(9); + checksum += 10 * sanitizedIsbn.charAt(9); } + if ((checksum % 11) === 0) { - return !!sanitized; + return true; } } else if (version === '13') { - if (!isbn13Maybe.test(sanitized)) { + if (!possibleIsbn13.test(sanitizedIsbn)) { return false; } - for (i = 0; i < 12; i++) { - checksum += factor[i % 2] * sanitized.charAt(i); + + for (let i = 0; i < 12; i++) { + checksum += factor[i % 2] * sanitizedIsbn.charAt(i); } - if (sanitized.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { - return !!sanitized; + + if (sanitizedIsbn.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { + return true; } } + 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..4734b7bd9 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; @@ -342,6 +342,37 @@ const validators = { }; return checkIdCardNo(str); }, + 'zh-HK': (str) => { + // sanitize user input + str = str.trim(); + + // HKID number starts with 1 or 2 letters, followed by 6 digits, + // then a checksum contained in square / round brackets or nothing + const regexHKID = /^[A-Z]{1,2}[0-9]{6}((\([0-9A]\))|(\[[0-9A]\])|([0-9A]))$/; + const regexIsDigit = /^[0-9]$/; + + // convert the user input to all uppercase and apply regex + str = str.toUpperCase(); + if (!regexHKID.test(str)) return false; + str = str.replace(/\[|\]|\(|\)/g, ''); + + if (str.length === 8) str = `3${str}`; + let checkSumVal = 0; + for (let i = 0; i <= 7; i++) { + let convertedChar; + if (!regexIsDigit.test(str[i])) convertedChar = (str[i].charCodeAt(0) - 55) % 11; + else convertedChar = str[i]; + checkSumVal += (convertedChar * (9 - i)); + } + checkSumVal %= 11; + + let checkSumConverted; + if (checkSumVal === 0) checkSumConverted = '0'; + else if (checkSumVal === 1) checkSumConverted = 'A'; + else checkSumConverted = String(11 - checkSumVal); + if (checkSumConverted === str[str.length - 1]) return true; + return false; + }, 'zh-TW': (str) => { const ALPHABET_CODES = { A: 10, 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..48f8ebe99 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -2,17 +2,22 @@ import assertString from './util/assertString'; const validators = { 'cs-CZ': str => - /^(([ABCDEFHKIJKLMNPRSTUVXYZ]|[0-9])-?){5,8}$/.test(str), + /^(([ABCDEFHIJKLMNPRSTUVXYZ]|[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|FF|F|FS|FD|FÜ|GE|G|GI|GF|GS|ZR|GG|GP|GR|NY|ZI|GÖ|GZ|GT|HA|HH|HM|HU|WL|HZ|WR|RN|HK|HD|HN|HS|GK|HE|HF|RZ|HI|HG|HO|HX|IK|IL|IN|J|JL|KL|KA|KS|KF|KE|KI|KT|KO|KN|KR|KC|KU|K|LD|LL|LA|L|OP|LM|LI|LB|LU|LÖ|HL|LG|MD|GN|MZ|MA|ML|MR|MY|AT|DM|MC|NZ|RM|RG|MM|ME|MB|MI|FG|DL|HC|MW|RL|MK|MG|MÜ|WS|MH|M|MS|NU|NB|ND|NM|NK|NW|NR|NI|NF|DZ|EB|OZ|TG|TO|N|OA|GM|OB|CA|EH|FW|OF|OL|OE|OG|BH|LR|OS|AA|GD|OH|KY|NP|WK|PB|PA|PE|PI|PS|P|PM|PR|RA|RV|RE|R|H|SB|WN|RS|RD|RT|BM|NE|GV|RP|SU|GL|RO|GÜ|RH|EG|RW|PN|SK|MQ|RU|SZ|RI|SL|SM|SC|HR|FZ|VS|SW|SN|CR|SE|SI|SO|LP|SG|NH|SP|IZ|ST|BF|TE|HV|OD|SR|S|AC|DW|ZW|TF|TS|TR|TÜ|UM|PZ|TP|UE|UN|UH|MN|KK|VB|V|AE|PL|RC|VG|GW|PW|VR|VK|KB|WA|WT|BE|WM|WE|AP|MO|WW|FB|WZ|WI|WB|JE|WF|WO|W|WÜ|BL|Z|GC)[- ]?[A-Z]{1,2}[- ]?\d{1,4}|(AIC|FDB|ABG|SLN|SAW|KLZ|BUL|ESB|NAB|SUL|WST|ABI|AZE|BTF|KÖT|DKB|FEU|ROT|ALZ|SMÜ|WER|AUR|NOR|DÜW|BRK|HAB|TÖL|WOR|BAD|BAR|BER|BIW|EBS|KEM|MÜB|PEG|BGL|BGD|REI|WIL|BKS|BIR|WAT|BOR|BOH|BOT|BRB|BLK|HHM|NEB|NMB|WSF|LEO|HDL|WMS|WZL|BÜS|CHA|KÖZ|ROD|WÜM|CLP|NEC|COC|ZEL|COE|CUX|DAH|LDS|DEG|DEL|RSL|DLG|DGF|LAN|HEI|MED|DON|KIB|ROK|JÜL|MON|SLE|EBE|EIC|HIG|WBS|BIT|PRÜ|LIB|EMD|WIT|ERH|HÖS|ERZ|ANA|ASZ|MAB|MEK|STL|SZB|FDS|HCH|HOR|WOL|FRG|GRA|WOS|FRI|FFB|GAP|GER|BRL|CLZ|GTH|NOH|HGW|GRZ|LÖB|NOL|WSW|DUD|HMÜ|OHA|KRU|HAL|HAM|HBS|QLB|HVL|NAU|HAS|EBN|GEO|HOH|HDH|ERK|HER|WAN|HEF|ROF|HBN|ALF|HSK|USI|NAI|REH|SAN|KÜN|ÖHR|HOL|WAR|ARN|BRG|GNT|HOG|WOH|KEH|MAI|PAR|RID|ROL|KLE|GEL|KUS|KYF|ART|SDH|LDK|DIL|MAL|VIB|LER|BNA|GHA|GRM|MTL|WUR|LEV|LIF|STE|WEL|LIP|VAI|LUP|HGN|LBZ|LWL|PCH|STB|DAN|MKK|SLÜ|MSP|TBB|MGH|MTK|BIN|MSH|EIL|HET|SGH|BID|MYK|MSE|MST|MÜR|WRN|MEI|GRH|RIE|MZG|MIL|OBB|BED|FLÖ|MOL|FRW|SEE|SRB|AIB|MOS|BCH|ILL|SOB|NMS|NEA|SEF|UFF|NEW|VOH|NDH|TDO|NWM|GDB|GVM|WIS|NOM|EIN|GAN|LAU|HEB|OHV|OSL|SFB|ERB|LOS|BSK|KEL|BSB|MEL|WTL|OAL|FÜS|MOD|OHZ|OPR|BÜR|PAF|PLÖ|CAS|GLA|REG|VIT|ECK|SIM|GOA|EMS|DIZ|GOH|RÜD|SWA|NES|KÖN|MET|LRO|BÜZ|DBR|ROS|TET|HRO|ROW|BRV|HIP|PAN|GRI|SHK|EIS|SRO|SOK|LBS|SCZ|MER|QFT|SLF|SLS|HOM|SLK|ASL|BBG|SBK|SFT|SHG|MGN|MEG|ZIG|SAD|NEN|OVI|SHA|BLB|SIG|SON|SPN|FOR|GUB|SPB|IGB|WND|STD|STA|SDL|OBG|HST|BOG|SHL|PIR|FTL|SEB|SÖM|SÜW|TIR|SAB|TUT|ANG|SDT|LÜN|LSZ|MHL|VEC|VER|VIE|OVL|ANK|OVP|SBG|UEM|UER|WLG|GMN|NVP|RDG|RÜG|DAU|FKB|WAF|WAK|SLZ|WEN|SOG|APD|WUG|GUN|ESW|WIZ|WES|DIN|BRA|BÜD|WHV|HWI|GHC|WTM|WOB|WUN|MAK|SEL|OCH|HOT|WDA)[- ]?(([A-Z][- ]?\d{1,4})|([A-Z]{2}[- ]?\d{1,3})))[- ]?(E|H)?$/.test(str), + /^((A|AA|AB|AC|AE|AH|AK|AM|AN|AÖ|AP|AS|AT|AU|AW|AZ|B|BA|BB|BC|BE|BF|BH|BI|BK|BL|BM|BN|BO|BÖ|BS|BT|BZ|C|CA|CB|CE|CO|CR|CW|D|DA|DD|DE|DH|DI|DL|DM|DN|DO|DU|DW|DZ|E|EA|EB|ED|EE|EF|EG|EH|EI|EL|EM|EN|ER|ES|EU|EW|F|FB|FD|FF|FG|FI|FL|FN|FO|FR|FS|FT|FÜ|FW|FZ|G|GA|GC|GD|GE|GF|GG|GI|GK|GL|GM|GN|GÖ|GP|GR|GS|GT|GÜ|GV|GW|GZ|H|HA|HB|HC|HD|HE|HF|HG|HH|HI|HK|HL|HM|HN|HO|HP|HR|HS|HU|HV|HX|HY|HZ|IK|IL|IN|IZ|J|JE|JL|K|KA|KB|KC|KE|KF|KG|KH|KI|KK|KL|KM|KN|KO|KR|KS|KT|KU|KW|KY|L|LA|LB|LC|LD|LF|LG|LH|LI|LL|LM|LN|LÖ|LP|LR|LU|M|MA|MB|MC|MD|ME|MG|MH|MI|MK|ML|MM|MN|MO|MQ|MR|MS|MÜ|MW|MY|MZ|N|NB|ND|NE|NF|NH|NI|NK|NM|NÖ|NP|NR|NT|NU|NW|NY|NZ|OA|OB|OC|OD|OE|OF|OG|OH|OK|OL|OP|OS|OZ|P|PA|PB|PE|PF|PI|PL|PM|PN|PR|PS|PW|PZ|R|RA|RC|RD|RE|RG|RH|RI|RL|RM|RN|RO|RP|RS|RT|RU|RV|RW|RZ|S|SB|SC|SE|SG|SI|SK|SL|SM|SN|SO|SP|SR|ST|SU|SW|SY|SZ|TE|TF|TG|TO|TP|TR|TS|TT|TÜ|ÜB|UE|UH|UL|UM|UN|V|VB|VG|VK|VR|VS|W|WA|WB|WE|WF|WI|WK|WL|WM|WN|WO|WR|WS|WT|WÜ|WW|WZ|Z|ZE|ZI|ZP|ZR|ZW|ZZ)[- ]?[A-Z]{1,2}[- ]?\d{1,4}|(ABG|ABI|AIB|AIC|ALF|ALZ|ANA|ANG|ANK|APD|ARN|ART|ASL|ASZ|AUR|AZE|BAD|BAR|BBG|BCH|BED|BER|BGD|BGL|BID|BIN|BIR|BIT|BIW|BKS|BLB|BLK|BNA|BOG|BOH|BOR|BOT|BRA|BRB|BRG|BRK|BRL|BRV|BSB|BSK|BTF|BÜD|BUL|BÜR|BÜS|BÜZ|CAS|CHA|CLP|CLZ|COC|COE|CUX|DAH|DAN|DAU|DBR|DEG|DEL|DGF|DIL|DIN|DIZ|DKB|DLG|DON|DUD|DÜW|EBE|EBN|EBS|ECK|EIC|EIL|EIN|EIS|EMD|EMS|ERB|ERH|ERK|ERZ|ESB|ESW|FDB|FDS|FEU|FFB|FKB|FLÖ|FOR|FRG|FRI|FRW|FTL|FÜS|GAN|GAP|GDB|GEL|GEO|GER|GHA|GHC|GLA|GMN|GNT|GOA|GOH|GRA|GRH|GRI|GRM|GRZ|GTH|GUB|GUN|GVM|HAB|HAL|HAM|HAS|HBN|HBS|HCH|HDH|HDL|HEB|HEF|HEI|HER|HET|HGN|HGW|HHM|HIG|HIP|HMÜ|HOG|HOH|HOL|HOM|HOR|HÖS|HOT|HRO|HSK|HST|HVL|HWI|IGB|ILL|JÜL|KEH|KEL|KEM|KIB|KLE|KLZ|KÖN|KÖT|KÖZ|KRU|KÜN|KUS|KYF|LAN|LAU|LBS|LBZ|LDK|LDS|LEO|LER|LEV|LIB|LIF|LIP|LÖB|LOS|LRO|LSZ|LÜN|LUP|LWL|MAB|MAI|MAK|MAL|MED|MEG|MEI|MEK|MEL|MER|MET|MGH|MGN|MHL|MIL|MKK|MOD|MOL|MON|MOS|MSE|MSH|MSP|MST|MTK|MTL|MÜB|MÜR|MYK|MZG|NAB|NAI|NAU|NDH|NEA|NEB|NEC|NEN|NES|NEW|NMB|NMS|NOH|NOL|NOM|NOR|NVP|NWM|OAL|OBB|OBG|OCH|OHA|ÖHR|OHV|OHZ|OPR|OSL|OVI|OVL|OVP|PAF|PAN|PAR|PCH|PEG|PIR|PLÖ|PRÜ|QFT|QLB|RDG|REG|REH|REI|RID|RIE|ROD|ROF|ROK|ROL|ROS|ROT|ROW|RSL|RÜD|RÜG|SAB|SAD|SAN|SAW|SBG|SBK|SCZ|SDH|SDL|SDT|SEB|SEE|SEF|SEL|SFB|SFT|SGH|SHA|SHG|SHK|SHL|SIG|SIM|SLE|SLF|SLK|SLN|SLS|SLÜ|SLZ|SMÜ|SOB|SOG|SOK|SÖM|SON|SPB|SPN|SRB|SRO|STA|STB|STD|STE|STL|SUL|SÜW|SWA|SZB|TBB|TDO|TET|TIR|TÖL|TUT|UEM|UER|UFF|USI|VAI|VEC|VER|VIB|VIE|VIT|VOH|WAF|WAK|WAN|WAR|WAT|WBS|WDA|WEL|WEN|WER|WES|WHV|WIL|WIS|WIT|WIZ|WLG|WMS|WND|WOB|WOH|WOL|WOR|WOS|WRN|WSF|WST|WSW|WTL|WTM|WUG|WÜM|WUN|WUR|WZL|ZEL|ZIG)[- ]?(([A-Z][- ]?\d{1,4})|([A-Z]{2}[- ]?\d{1,3})))[- ]?(E|H)?$/.test(str), 'de-LI': str => /^FL[- ]?\d{1,5}[UZ]?$/.test(str), + 'en-IN': str => /^[A-Z]{2}[ -]?[0-9]{1,2}(?:[ -]?[A-Z])(?:[ -]?[A-Z]*)?[ -]?[0-9]{4}$/.test(str), + 'es-AR': str => /^(([A-Z]{2} ?[0-9]{3} ?[A-Z]{2})|([A-Z]{3} ?[0-9]{3}))$/.test(str), 'fi-FI': str => /^(?=.{4,7})(([A-Z]{1,3}|[0-9]{1,3})[\s-]?([A-Z]{1,3}|[0-9]{1,5}))$/.test(str), + 'hu-HU': str => /^((((?!AAA)(([A-NPRSTVZWXY]{1})([A-PR-Z]{1})([A-HJ-NPR-Z]))|(A[ABC]I)|A[ABC]O|A[A-W]Q|BPI|BPO|UCO|UDO|XAO)-(?!000)\d{3})|(M\d{6})|((CK|DT|CD|HC|H[ABEFIKLMNPRSTVX]|MA|OT|R[A-Z]) \d{2}-\d{2})|(CD \d{3}-\d{3})|(C-(C|X) \d{4})|(X-(A|B|C) \d{4})|(([EPVZ]-\d{5}))|(S A[A-Z]{2} \d{2})|(SP \d{2}-\d{2}))$/.test(str), + 'pt-BR': str => + /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), 'pt-PT': str => /^([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})$/.test(str), 'sq-AL': str => /^[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()), }; export default function isLicensePlate(str, locale) { diff --git a/src/lib/isLuhnNumber.js b/src/lib/isLuhnNumber.js new file mode 100644 index 000000000..95a066115 --- /dev/null +++ b/src/lib/isLuhnNumber.js @@ -0,0 +1,26 @@ +import assertString from './util/assertString'; + +export default function isLuhnNumber(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/isMACAddress.js b/src/lib/isMACAddress.js index 8ffc8c254..d87cd4aa7 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -1,18 +1,34 @@ import assertString from './util/assertString'; -const macAddress = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/; -const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/; -const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; +const macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/; +const macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/; +const macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; +const macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/; +const macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/; +const macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/; export default function isMACAddress(str, options) { assertString(str); + if (options?.eui) { + options.eui = String(options.eui); + } /** * @deprecated `no_colons` TODO: remove it in the next major */ - if (options && (options.no_colons || options.no_separators)) { - return macAddressNoSeparators.test(str); + if (options?.no_colons || options?.no_separators) { + if (options.eui === '48') { + return macAddress48NoSeparators.test(str); + } + if (options.eui === '64') { + return macAddress64NoSeparators.test(str); + } + return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str); } - - return macAddress.test(str) - || macAddressWithDots.test(str); + if (options?.eui === '48') { + return macAddress48.test(str) || macAddress48WithDots.test(str); + } + if (options?.eui === '64') { + return macAddress64.test(str) || macAddress64WithDots.test(str); + } + return isMACAddress(str, { eui: '48' }) || isMACAddress(str, { eui: '64' }); } 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/isMimeType.js b/src/lib/isMimeType.js index 1dfa77767..4081117af 100644 --- a/src/lib/isMimeType.js +++ b/src/lib/isMimeType.js @@ -26,7 +26,7 @@ import assertString from './util/assertString'; // NB : // Subtype length must not exceed 100 characters. // This rule does not comply to the RFC specs (what is the max length ?). -const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len +const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+_]{1,100}$/i; // eslint-disable-line max-len // Handle "charset" in "text/*" const mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index c70310ef7..5c37d42bb 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -10,7 +10,7 @@ const phones = { 'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/, 'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, 'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, - 'ar-KW': /^(\+?965)[569]\d{7}$/, + 'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/, 'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, 'ar-MA': /^(?:(?:\+|00)212|0)[5-7]\d{8}$/, 'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, @@ -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}$/, @@ -26,14 +26,18 @@ const phones = { 'ca-AD': /^(\+376)?[346]\d{5}$/, 'cs-CZ': /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/, 'da-DK': /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/, - 'de-DE': /^((\+49|0)[1|3])([0|5][0-45-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/, + 'de-DE': /^((\+49|0)1)(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/, 'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, 'de-CH': /^(\+41|0)([1-9])\d{1,9}$/, 'de-LU': /^(\+352)?((6\d1)\d{6})$/, - 'dv-MV': /^(\+?960)?(7[2-9]|91|9[3-9])\d{7}$/, - 'el-GR': /^(\+?30|0)?(69\d{8})$/, + 'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/, + 'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/, + 'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, + 'en-AI': /^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/, 'en-AU': /^(\+?61|0)4\d{8}$/, - 'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}))/, + '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-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, 'en-GB': /^(\+?44|0)7\d{9}$/, 'en-GG': /^(\+?44|0)1481\d{6}$/, 'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28|55|59)\d{7}$/, @@ -42,13 +46,18 @@ const phones = { 'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/, 'en-IE': /^(\+?353|0)8[356789]\d{7}$/, 'en-IN': /^(\+?91|0)?[6789]\d{9}$/, + 'en-JM': /^(\+?876)?\d{7}$/, 'en-KE': /^(\+?254|0)(7|1)\d{8}$/, + '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}$/, + '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,11 +77,12 @@ 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}$/, 'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, + 'es-NI': /^(\+?505)\d{7,8}$/, 'es-PA': /^(\+?507)\d{7,8}$/, 'es-PY': /^(\+?595|0)9[9876]\d{7}$/, 'es-SV': /^(\+?503)?[67]\d{7}$/, @@ -80,10 +90,12 @@ 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-CD': /^(\+?243|0)?(8|9)\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,27 +106,34 @@ 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}$/, - 'ms-MY': /^(\+?6?01){1}(([0145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\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': /^(\+?60|0)1(([0145](-|\s)?\d{7,8})|([236-9](-|\s)?\d{7}))$/, 'mz-MZ': /^(\+?258)?8[234567]\d{7}$/, 'nb-NO': /^(\+?47)?[49]\d{7}$/, '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}$/, + 'ro-MD': /^(\+?373|0)((6(0|1|2|6|7|8|9))|(7(6|7|8|9)))\d{6}$/, + 'ro-RO': /^(\+?40|0)\s?7\d{2}(\/|\s|\.|-)?\d{3}(\s|\.|-)?\d{3}$/, 'ru-RU': /^(\+?7|8)?9\d{9}$/, '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})$/, @@ -132,6 +151,9 @@ const phones = { 'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/, 'zh-TW': /^(\+?886\-?|0)?9\d{8}$/, 'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/, + 'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/, + 'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/, + 'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/, }; /* eslint-enable max-len */ diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js index 4c38bfbbe..11d01e8d1 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -11,6 +11,7 @@ const passportRegexByCountryCode = { AR: /^[A-Z]{3}\d{6}$/, // ARGENTINA AT: /^[A-Z]\d{7}$/, // AUSTRIA AU: /^[A-Z]\d{7}$/, // AUSTRALIA + AZ: /^[A-Z]{2,3}\d{7,8}$/, // AZERBAIJAN BE: /^[A-Z]{2}\d{6}$/, // BELGIUM BG: /^\d{9}$/, // BULGARIA BR: /^[A-Z]{2}\d{6}$/, // BRAZIL @@ -37,8 +38,11 @@ const passportRegexByCountryCode = { IR: /^[A-Z]\d{8}$/, // IRAN IS: /^(A)\d{7}$/, // ICELAND IT: /^[A-Z0-9]{2}\d{7}$/, // ITALY + JM: /^[Aa]\d{7}$/, // JAMAICA JP: /^[A-Z]{2}\d{7}$/, // JAPAN KR: /^[MS]\d{8}$/, // SOUTH KOREA, REPUBLIC OF KOREA, [S=PS Passports, M=PM Passports] + KZ: /^[a-zA-Z]\d{7}$/, // KAZAKHSTAN + LI: /^[a-zA-Z]\d{5}$/, // LIECHTENSTEIN LT: /^[A-Z0-9]{8}$/, // LITHUANIA LU: /^[A-Z0-9]{8}$/, // LUXEMBURG LV: /^[A-Z0-9]{2}\d{7}$/, // LATVIA @@ -46,14 +50,19 @@ 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 + NZ: /^([Ll]([Aa]|[Dd]|[Ff]|[Hh])|[Ee]([Aa]|[Pp])|[Nn])\d{6}$/, // NEW ZEALAND + PH: /^([A-Z](\d{6}|\d{7}[A-Z]))|([A-Z]{2}(\d{6}|\d{7}))$/, // PHILIPPINES + PK: /^[A-Z]{2}\d{7}$/, // PAKISTAN PL: /^[A-Z]{2}\d{7}$/, // POLAND PT: /^[A-Z]\d{6}$/, // PORTUGAL RO: /^\d{8,9}$/, // ROMANIA RU: /^\d{9}$/, // RUSSIAN FEDERATION SE: /^\d{8}$/, // SWEDEN - SL: /^(P)[A-Z]\d{7}$/, // SLOVANIA + SL: /^(P)[A-Z]\d{7}$/, // SLOVENIA SK: /^[0-9A-Z]\d{7}$/, // SLOVAKIA + TH: /^[A-Z]{1,2}\d{6,7}$/, // THAILAND TR: /^[A-Z]\d{8}$/, // TURKEY UA: /^[A-Z]{2}\d{6}$/, // UKRAINE US: /^\d{9}$/, // UNITED STATES diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index 7ef17abcf..e6213914f 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -11,10 +11,11 @@ const patterns = { AT: fourDigit, AU: fourDigit, AZ: /^AZ\d{4}$/, + BA: /^([7-8]\d{4}$)/, BE: fourDigit, BG: fourDigit, BR: /^\d{5}-\d{3}$/, - BY: /2[1-4]{1}\d{4}$/, + BY: /^2[1-4]\d{4}$/, CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i, CH: fourDigit, CN: /^(0[1-7]|1[012356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[1-5]|8[1345]|9[09])\d{4}$/, @@ -36,7 +37,7 @@ const patterns = { IE: /^(?!.*(?:o))[A-Za-z]\d[\dw]\s\w{4}$/i, IL: /^(\d{5}|\d{7})$/, IN: /^((?!10|29|35|54|55|65|66|86|87|88|89)[1-9][0-9]{5})$/, - IR: /\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b/, + IR: /^(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}$/, IS: threeDigit, IT: fiveDigit, JP: /^\d{3}\-\d{4}$/, @@ -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/isRgbColor.js b/src/lib/isRgbColor.js index e6508e29a..9458522ab 100644 --- a/src/lib/isRgbColor.js +++ b/src/lib/isRgbColor.js @@ -2,8 +2,8 @@ import assertString from './util/assertString'; const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/; const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; -const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)/; -const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/; +const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)$/; +const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; export default function isRgbColor(str, includePercentValues = true) { assertString(str); diff --git a/src/lib/isStrongPassword.js b/src/lib/isStrongPassword.js index 28bb0637f..5db901fa3 100644 --- a/src/lib/isStrongPassword.js +++ b/src/lib/isStrongPassword.js @@ -4,7 +4,7 @@ import assertString from './util/assertString'; const upperCaseRegex = /^[A-Z]$/; const lowerCaseRegex = /^[a-z]$/; const numberRegex = /^[0-9]$/; -const symbolRegex = /^[-#!$@%^&*()_+|~=`{}\[\]:";'<>?,.\/ ]$/; +const symbolRegex = /^[-#!$@£%^&*()_+|~=`{}\[\]:";'<>?,.\/ ]$/; const defaultOptions = { minLength: 8, 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/isTime.js b/src/lib/isTime.js new file mode 100644 index 000000000..076bbfa34 --- /dev/null +++ b/src/lib/isTime.js @@ -0,0 +1,23 @@ +import merge from './util/merge'; + +const default_time_options = { + hourFormat: 'hour24', + mode: 'default', +}; + +const formats = { + hour24: { + default: /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/, + withSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/, + }, + hour12: { + default: /^(0?[1-9]|1[0-2]):([0-5][0-9]) (A|P)M$/, + withSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]) (A|P)M$/, + }, +}; + +export default function isTime(input, options) { + options = merge(options, default_time_options); + if (typeof input !== 'string') return false; + return formats[options.hourFormat][options.mode].test(input); +} 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/isVAT.js b/src/lib/isVAT.js index 884b066ff..95593569d 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -1,9 +1,101 @@ import assertString from './util/assertString'; +import * as algorithms from './util/algorithms'; + +const PT = (str) => { + const match = str.match(/^(PT)?(\d{9})$/); + if (!match) { + return false; + } + + const tin = match[2]; + + const checksum = 11 - (algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(a => parseInt(a, 10)), 9) % 11); + if (checksum > 9) { + return parseInt(tin[8], 10) === 0; + } + return checksum === parseInt(tin[8], 10); +}; export const vatMatchers = { - GB: /^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}))$/, - IT: /^(IT)?[0-9]{11}$/, - NL: /^(NL)?[0-9]{9}B[0-9]{2}$/, + /** + * European Union VAT identification numbers + */ + AT: str => /^(AT)?U\d{8}$/.test(str), + BE: str => /^(BE)?\d{10}$/.test(str), + BG: str => /^(BG)?\d{9,10}$/.test(str), + HR: str => /^(HR)?\d{11}$/.test(str), + CY: str => /^(CY)?\w{9}$/.test(str), + CZ: str => /^(CZ)?\d{8,10}$/.test(str), + DK: str => /^(DK)?\d{8}$/.test(str), + EE: str => /^(EE)?\d{9}$/.test(str), + FI: str => /^(FI)?\d{8}$/.test(str), + FR: str => /^(FR)?\w{2}\d{9}$/.test(str), + DE: str => /^(DE)?\d{9}$/.test(str), + EL: str => /^(EL)?\d{9}$/.test(str), + HU: str => /^(HU)?\d{8}$/.test(str), + IE: str => /^(IE)?\d{7}\w{1}(W)?$/.test(str), + IT: str => /^(IT)?\d{11}$/.test(str), + LV: str => /^(LV)?\d{11}$/.test(str), + LT: str => /^(LT)?\d{9,12}$/.test(str), + LU: str => /^(LU)?\d{8}$/.test(str), + MT: str => /^(MT)?\d{8}$/.test(str), + NL: str => /^(NL)?\d{9}B\d{2}$/.test(str), + PL: str => /^(PL)?(\d{10}|(\d{3}-\d{3}-\d{2}-\d{2})|(\d{3}-\d{2}-\d{2}-\d{3}))$/.test(str), + PT, + RO: str => /^(RO)?\d{2,10}$/.test(str), + SK: str => /^(SK)?\d{10}$/.test(str), + SI: str => /^(SI)?\d{8}$/.test(str), + ES: str => /^(ES)?\w\d{7}[A-Z]$/.test(str), + SE: str => /^(SE)?\d{12}$/.test(str), + + /** + * VAT numbers of non-EU countries + */ + AL: str => /^(AL)?\w{9}[A-Z]$/.test(str), + MK: str => /^(MK)?\d{13}$/.test(str), + AU: str => /^(AU)?\d{11}$/.test(str), + BY: str => /^(УНП )?\d{9}$/.test(str), + CA: str => /^(CA)?\d{9}$/.test(str), + IS: str => /^(IS)?\d{5,6}$/.test(str), + IN: str => /^(IN)?\d{15}$/.test(str), + ID: str => /^(ID)?(\d{15}|(\d{2}.\d{3}.\d{3}.\d{1}-\d{3}.\d{3}))$/.test(str), + IL: str => /^(IL)?\d{9}$/.test(str), + KZ: str => /^(KZ)?\d{9}$/.test(str), + NZ: str => /^(NZ)?\d{9}$/.test(str), + NG: str => /^(NG)?(\d{12}|(\d{8}-\d{4}))$/.test(str), + NO: str => /^(NO)?\d{9}MVA$/.test(str), + PH: str => /^(PH)?(\d{12}|\d{3} \d{3} \d{3} \d{3})$/.test(str), + RU: str => /^(RU)?(\d{10}|\d{12})$/.test(str), + 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), + 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), + UZ: str => /^(UZ)?\d{9}$/.test(str), + + /** + * VAT numbers of Latin American countries + */ + AR: str => /^(AR)?\d{11}$/.test(str), + BO: str => /^(BO)?\d{7}$/.test(str), + BR: str => /^(BR)?((\d{2}.\d{3}.\d{3}\/\d{4}-\d{2})|(\d{3}.\d{3}.\d{3}-\d{2}))$/.test(str), + CL: str => /^(CL)?\d{8}-\d{1}$/.test(str), + CO: str => /^(CO)?\d{10}$/.test(str), + CR: str => /^(CR)?\d{9,12}$/.test(str), + EC: str => /^(EC)?\d{13}$/.test(str), + SV: str => /^(SV)?\d{4}-\d{6}-\d{3}-\d{1}$/.test(str), + GT: str => /^(GT)?\d{7}-\d{1}$/.test(str), + HN: str => /^(HN)?$/.test(str), + MX: str => /^(MX)?\w{3,4}\d{6}\w{3}$/.test(str), + NI: str => /^(NI)?\d{3}-\d{6}-\d{4}\w{1}$/.test(str), + PA: str => /^(PA)?$/.test(str), + PY: str => /^(PY)?\d{6,8}-\d{1}$/.test(str), + PE: str => /^(PE)?\d{11}$/.test(str), + DO: str => /^(DO)?(\d{11}|(\d{3}-\d{7}-\d{1})|[1,4,5]{1}\d{8}|([1,4,5]{1})-\d{2}-\d{5}-\d{1})$/.test(str), + UY: str => /^(UY)?\d{12}$/.test(str), + VE: str => /^(VE)?[J,G,V,E]{1}-(\d{9}|(\d{8}-\d{1}))$/.test(str), }; export default function isVAT(str, countryCode) { @@ -11,7 +103,7 @@ export default function isVAT(str, countryCode) { assertString(countryCode); if (countryCode in vatMatchers) { - return vatMatchers[countryCode].test(str); + return vatMatchers[countryCode](str); } throw new Error(`Invalid country code: '${countryCode}'`); } 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/src/lib/toDate.js b/src/lib/toDate.js index 7cf80510e..179645fda 100644 --- a/src/lib/toDate.js +++ b/src/lib/toDate.js @@ -2,6 +2,7 @@ import assertString from './util/assertString'; export default function toDate(date) { assertString(date); + date = Date.parse(date); return !isNaN(date) ? new Date(date) : null; } diff --git a/test/client-side.js b/test/clientSide.test.js similarity index 100% rename from test/client-side.js rename to test/clientSide.test.js diff --git a/test/exports.js b/test/exports.test.js similarity index 100% rename from test/exports.js rename to test/exports.test.js diff --git a/test/sanitizers.js b/test/sanitizers.test.js similarity index 100% rename from test/sanitizers.js rename to test/sanitizers.test.js diff --git a/test/testFunctions.js b/test/testFunctions.js new file mode 100644 index 000000000..bcd7c15b0 --- /dev/null +++ b/test/testFunctions.js @@ -0,0 +1,56 @@ +import assert from 'assert'; +import { format } from 'util'; +import validator from '../src/index'; + +export default function test(options) { + const args = options.args || []; + + args.unshift(null); + + if (options.error) { + options.error.forEach((error) => { + args[0] = error; + + try { + assert.throws(() => validator[options.validator](...args)); + } catch (err) { + const warning = format( + 'validator.%s(%s) passed but should error', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.valid) { + options.valid.forEach((valid) => { + args[0] = valid; + + if (validator[options.validator](...args) !== true) { + const warning = format( + 'validator.%s(%s) failed but should have passed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.invalid) { + options.invalid.forEach((invalid) => { + args[0] = invalid; + + if (validator[options.validator](...args) !== false) { + const warning = format( + 'validator.%s(%s) passed but should have failed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } +} diff --git a/test/util.js b/test/util.test.js similarity index 100% rename from test/util.js rename to test/util.test.js diff --git a/test/validators.js b/test/validators.test.js similarity index 82% rename from test/validators.js rename to test/validators.test.js index a4e00293b..a1079b34f 100644 --- a/test/validators.js +++ b/test/validators.test.js @@ -3,60 +3,10 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; +import test from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); -function test(options) { - let args = options.args || []; - args.unshift(null); - if (options.error) { - options.error.forEach((error) => { - args[0] = error; - try { - assert.throws(() => validator[options.validator](...args)); - } catch (err) { - let warning = format( - 'validator.%s(%s) passed but should error', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.valid) { - options.valid.forEach((valid) => { - args[0] = valid; - if (validator[options.validator](...args) !== true) { - let warning = format( - 'validator.%s(%s) failed but should have passed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.invalid) { - options.invalid.forEach((invalid) => { - args[0] = invalid; - if (validator[options.validator](...args) !== false) { - let warning = format( - 'validator.%s(%s) passed but should have failed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } -} - -function repeat(str, count) { - let result = ''; - for (; count; count--) { - result += str; - } - return result; -} - describe('Validators', () => { it('should validate email addresses', () => { test({ @@ -74,9 +24,9 @@ describe('Validators', () => { '"foobar"@example.com', '" foo m端ller "@example.com', '"foo\\@bar"@example.com', - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', 'test@1337.com', @@ -90,10 +40,10 @@ describe('Validators', () => { 'foo@bar.co.uk.', 'z@co.c', 'gmailgmailgmailgmailgmail@gmail.com', - `${repeat('a', 64)}@${repeat('a', 251)}.com`, - `${repeat('a', 65)}@${repeat('a', 250)}.com`, - `${repeat('a', 64)}@${repeat('a', 64)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(251)}.com`, + `${'a'.repeat(65)}@${'a'.repeat(250)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(64)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`, 'test1@invalid.co m', 'test2@invalid.co m', 'test3@invalid.co m', @@ -128,10 +78,10 @@ describe('Validators', () => { 'foobar@gmail.com', 'foo.bar@gmail.com', 'foo.bar@googlemail.com', - `${repeat('a', 30)}@gmail.com`, + `${'a'.repeat(30)}@gmail.com`, ], invalid: [ - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', '.foobar@gmail.com', @@ -328,6 +278,15 @@ describe('Validators', () => { ], invalid: [], }); + + test({ + validator: 'isEmail', + args: [{ ignore_max_length: true }], + valid: [ + 'Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX@Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX.com', + ], + invalid: [], + }); }); it('should not validate email addresses with denylisted domains', () => { @@ -344,6 +303,22 @@ describe('Validators', () => { }); }); + it('should validate only email addresses with whitelisted domains', () => { + test({ + validator: 'isEmail', + args: [{ host_whitelist: ['gmail.com', 'foo.bar.com'] }], + valid: [ + 'email@gmail.com', + 'test@foo.bar.com', + ], + invalid: [ + 'foo+bar@test.com', + 'email@foo.com', + 'email@bar.com', + ], + }); + }); + it('should validate URLs', () => { test({ validator: 'isURL', @@ -472,6 +447,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', @@ -810,6 +803,14 @@ describe('Validators', () => { '01 02 03 04 05 ab', '01-02-03-04-05-ab', '0102.0304.05ab', + 'ab:ab:ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:06:07:ab', + '01:AB:03:04:05:06:07:08', + 'A9 C5 D4 9F EB D3 B6 65', + '01 02 03 04 05 06 07 ab', + '01-02-03-04-05-06-07-ab', + '0102.0304.0506.07ab', ], invalid: [ 'abc', @@ -822,6 +823,67 @@ describe('Validators', () => { '01-02 03:04 05 ab', '0102.03:04.05ab', '900f/dffs/sdea', + '01:02:03:04:05:06:07', + '01:02:03:04:05:06:07:z0', + '01:02:03:04:05:06::ab', + '1:2:3:4:5:6:7:8', + 'AB:CD:EF:GH:01:02:03:04', + 'A9C5 D4 9F EB D3 B6 65', + '01-02 03:04 05 06 07 ab', + '0102.03:04.0506.07ab', + '900f/dffs/sdea/54gh', + ], + }); + test({ + validator: 'isMACAddress', + args: [{ + eui: '48', + }], + valid: [ + 'ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:ab', + '01:AB:03:04:05:06', + 'A9 C5 D4 9F EB D3', + '01 02 03 04 05 ab', + '01-02-03-04-05-ab', + '0102.0304.05ab', + ], + invalid: [ + 'ab:ab:ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:06:07:ab', + '01:AB:03:04:05:06:07:08', + 'A9 C5 D4 9F EB D3 B6 65', + '01 02 03 04 05 06 07 ab', + '01-02-03-04-05-06-07-ab', + '0102.0304.0506.07ab', + ], + }); + test({ + validator: 'isMACAddress', + args: [{ + eui: '64', + }], + valid: [ + 'ab:ab:ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:06:07:ab', + '01:AB:03:04:05:06:07:08', + 'A9 C5 D4 9F EB D3 B6 65', + '01 02 03 04 05 06 07 ab', + '01-02-03-04-05-06-07-ab', + '0102.0304.0506.07ab', + ], + invalid: [ + 'ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:ab', + '01:AB:03:04:05:06', + 'A9 C5 D4 9F EB D3', + '01 02 03 04 05 ab', + '01-02-03-04-05-ab', + '0102.0304.05ab', ], }); }); @@ -837,6 +899,10 @@ describe('Validators', () => { 'FFFFFFFFFFFF', '0102030405ab', '01AB03040506', + 'abababababababab', + 'FFFFFFFFFFFFFFFF', + '01020304050607ab', + '01AB030405060708', ], invalid: [ 'abc', @@ -852,6 +918,56 @@ describe('Validators', () => { '01020304ab', '123456', 'ABCDEFGH0102', + '01:02:03:04:05:06:07', + '01:02:03:04:05:06::ab', + '1:2:3:4:5:6:7:8', + 'AB:CD:EF:GH:01:02:03:04', + 'ab:ab:ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:06:07:ab', + '01:AB:03:04:05:06:07:08', + '01020304050607', + '010203040506ab', + '12345678', + 'ABCDEFGH01020304', + ], + }); + test({ + validator: 'isMACAddress', + args: [{ + no_separators: true, + eui: '48', + }], + valid: [ + 'abababababab', + 'FFFFFFFFFFFF', + '0102030405ab', + '01AB03040506', + ], + invalid: [ + 'abababababababab', + 'FFFFFFFFFFFFFFFF', + '01020304050607ab', + '01AB030405060708', + ], + }); + test({ + validator: 'isMACAddress', + args: [{ + no_separators: true, + eui: '64', + }], + valid: [ + 'abababababababab', + 'FFFFFFFFFFFFFFFF', + '01020304050607ab', + '01AB030405060708', + ], + invalid: [ + 'abababababab', + 'FFFFFFFFFFFF', + '0102030405ab', + '01AB03040506', ], }); }); @@ -1168,7 +1284,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', @@ -1272,6 +1399,26 @@ describe('Validators', () => { }); }); + it('should validate Bengali alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['bn-BD'], + valid: [ + 'অয়াওর', + 'ফগফদ্রত', + 'ফদ্ম্যতভ', + 'বেরেওভচনভন', + 'আমারবাসগা', + ], + invalid: [ + 'দাস২৩৪', + ' দ্গফহ্নভ ', + '', + '(গফদ)', + ], + }); + }); + it('should validate czech alpha strings', () => { test({ validator: 'isAlpha', @@ -1431,6 +1578,32 @@ describe('Validators', () => { }); }); + it('should validate Japanese alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['ja-JP'], + valid: [ + 'あいうえお', + 'がぎぐげご', + 'ぁぃぅぇぉ', + 'アイウエオ', + 'ァィゥェ', + 'アイウエオ', + '吾輩は猫である', + '臥薪嘗胆', + '新世紀エヴァンゲリオン', + '天国と地獄', + '七人の侍', + 'シン・ウルトラマン', + ], + invalid: [ + 'あいう123', + 'abcあいう', + '1984', + ], + }); + }); + it('should validate Vietnamese alpha strings', () => { test({ validator: 'isAlpha', @@ -1797,6 +1970,47 @@ describe('Validators', () => { }); }); + it('should validate Korea alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['ko-KR'], + valid: [ + 'ㄱ', + 'ㅑ', + 'ㄱㄴㄷㅏㅕ', + '세종대왕', + '나랏말싸미듕귁에달아문자와로서르사맛디아니할쎄', + ], + invalid: [ + 'abc', + '123', + '흥선대원군 문호개방', + '1592년임진왜란', + '대한민국!', + ], + }); + }); + + 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', @@ -1923,6 +2137,25 @@ describe('Validators', () => { }); }); + it('should validate Bengali alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['bn-BD'], + valid: [ + 'দ্গজ্ঞহ্রত্য১২৩', + 'দ্গগফ৮৯০', + 'চব৩৬৫ভবচ', + '১২৩৪', + '৩৪২৩৪দফজ্ঞদফ', + ], + invalid: [ + ' ', + '১২৩ ', + 'hel৩২0', + ], + }); + }); + it('should validate czech alphanumeric strings', () => { test({ validator: 'isAlphanumeric', @@ -2178,6 +2411,28 @@ describe('Validators', () => { }); }); + it('should validate Japanese alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['ja-JP'], + valid: [ + 'あいうえお123', + '123がぎぐげご', + 'ぁぃぅぇぉ', + 'アイウエオ', + 'ァィゥェ', + 'アイウエオ', + '20世紀少年', + '華氏451度', + ], + invalid: [ + ' あいう123 ', + 'abcあいう', + '生きろ!!', + ], + }); + }); + it('should validate kurdish alphanumeric strings', () => { test({ validator: 'isAlphanumeric', @@ -2384,6 +2639,44 @@ describe('Validators', () => { }); }); + it('should validate Korea alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['ko-KR'], + valid: [ + '2002', + '훈민정음', + '1446년훈민정음반포', + ], + invalid: [ + '2022!', + '2019 코로나시작', + '1.로렘입숨', + ], + }); + }); + + 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', @@ -2571,6 +2864,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['AZ'], + valid: [ + 'AZE16175905', + 'AA1617595', + ], + invalid: [ + 'A12345843', + ], + }); + test({ validator: 'isPassportNumber', args: ['BE'], @@ -2906,6 +3211,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['JM'], + valid: [ + 'A0123456', + ], + invalid: [ + 's0123456', + 'a01234567', + ], + }); + test({ validator: 'isPassportNumber', args: ['JP'], @@ -2933,6 +3250,31 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['KZ'], + valid: [ + 'A0123456', + 'b0123456', + ], + invalid: [ + '01234567', + 'bb0123456', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['LI'], + valid: [ + 'a01234', + 'f01234', + ], + invalid: [ + '012345', + ], + }); + test({ validator: 'isPassportNumber', args: ['LT'], @@ -3022,6 +3364,19 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['MX'], + valid: [ + '43986369222', + '01234567890', + ], + invalid: [ + 'ABC34567890', + '34567890', + ], + }); + test({ validator: 'isPassportNumber', args: ['NL'], @@ -3034,42 +3389,91 @@ describe('Validators', () => { 'XR1001R58A', ], }); - test({ validator: 'isPassportNumber', - args: ['PL'], + args: ['PK'], valid: [ - 'ZS 0000177', - 'AN 3000011', + 'QZ1791293', + 'XR1001458', ], invalid: [ - 'A1 0000177', - '012345678', + 'XTR11013R', + 'XR1001R58A', ], }); test({ validator: 'isPassportNumber', - args: ['PT'], + args: ['PH'], valid: [ - 'I700044', - 'K453286', + 'X123456', + 'XY123456', + 'XY1234567', + 'X1234567Y', ], invalid: [ - '0700044', - 'K4532861', + 'XY12345', + 'X12345Z', + 'XY12345Z', ], }); test({ validator: 'isPassportNumber', - args: ['RO'], + args: ['NZ'], valid: [ - '05485968', - '040005646', + 'Lf012345', + 'La012345', + 'Ld012345', + 'Lh012345', + 'ea012345', + 'ep012345', + 'n012345', ], invalid: [ - 'R05485968', + 'Lp012345', + 'nd012345', + 'ed012345', + 'eh012345', + 'ef012345', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['PL'], + valid: [ + 'ZS 0000177', + 'AN 3000011', + ], + invalid: [ + 'A1 0000177', + '012345678', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['PT'], + valid: [ + 'I700044', + 'K453286', + ], + invalid: [ + '0700044', + 'K4532861', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['RO'], + valid: [ + '05485968', + '040005646', + ], + invalid: [ + 'R05485968', '0511060461', ], }); @@ -3129,6 +3533,22 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['TH'], + valid: [ + 'A123456', + 'B1234567', + 'CD123456', + 'EF1234567', + ], + invalid: [ + '123456', + '1234567', + '010485371AA', + ], + }); + test({ validator: 'isPassportNumber', args: ['TR'], @@ -3702,6 +4122,7 @@ describe('Validators', () => { ' ', '', '.', + ',', 'foo', '20.foo', '2020-01-06T14:31:00.135Z', @@ -4046,6 +4467,8 @@ describe('Validators', () => { 'rgba(3,3,3%,.3)', 'rgb(101%,101%,101%)', 'rgba(3%,3%,101%,0.3)', + 'rgb(101%,101%,101%) additional invalid string part', + 'rgba(3%,3%,101%,0.3) additional invalid string part', ], }); @@ -4463,6 +4886,11 @@ describe('Validators', () => { validator: 'isLength', valid: ['a', '', 'asds'], }); + test({ + validator: 'isLength', + args: [{ max: 8 }], + valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], + }); }); it('should validate strings by byte length', () => { @@ -4675,31 +5103,6 @@ describe('Validators', () => { }); }); - it('should validate dates against a start date', () => { - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2011-08-04', new Date(2011, 8, 10).toString()], - invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], - }); - test({ - validator: 'isAfter', - valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], - invalid: ['2010-07-02', new Date(0).toString()], - }); - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2015-09-17'], - invalid: ['invalid date'], - }); - test({ - validator: 'isAfter', - args: ['invalid date'], - invalid: ['invalid date', '2015-09-17'], - }); - }); - it('should validate dates against an end date', () => { test({ validator: 'isBefore', @@ -4780,6 +5183,7 @@ describe('Validators', () => { 'SBICKEN1', 'SBICKENY', 'SBICKEN1YYP', + 'SBICXKN1YYP', ], invalid: [ 'SBIC23NXXX', @@ -4788,6 +5192,7 @@ describe('Validators', () => { 'SBICKENXX9', 'SBICKEN13458', 'SBICKEN', + 'SBICXK', ], }); }); @@ -4808,6 +5213,34 @@ describe('Validators', () => { }); }); + it('should validate luhn numbers', () => { + test({ + validator: 'isLuhnNumber', + 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', @@ -4850,1463 +5283,1726 @@ describe('Validators', () => { }); }); - it('should validate identity cards', () => { - const fixtures = [ - { - locale: 'LK', - valid: [ - '722222222v', - '722222222V', - '993151225x', - '993151225X', - '188888388x', - '935632124V', - '199931512253', - '200023125632', - ], - invalid: [ - '023125648V', - '023345621v', - '021354211X', - '055321231x', - '02135465462', - '199931512253X', - ], - }, - { - locale: 'PL', - valid: [ - '99012229019', - '09210215408', - '20313034701', - '86051575214', - '77334586883', - '54007481320', - '06566860643', - '77552478861', - ], - invalid: [ - 'aa', - '5', - '195', - '', - ' ', - '12345678901', - '99212229019', - '09210215402', - '20313534701', - '86241579214', - ], - }, - { - locale: 'ES', - valid: [ - '99999999R', - '12345678Z', - '01234567L', - '01234567l', - 'X1234567l', - 'x1234567l', - 'X1234567L', - 'Y1234567X', - 'Z1234567R', - ], - invalid: [ - '123456789', - '12345678A', - '12345 678Z', - '12345678-Z', - '1234*6789', - '1234*678Z', - '12345678!', - '1234567L', - 'A1234567L', - 'X1234567A', - 'Y1234567B', - 'Z1234567C', - ], - }, - { - locale: 'FI', - valid: [ - '131052-308T', // People born in 1900s - '131052A308T', // People born in 2000s - '131052+308T', // People born in 1800s - '131052-313Y', - ], - invalid: [ - '131052308T', - '131052-308T ', - '131052-308A', - ], - }, - { - locale: 'IN', - valid: [ - '298448863364', - '2984 4886 3364', - ], - invalid: [ - '99999999R', - '12345678Z', - '01234567L', - '01234567l', - 'X1234567l', - 'x1234567l', - 'X1234567L', - ], - }, - { - locale: 'IR', - valid: [ - '0499370899', - '0790419904', - '0084575948', - '0963695398', - '0684159414', - '0067749828', - '0650451252', - '1583250689', - '4032152314', - '0076229645', - '4271467685', - '0200203241', - ], - invalid: [ - '1260293040', - '0000000001', - '1999999999', - '9999999991', - 'AAAAAAAAAA', - '0684159415', - ], - }, - { - locale: 'IT', - valid: [ - 'CR43675TM', - 'CA79382RA', - ], - invalid: [ - 'CA00000AA', - 'CB2342TG', - 'CS123456A', - 'C1236EC', - ], - }, - { - locale: 'NO', - valid: [ - '09053426694', - '26028338723', - '08031470790', - '12051539514', - '02077448074', - '14035638319', - '13031379673', - '29126214926', - ], - invalid: [ - '09053426699', - '00000000000', - '26028338724', - '92031470790', - ], - }, - { - locale: 'TH', - valid: [ - '1101230000001', - '1101230000060', - ], - invalid: [ - 'abc', - '1101230', - '11012300000011', - 'aaaaaaaaaaaaa', - '110123abcd001', - '1101230000007', - '0101123450000', - '0101123450004', - '9101123450008', - ], - }, - { - locale: 'he-IL', - valid: [ - '219472156', - '219486610', - '219488962', - '219566726', - '219640216', - '219645041', - '334795465', - '335211686', - '335240479', - '335472171', - '336999842', - '337090443', - ], - invalid: [ - '123456789', - '12345678A', - '12345 678Z', - '12345678-Z', - '1234*6789', - '1234*678Z', - '12345678!', - '1234567L', - 'A1234567L', - 'X1234567A', - 'Y1234567B', - 'Z1234567C', - '219772156', - '219487710', - '334705465', - '336000842', - ], - }, - { - locale: 'ar-LY', - valid: [ - '119803455876', - '120024679875', - '219624876201', - '220103480657', - ], - invalid: [ - '987654320123', - '123-456-7890', - '012345678912', - '1234567890', - 'AFJBHUYTREWR', - 'C4V6B1X0M5T6', - '9876543210123', - ], - }, - { - locale: 'ar-TN', - valid: [ - '09958092', - '09151092', - '65126506', - '79378815', - '58994407', - '73089789', - '73260311', - ], - invalid: [ - '123456789546', - '123456789', - '023456789', - '12345678A', - '12345', - '1234578A', - '123 578A', - '12345 678Z', - '12345678-Z', - '1234*6789', - '1234*678Z', - 'GE9800as98', - 'X231071922', - '1234*678Z', - '12345678!', - ], - }, - { - locale: 'zh-CN', - valid: [ - '235407195106112745', - '210203197503102721', - '520323197806058856', - '110101491001001', - ], - invalid: [ - '160323197806058856', - '010203197503102721', - '520323297806058856', - '520323197802318856', - '235407195106112742', - '010101491001001', - '110101491041001', - '160101491001001', - '110101940231001', - 'xx1234567', - '135407195106112742', - '123456789546', - '123456789', - '023456789', - '12345678A', - '12345', - '1234578A', - '123 578A', - '12345 678Z', - '12345678-Z', - '1234*6789', - '1234*678Z', - 'GE9800as98', - 'X231071922', - '1234*678Z', - '12345678!', - '235407207006112742', - ], - }, - { - locale: 'zh-TW', - valid: [ - 'B176944193', - 'K101189797', - 'F112866121', - 'A219758834', - 'A244144802', - 'A146047171', - 'Q170219004', - 'Z277018381', - 'X231071923', - ], - invalid: [ - '123456789', - 'A185034995', - 'X431071923', - 'GE9800as98', - 'X231071922', - '1234*678Z', - '12345678!', - '1234567L', - 'A1234567L', - 'X1234567A', - 'Y1234567B', - 'Z1234567C', - '219772156', - '219487710', - '334705465', - '336000842', - ], - }, - ]; - - let allValid = []; - - // Test fixtures - fixtures.forEach((fixture) => { - if (fixture.valid) allValid = allValid.concat(fixture.valid); - test({ - validator: 'isIdentityCard', - valid: fixture.valid, - invalid: fixture.invalid, - args: [fixture.locale], - }); - }); - // Test generics + it('should validate credit cards without a proper provider', () => { test({ - validator: 'isIdentityCard', - valid: [ - ...allValid, - ], - invalid: [ + validator: 'isCreditCard', + args: [{ provider: 'Plorf' }], + error: [ 'foo', + // valid cc # + '375556917985515', + '4716-2210-5188-5662', + '375556917985515999999993', + '6234917882863855suffix', ], - args: ['any'], }); }); - it('should error on invalid locale', () => { - test({ - validator: 'isIdentityCard', - args: ['is-NOT'], - error: [ - '99999999R', - '12345678Z', - ], - }); - }); - it('should validate ISINs', () => { + it('should validate AmEx provided credit cards', () => { test({ - validator: 'isISIN', + validator: 'isCreditCard', + args: [{ provider: 'AmEx' }], valid: [ - 'AU0000XVGZA3', - 'DE000BAY0017', - 'BE0003796134', - 'SG1G55870362', - 'GB0001411924', - 'DE000WCH8881', - 'PLLWBGD00016', - 'US0378331005', + '375556917985515', ], invalid: [ - 'DE000BAY0018', - 'PLLWBGD00019', 'foo', - '5398228707871528', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '5398228707871527', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); }); - it('should validate ISBNs', () => { - test({ - validator: 'isISBN', - args: [10], - valid: [ - '3836221195', '3-8362-2119-5', '3 8362 2119 5', - '1617290858', '1-61729-085-8', '1 61729 085-8', - '0007269706', '0-00-726970-6', '0 00 726970 6', - '3423214120', '3-423-21412-0', '3 423 21412 0', - '340101319X', '3-401-01319-X', '3 401 01319 X', - ], - invalid: [ - '3423214121', '3-423-21412-1', '3 423 21412 1', - '978-3836221191', '9783836221191', - '123456789a', 'foo', '', - ], - }); - test({ - validator: 'isISBN', - args: [13], - valid: [ - '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', - '9783401013190', '978-3401013190', '978 3401013190', - '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', - ], - invalid: [ - '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', - '3836221195', '3-8362-2119-5', '3 8362 2119 5', - '01234567890ab', 'foo', '', - ], - }); + + it('should validate Diners Club provided credit cards', () => { test({ - validator: 'isISBN', + validator: 'isCreditCard', + args: [{ provider: 'DinersClub' }], valid: [ - '340101319X', - '9784873113685', + '36050234196908', ], invalid: [ - '3423214121', - '9783836221190', - ], - }); - test({ - validator: 'isISBN', - args: ['foo'], - invalid: [ - '340101319X', - '9784873113685', + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '375556917985515', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '5398228707871527', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); }); - it('should validate EANs', () => { + it('should validate Discover provided credit cards', () => { test({ - validator: 'isEAN', + validator: 'isCreditCard', + args: [{ provider: 'Discover' }], valid: [ - '9421023610112', - '1234567890128', - '4012345678901', - '9771234567003', - '9783161484100', - '73513537', - '00012345600012', - '10012345678902', - '20012345678909', + '6011111111111117', + '6011000990139424', ], invalid: [ - '5901234123451', - '079777681629', - '0705632085948', + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '5398228707871527', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); }); - it('should validate ISSNs', () => { - test({ - validator: 'isISSN', - valid: [ - '0378-5955', - '0000-0000', - '2434-561X', - '2434-561x', - '01896016', - '20905076', - ], - invalid: [ - '0378-5954', - '0000-0001', - '0378-123', - '037-1234', - '0', - '2434-561c', - '1684-5370', - '19960791', - '', - ], - }); - test({ - validator: 'isISSN', - args: [{ case_sensitive: true }], - valid: [ - '2434-561X', - '2434561X', - '0378-5955', - '03785955', - ], - invalid: [ - '2434-561x', - '2434561x', - ], - }); + it('should validate JCB provided credit cards', () => { test({ - validator: 'isISSN', - args: [{ require_hyphen: true }], + validator: 'isCreditCard', + args: [{ provider: 'JCB' }], valid: [ - '2434-561X', - '2434-561x', - '0378-5955', + '3530111333300000', + '3566002020360505', ], invalid: [ - '2434561X', - '2434561x', - '03785955', + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '5398228707871527', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); + }); + + + it('should validate Mastercard provided credit cards', () => { test({ - validator: 'isISSN', - args: [{ case_sensitive: true, require_hyphen: true }], + validator: 'isCreditCard', + args: [{ provider: 'Mastercard' }], valid: [ - '2434-561X', - '0378-5955', + '2222155765072228', + '2225855203075256', + '2718760626256570', + '2720428011723762', + '5398228707871527', ], invalid: [ - '2434-561x', - '2434561X', - '2434561x', - '03785955', + 'foo', + '36050234196908', + '375556917985515', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); }); - it('should validate JSON', () => { + + it('should validate Union Pay provided credit cards', () => { test({ - validator: 'isJSON', + validator: 'isCreditCard', + args: [{ provider: 'UnionPay' }], valid: [ - '{ "key": "value" }', - '{}', + '6226050967750613', + '6234917882863855', + '6234698580215388', + '6246281879460688', + '6263892624162870', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], invalid: [ - '{ key: "value" }', - '{ \'key\': \'value\' }', - 'null', - '1234', - '"nope"', + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515', + '375556917985515999999993', + '4716461583322103', + '4716-2210-5188-5662', + '4716989580001715211', + '4929 7226 5379 7141', + '5398228707871527', + '6234917882863855suffix', ], }); }); - it('should validate JSON with primitives', () => { + + it('should validate Visa provided credit cards', () => { test({ - validator: 'isJSON', - args: [{ allow_primitives: true }], + validator: 'isCreditCard', + args: [{ provider: 'Visa' }], valid: [ - '{ "key": "value" }', - '{}', - 'null', - 'false', - 'true', + '4716-2210-5188-5662', + '4716461583322103', + '4716989580001715211', + '4929 7226 5379 7141', ], invalid: [ - '{ key: "value" }', - '{ \'key\': \'value\' }', - '{ "key": value }', - '1234', - '"nope"', + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515', + '375556917985515999999993', + '5398228707871527', + '6234917882863855suffix', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', ], }); }); - it('should validate multibyte strings', () => { - test({ - validator: 'isMultibyte', - valid: [ - 'ひらがな・カタカナ、.漢字', - 'あいうえお foobar', - 'test@example.com', - '1234abcDExyz', - 'カタカナ', - '中文', - ], - invalid: [ - 'abc', - 'abc123', - '<>@" *.', - ], - }); - }); - - it('should validate ascii strings', () => { - test({ - validator: 'isAscii', - valid: [ - 'foobar', - '0987654321', - 'test@example.com', - '1234abcDEF', - ], - invalid: [ - 'foobar', - 'xyz098', - '123456', - 'カタカナ', - ], - }); - }); - - it('should validate full-width strings', () => { - test({ - validator: 'isFullWidth', - valid: [ - 'ひらがな・カタカナ、.漢字', - '3ー0 a@com', - 'Fカタカナ゙ᆲ', - 'Good=Parts', - ], - invalid: [ - 'abc', - 'abc123', - '!"#$%&()<>/+=-_? ~^|.,@`{}[]', - ], - }); - }); - - it('should validate half-width strings', () => { - test({ - validator: 'isHalfWidth', - valid: [ - '!"#$%&()<>/+=-_? ~^|.,@`{}[]', - 'l-btn_02--active', - 'abc123い', - 'カタカナ゙ᆲ←', - ], - invalid: [ - 'あいうえお', - '0011', - ], - }); - }); - - it('should validate variable-width strings', () => { - test({ - validator: 'isVariableWidth', - valid: [ - 'ひらがなカタカナ漢字ABCDE', - '3ー0123', - 'Fカタカナ゙ᆲ', - 'Good=Parts', - ], - invalid: [ - 'abc', - 'abc123', - '!"#$%&()<>/+=-_? ~^|.,@`{}[]', - 'ひらがな・カタカナ、.漢字', - '123456', - 'カタカナ゙ᆲ', - ], - }); - }); - - it('should validate surrogate pair strings', () => { - test({ - validator: 'isSurrogatePair', - valid: [ - '𠮷野𠮷', - '𩸽', - 'ABC千𥧄1-2-3', - ], - invalid: [ - '吉野竈', - '鮪', - 'ABC1-2-3', - ], - }); - }); - - it('should validate Semantic Versioning Specification (SemVer) strings', () => { - test({ - validator: 'isSemVer', - valid: [ - '0.0.4', - '1.2.3', - '10.20.30', - '1.1.2-prerelease+meta', - '1.1.2+meta', - '1.1.2+meta-valid', - '1.0.0-alpha', - '1.0.0-beta', - '1.0.0-alpha.beta', - '1.0.0-alpha.beta.1', - '1.0.0-alpha.1', - '1.0.0-alpha0.valid', - '1.0.0-alpha.0valid', - '1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay', - '1.0.0-rc.1+build.1', - '2.0.0-rc.1+build.123', - '1.2.3-beta', - '10.2.3-DEV-SNAPSHOT', - '1.2.3-SNAPSHOT-123', - '1.0.0', - '2.0.0', - '1.1.7', - '2.0.0+build.1848', - '2.0.1-alpha.1227', - '1.0.0-alpha+beta', - '1.2.3----RC-SNAPSHOT.12.9.1--.12+788', - '1.2.3----R-S.12.9.1--.12+meta', - '1.2.3----RC-SNAPSHOT.12.9.1--.12', - '1.0.0+0.build.1-rc.10000aaa-kk-0.1', - '99999999999999999999999.999999999999999999.99999999999999999', - '1.0.0-0A.is.legal', - ], - invalid: [ - '-invalid+invalid', - '-invalid.01', - 'alpha', - 'alpha.beta', - 'alpha.beta.1', - 'alpha.1', - 'alpha+beta', - 'alpha_beta', - 'alpha.', - 'alpha..', - 'beta', - '1.0.0-alpha_beta', - '-alpha.', - '1.0.0-alpha..', - '1.0.0-alpha..1', - '1.0.0-alpha...1', - '1.0.0-alpha....1', - '1.0.0-alpha.....1', - '1.0.0-alpha......1', - '1.0.0-alpha.......1', - '01.1.1', - '1.01.1', - '1.1.01', - '1.2', - '1.2.3.DEV', - '1.2-SNAPSHOT', - '1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788', - '1.2-RC-SNAPSHOT', - '-1.0.3-gamma+b7718', - '+justmeta', - '9.8.7+meta+meta', - '9.8.7-whatever+meta+meta', - '99999999999999999999999.999999999999999999.99999999999999999-', - '---RC-SNAPSHOT.12.09.1--------------------------------..12', - ], - }); - }); - - it('should validate base32 strings', () => { - test({ - validator: 'isBase32', - valid: [ - 'ZG======', - 'JBSQ====', - 'JBSWY===', - 'JBSWY3A=', - 'JBSWY3DP', - 'JBSWY3DPEA======', - 'K5SWYY3PNVSSA5DPEBXG6ZA=', - 'K5SWYY3PNVSSA5DPEBXG6===', - ], - invalid: [ - '12345', - '', - 'JBSWY3DPtesting123', - 'ZG=====', - 'Z======', - 'Zm=8JBSWY3DP', - '=m9vYg==', - 'Zm9vYm/y====', - ], - }); - }); - - it('should validate base58 strings', () => { - test({ - validator: 'isBase58', - valid: [ - 'BukQL', - '3KMUV89zab', - '91GHkLMNtyo98', - 'YyjKm3H', - 'Mkhss145TRFg', - '7678765677', - 'abcodpq', - 'AAVHJKLPY', - ], - invalid: [ - '0OPLJH', - 'IMKLP23', - 'KLMOmk986', - 'LL1l1985hG', - '*MP9K', - 'Zm=8JBSWY3DP', - ')()(=9292929MKL', - ], - }); - }); - - it('should validate base64 strings', () => { - test({ - validator: 'isBase64', - valid: [ - '', - 'Zg==', - 'Zm8=', - 'Zm9v', - 'Zm9vYg==', - 'Zm9vYmE=', - 'Zm9vYmFy', - 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4=', - 'Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg==', - 'U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==', - 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMPNS1Ufof9EW/M98FNw' + - 'UAKrwflsqVxaxQjBQnHQmiI7Vac40t8x7pIb8gLGV6wL7sBTJiPovJ0V7y7oc0Ye' + - 'rhKh0Rm4skP2z/jHwwZICgGzBvA0rH8xlhUiTvcwDCJ0kc+fh35hNt8srZQM4619' + - 'FTgB66Xmp4EtVyhpQV+t02g6NzK72oZI0vnAvqhpkxLeLiMCyrI416wHm5Tkukhx' + - 'QmcL2a6hNOyu0ixX/x2kSFXApEnVrJ+/IxGyfyw8kf4N2IZpW5nEP847lpfj0SZZ' + - 'Fwrd1mnfnDbYohX2zRptLy2ZUn06Qo9pkG5ntvFEPo9bfZeULtjYzIl6K8gJ2uGZ' + - 'HQIDAQAB', - ], - invalid: [ - '12345', - 'Vml2YW11cyBmZXJtZtesting123', - 'Zg=', - 'Z===', - 'Zm=8', - '=m9vYg==', - 'Zm9vYmFy====', - ], - }); - test({ - validator: 'isBase64', - args: [{ urlSafe: true }], - valid: [ - '', - 'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ', - '1234', - 'bXVtLW5ldmVyLXByb3Vk', - 'PDw_Pz8-Pg', - 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw', - ], - invalid: [ - ' AA', - '\tAA', - '\rAA', - '\nAA', - 'This+isa/bad+base64Url==', - '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw', - ], - error: [ - null, - undefined, - {}, - [], - 42, - ], - }); - - for (let i = 0, str = '', encoded; i < 1000; i++) { - str += String.fromCharCode(Math.random() * 26 | 97); // eslint-disable-line no-bitwise - encoded = Buffer.from(str).toString('base64'); - if (!validator.isBase64(encoded)) { - let msg = format('validator.isBase64() failed with "%s"', encoded); - throw new Error(msg); - } - } - }); - - it('should validate hex-encoded MongoDB ObjectId', () => { - test({ - validator: 'isMongoId', - valid: [ - '507f1f77bcf86cd799439011', - ], - invalid: [ - '507f1f77bcf86cd7994390', - '507f1f77bcf86cd79943901z', - '', - '507f1f77bcf86cd799439011 ', - ], - }); - }); - - it('should define the module using an AMD-compatible loader', () => { - let window = { - validator: null, - define(module) { - window.validator = module(); - }, - }; - window.define.amd = true; - - let sandbox = vm.createContext(window); - vm.runInContext(validator_js, sandbox); - assert.strictEqual(window.validator.trim(' foobar '), 'foobar'); - }); - - it('should bind validator to the window if no module loaders are available', () => { - let window = {}; - let sandbox = vm.createContext(window); - vm.runInContext(validator_js, sandbox); - assert.strictEqual(window.validator.trim(' foobar '), 'foobar'); - }); - - it('should validate mobile phone number', () => { - let fixtures = [ + it('should validate identity cards', () => { + const fixtures = [ { - locale: 'am-AM', + locale: 'zh-HK', valid: [ - '+37410324123', - '+37422298765', - '+37431276521', - '022698763', - '37491987654', - '+37494567890', + 'OV290326[A]', + 'Q803337[0]', + 'Z0977986', + 'W520128(7)', + 'A494866[4]', + 'A494866(4)', + 'Z867821A', + 'ag293013(9)', + 'k348609(5)', ], invalid: [ - '12345', - '+37411498855', - '+37411498123', - '05614988556', - '', - '37456789000', + 'A1234567890', + '98765432', + 'O962472(9)', + 'M4578601', + 'X731324[8]', + 'C503134(5)', + 'RH265886(3)', ], }, { - locale: 'ar-AE', + locale: 'LK', valid: [ - '+971502674453', - '+971521247658', - '+971541255684', - '+971555454458', - '+971561498855', - '+971585215778', - '971585215778', - '0585215778', - '585215778', + '722222222v', + '722222222V', + '993151225x', + '993151225X', + '188888388x', + '935632124V', + '199931512253', + '200023125632', ], invalid: [ - '12345', - '+971511498855', - '+9715614988556', - '+9745614988556', - '', - '+9639626626262', - '+963332210972', - '0114152198', - '962796477263', + '023125648V', + '023345621v', + '021354211X', + '055321231x', + '02135465462', + '199931512253X', ], }, { - locale: 'ar-BH', + locale: 'PL', valid: [ - '+97335078110', - '+97339534385', - '+97366331055', - '+97333146000', - '97335078110', - '35078110', - '66331055', + '99012229019', + '09210215408', + '20313034701', + '86051575214', + '77334586883', + '54007481320', + '06566860643', + '77552478861', ], invalid: [ - '12345', - '+973350781101', - '+97379534385', - '+973035078110', + 'aa', + '5', + '195', '', - '+9639626626262', - '+963332210972', - '0114152198', - '962796477263', - '035078110', - '16331055', - 'hello', - '+9733507811a', + ' ', + '12345678901', + '99212229019', + '09210215402', + '20313534701', + '86241579214', ], }, { - locale: 'ar-EG', + locale: 'ES', valid: [ - '+201004513789', - '+201111453489', - '+201221204610', - '+201144621154', - '+201200124304', - '+201011201564', - '+201124679001', - '+201064790156', - '+201274652177', - '+201280134679', - '+201090124576', - '+201583728900', - '201599495596', - '201090124576', - '01090124576', - '01538920744', - '1593075993', - '1090124576', + '99999999R', + '12345678Z', + '01234567L', + '01234567l', + 'X1234567l', + 'x1234567l', + 'X1234567L', + 'Y1234567X', + 'Z1234567R', ], invalid: [ - '+221004513789', - '+201404513789', - '12345', - '', - '+9639626626262', - '+963332210972', - '0114152198', - '962796477263', + '123456789', + '12345678A', + '12345 678Z', + '12345678-Z', + '1234*6789', + '1234*678Z', + '12345678!', + '1234567L', + 'A1234567L', + 'X1234567A', + 'Y1234567B', + 'Z1234567C', ], }, { - locale: 'ar-JO', + locale: 'FI', valid: [ - '0796477263', - '0777866254', - '0786725261', - '+962796477263', - '+962777866254', - '+962786725261', - '962796477263', - '962777866254', - '962786725261', + '131052-308T', // People born in 1900s + '131052A308T', // People born in 2000s + '131052+308T', // People born in 1800s + '131052-313Y', ], invalid: [ - '00962786725261', - '00962796477263', - '12345', - '', - '+9639626626262', - '+963332210972', - '0114152198', + '131052308T', + '131052-308T ', + '131052-308A', ], }, { - locale: 'ar-KW', + locale: 'IN', valid: [ - '96550000000', - '96560000000', - '96590000000', - '+96550000000', - '+96550000220', - '+96551111220', + '298448863364', + '2984 4886 3364', ], invalid: [ - '+96570000220', - '00962786725261', - '00962796477263', - '12345', - '', - '+9639626626262', - '+963332210972', - '0114152198', - ], - }, - { - locale: 'ar-LB', - valid: [ - '+96171234568', - '+9613123456', - '3456123', - '3123456', - '81978468', - '77675798', - ], - invalid: [ - '+961712345688888', - '00912220000', - '7767579888', - '+0921110000', - '+3123456888', - '021222200000', - '213333444444', - '', - '+212234', - '+21', - '02122333', + '99999999R', + '12345678Z', + '01234567L', + '01234567l', + 'X1234567l', + 'x1234567l', + 'X1234567L', ], }, { - locale: 'ar-LY', + locale: 'IR', valid: [ - '912220000', - '0923330000', - '218945550000', - '+218958880000', - '212220000', - '0212220000', - '+218212220000', + '0499370899', + '0790419904', + '0084575948', + '0963695398', + '0684159414', + '0067749828', + '0650451252', + '1583250689', + '4032152314', + '0076229645', + '4271467685', + '0200203241', ], invalid: [ - '9122220000', - '00912220000', - '09211110000', - '+0921110000', - '+2180921110000', - '021222200000', - '213333444444', - '', - '+212234', - '+21', - '02122333', + '1260293040', + '0000000001', + '1999999999', + '9999999991', + 'AAAAAAAAAA', + '0684159415', ], }, { - locale: 'ar-MA', + locale: 'IT', valid: [ - '0522714782', - '0690851123', - '0708186135', - '+212522714782', - '+212690851123', - '+212708186135', - '00212522714782', - '00212690851123', - '00212708186135', + 'CR43675TM', + 'CA79382RA', ], invalid: [ - '522714782', - '690851123', - '708186135', - '212522714782', - '212690851123', - '212708186135', - '0212522714782', - '0212690851123', - '0212708186135', - '', - '12345', - '0922714782', - '+212190851123', - '00212408186135', + 'CA00000AA', + 'CB2342TG', + 'CS123456A', + 'C1236EC', ], }, { - locale: 'dz-BT', + locale: 'NO', valid: [ - '+97517374354', - '+97517454971', - '77324646', - '016329712', - '97517265559', + '09053426694', + '26028338723', + '08031470790', + '12051539514', + '02077448074', + '14035638319', + '13031379673', + '29126214926', ], invalid: [ - '', - '9898347255', - '+96326626262', - '963372', - '0114152198', + '09053426699', + '00000000000', + '26028338724', + '92031470790', ], }, { - locale: 'ar-OM', + locale: 'TH', valid: [ - '+96891212121', - '0096899999999', - '93112211', - '99099009', + '1101230000001', + '1101230000060', ], invalid: [ - '+96890212121', - '0096890999999', - '0090999999', - '+9689021212', - '', - '+212234', - '+21', - '02122333', + 'abc', + '1101230', + '11012300000011', + 'aaaaaaaaaaaaa', + '110123abcd001', + '1101230000007', + '0101123450000', + '0101123450004', + '9101123450008', ], }, { - locale: 'ar-PS', + locale: 'he-IL', valid: [ - '+970563459876', - '970592334218', - '0566372345', - '0598273583', + '219472156', + '219486610', + '219488962', + '219566726', + '219640216', + '219645041', + '334795465', + '335211686', + '335240479', + '335472171', + '336999842', + '337090443', ], invalid: [ - '+9759029487', - '97059123456789', - '598372348', - '97058aaaafjd', - '', - '05609123484', - '+97059', - '+970', - '97056', + '123456789', + '12345678A', + '12345 678Z', + '12345678-Z', + '1234*6789', + '1234*678Z', + '12345678!', + '1234567L', + 'A1234567L', + 'X1234567A', + 'Y1234567B', + 'Z1234567C', + '219772156', + '219487710', + '334705465', + '336000842', ], }, { - locale: 'ar-SY', + locale: 'ar-LY', valid: [ - '0944549710', - '+963944549710', - '956654379', - '0944549710', - '0962655597', + '119803455876', + '120024679875', + '219624876201', + '220103480657', ], invalid: [ - '12345', - '', - '+9639626626262', - '+963332210972', - '0114152198', + '987654320123', + '123-456-7890', + '012345678912', + '1234567890', + 'AFJBHUYTREWR', + 'C4V6B1X0M5T6', + '9876543210123', ], }, { - locale: 'ar-SA', + locale: 'ar-TN', valid: [ - '0556578654', - '+966556578654', - '966556578654', - '596578654', - '572655597', + '09958092', + '09151092', + '65126506', + '79378815', + '58994407', + '73089789', + '73260311', ], invalid: [ + '123456789546', + '123456789', + '023456789', + '12345678A', '12345', - '', - '+9665626626262', - '+96633221097', - '0114152198', + '1234578A', + '123 578A', + '12345 678Z', + '12345678-Z', + '1234*6789', + '1234*678Z', + 'GE9800as98', + 'X231071922', + '1234*678Z', + '12345678!', ], }, { - locale: 'ar-TN', + locale: 'zh-CN', valid: [ - '23456789', - '+21623456789', - '21623456789', + '235407195106112745', + '210203197503102721', + '520323197806058856', + '110101491001001', ], invalid: [ - '12345', - '75200123', - '+216512345678', - '13520459', - '85479520', - ], - }, - { - locale: 'bg-BG', - valid: [ - '+359897123456', - '+359898888888', - '0897123123', - ], - invalid: [ - '', - '0898123', - '+359212555666', - '18001234567', - '12125559999', - ], - }, - { - locale: 'bn-BD', - valid: [ - '+8801794626846', - '01399098893', - '8801671163269', - '01717112029', - '8801898765432', - '+8801312345678', - '01494676946', - ], - invalid: [ - '', - '0174626346', - '017943563469', - '18001234567', - '0131234567', - ], - }, - { - locale: 'bs-BA', - valid: [ - '060123456', - '061123456', - '062123456', - '063123456', - '0641234567', - '065123456', - '066123456', - '+38760123456', - '+38761123456', - '+38762123456', - '+38763123456', - '+387641234567', - '+38765123456', - '+38766123456', - '0038760123456', - '0038761123456', - '0038762123456', - '0038763123456', - '00387641234567', - '0038765123456', - '0038766123456', - ], - invalid: [ - '0601234567', - '0611234567', - '06212345', - '06312345', - '064123456', - '0651234567', - '06612345', - '+3866123456', - '+3856123456', - '00038760123456', - '038761123456', - ], - }, - { - locale: 'cs-CZ', - valid: [ - '+420 123 456 789', - '+420 123456789', - '+420123456789', - '123 456 789', - '123456789', - ], - invalid: [ - '', - '+42012345678', - '+421 123 456 789', - '+420 023456789', - '+4201234567892', - ], - }, - { - locale: 'sk-SK', - valid: [ - '+421 123 456 789', - '+421 123456789', - '+421123456789', - '123 456 789', + '160323197806058856', + '010203197503102721', + '520323297806058856', + '520323197802318856', + '235407195106112742', + '010101491001001', + '110101491041001', + '160101491001001', + '110101940231001', + 'xx1234567', + '135407195106112742', + '123456789546', '123456789', - ], - invalid: [ - '', - '+42112345678', - '+422 123 456 789', - '+421 023456789', - '+4211234567892', - ], - }, - { - locale: 'de-DE', - valid: [ - '+4915123456789', - '+4930405044550', - '015123456789', - '015123456789', - '015623456789', - '015623456789', - '01601234567', - '016012345678', - '01621234567', - '01631234567', - '01701234567', - '017612345678', - '015345678910', - '015412345678', - ], - invalid: [ - '34412345678', - '14412345678', - '16212345678', - '1761234567', - '16412345678', - '17012345678', - '+4912345678910', - '+49015123456789', - ], - }, - { - locale: 'de-AT', - valid: [ - '+436761234567', - '06761234567', - '00436123456789', - '+436123456789', - '01999', - '+4372876', - '06434908989562345', - ], - invalid: [ - '167612345678', - '1234', - '064349089895623459', - ], - }, - { - locale: 'hu-HU', - valid: [ - '06301234567', - '+36201234567', - '06701234567', - ], - invalid: [ - '1234', - '06211234567', - '+3620123456', + '023456789', + '12345678A', + '12345', + '1234578A', + '123 578A', + '12345 678Z', + '12345678-Z', + '1234*6789', + '1234*678Z', + 'GE9800as98', + 'X231071922', + '1234*678Z', + '12345678!', + '235407207006112742', ], }, { - locale: 'mz-MZ', + locale: 'zh-TW', valid: [ - '+258849229754', - '258849229754', - '849229754', - '829229754', - '839229754', - '869229754', - '859229754', - '869229754', - '879229754', - '+258829229754', - '+258839229754', - '+258869229754', - '+258859229754', - '+258869229754', - '+258879229754', - '258829229754', - '258839229754', - '258869229754', - '258859229754', - '258869229754', - '258879229754', + 'B176944193', + 'K101189797', + 'F112866121', + 'A219758834', + 'A244144802', + 'A146047171', + 'Q170219004', + 'Z277018381', + 'X231071923', ], invalid: [ - '+248849229754', - '158849229754', - '249229754', - '819229754', - '899229754', - '889229754', - '89229754', - '8619229754', - '87922975411', - '257829229754', - '+255839229754', - '+2258869229754', - '+1258859229754', - '+2588692297541', - '+2588792519754', - '25882922975411', + '123456789', + 'A185034995', + 'X431071923', + 'GE9800as98', + 'X231071922', + '1234*678Z', + '12345678!', + '1234567L', + 'A1234567L', + 'X1234567A', + 'Y1234567B', + 'Z1234567C', + '219772156', + '219487710', + '334705465', + '336000842', ], }, - { + ]; + + let allValid = []; + + // Test fixtures + fixtures.forEach((fixture) => { + if (fixture.valid) allValid = allValid.concat(fixture.valid); + test({ + validator: 'isIdentityCard', + valid: fixture.valid, + invalid: fixture.invalid, + args: [fixture.locale], + }); + }); + + // Test generics + test({ + validator: 'isIdentityCard', + valid: [ + ...allValid, + ], + invalid: [ + 'foo', + ], + args: ['any'], + }); + }); + + it('should error on invalid locale', () => { + test({ + validator: 'isIdentityCard', + args: ['is-NOT'], + error: [ + '99999999R', + '12345678Z', + ], + }); + }); + + it('should validate ISINs', () => { + test({ + validator: 'isISIN', + valid: [ + 'AU0000XVGZA3', + 'DE000BAY0017', + 'BE0003796134', + 'SG1G55870362', + 'GB0001411924', + 'DE000WCH8881', + 'PLLWBGD00016', + 'US0378331005', + ], + invalid: [ + 'DE000BAY0018', + 'PLLWBGD00019', + 'foo', + '5398228707871528', + ], + }); + }); + + it('should validate EANs', () => { + test({ + validator: 'isEAN', + valid: [ + '9421023610112', + '1234567890128', + '4012345678901', + '9771234567003', + '9783161484100', + '73513537', + '00012345600012', + '10012345678902', + '20012345678909', + ], + invalid: [ + '5901234123451', + '079777681629', + '0705632085948', + ], + }); + }); + + it('should validate ISSNs', () => { + test({ + validator: 'isISSN', + valid: [ + '0378-5955', + '0000-0000', + '2434-561X', + '2434-561x', + '01896016', + '20905076', + ], + invalid: [ + '0378-5954', + '0000-0001', + '0378-123', + '037-1234', + '0', + '2434-561c', + '1684-5370', + '19960791', + '', + ], + }); + test({ + validator: 'isISSN', + args: [{ case_sensitive: true }], + valid: [ + '2434-561X', + '2434561X', + '0378-5955', + '03785955', + ], + invalid: [ + '2434-561x', + '2434561x', + ], + }); + test({ + validator: 'isISSN', + args: [{ require_hyphen: true }], + valid: [ + '2434-561X', + '2434-561x', + '0378-5955', + ], + invalid: [ + '2434561X', + '2434561x', + '03785955', + ], + }); + test({ + validator: 'isISSN', + args: [{ case_sensitive: true, require_hyphen: true }], + valid: [ + '2434-561X', + '0378-5955', + ], + invalid: [ + '2434-561x', + '2434561X', + '2434561x', + '03785955', + ], + }); + }); + + it('should validate JSON', () => { + test({ + validator: 'isJSON', + valid: [ + '{ "key": "value" }', + '{}', + ], + invalid: [ + '{ key: "value" }', + '{ \'key\': \'value\' }', + 'null', + '1234', + '"nope"', + ], + }); + }); + + it('should validate JSON with primitives', () => { + test({ + validator: 'isJSON', + args: [{ allow_primitives: true }], + valid: [ + '{ "key": "value" }', + '{}', + 'null', + 'false', + 'true', + ], + invalid: [ + '{ key: "value" }', + '{ \'key\': \'value\' }', + '{ "key": value }', + '1234', + '"nope"', + ], + }); + }); + + it('should validate multibyte strings', () => { + test({ + validator: 'isMultibyte', + valid: [ + 'ひらがな・カタカナ、.漢字', + 'あいうえお foobar', + 'test@example.com', + '1234abcDExyz', + 'カタカナ', + '中文', + ], + invalid: [ + 'abc', + 'abc123', + '<>@" *.', + ], + }); + }); + + it('should validate ascii strings', () => { + test({ + validator: 'isAscii', + valid: [ + 'foobar', + '0987654321', + 'test@example.com', + '1234abcDEF', + ], + invalid: [ + 'foobar', + 'xyz098', + '123456', + 'カタカナ', + ], + }); + }); + + it('should validate full-width strings', () => { + test({ + validator: 'isFullWidth', + valid: [ + 'ひらがな・カタカナ、.漢字', + '3ー0 a@com', + 'Fカタカナ゙ᆲ', + 'Good=Parts', + ], + invalid: [ + 'abc', + 'abc123', + '!"#$%&()<>/+=-_? ~^|.,@`{}[]', + ], + }); + }); + + it('should validate half-width strings', () => { + test({ + validator: 'isHalfWidth', + valid: [ + '!"#$%&()<>/+=-_? ~^|.,@`{}[]', + 'l-btn_02--active', + 'abc123い', + 'カタカナ゙ᆲ←', + ], + invalid: [ + 'あいうえお', + '0011', + ], + }); + }); + + it('should validate variable-width strings', () => { + test({ + validator: 'isVariableWidth', + valid: [ + 'ひらがなカタカナ漢字ABCDE', + '3ー0123', + 'Fカタカナ゙ᆲ', + 'Good=Parts', + ], + invalid: [ + 'abc', + 'abc123', + '!"#$%&()<>/+=-_? ~^|.,@`{}[]', + 'ひらがな・カタカナ、.漢字', + '123456', + 'カタカナ゙ᆲ', + ], + }); + }); + + it('should validate surrogate pair strings', () => { + test({ + validator: 'isSurrogatePair', + valid: [ + '𠮷野𠮷', + '𩸽', + 'ABC千𥧄1-2-3', + ], + invalid: [ + '吉野竈', + '鮪', + 'ABC1-2-3', + ], + }); + }); + + it('should validate Semantic Versioning Specification (SemVer) strings', () => { + test({ + validator: 'isSemVer', + valid: [ + '0.0.4', + '1.2.3', + '10.20.30', + '1.1.2-prerelease+meta', + '1.1.2+meta', + '1.1.2+meta-valid', + '1.0.0-alpha', + '1.0.0-beta', + '1.0.0-alpha.beta', + '1.0.0-alpha.beta.1', + '1.0.0-alpha.1', + '1.0.0-alpha0.valid', + '1.0.0-alpha.0valid', + '1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay', + '1.0.0-rc.1+build.1', + '2.0.0-rc.1+build.123', + '1.2.3-beta', + '10.2.3-DEV-SNAPSHOT', + '1.2.3-SNAPSHOT-123', + '1.0.0', + '2.0.0', + '1.1.7', + '2.0.0+build.1848', + '2.0.1-alpha.1227', + '1.0.0-alpha+beta', + '1.2.3----RC-SNAPSHOT.12.9.1--.12+788', + '1.2.3----R-S.12.9.1--.12+meta', + '1.2.3----RC-SNAPSHOT.12.9.1--.12', + '1.0.0+0.build.1-rc.10000aaa-kk-0.1', + '99999999999999999999999.999999999999999999.99999999999999999', + '1.0.0-0A.is.legal', + ], + invalid: [ + '-invalid+invalid', + '-invalid.01', + 'alpha', + 'alpha.beta', + 'alpha.beta.1', + 'alpha.1', + 'alpha+beta', + 'alpha_beta', + 'alpha.', + 'alpha..', + 'beta', + '1.0.0-alpha_beta', + '-alpha.', + '1.0.0-alpha..', + '1.0.0-alpha..1', + '1.0.0-alpha...1', + '1.0.0-alpha....1', + '1.0.0-alpha.....1', + '1.0.0-alpha......1', + '1.0.0-alpha.......1', + '01.1.1', + '1.01.1', + '1.1.01', + '1.2', + '1.2.3.DEV', + '1.2-SNAPSHOT', + '1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788', + '1.2-RC-SNAPSHOT', + '-1.0.3-gamma+b7718', + '+justmeta', + '9.8.7+meta+meta', + '9.8.7-whatever+meta+meta', + '99999999999999999999999.999999999999999999.99999999999999999-', + '---RC-SNAPSHOT.12.09.1--------------------------------..12', + ], + }); + }); + + it('should validate base32 strings', () => { + test({ + validator: 'isBase32', + valid: [ + 'ZG======', + 'JBSQ====', + 'JBSWY===', + 'JBSWY3A=', + 'JBSWY3DP', + 'JBSWY3DPEA======', + 'K5SWYY3PNVSSA5DPEBXG6ZA=', + 'K5SWYY3PNVSSA5DPEBXG6===', + ], + invalid: [ + '12345', + '', + 'JBSWY3DPtesting123', + 'ZG=====', + 'Z======', + 'Zm=8JBSWY3DP', + '=m9vYg==', + 'Zm9vYm/y====', + ], + }); + }); + + 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', + valid: [ + 'BukQL', + '3KMUV89zab', + '91GHkLMNtyo98', + 'YyjKm3H', + 'Mkhss145TRFg', + '7678765677', + 'abcodpq', + 'AAVHJKLPY', + ], + invalid: [ + '0OPLJH', + 'IMKLP23', + 'KLMOmk986', + 'LL1l1985hG', + '*MP9K', + 'Zm=8JBSWY3DP', + ')()(=9292929MKL', + ], + }); + }); + + it('should validate base64 strings', () => { + test({ + validator: 'isBase64', + valid: [ + '', + 'Zg==', + 'Zm8=', + 'Zm9v', + 'Zm9vYg==', + 'Zm9vYmE=', + 'Zm9vYmFy', + 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4=', + 'Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg==', + 'U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==', + 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMPNS1Ufof9EW/M98FNw' + + 'UAKrwflsqVxaxQjBQnHQmiI7Vac40t8x7pIb8gLGV6wL7sBTJiPovJ0V7y7oc0Ye' + + 'rhKh0Rm4skP2z/jHwwZICgGzBvA0rH8xlhUiTvcwDCJ0kc+fh35hNt8srZQM4619' + + 'FTgB66Xmp4EtVyhpQV+t02g6NzK72oZI0vnAvqhpkxLeLiMCyrI416wHm5Tkukhx' + + 'QmcL2a6hNOyu0ixX/x2kSFXApEnVrJ+/IxGyfyw8kf4N2IZpW5nEP847lpfj0SZZ' + + 'Fwrd1mnfnDbYohX2zRptLy2ZUn06Qo9pkG5ntvFEPo9bfZeULtjYzIl6K8gJ2uGZ' + + 'HQIDAQAB', + ], + invalid: [ + '12345', + 'Vml2YW11cyBmZXJtZtesting123', + 'Zg=', + 'Z===', + 'Zm=8', + '=m9vYg==', + 'Zm9vYmFy====', + ], + }); + + test({ + validator: 'isBase64', + args: [{ urlSafe: true }], + valid: [ + '', + 'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ', + '1234', + 'bXVtLW5ldmVyLXByb3Vk', + 'PDw_Pz8-Pg', + 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw', + ], + invalid: [ + ' AA', + '\tAA', + '\rAA', + '\nAA', + 'This+isa/bad+base64Url==', + '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw', + ], + error: [ + null, + undefined, + {}, + [], + 42, + ], + }); + + for (let i = 0, str = '', encoded; i < 1000; i++) { + str += String.fromCharCode(Math.random() * 26 | 97); // eslint-disable-line no-bitwise + encoded = Buffer.from(str).toString('base64'); + if (!validator.isBase64(encoded)) { + let msg = format('validator.isBase64() failed with "%s"', encoded); + throw new Error(msg); + } + } + }); + + it('should validate hex-encoded MongoDB ObjectId', () => { + test({ + validator: 'isMongoId', + valid: [ + '507f1f77bcf86cd799439011', + ], + invalid: [ + '507f1f77bcf86cd7994390', + '507f1f77bcf86cd79943901z', + '', + '507f1f77bcf86cd799439011 ', + ], + }); + }); + + it('should define the module using an AMD-compatible loader', () => { + let window = { + validator: null, + define(module) { + window.validator = module(); + }, + }; + window.define.amd = true; + + let sandbox = vm.createContext(window); + vm.runInContext(validator_js, sandbox); + assert.strictEqual(window.validator.trim(' foobar '), 'foobar'); + }); + + it('should bind validator to the window if no module loaders are available', () => { + let window = {}; + let sandbox = vm.createContext(window); + vm.runInContext(validator_js, sandbox); + assert.strictEqual(window.validator.trim(' foobar '), 'foobar'); + }); + + it('should validate mobile phone number', () => { + let fixtures = [ + { + locale: 'am-AM', + valid: [ + '+37410324123', + '+37422298765', + '+37431276521', + '022698763', + '37491987654', + '+37494567890', + ], + invalid: [ + '12345', + '+37411498855', + '+37411498123', + '05614988556', + '', + '37456789000', + ], + }, + { + locale: 'ar-AE', + valid: [ + '+971502674453', + '+971521247658', + '+971541255684', + '+971555454458', + '+971561498855', + '+971585215778', + '971585215778', + '0585215778', + '585215778', + ], + invalid: [ + '12345', + '+971511498855', + '+9715614988556', + '+9745614988556', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '962796477263', + ], + }, + { + locale: 'ar-BH', + valid: [ + '+97335078110', + '+97339534385', + '+97366331055', + '+97333146000', + '97335078110', + '35078110', + '66331055', + ], + invalid: [ + '12345', + '+973350781101', + '+97379534385', + '+973035078110', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '962796477263', + '035078110', + '16331055', + 'hello', + '+9733507811a', + ], + }, + { + locale: 'ar-EG', + valid: [ + '+201004513789', + '+201111453489', + '+201221204610', + '+201144621154', + '+201200124304', + '+201011201564', + '+201124679001', + '+201064790156', + '+201274652177', + '+201280134679', + '+201090124576', + '+201583728900', + '201599495596', + '201090124576', + '01090124576', + '01538920744', + '1593075993', + '1090124576', + ], + invalid: [ + '+221004513789', + '+201404513789', + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '962796477263', + ], + }, + { + locale: 'ar-JO', + valid: [ + '0796477263', + '0777866254', + '0786725261', + '+962796477263', + '+962777866254', + '+962786725261', + '962796477263', + '962777866254', + '962786725261', + ], + invalid: [ + '00962786725261', + '00962796477263', + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + ], + }, + { + locale: 'ar-KW', + valid: [ + '96550000000', + '96560000000', + '96590000000', + '96541000000', + '+96550000000', + '+96550000220', + '+96551111220', + '+96541000000', + ], + invalid: [ + '+96570000220', + '00962786725261', + '00962796477263', + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '+96540000000', + ], + }, + { + locale: 'ar-LB', + valid: [ + '+96171234568', + '+9613123456', + '3456123', + '3123456', + '81978468', + '77675798', + ], + invalid: [ + '+961712345688888', + '00912220000', + '7767579888', + '+0921110000', + '+3123456888', + '021222200000', + '213333444444', + '', + '+212234', + '+21', + '02122333', + ], + }, + { + locale: 'ar-LY', + valid: [ + '912220000', + '0923330000', + '218945550000', + '+218958880000', + '212220000', + '0212220000', + '+218212220000', + ], + invalid: [ + '9122220000', + '00912220000', + '09211110000', + '+0921110000', + '+2180921110000', + '021222200000', + '213333444444', + '', + '+212234', + '+21', + '02122333', + ], + }, + { + locale: 'ar-MA', + valid: [ + '0522714782', + '0690851123', + '0708186135', + '+212522714782', + '+212690851123', + '+212708186135', + '00212522714782', + '00212690851123', + '00212708186135', + ], + invalid: [ + '522714782', + '690851123', + '708186135', + '212522714782', + '212690851123', + '212708186135', + '0212522714782', + '0212690851123', + '0212708186135', + '', + '12345', + '0922714782', + '+212190851123', + '00212408186135', + ], + }, + { + locale: 'dz-BT', + valid: [ + '+97517374354', + '+97517454971', + '77324646', + '016329712', + '97517265559', + ], + invalid: [ + '', + '9898347255', + '+96326626262', + '963372', + '0114152198', + ], + }, + { + locale: 'ar-OM', + valid: [ + '+96891212121', + '0096899999999', + '93112211', + '99099009', + ], + invalid: [ + '+96890212121', + '0096890999999', + '0090999999', + '+9689021212', + '', + '+212234', + '+21', + '02122333', + ], + }, + { + locale: 'ar-PS', + valid: [ + '+970563459876', + '970592334218', + '0566372345', + '0598273583', + ], + invalid: [ + '+9759029487', + '97059123456789', + '598372348', + '97058aaaafjd', + '', + '05609123484', + '+97059', + '+970', + '97056', + ], + }, + { + locale: 'ar-SY', + valid: [ + '0944549710', + '+963944549710', + '956654379', + '0944549710', + '0962655597', + ], + invalid: [ + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + ], + }, + { + locale: 'ar-SA', + valid: [ + '0556578654', + '+966556578654', + '966556578654', + '596578654', + '572655597', + ], + invalid: [ + '12345', + '', + '+9665626626262', + '+96633221097', + '0114152198', + ], + }, + { + locale: 'ar-TN', + valid: [ + '23456789', + '+21623456789', + '21623456789', + ], + invalid: [ + '12345', + '75200123', + '+216512345678', + '13520459', + '85479520', + ], + }, + { + locale: 'bg-BG', + valid: [ + '+359897123456', + '+359898888888', + '0897123123', + ], + invalid: [ + '', + '0898123', + '+359212555666', + '18001234567', + '12125559999', + ], + }, + { + locale: 'bn-BD', + valid: [ + '+8801794626846', + '01399098893', + '8801671163269', + '01717112029', + '8801898765432', + '+8801312345678', + '01494676946', + ], + invalid: [ + '', + '0174626346', + '017943563469', + '18001234567', + '0131234567', + ], + }, + { + locale: 'bs-BA', + valid: [ + '060123456', + '061123456', + '062123456', + '063123456', + '0641234567', + '065123456', + '066123456', + '+38760123456', + '+38761123456', + '+38762123456', + '+38763123456', + '+387641234567', + '+38765123456', + '+38766123456', + '0038760123456', + '0038761123456', + '0038762123456', + '0038763123456', + '00387641234567', + '0038765123456', + '0038766123456', + ], + invalid: [ + '0601234567', + '0611234567', + '06212345', + '06312345', + '064123456', + '0651234567', + '06612345', + '+3866123456', + '+3856123456', + '00038760123456', + '038761123456', + ], + }, + { + locale: 'cs-CZ', + valid: [ + '+420 123 456 789', + '+420 123456789', + '+420123456789', + '123 456 789', + '123456789', + ], + invalid: [ + '', + '+42012345678', + '+421 123 456 789', + '+420 023456789', + '+4201234567892', + ], + }, + { + locale: 'sk-SK', + valid: [ + '+421 123 456 789', + '+421 123456789', + '+421123456789', + '123 456 789', + '123456789', + ], + invalid: [ + '', + '+42112345678', + '+422 123 456 789', + '+421 023456789', + '+4211234567892', + ], + }, + { + locale: 'de-DE', + valid: [ + '+4915123456789', + '015123456789', + '015123456789', + '015623456789', + '015623456789', + '01601234567', + '016012345678', + '01621234567', + '01631234567', + '01701234567', + '017612345678', + ], + invalid: [ + '+4930405044550', + '34412345678', + '14412345678', + '16212345678', + '1761234567', + '16412345678', + '17012345678', + '+4912345678910', + '+49015123456789', + '015345678910', + '015412345678', + ], + }, + { + locale: 'de-AT', + valid: [ + '+436761234567', + '06761234567', + '00436123456789', + '+436123456789', + '01999', + '+4372876', + '06434908989562345', + ], + invalid: [ + '167612345678', + '1234', + '064349089895623459', + ], + }, + { + locale: 'hu-HU', + valid: [ + '06301234567', + '+36201234567', + '06701234567', + ], + invalid: [ + '1234', + '06211234567', + '+3620123456', + ], + }, + { + locale: 'mz-MZ', + valid: [ + '+258849229754', + '258849229754', + '849229754', + '829229754', + '839229754', + '869229754', + '859229754', + '869229754', + '879229754', + '+258829229754', + '+258839229754', + '+258869229754', + '+258859229754', + '+258869229754', + '+258879229754', + '258829229754', + '258839229754', + '258869229754', + '258859229754', + '258869229754', + '258879229754', + ], + invalid: [ + '+248849229754', + '158849229754', + '249229754', + '819229754', + '899229754', + '889229754', + '89229754', + '8619229754', + '87922975411', + '257829229754', + '+255839229754', + '+2258869229754', + '+1258859229754', + '+2588692297541', + '+2588792519754', + '25882922975411', + ], + }, + { locale: 'pt-BR', valid: [ '+55 12 996551215', @@ -6334,6 +7030,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', @@ -6342,12 +7044,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', ], }, { @@ -6427,6 +7129,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: [ @@ -6445,6 +7166,29 @@ describe('Validators', () => { '+4418970973', '', '+1441897465', + '+1441897465 additional invalid string part', + ], + }, + { + locale: 'en-BS', + valid: [ + '+12421231234', + '2421231234', + '+1-2421231234', + '+1-242-123-1234', + '(242)-123-1234', + '+1 (242)-123-1234', + '242 123-1234', + '(242) 123 1234', + ], + invalid: [ + '85763287', + '+1 242 12 12 12 12', + '+1424123123', + '+14418245567', + '+14416546789', + 'not a number', + '', ], }, { @@ -6625,6 +7369,22 @@ describe('Validators', () => { '353811234567', ], }, + { + locale: 'en-JM', + valid: [ + '+8761021234', + '8761211234', + '8763511274', + '+8764511274', + ], + invalid: [ + '999', + '+876102123422', + '+8861021234', + '8761021212213', + '876102123', + ], + }, { locale: 'en-KE', valid: [ @@ -6810,6 +7570,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'], @@ -6821,6 +7599,24 @@ describe('Validators', () => { '11435213543', ], }, + { + locale: 'fr-CD', + valid: [ + '+243818590432', + '+243893875610', + '243978590234', + '0813346543', + '0820459022', + '+243902590221', + ], + invalid: [ + '243', + '+254818590432', + '+24389032', + '123456789', + '+243700723845', + ], + }, { locale: 'fr-GF', valid: [ @@ -6946,18 +7742,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', @@ -6968,8 +7765,22 @@ describe('Validators', () => { locale: 'el-GR', valid: [ '+306944848966', - '6944848966', '306944848966', + '06904567890', + '6944848966', + '6904567890', + '6914567890', + '6934567890', + '6944567890', + '6954567890', + '6974567890', + '6984567890', + '6994567890', + '6854567890', + '6864567890', + '6874567890', + '6884567890', + '6894567890', ], invalid: [ '2102323234', @@ -6979,6 +7790,37 @@ describe('Validators', () => { '68129485729', '6589394827', '298RI89572', + '6924567890', + '6964567890', + '6844567890', + '690456789', + '00690456789', + 'not a number', + ], + }, + { + locale: 'el-CY', + valid: [ + '96546247', + '96978927', + '+35799837145', + '+35799646792', + '96056927', + '99629593', + '99849980', + '3599701619', + '+3599148725', + '96537247', + '3596676533', + ], + invalid: [ + '', + 'somechars', + '9697892', + '998499803', + '33799837145', + '+3799646792', + '93056927', ], }, { @@ -7462,6 +8304,10 @@ describe('Validators', () => { '+50489234567', '+50488987896', '+50497567389', + '+50427367389', + '+50422357389', + '+50431257389', + '+50430157389', ], invalid: [ '12345', @@ -7525,6 +8371,27 @@ describe('Validators', () => { '+34754789321', ], }, + { + locale: 'es-NI', + valid: [ + '+5051234567', + '+50512345678', + '5051234567', + '50512345678', + '+50555555555', + ], + invalid: [ + '1234', + '', + '1234567', + '12345678', + '+12345678', + '+505123456789', + '+50612345678', + '+50712345678', + '-50512345678', + ], + }, { locale: 'es-PA', valid: [ @@ -7712,6 +8579,9 @@ describe('Validators', () => { '0457 123 45 67', '+358457 123 45 67', '+358 50 555 7171', + '0501234', + '+358501234', + '050 1234', ], invalid: [ '12345', @@ -7777,6 +8647,8 @@ describe('Validators', () => { '088-261987', '1800-88-8687', '088-320000', + '+01112353576', + '+0111419752', ], }, { @@ -7823,6 +8695,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: [ @@ -7862,6 +8748,22 @@ describe('Validators', () => { '90 1234 5678', ], }, + { + locale: 'ir-IR', + valid: [ + '09023818688', + '09123809999', + '+989023818688', + '+989103923523', + ], + invalid: [ + '19023818688', + '323254', + '+903232323257', + '++3567868', + '0902381888832', + ], + }, { locale: 'it-IT', valid: [ @@ -7955,6 +8857,65 @@ 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-MD', + valid: [ + '+37360375781', + '+37361945673', + '+37362387563', + '+37368447788', + '+37369000101', + '+37367568910', + '+37376758294', + '+37378457892', + '+37379067436', + '37362387563', + '37368447788', + '37369000101', + '37367568910', + ], + invalid: [ + '', + '+37363373381', + '+37364310581', + '+37365578199', + '+37371088636', + 'Vml2YW11cyBmZXJtZtesting123', + '123456', + '740123456', + '+40640123456', + '+40210123456', + ], + }, { locale: 'ro-RO', valid: [ @@ -7981,6 +8942,8 @@ describe('Validators', () => { '740123456', '+40640123456', '+40210123456', + '+0765351689', + '+0711419752', ], }, { @@ -8261,35 +9224,6 @@ describe('Validators', () => { '998900066506', ], }, - { - locale: ['en-ZA', 'be-BY'], - valid: [ - '0821231234', - '+27821231234', - '27821231234', - '+375241234567', - '+375251234567', - '+375291234567', - '+375331234567', - '+375441234567', - '375331234567', - ], - invalid: [ - '082123', - '08212312345', - '21821231234', - '+21821231234', - '+0821231234', - '12345', - '', - 'ASDFGJKLmZXJtZtesting123', - '010-38238383', - '+9676338855', - '19676338855', - '6676338855', - '+99676338855', - ], - }, { locale: 'en-SL', valid: [ @@ -8338,8 +9272,10 @@ describe('Validators', () => { '+994502111111', '0505436743', '0554328772', + '0104328772', '0993301022', '+994776007139', + '+994106007139', ], invalid: [ 'wrong-number', @@ -8441,6 +9377,168 @@ 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: [ + '+959750202595', + '09750202595', + '9750202595', + '+959260000966', + '09256000323', + '09276000323', + '09426000323', + '09456000323', + '09761234567', + '09791234567', + '09961234567', + '09771234567', + '09660000234', + ], + invalid: [ + '59750202595', + '+9597502025', + '08943234524', + '09950000966', + '959240000966', + '09246000323', + '09466000323', + '09951234567', + '09801234567', + '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-AI', + valid: [ + '+12642351234', + '12642351234', + '+12644612222', + '+12645366326', + '+12645376326', + '+12647246326', + '+12647726326', + ], + invalid: [ + '', + 'not a number', + '+22642351234', + '+12902351234', + '+12642331234', + '+1264235', + '22642353456', + '+12352643456', + ], + }, + { + locale: 'en-KN', + valid: [ + '+18694699040', + '18694699040', + '+18697652917', + '18697652917', + '18694658472', + '+18696622969', + '+18694882224', + ], + invalid: [ + '', + '+18694238545', + '+1 8694882224', + '8694658472', + '+186946990', + '+1869469904', + '1869469904', + ], + }, { locale: 'en-PK', valid: [ @@ -8483,20 +9581,119 @@ describe('Validators', () => { ], }, { - locale: 'dv-MV', + locale: 'dv-MV', + valid: [ + '+9609112345', + '+9609958973', + '+9607258963', + '+9607958463', + '9609112345', + '9609958973', + '9607212963', + '9607986963', + '9112345', + '9958973', + '7258963', + '7958963', + ], + invalid: [ + '+96059234567', + '+96045789', + '7812463784', + 'NotANumber', + '+9607112345', + '+9609012345', + '+609012345', + '+96071123456', + '3412345', + '9603412345', + ], + }, + { + locale: 'ar-YE', + valid: [ + '737198225', + '733111355', + '+967700990270', + ], + invalid: [ + '+5032136663', + '21346663', + '+50321366663', + '12345', + 'Yemen', + 'this should fail', + '+5032222', + '+503 1111 1111', + '00 +503 1234 5678', + ], + }, + { + locale: 'ar-EH', + valid: [ + '+212-5288-12312', + '+212-5288 12312', + '+212 5288 12312', + '212528912312', + '+212528912312', + '+212528812312', + ], + invalid: [ + '212528812312123', + '+212-5290-12312', + '++212528812312', + '12345', + 'Wester Sahara', + 'this should fail', + '212 5288---12312', + '+503 1111 1111', + '00 +503 1234 5678', + ], + }, + { + locale: 'fa-AF', + valid: [ + '0511231231', + '+93511231231', + '+93281234567', + ], + invalid: [ + '212528812312123', + '+212-5290-12312', + '++212528812312', + '12345', + 'Afghanistan', + 'this should fail', + '212 5288---12312', + '+503 1111 1111', + '00 +503 1234 5678', + ], + }, + { + locale: 'en-SS', valid: [ - '+960973256874', - '781246378', - '+960766354789', - '+960912354789', + '+211928530422', + '+211913384561', + '+211972879174', + '+211952379334', + '0923346543', + '0950459022', + '0970934567', + '211979841238', + '211929843238', + '211959840238', ], invalid: [ - '+96059234567', - '+96045789', - '7812463784', - '+960706985478', - '+960926985478', - 'NotANumber', + '911', + '+211999', + '123456789909', + 'South Sudan', + '21195 840 238', + '+211981234567', + '+211931234567', + '+211901234567', + '+211991234567', + ], }, ]; @@ -9620,6 +10817,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', @@ -9989,6 +11194,7 @@ describe('Validators', () => { valid: [ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC', + 'data:application/media_control+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC', ' data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC ', 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E', 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCBmaWxsPSIjMDBCMUZGIiB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIvPjwvc3ZnPg==', @@ -9998,6 +11204,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', @@ -10028,6 +11235,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', @@ -10040,6 +11249,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 */ @@ -10150,6 +11361,12 @@ describe('Validators', () => { '247710', '231960', ], + invalid: [ + 'test 225320', + '211120 test', + '317543', + '267946', + ], }, { locale: 'CA', @@ -10159,6 +11376,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', ], }, { @@ -10318,6 +11552,9 @@ describe('Validators', () => { '43516 6456', '123443516 6456', '891123', + 'test 4351666456', + '4351666456 test', + 'test 4351666456 test', ], }, { @@ -10364,6 +11601,8 @@ describe('Validators', () => { '78-399', '39-490', '38-483', + '05-800', + '54-060', ], }, { @@ -10422,6 +11661,9 @@ describe('Validators', () => { '65000', '65080', '01000', + '51901', + '51909', + '49125', ], }, { @@ -10450,6 +11692,15 @@ describe('Validators', () => { '4144', ], }, + { + locale: 'MG', + valid: [ + '101', + '303', + '407', + '512', + ], + }, { locale: 'MT', valid: [ @@ -10571,1108 +11822,2255 @@ describe('Validators', () => { '982', ], }, + { + locale: 'BA', + valid: [ + '76300', + '71000', + '75412', + '76100', + '88202', + '88313', + ], + invalid: [ + '1234', + '789389', + '98212', + '11000', + ], + }, ]; - let allValid = []; + let allValid = []; + + // Test fixtures + fixtures.forEach((fixture) => { + if (fixture.valid) allValid = allValid.concat(fixture.valid); + test({ + validator: 'isPostalCode', + valid: fixture.valid, + invalid: fixture.invalid, + args: [fixture.locale], + }); + }); + + // Test generics + test({ + validator: 'isPostalCode', + valid: [ + ...allValid, + '1234', + '6900', + '1292', + '9400', + '27616', + '90210', + '10001', + '21201', + '33142', + '060623', + '123456', + '293940', + '002920', + ], + invalid: [ + 'asdf', + '1', + 'ASDFGJKLmZXJtZtesting123', + 'Vml2YW11cyBmZXJtZtesting123', + '48380480343', + '29923-329393-2324', + '4294924224', + '13', + ], + args: ['any'], + }); + }); + + it('should error on invalid locale', () => { + test({ + validator: 'isPostalCode', + args: ['is-NOT'], + error: [ + '293940', + '1234', + ], + }); + }); + + it('should validate MIME types', () => { + test({ + validator: 'isMimeType', + valid: [ + 'application/json', + 'application/xhtml+xml', + 'audio/mp4', + 'image/bmp', + 'font/woff2', + 'message/http', + 'model/vnd.gtw', + 'application/media_control+xml', + 'multipart/form-data', + 'multipart/form-data; boundary=something', + 'multipart/form-data; charset=utf-8; boundary=something', + 'multipart/form-data; boundary=something; charset=utf-8', + 'multipart/form-data; boundary=something; charset="utf-8"', + 'multipart/form-data; boundary="something"; charset=utf-8', + 'multipart/form-data; boundary="something"; charset="utf-8"', + 'text/css', + 'text/plain; charset=utf8', + 'Text/HTML;Charset="utf-8"', + 'text/html;charset=UTF-8', + 'Text/html;charset=UTF-8', + 'text/html; charset=us-ascii', + 'text/html; charset=us-ascii (Plain text)', + 'text/html; charset="us-ascii"', + 'video/mp4', + ], + invalid: [ + '', + ' ', + '/', + 'f/b', + 'application', + 'application\\json', + 'application/json/text', + 'application/json; charset=utf-8', + 'audio/mp4; charset=utf-8', + 'image/bmp; charset=utf-8', + 'font/woff2; charset=utf-8', + 'message/http; charset=utf-8', + 'model/vnd.gtw; charset=utf-8', + 'video/mp4; charset=utf-8', + ], + }); + }); + + // 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({ + validator: 'isTaxID', + args: ['bg-BG'], + valid: [ + '7501010010', + '0101010012', + '0111010010', + '7521010014', + '7541010019'], + invalid: [ + '750101001', + '75010100101', + '75-01010/01 0', + '7521320010', + '7501010019'], + }); + test({ + validator: 'isTaxID', + args: ['cs-CZ'], + valid: [ + '530121999', + '530121/999', + '530121/9990', + '5301219990', + '1602295134', + '5451219994', + '0424175466', + '0532175468', + '7159079940'], + invalid: [ + '53-0121 999', + '530121000', + '960121999', + '0124175466', + '0472301754', + '1975116400', + '7159079945'], + }); + test({ + validator: 'isTaxID', + args: ['de-AT'], + valid: [ + '931736581', + '93-173/6581', + '93--173/6581'], + invalid: [ + '999999999', + '93 173 6581', + '93-173/65811', + '93-173/658'], + }); + test({ + validator: 'isTaxID', + args: ['de-DE'], + valid: [ + '26954371827', + '86095742719', + '65929970489', + '79608434120', + '659/299/7048/9'], + invalid: [ + '26954371828', + '86095752719', + '8609575271', + '860957527190', + '65299970489', + '65999970489', + '6592997048-9'], + }); + test({ + validator: 'isTaxID', + args: ['dk-DK'], + valid: [ + '010111-1113', + '0101110117', + '2110084008', + '2110489008', + '2110595002', + '2110197007', + '0101110117', + '0101110230'], + invalid: [ + '010111/1113', + '010111111', + '01011111133', + '2110485008', + '2902034000', + '0101110630'], + }); + test({ + validator: 'isTaxID', + args: ['el-CY'], + valid: [ + '00123123T', + '99652156X'], + invalid: [ + '99652156A', + '00124123T', + '00123123', + '001123123T', + '00 12-3123/T'], + }); + test({ + validator: 'isTaxID', + args: ['el-GR'], + valid: [ + '758426713', + '032792320', + '054100004'], + invalid: [ + '054100005', + '05410000', + '0541000055', + '05 4100005', + '05-410/0005', + '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'], + valid: [ + '1234567890', + 'AA123456A', + 'AA123456 '], + invalid: [ + 'GB123456A', + '123456789', + '12345678901', + 'NK123456A', + 'TN123456A', + 'ZZ123456A', + 'GB123456Z', + 'DM123456A', + 'AO123456A', + 'GB-123456A', + 'GB 123456 A', + 'GB123456 '], + }); + test({ + validator: 'isTaxID', + args: ['en-IE'], + valid: [ + '1234567T', + '1234567TW', + '1234577W', + '1234577WW', + '1234577IA'], + invalid: [ + '1234567', + '1234577WWW', + '1234577A', + '1234577JA'], + }); + test({ + validator: 'isTaxID', + args: ['en-US'], + valid: [ + '01-1234567', + '01 1234567', + '011234567', + '10-1234567', + '02-1234567', + '67-1234567', + '15-1234567', + '31-1234567', + '99-1234567'], + invalid: [ + '0-11234567', + '01#1234567', + '01 1234567', + '01 1234 567', + '07-1234567', + '28-1234567', + '96-1234567'], + }); + test({ + validator: 'isTaxID', + args: ['es-ES'], + valid: [ + '00054237A', + '54237A', + 'X1234567L', + 'Z1234567R', + 'M2812345C', + 'Y2812345B'], + invalid: [ + 'M2812345CR', + 'A2812345C', + '0/005 423-7A', + '00054237U'], + }); + test({ + validator: 'isTaxID', + args: ['et-EE'], + valid: [ + '10001010080', + '46304280206', + '37102250382', + '32708101201'], + invalid: [ + '46304280205', + '61002293333', + '4-6304 28/0206', + '4630428020', + '463042802066'], + }); + test({ + validator: 'isTaxID', + args: ['fi-FI'], + valid: [ + '131052-308T', + '131002+308W', + '131019A3089'], + invalid: [ + '131052308T', + '131052-308TT', + '131052S308T', + '13 1052-308/T', + '290219A1111'], + }); + test({ + validator: 'isTaxID', + args: ['fr-BE'], + valid: [ + '00012511119'], + }); + test({ + validator: 'isTaxID', + args: ['fr-FR'], + valid: [ + '30 23 217 600 053', + '3023217600053'], + invalid: [ + '30 2 3 217 600 053', + '3 023217-600/053', + '3023217600052', + '3023217500053', + '30232176000534', + '302321760005'], + }); + test({ + validator: 'isTaxID', + args: ['nl-BE'], + valid: [ + '00012511148', + '00/0125-11148', + '00000011115'], + invalid: [ + '00 01 2511148', + '01022911148', + '00013211148', + '0001251114', + '000125111480', + '00012511149'], + }); + test({ + validator: 'isTaxID', + args: ['fr-LU'], + valid: [ + '1893120105732'], + invalid: [ + '189312010573', + '18931201057322', + '1893 12-01057/32', + '1893120105742', + '1893120105733'], + }); + test({ + validator: 'isTaxID', + args: ['lb-LU'], + invalid: [ + '2016023005732'], + }); + test({ + validator: 'isTaxID', + args: ['hr-HR'], + valid: [ + '94577403194'], + invalid: [ + '94 57-7403/194', + '9457740319', + '945774031945', + '94577403197', + '94587403194'], + }); + test({ + validator: 'isTaxID', + args: ['hu-HU'], + valid: [ + '8071592153'], + invalid: [ + '80 71-592/153', + '80715921534', + '807159215', + '8071592152', + '8071582153'], + }); + test({ + validator: 'isTaxID', + args: ['lt-LT'], + valid: [ + '33309240064'], + }); + test({ + validator: 'isTaxID', + args: ['it-IT'], + valid: [ + 'DMLPRY77D15H501F', + 'AXXFAXTTD41H501D'], + invalid: [ + 'DML PRY/77D15H501-F', + 'DMLPRY77D15H501', + 'DMLPRY77D15H501FF', + 'AAPPRY77D15H501F', + 'DMLAXA77D15H501F', + 'AXXFAX90A01Z001F', + 'DMLPRY77B29H501F', + 'AXXFAX3TD41H501E'], + }); + test({ + validator: 'isTaxID', + args: ['lv-LV'], + valid: [ + '01011012344', + '32579461005', + '01019902341', + '325794-61005'], + invalid: [ + '010110123444', + '0101101234', + '01001612345', + '290217-22343'], + }); + test({ + validator: 'isTaxID', + args: ['mt-MT'], + valid: [ + '1234567A', + '882345608', + '34581M', + '199Z'], + invalid: [ + '812345608', + '88234560', + '8823456088', + '11234567A', + '12/34-567 A', + '88 23-456/08', + '1234560A', + '0000000M', + '3200100G'], + }); + test({ + validator: 'isTaxID', + args: ['nl-NL'], + valid: [ + '174559434'], + invalid: [ + '17455943', + '1745594344', + '17 455-94/34'], + }); + test({ + validator: 'isTaxID', + args: ['pl-PL'], + valid: [ + '2234567895', + '02070803628', + '02870803622', + '02670803626', + '01510813623'], + invalid: [ + '020708036285', + '223456789', + '22 345-678/95', + '02 070-8036/28', + '2234567855', + '02223013623'], + }); + test({ + validator: 'isTaxID', + args: ['pt-BR'], + valid: [ + '35161990910', + '74407265027', + '05423994000172', + '11867044000130'], + invalid: [ + 'ABCDEFGH', + '170.691.440-72', + '11494282142', + '74405265037', + '11111111111', + '48469799384', + '94.592.973/0001-82', + '28592361000192', + '11111111111111', + '111111111111112', + '61938188550993', + '82168365502729', + ], + }); + test({ + validator: 'isTaxID', + args: ['pt-PT'], + valid: [ + '299999998', + '299992020'], + invalid: [ + '2999999988', + '29999999', + '29 999-999/8'], + }); + test({ + validator: 'isTaxID', + args: ['ro-RO'], + valid: [ + '8001011234563', + '9000123456789', + '1001011234560', + '3001011234564', + '5001011234568'], + invalid: [ + '5001011234569', + '500 1011-234/568', + '500101123456', + '50010112345688', + '5001011504568', + '8000230234563', + '6000230234563'], + }); + test({ + validator: 'isTaxID', + args: ['sk-SK'], + valid: [ + '530121999', + '536221/999', + '031121999', + '520229999', + '1234567890'], + invalid: [ + '53012199999', + '990101999', + '530121000', + '53012199', + '53-0121 999', + '535229999'], + }); + test({ + validator: 'isTaxID', + args: ['sl-SI'], + valid: [ + '15012557', + '15012590'], + invalid: [ + '150125577', + '1501255', + '15 01-255/7'], + }); + test({ + validator: 'isTaxID', + args: ['sv-SE'], + valid: [ + '640823-3234', + '640883-3231', + '6408833231', + '19640823-3233', + '196408233233', + '19640883-3230', + '200228+5266', + '20180101-5581'], + invalid: [ + '640823+3234', + '160230-3231', + '160260-3231', + '160260-323', + '160260323', + '640823+323', + '640823323', + '640823+32344', + '64082332344', + '19640823-32333', + '1964082332333'], + }); + test({ + validator: 'isTaxID', + valid: [ + '01-1234567'], + }); + test({ + validator: 'isTaxID', + args: ['is-NOT'], + error: [ + '01-1234567', + '01 1234567', + '011234567', + '0-11234567', + '01#1234567', + '01 1234567', + '01 1234 567', + '07-1234567', + '28-1234567', + '96-1234567', + ], + }); + }); + - // Test fixtures - fixtures.forEach((fixture) => { - if (fixture.valid) allValid = allValid.concat(fixture.valid); - test({ - validator: 'isPostalCode', - valid: fixture.valid, - invalid: fixture.invalid, - args: [fixture.locale], - }); + it('should validate slug', () => { + test({ + validator: 'isSlug', + valid: [ + 'foo', + 'foo-bar', + 'foo_bar', + 'foo-bar-foo', + 'foo-bar_foo', + 'foo-bar_foo*75-b4r-**_foo', + 'foo-bar_foo*75-b4r-**_foo-&&', + ], + invalid: [ + 'not-----------slug', + '@#_$@', + '-not-slug', + 'not-slug-', + '_not-slug', + 'not-slug_', + 'not slug', + ], + }); + }); + + it('should validate strong passwords', () => { + test({ + validator: 'isStrongPassword', + args: [{ + minLength: 8, + minLowercase: 1, + minUppercase: 1, + minNumbers: 1, + minSymbols: 1, + }], + valid: [ + '%2%k{7BsL"M%Kd6e', + 'EXAMPLE of very long_password123!', + 'mxH_+2vs&54_+H3P', + '+&DxJ=X7-4L8jRCD', + 'etV*p%Nr6w&H%FeF', + '£3.ndSau_7', + ], + invalid: [ + '', + 'password', + 'hunter2', + 'hello world', + 'passw0rd', + 'password!', + 'PASSWORD!', + ], + }); + }); + + it('should validate base64URL', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: true }], + valid: [ + '', + 'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ', + '1234', + 'bXVtLW5ldmVyLXByb3Vk', + 'PDw_Pz8-Pg', + 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw', + ], + invalid: [ + ' AA', + '\tAA', + '\rAA', + '\nAA', + '123=', + 'This+isa/bad+base64Url==', + '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw', + ], + error: [ + null, + undefined, + {}, + [], + 42, + ], + }); + }); + + it('should validate date', () => { + test({ + validator: 'isDate', + valid: [ + new Date(), + new Date([2014, 2, 15]), + new Date('2014-03-15'), + '2020/02/29', + ], + invalid: [ + '', + '15072002', + null, + undefined, + { year: 2002, month: 7, day: 15 }, + 42, + { toString() { return '[object Date]'; } }, // faking + '2020-02-30', // invalid date + '2019-02-29', // non-leap year + '2020-04-31', // invalid date + '2020/03-15', // mixed delimiter + ], + }); + test({ + validator: 'isDate', + args: ['DD/MM/YYYY'], // old format for backward compatibility + valid: [ + '15-07-2002', + '15/07/2002', + ], + invalid: [ + '15/7/2002', + '15-7-2002', + '15/7/02', + '15-7-02', + '15-07/2002', + ], + }); + test({ + validator: 'isDate', + args: [{ format: 'DD/MM/YYYY' }], + valid: [ + '15-07-2002', + '15/07/2002', + ], + invalid: [ + '15/7/2002', + '15-7-2002', + '15/7/02', + '15-7-02', + '15-07/2002', + ], + }); + test({ + validator: 'isDate', + args: [{ format: 'DD/MM/YY' }], + valid: [ + '15-07-02', + '15/07/02', + ], + invalid: [ + '15/7/2002', + '15-7-2002', + '15/07-02', + ], + }); + test({ + validator: 'isDate', + args: [{ format: 'D/M/YY' }], + valid: [ + '5-7-02', + '5/7/02', + ], + invalid: [ + '5/07/02', + '15/7/02', + '15-7-02', + '5/7-02', + ], + }); + test({ + validator: 'isDate', + args: [{ format: 'DD/MM/YYYY', strictMode: true }], + valid: [ + '15/07/2002', + ], + invalid: [ + '15-07-2002', + '15/7/2002', + '15-7-2002', + '15/7/02', + '15-7-02', + '15-07/2002', + ], + }); + test({ + validator: 'isDate', + args: [{ strictMode: true }], + valid: [ + '2020/01/15', + '2014/02/15', + '2014/03/15', + '2020/02/29', + ], + invalid: [ + '2014-02-15', + '2020-02-29', + '15-07/2002', + new Date(), + new Date([2014, 2, 15]), + new Date('2014-03-15'), + ], + }); + test({ + validator: 'isDate', + args: [{ delimiters: ['/', ' '] }], + valid: [ + new Date(), + new Date([2014, 2, 15]), + new Date('2014-03-15'), + '2020/02/29', + '2020 02 29', + ], + invalid: [ + '2020-02-29', + '', + '15072002', + null, + undefined, + { year: 2002, month: 7, day: 15 }, + 42, + { toString() { return '[object Date]'; } }, + '2020/02/30', + '2019/02/29', + '2020/04/31', + '2020/03-15', + ], + }); + test({ + validator: 'isDate', + args: [{ format: 'MM.DD.YYYY', delimiters: ['.'], strictMode: true }], + valid: [ + '01.15.2020', + '02.15.2014', + '03.15.2014', + '02.29.2020', + ], + invalid: [ + '2014-02-15', + '2020-02-29', + '15-07/2002', + new Date(), + new Date([2014, 2, 15]), + new Date('2014-03-15'), + '29.02.2020', + ], + }); + }); + it('should validate time', () => { + test({ + validator: 'isTime', + valid: [ + '00:00', + '23:59', + '9:00', + ], + invalid: [ + '', + null, + undefined, + 0, + '07:00 PM', + '23', + '00:60', + '00:', + '01:0 ', + '001:01', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour24', mode: 'withSeconds' }], + valid: [ + '23:59:59', + '00:00:00', + '9:50:01', + ], + invalid: [ + '', + null, + undefined, + 23, + '01:00:01 PM', + '13:00:', + '00', + '26', + '00;01', + '0 :09', + '59:59:59', + '24:00:00', + '00:59:60', + '99:99:99', + '009:50:01', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour12' }], + valid: [ + '12:59 PM', + '12:59 AM', + '01:00 PM', + '01:00 AM', + '7:00 AM', + ], + invalid: [ + '', + null, + undefined, + 0, + '12:59 MM', + '12:59 MA', + '12:59 PA', + '12:59 A M', + '13:00 PM', + '23', + '00:60', + '00:', + '9:00', + '01:0 ', + '001:01', + '12:59:00 PM', + '12:59:00 A M', + '12:59:00 ', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour12', mode: 'withSeconds' }], + valid: [ + '12:59:59 PM', + '2:34:45 AM', + '7:00:00 AM', + ], + invalid: [ + '', + null, + undefined, + 23, + '01:00: 1 PM', + '13:00:', + '13:00:00 PM', + '00', + '26', + '00;01', + '0 :09', + '59:59:59', + '24:00:00', + '00:59:60', + '99:99:99', + '9:50:01', + '009:50:01', + ], + }); + }); + it('should be valid license plate', () => { + test({ + validator: 'isLicensePlate', + args: ['es-AR'], + valid: [ + 'AB 123 CD', + 'AB123CD', + 'ABC 123', + 'ABC123', + ], + invalid: [ + '', + 'notalicenseplate', + 'AB-123-CD', + 'ABC-123', + 'AABC 123', + 'AB CDE FG', + 'ABC DEF', + '12 ABC 34', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['pt-PT'], + valid: [ + 'AA-12-34', + '12·34·AB', + '12·AB·34', + 'AB 12 CD', + 'AB12CD', + ], + invalid: [ + '', + 'notalicenseplate', + 'A1-B2-C3', + 'ABC-1-EF', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['de-LI'], + valid: [ + 'FL 1', + 'FL 99999', + 'FL 1337', + ], + invalid: [ + '', + 'FL 999999', + 'AB 12345', + 'FL -1', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['de-DE'], + valid: [ + 'M A 1', + 'M A 12', + 'M A 123', + 'M A 1234', + 'M AB 1', + 'M AB 12', + 'M AB 123', + 'M AB 1234', + 'FS A 1', + 'FS A 12', + 'FS A 123', + 'FS A 1234', + 'FS AB 1', + 'FS AB 12', + 'FS AB 123', + 'FS AB 1234', + 'FSAB1234', + 'FS-AB-1234', + 'FS AB 1234 H', + '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', + 'PAF AB 1234', + '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({ + validator: 'isLicensePlate', + args: ['fi-FI'], + valid: [ + 'ABC-123', + 'ABC 123', + 'ABC123', + 'A100', + 'A 100', + 'A-100', + 'C10001', + 'C 10001', + 'C-10001', + '123-ABC', + '123 ABC', + '123ABC', + '123-A', + '123 A', + '123A', + '199AA', + '199 AA', + '199-AA', + ], + invalid: [ + ' ', + 'A-1', + 'A1A-100', + '1-A-2', + 'C1234567', + 'A B C 1 2 3', + 'abc-123', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['sq-AL'], + valid: [ + 'AA 000 AA', + 'ZZ 999 ZZ', + ], + invalid: [ + '', + 'AA 0 A', + 'AAA 00 AAA', + ], + }); + test({ + validator: 'isLicensePlate', + args: ['cs-CZ'], + valid: [ + 'ALA4011', + '4A23000', + 'DICTAT0R', + 'VETERAN', + 'AZKVIZ8', + '2A45876', + 'DIC-TAT0R', + ], + invalid: [ + '', + 'invalidlicenseplate', + 'LN5758898', + 'X-|$|-X', + 'AE0F-OP4', + 'GO0MER', + '2AAAAAAAA', + 'FS AB 1234 E', + 'GB999 9999 00', + ], }); - // Test generics test({ - validator: 'isPostalCode', + validator: 'isLicensePlate', + args: ['pt-BR'], valid: [ - ...allValid, - '1234', - '6900', - '1292', - '9400', - '27616', - '90210', - '10001', - '21201', - '33142', - '060623', - '123456', - '293940', - '002920', + 'ABC1234', + 'ABC 1234', + 'ABC-1234', + 'ABC1D23', + 'ABC1K23', + 'ABC1Z23', + 'ABC 1D23', + 'ABC-1D23', ], invalid: [ - 'asdf', - '1', - 'ASDFGJKLmZXJtZtesting123', - 'Vml2YW11cyBmZXJtZtesting123', - '48380480343', - '29923-329393-2324', - '4294924224', - '13', + '', + 'AA 0 A', + 'AAA 00 AAA', + 'ABCD123', + 'AB12345', + 'AB123DC', ], + }); + test({ + validator: 'isLicensePlate', + args: ['hu-HU'], + valid: [ + 'AAB-001', + 'AVC-987', + 'KOC-124', + 'JCM-871', + 'AWQ-777', + 'BPO-001', + 'BPI-002', + 'UCO-342', + 'UDO-385', + 'XAO-987', + 'AAI-789', + 'ABI-789', + 'ACI-789', + 'AAO-789', + 'ABO-789', + 'ACO-789', + 'YAA-123', + 'XAA-123', + 'WAA-258', + 'XZZ-784', + 'M123456', + 'CK 12-34', + 'DT 12-34', + 'CD 12-34', + 'HC 12-34', + 'HB 12-34', + 'HK 12-34', + 'MA 12-34', + 'OT 12-34', + 'RR 17-87', + 'CD 124-348', + 'C-C 2021', + 'C-X 2458', + 'X-A 7842', + 'E-72345', + 'Z-07458', + 'S ACF 83', + 'SP 04-68', + ], + invalid: [ + 'AAA-547', + 'aab-001', + 'AAB 001', + 'AB34', + '789-LKJ', + 'BBO-987', + 'BBI-987', + 'BWQ-777', + 'BQW-987', + 'BAI-789', + 'BBI-789', + 'BCI-789', + 'BAO-789', + 'BBO-789', + 'BCO-789', + 'ADI-789', + 'ADO-789', + 'KOC-1234', + 'M1234567', + 'W-12345', + 'S BCF 83', + 'X-D 1234', + 'C-D 1234', + 'HU 12-34', + ], + }); + test({ + validator: 'isLicensePlate', args: ['any'], + valid: [ + 'FL 1', + 'FS AB 123', + ], + invalid: [ + '', + 'FL 999999', + 'FS AB 1234 A', + ], }); - }); - - it('should error on invalid locale', () => { test({ - validator: 'isPostalCode', - args: ['is-NOT'], + validator: 'isLicensePlate', + args: ['asdfasdf'], error: [ - '293940', - '1234', + 'FL 1', + 'FS AB 123', + 'FL 999999', + '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 MIME types', () => { + it('should validate VAT numbers', () => { test({ - validator: 'isMimeType', + validator: 'isVAT', + args: ['AT'], valid: [ - 'application/json', - 'application/xhtml+xml', - 'audio/mp4', - 'image/bmp', - 'font/woff2', - 'message/http', - 'model/vnd.gtw', - 'multipart/form-data', - 'multipart/form-data; boundary=something', - 'multipart/form-data; charset=utf-8; boundary=something', - 'multipart/form-data; boundary=something; charset=utf-8', - 'multipart/form-data; boundary=something; charset="utf-8"', - 'multipart/form-data; boundary="something"; charset=utf-8', - 'multipart/form-data; boundary="something"; charset="utf-8"', - 'text/css', - 'text/plain; charset=utf8', - 'Text/HTML;Charset="utf-8"', - 'text/html;charset=UTF-8', - 'Text/html;charset=UTF-8', - 'text/html; charset=us-ascii', - 'text/html; charset=us-ascii (Plain text)', - 'text/html; charset="us-ascii"', - 'video/mp4', + 'ATU12345678', + 'U12345678', + ], + invalid: [ + 'AT 12345678', + '12345678', + ], + }); + test({ + validator: 'isVAT', + args: ['BE'], + valid: [ + 'BE1234567890', + '1234567890', + ], + invalid: [ + 'BE 1234567890', + '123456789', + ], + }); + test({ + validator: 'isVAT', + args: ['BG'], + valid: [ + 'BG1234567890', + '1234567890', + 'BG123456789', + '123456789', + ], + invalid: [ + 'BG 1234567890', + '12345678', + ], + }); + test({ + validator: 'isVAT', + args: ['HR'], + valid: [ + 'HR12345678901', + '12345678901', + ], + invalid: [ + 'HR 12345678901', + '1234567890', + ], + }); + test({ + validator: 'isVAT', + args: ['CY'], + valid: [ + 'CY123456789', + '123456789', + ], + invalid: [ + 'CY 123456789', + '12345678', + ], + }); + test({ + validator: 'isVAT', + args: ['CZ'], + valid: [ + 'CZ1234567890', + 'CZ123456789', + 'CZ12345678', + '1234567890', + '123456789', + '12345678', + ], + invalid: [ + 'CZ 123456789', + '1234567', + ], + }); + test({ + validator: 'isVAT', + args: ['DK'], + valid: [ + 'DK12345678', + '12345678', + ], + invalid: [ + 'DK 12345678', + '1234567', + ], + }); + test({ + validator: 'isVAT', + args: ['EE'], + valid: [ + 'EE123456789', + '123456789', + ], + invalid: [ + 'EE 123456789', + '12345678', + ], + }); + test({ + validator: 'isVAT', + args: ['FI'], + valid: [ + 'FI12345678', + '12345678', ], invalid: [ - '', - ' ', - '/', - 'f/b', - 'application', - 'application\\json', - 'application/json/text', - 'application/json; charset=utf-8', - 'audio/mp4; charset=utf-8', - 'image/bmp; charset=utf-8', - 'font/woff2; charset=utf-8', - 'message/http; charset=utf-8', - 'model/vnd.gtw; charset=utf-8', - 'video/mp4; charset=utf-8', + 'FI 12345678', + '1234567', ], }); - }); - - // 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({ - validator: 'isTaxID', - args: ['bg-BG'], + validator: 'isVAT', + args: ['FR'], valid: [ - '7501010010', - '0101010012', - '0111010010', - '7521010014', - '7541010019'], + 'FRAA123456789', + 'AA123456789', + ], invalid: [ - '750101001', - '75010100101', - '75-01010/01 0', - '7521320010', - '7501010019'], + 'FR AA123456789', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['cs-CZ'], + validator: 'isVAT', + args: ['DE'], valid: [ - '530121999', - '530121/999', - '530121/9990', - '5301219990', - '1602295134', - '5451219994', - '0424175466', - '0532175468', - '7159079940'], + 'DE123456789', + '123456789', + ], invalid: [ - '53-0121 999', - '530121000', - '960121999', - '0124175466', - '0472301754', - '1975116400', - '7159079945'], + 'DE 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['de-AT'], + validator: 'isVAT', + args: ['EL'], valid: [ - '931736581', - '93-173/6581', - '93--173/6581'], + 'EL123456789', + '123456789', + ], invalid: [ - '999999999', - '93 173 6581', - '93-173/65811', - '93-173/658'], + 'EL 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['de-DE'], + validator: 'isVAT', + args: ['HU'], valid: [ - '26954371827', - '86095742719', - '65929970489', - '79608434120', - '659/299/7048/9'], + 'HU12345678', + '12345678', + ], invalid: [ - '26954371828', - '86095752719', - '8609575271', - '860957527190', - '65299970489', - '65999970489', - '6592997048-9'], + 'HU 12345678', + '1234567', + ], }); test({ - validator: 'isTaxID', - args: ['dk-DK'], + validator: 'isVAT', + args: ['IE'], valid: [ - '010111-1113', - '0101110117', - '2110084008', - '2110489008', - '2110595002', - '2110197007', - '0101110117', - '0101110230'], + 'IE1234567AW', + '1234567AW', + ], invalid: [ - '010111/1113', - '010111111', - '01011111133', - '2110485008', - '2902034000', - '0101110630'], + 'IE 1234567', + '1234567', + ], }); test({ - validator: 'isTaxID', - args: ['el-CY'], + validator: 'isVAT', + args: ['IT'], valid: [ - '00123123T', - '99652156X'], + 'IT12345678910', + '12345678910', + ], invalid: [ - '99652156A', - '00124123T', - '00123123', - '001123123T', - '00 12-3123/T'], + 'IT12345678 910', + 'IT 123456789101', + 'IT123456789101', + 'GB12345678910', + 'IT123456789', + ], }); test({ - validator: 'isTaxID', - args: ['el-GR'], + validator: 'isVAT', + args: ['LV'], valid: [ - '758426713', - '032792320', - '054100004'], + 'LV12345678901', + '12345678901', + ], invalid: [ - '054100005', - '05410000', - '0541000055', - '05 4100005', - '05-410/0005', - '658426713', - '558426713'], + 'LV 12345678901', + '1234567890', + ], }); test({ - validator: 'isTaxID', - args: ['en-GB'], + validator: 'isVAT', + args: ['LT'], valid: [ + 'LT123456789012', + '123456789012', + 'LT12345678901', + '12345678901', + 'LT1234567890', '1234567890', - 'AA123456A', - 'AA123456 '], - invalid: [ - 'GB123456A', + 'LT123456789', '123456789', - '12345678901', - 'NK123456A', - 'TN123456A', - 'ZZ123456A', - 'GB123456Z', - 'DM123456A', - 'AO123456A', - 'GB-123456A', - 'GB 123456 A', - 'GB123456 '], + ], + invalid: [ + 'LT 123456789012', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['en-IE'], + validator: 'isVAT', + args: ['LU'], valid: [ - '1234567T', - '1234567TW', - '1234577W', - '1234577WW', - '1234577IA'], + 'LU12345678', + '12345678', + ], invalid: [ + 'LU 12345678', '1234567', - '1234577WWW', - '1234577A', - '1234577JA'], + ], }); test({ - validator: 'isTaxID', - args: ['en-US'], + validator: 'isVAT', + args: ['MT'], valid: [ - '01-1234567', - '01 1234567', - '011234567', - '10-1234567', - '02-1234567', - '67-1234567', - '15-1234567', - '31-1234567', - '99-1234567'], + 'MT12345678', + '12345678', + ], invalid: [ - '0-11234567', - '01#1234567', - '01 1234567', - '01 1234 567', - '07-1234567', - '28-1234567', - '96-1234567'], + 'MT 12345678', + '1234567', + ], }); test({ - validator: 'isTaxID', - args: ['es-ES'], + validator: 'isVAT', + args: ['NL'], valid: [ - '00054237A', - '54237A', - 'X1234567L', - 'Z1234567R', - 'M2812345C', - 'Y2812345B'], + 'NL123456789B10', + '123456789B10', + ], invalid: [ - 'M2812345CR', - 'A2812345C', - '0/005 423-7A', - '00054237U'], + 'NL12345678 910', + 'NL 123456789101', + 'NL123456789B1', + 'GB12345678910', + 'NL123456789', + ], }); test({ - validator: 'isTaxID', - args: ['et-EE'], - valid: [ - '10001010080', - '46304280206', - '37102250382', - '32708101201'], + validator: 'isVAT', + args: ['PL'], + valid: [ + 'PL1234567890', + '1234567890', + 'PL123-456-78-90', + '123-456-78-90', + 'PL123-45-67-890', + '123-45-67-890', + ], invalid: [ - '46304280205', - '61002293333', - '4-6304 28/0206', - '4630428020', - '463042802066'], + 'PL 1234567890', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['fi-FI'], + validator: 'isVAT', + args: ['PT'], valid: [ - '131052-308T', - '131002+308W', - '131019A3089'], + 'PT123456789', + '123456789', + ], invalid: [ - '131052308T', - '131052-308TT', - '131052S308T', - '13 1052-308/T', - '290219A1111'], + 'PT 123456789', + '000000001', + ], }); test({ - validator: 'isTaxID', - args: ['fr-BE'], + validator: 'isVAT', + args: ['RO'], valid: [ - '00012511119'], + 'RO1234567890', + '1234567890', + 'RO12', + '12', + ], + invalid: [ + 'RO 12', + '1', + ], }); test({ - validator: 'isTaxID', - args: ['fr-FR'], + validator: 'isVAT', + args: ['SK'], valid: [ - '30 23 217 600 053', - '3023217600053'], + 'SK1234567890', + '1234567890', + ], invalid: [ - '30 2 3 217 600 053', - '3 023217-600/053', - '3023217600052', - '3023217500053', - '30232176000534', - '302321760005'], + 'SK 1234567890', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['nl-BE'], + validator: 'isVAT', + args: ['SI'], valid: [ - '00012511148', - '00/0125-11148', - '00000011115'], + 'SI12345678', + '12345678', + ], invalid: [ - '00 01 2511148', - '01022911148', - '00013211148', - '0001251114', - '000125111480', - '00012511149'], + 'SI 12345678', + '1234567', + ], }); test({ - validator: 'isTaxID', - args: ['fr-LU'], + validator: 'isVAT', + args: ['ES'], valid: [ - '1893120105732'], + 'ESA1234567A', + 'A1234567A', + ], invalid: [ - '189312010573', - '18931201057322', - '1893 12-01057/32', - '1893120105742', - '1893120105733'], + 'ES 1234567A', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['lb-LU'], + validator: 'isVAT', + args: ['SE'], + valid: [ + 'SE123456789012', + '123456789012', + ], invalid: [ - '2016023005732'], + 'SE 123456789012', + '12345678901', + ], }); test({ - validator: 'isTaxID', - args: ['hr-HR'], + validator: 'isVAT', + args: ['AL'], valid: [ - '94577403194'], + 'AL123456789A', + '123456789A', + ], invalid: [ - '94 57-7403/194', - '9457740319', - '945774031945', - '94577403197', - '94587403194'], + 'AL 123456789A', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['hu-HU'], + validator: 'isVAT', + args: ['MK'], valid: [ - '8071592153'], + 'MK1234567890123', + '1234567890123', + ], invalid: [ - '80 71-592/153', - '80715921534', - '807159215', - '8071592152', - '8071582153'], + 'MK 1234567890123', + '123456789012', + ], }); test({ - validator: 'isTaxID', - args: ['lt-LT'], + validator: 'isVAT', + args: ['AU'], valid: [ - '33309240064'], + 'AU12345678901', + '12345678901', + ], + invalid: [ + 'AU 12345678901', + '1234567890', + ], }); test({ - validator: 'isTaxID', - args: ['it-IT'], + validator: 'isVAT', + args: ['BY'], valid: [ - 'DMLPRY77D15H501F', - 'AXXFAXTTD41H501D'], + 'УНП 123456789', + '123456789', + ], invalid: [ - 'DML PRY/77D15H501-F', - 'DMLPRY77D15H501', - 'DMLPRY77D15H501FF', - 'AAPPRY77D15H501F', - 'DMLAXA77D15H501F', - 'AXXFAX90A01Z001F', - 'DMLPRY77B29H501F', - 'AXXFAX3TD41H501E'], + 'BY 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['lv-LV'], + validator: 'isVAT', + args: ['CA'], valid: [ - '01011012344', - '32579461005', - '01019902341', - '325794-61005'], + 'CA123456789', + '123456789', + ], invalid: [ - '010110123444', - '0101101234', - '01001612345', - '290217-22343'], + 'CA 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['mt-MT'], + validator: 'isVAT', + args: ['IS'], valid: [ - '1234567A', - '882345608', - '34581M', - '199Z'], + 'IS123456', + '12345', + ], invalid: [ - '812345608', - '88234560', - '8823456088', - '11234567A', - '12/34-567 A', - '88 23-456/08', - '1234560A', - '0000000M', - '3200100G'], + 'IS 12345', + '1234', + ], }); test({ - validator: 'isTaxID', - args: ['nl-NL'], + validator: 'isVAT', + args: ['IN'], valid: [ - '174559434'], + 'IN123456789012345', + '123456789012345', + ], invalid: [ - '17455943', - '1745594344', - '17 455-94/34'], + 'IN 123456789012345', + '12345678901234', + ], }); test({ - validator: 'isTaxID', - args: ['pl-PL'], + validator: 'isVAT', + args: ['ID'], valid: [ - '2234567895', - '02070803628', - '02870803622', - '02670803626', - '01510813623'], + 'ID123456789012345', + '123456789012345', + 'ID12.345.678.9-012.345', + '12.345.678.9-012.345', + ], invalid: [ - '020708036285', - '223456789', - '22 345-678/95', - '02 070-8036/28', - '2234567855', - '02223013623'], + 'ID 123456789012345', + '12345678901234', + ], }); test({ - validator: 'isTaxID', - args: ['pt-BR'], + validator: 'isVAT', + args: ['IL'], valid: [ - '35161990910', - '74407265027', - '05423994000172', - '11867044000130'], + 'IL123456789', + '123456789', + ], invalid: [ - 'ABCDEFGH', - '170.691.440-72', - '11494282142', - '74405265037', - '11111111111', - '48469799384', - '94.592.973/0001-82', - '28592361000192', - '11111111111111', - '111111111111112', - '61938188550993', - '82168365502729', + 'IL 123456789', + '12345678', ], }); test({ - validator: 'isTaxID', - args: ['pt-PT'], + validator: 'isVAT', + args: ['KZ'], valid: [ - '299999998', - '299992020'], + 'KZ123456789', + '123456789', + ], invalid: [ - '2999999988', - '29999999', - '29 999-999/8'], + 'KZ 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['ro-RO'], + validator: 'isVAT', + args: ['NZ'], valid: [ - '8001011234563', - '9000123456789', - '1001011234560', - '3001011234564', - '5001011234568'], + 'NZ123456789', + '123456789', + ], invalid: [ - '5001011234569', - '500 1011-234/568', - '500101123456', - '50010112345688', - '5001011504568', - '8000230234563', - '6000230234563'], + 'NZ 123456789', + '12345678', + ], }); test({ - validator: 'isTaxID', - args: ['sk-SK'], + validator: 'isVAT', + args: ['NG'], valid: [ - '530121999', - '536221/999', - '031121999', - '520229999', - '1234567890'], + 'NG123456789012', + '123456789012', + 'NG12345678-9012', + '12345678-9012', + ], invalid: [ - '53012199999', - '990101999', - '530121000', - '53012199', - '53-0121 999', - '535229999'], + 'NG 123456789012', + '12345678901', + ], }); test({ - validator: 'isTaxID', - args: ['sl-SI'], + validator: 'isVAT', + args: ['NO'], valid: [ - '15012557', - '15012590'], + 'NO123456789MVA', + '123456789MVA', + ], invalid: [ - '150125577', - '1501255', - '15 01-255/7'], + 'NO 123456789MVA', + '123456789', + ], }); test({ - validator: 'isTaxID', - args: ['sv-SE'], + validator: 'isVAT', + args: ['PH'], valid: [ - '640823-3234', - '640883-3231', - '6408833231', - '19640823-3233', - '196408233233', - '19640883-3230', - '200228+5266', - '20180101-5581'], + 'PH123456789012', + '123456789012', + 'PH123 456 789 012', + '123 456 789 012', + ], invalid: [ - '640823+3234', - '160230-3231', - '160260-3231', - '160260-323', - '160260323', - '640823+323', - '640823323', - '640823+32344', - '64082332344', - '19640823-32333', - '1964082332333'], + 'PH 123456789012', + '12345678901', + ], }); test({ - validator: 'isTaxID', + validator: 'isVAT', + args: ['RU'], valid: [ - '01-1234567'], + 'RU1234567890', + '1234567890', + 'RU123456789012', + '123456789012', + ], + invalid: [ + 'RU 123456789012', + '12345678901', + ], }); test({ - validator: 'isTaxID', - args: ['is-NOT'], - error: [ - '01-1234567', - '01 1234567', - '011234567', - '0-11234567', - '01#1234567', - '01 1234567', - '01 1234 567', - '07-1234567', - '28-1234567', - '96-1234567', + validator: 'isVAT', + args: ['SM'], + valid: [ + 'SM12345', + '12345', + ], + invalid: [ + 'SM 12345', + '1234', ], }); - }); - - - it('should validate slug', () => { test({ - validator: 'isSlug', + validator: 'isVAT', + args: ['SA'], valid: [ - 'foo', - 'foo-bar', - 'foo_bar', - 'foo-bar-foo', - 'foo-bar_foo', - 'foo-bar_foo*75-b4r-**_foo', - 'foo-bar_foo*75-b4r-**_foo-&&', + 'SA123456789012345', + '123456789012345', ], invalid: [ - 'not-----------slug', - '@#_$@', - '-not-slug', - 'not-slug-', - '_not-slug', - 'not-slug_', - 'not slug', + 'SA 123456789012345', + '12345678901234', ], }); - }); - - it('should validate strong passwords', () => { test({ - validator: 'isStrongPassword', - args: [{ - minLength: 8, - minLowercase: 1, - minUppercase: 1, - minNumbers: 1, - minSymbols: 1, - }], + validator: 'isVAT', + args: ['RS'], valid: [ - '%2%k{7BsL"M%Kd6e', - 'EXAMPLE of very long_password123!', - 'mxH_+2vs&54_+H3P', - '+&DxJ=X7-4L8jRCD', - 'etV*p%Nr6w&H%FeF', + 'RS123456789', + '123456789', ], invalid: [ - '', - 'password', - 'hunter2', - 'hello world', - 'passw0rd', - 'password!', - 'PASSWORD!', + 'RS 123456789', + '12345678', ], }); - }); - - it('should validate base64URL', () => { test({ - validator: 'isBase64', - args: [{ urlSafe: true }], + validator: 'isVAT', + args: ['CH'], valid: [ - '', - 'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ', - '1234', - 'bXVtLW5ldmVyLXByb3Vk', - 'PDw_Pz8-Pg', - 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw', + 'CH123456TVA', + '123456TVA', + 'CH123456789MWST', + '123456789MWST', + 'CH123.456IVA', + '123.456IVA', + 'CH123.456.789TVA', + '123.456.789TVA', ], invalid: [ - ' AA', - '\tAA', - '\rAA', - '\nAA', - '123=', - 'This+isa/bad+base64Url==', - '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw', + 'CH 123456', + '12345', ], - error: [ - null, - undefined, - {}, - [], - 42, + }); + test({ + validator: 'isVAT', + args: ['TR'], + valid: [ + 'TR1234567890', + '1234567890', + ], + invalid: [ + 'TR 1234567890', + '123456789', ], }); - }); - - it('should validate date', () => { test({ - validator: 'isDate', + validator: 'isVAT', + args: ['UA'], valid: [ - new Date(), - new Date([2014, 2, 15]), - new Date('2014-03-15'), - '2020/02/29', + 'UA123456789012', + '123456789012', ], invalid: [ - '', - '15072002', - null, - undefined, - { year: 2002, month: 7, day: 15 }, - 42, - { toString() { return '[object Date]'; } }, // faking - '2020-02-30', // invalid date - '2019-02-29', // non-leap year - '2020-04-31', // invalid date - '2020/03-15', // mixed delimiter + 'UA 123456789012', + '12345678901', ], }); test({ - validator: 'isDate', - args: ['DD/MM/YYYY'], // old format for backward compatibility + validator: 'isVAT', + args: ['GB'], valid: [ - '15-07-2002', - '15/07/2002', + 'GB999 9999 00', + 'GB999 9999 96', + 'GB999999999 999', + 'GBGD000', + 'GBGD499', + 'GBHA500', + 'GBHA999', ], invalid: [ - '15/7/2002', - '15-7-2002', - '15/7/02', - '15-7-02', - '15-07/2002', + 'GB999999900', + 'GB999999996', + 'GB999 9999 97', + 'GB999999999999', + 'GB999999999 9999', + 'GB9999999999 999', + 'GBGD 000', + 'GBGD 499', + 'GBHA 500', + 'GBHA 999', + 'GBGD500', + 'GBGD999', + 'GBHA000', + 'GBHA499', ], }); test({ - validator: 'isDate', - args: [{ format: 'DD/MM/YYYY' }], + validator: 'isVAT', + args: ['UZ'], valid: [ - '15-07-2002', - '15/07/2002', + 'UZ123456789', + '123456789', ], invalid: [ - '15/7/2002', - '15-7-2002', - '15/7/02', - '15-7-02', - '15-07/2002', + 'UZ 123456789', + '12345678', ], }); test({ - validator: 'isDate', - args: [{ format: 'DD/MM/YY' }], + validator: 'isVAT', + args: ['AR'], valid: [ - '15-07-02', - '15/07/02', + 'AR12345678901', + '12345678901', ], invalid: [ - '15/7/2002', - '15-7-2002', - '15/07-02', + 'AR 12345678901', + '1234567890', ], }); test({ - validator: 'isDate', - args: [{ format: 'D/M/YY' }], + validator: 'isVAT', + args: ['BO'], valid: [ - '5-7-02', - '5/7/02', + 'BO1234567', + '1234567', ], invalid: [ - '5/07/02', - '15/7/02', - '15-7-02', - '5/7-02', + 'BO 1234567', + '123456', ], }); test({ - validator: 'isDate', - args: [{ format: 'DD/MM/YYYY', strictMode: true }], + validator: 'isVAT', + args: ['BR'], valid: [ - '15/07/2002', + 'BR12.345.678/9012-34', + '12.345.678/9012-34', + 'BR123.456.789-01', + '123.456.789-01', ], invalid: [ - '15-07-2002', - '15/7/2002', - '15-7-2002', - '15/7/02', - '15-7-02', - '15-07/2002', + 'BR 12.345.678/9012-34', + '12345678901234', ], }); test({ - validator: 'isDate', - args: [{ strictMode: true }], + validator: 'isVAT', + args: ['CL'], valid: [ - '2020/01/15', - '2014/02/15', - '2014/03/15', - '2020/02/29', + 'CL12345678-9', + '12345678-9', ], invalid: [ - '2014-02-15', - '2020-02-29', - '15-07/2002', - new Date(), - new Date([2014, 2, 15]), - new Date('2014-03-15'), + 'CL 12345678-9', + '12345678', ], }); test({ - validator: 'isDate', - args: [{ delimiters: ['/', ' '] }], + validator: 'isVAT', + args: ['CO'], valid: [ - new Date(), - new Date([2014, 2, 15]), - new Date('2014-03-15'), - '2020/02/29', - '2020 02 29', + 'CO1234567890', + '1234567890', ], invalid: [ - '2020-02-29', - '', - '15072002', - null, - undefined, - { year: 2002, month: 7, day: 15 }, - 42, - { toString() { return '[object Date]'; } }, - '2020/02/30', - '2019/02/29', - '2020/04/31', - '2020/03-15', + 'CO 1234567890', + '123456789', ], }); test({ - validator: 'isDate', - args: [{ format: 'MM.DD.YYYY', delimiters: ['.'], strictMode: true }], + validator: 'isVAT', + args: ['CR'], valid: [ - '01.15.2020', - '02.15.2014', - '03.15.2014', - '02.29.2020', + 'CR123456789012', + '123456789012', + 'CR123456789', + '123456789', ], invalid: [ - '2014-02-15', - '2020-02-29', - '15-07/2002', - new Date(), - new Date([2014, 2, 15]), - new Date('2014-03-15'), - '29.02.2020', + 'CR 123456789', + '12345678', ], }); - }); - it('should be valid license plate', () => { test({ - validator: 'isLicensePlate', - args: ['pt-PT'], + validator: 'isVAT', + args: ['EC'], valid: [ - 'AA-12-34', - '12·34·AB', - '12·AB·34', - 'AB 12 CD', - 'AB12CD', + 'EC1234567890123', + '1234567890123', ], invalid: [ - '', - 'notalicenseplate', - 'A1-B2-C3', - 'ABC-1-EF', + 'EC 1234567890123', + '123456789012', ], }); test({ - validator: 'isLicensePlate', - args: ['de-LI'], + validator: 'isVAT', + args: ['SV'], valid: [ - 'FL 1', - 'FL 99999', - 'FL 1337', + 'SV1234-567890-123-1', + '1234-567890-123-1', ], invalid: [ - '', - 'FL 999999', - 'AB 12345', - 'FL -1', + 'SV 1234-567890-123-1', + '1234567890123', ], }); test({ - validator: 'isLicensePlate', - args: ['de-DE'], + validator: 'isVAT', + args: ['GT'], valid: [ - 'M A 1', - 'M A 12', - 'M A 123', - 'M A 1234', - 'M AB 1', - 'M AB 12', - 'M AB 123', - 'M AB 1234', - 'FS A 1', - 'FS A 12', - 'FS A 123', - 'FS A 1234', - 'FS AB 1', - 'FS AB 12', - 'FS AB 123', - 'FS AB 1234', - 'FSAB1234', - 'FS-AB-1234', - 'FS AB 1234 H', - 'FS AB 1234 E', - 'FSAB1234E', - 'FS-AB-1234-E', + 'GT1234567-8', + '1234567-8', ], invalid: [ - 'YY AB 123', - 'PAF AB 1234', - 'M ABC 123', - 'M AB 12345', - 'FS AB 1234 A', + 'GT 1234567-8', + '1234567', ], }); test({ - validator: 'isLicensePlate', - args: ['fi-FI'], + validator: 'isVAT', + args: ['HN'], valid: [ - 'ABC-123', - 'ABC 123', - 'ABC123', - 'A100', - 'A 100', - 'A-100', - 'C10001', - 'C 10001', - 'C-10001', - '123-ABC', - '123 ABC', - '123ABC', - '123-A', - '123 A', - '123A', - '199AA', - '199 AA', - '199-AA', + 'HN', ], invalid: [ - ' ', - 'A-1', - 'A1A-100', - '1-A-2', - 'C1234567', - 'A B C 1 2 3', - 'abc-123', + 'HN ', ], }); test({ - validator: 'isLicensePlate', - args: ['sq-AL'], + validator: 'isVAT', + args: ['MX'], valid: [ - 'AA 000 AA', - 'ZZ 999 ZZ', + 'MXABCD123456EFG', + 'ABCD123456EFG', + 'MXABC123456DEF', + 'ABC123456DEF', ], invalid: [ - '', - 'AA 0 A', - 'AAA 00 AAA', + 'MX ABC123456EFG', + '123456', ], }); test({ - validator: 'isLicensePlate', - args: ['cs-CZ'], + validator: 'isVAT', + args: ['NI'], valid: [ - 'ALA4011', - '4A23000', - 'DICTAT0R', - 'VETERAN', - 'AZKVIZ8', - '2A45876', - 'DIC-TAT0R', + 'NI123-456789-0123A', + '123-456789-0123A', ], invalid: [ - '', - 'invalidlicenseplate', - 'LN5758898', - 'X-|$|-X', - 'AE0F-OP4', - 'GO0MER', - '2AAAAAAAA', - 'FS AB 1234 E', - 'GB999 9999 00', + 'NI 123-456789-0123A', + '1234567890123', ], }); - test({ - validator: 'isLicensePlate', - args: ['pt-BR'], + validator: 'isVAT', + args: ['PA'], valid: [ - 'ABC1234', - 'ABC 1234', - 'ABC-1234', - 'ABC1D23', - 'ABC1K23', - 'ABC1Z23', - 'ABC 1D23', - 'ABC-1D23', + 'PA', ], invalid: [ - '', - 'AA 0 A', - 'AAA 00 AAA', - 'ABCD123', - 'AB12345', - 'AB123DC', + 'PA ', ], }); test({ - validator: 'isLicensePlate', - args: ['any'], + validator: 'isVAT', + args: ['PY'], valid: [ - 'FL 1', - 'FS AB 123', + 'PY12345678-9', + '12345678-9', + 'PY123456-7', + '123456-7', ], invalid: [ - '', - 'FL 999999', - 'FS AB 1234 A', + 'PY 123456-7', + '123456', ], }); test({ - validator: 'isLicensePlate', - args: ['asdfasdf'], - error: [ - 'FL 1', - 'FS AB 123', - 'FL 999999', - 'FS AB 1234 A', + validator: 'isVAT', + args: ['PE'], + valid: [ + 'PE12345678901', + '12345678901', + ], + invalid: [ + 'PE 12345678901', + '1234567890', ], }); - }); - it('should validate VAT numbers', () => { test({ validator: 'isVAT', - args: ['GB'], + args: ['DO'], valid: [ - 'GB999 9999 00', - 'GB999 9999 96', - 'GB999999999 999', - 'GBGD000', - 'GBGD499', - 'GBHA500', - 'GBHA999', + 'DO12345678901', + '12345678901', + 'DO123-4567890-1', + '123-4567890-1', + 'DO123456789', + '123456789', + 'DO1-23-45678-9', + '1-23-45678-9', ], invalid: [ - 'GB999999900', - 'GB999999996', - 'GB999 9999 97', - 'GB999999999999', - 'GB999999999 9999', - 'GB9999999999 999', - 'GBGD 000', - 'GBGD 499', - 'GBHA 500', - 'GBHA 999', - 'GBGD500', - 'GBGD999', - 'GBHA000', - 'GBHA499', + 'DO 12345678901', + '1234567890', ], }); test({ validator: 'isVAT', - args: ['IT'], + args: ['UY'], valid: [ - 'IT12345678910', - '12345678910', + 'UY123456789012', + '123456789012', ], invalid: [ - 'IT12345678 910', - 'IT 123456789101', - 'IT123456789101', - 'GB12345678910', - 'IT123456789', + 'UY 123456789012', + '12345678901', ], }); test({ validator: 'isVAT', - args: ['NL'], + args: ['VE'], valid: [ - 'NL123456789B10', - '123456789B10', + 'VEJ-123456789', + 'J-123456789', + 'VEJ-12345678-9', + 'J-12345678-9', ], invalid: [ - 'NL12345678 910', - 'NL 123456789101', - 'NL123456789B1', - 'GB12345678910', - 'NL123456789', + 'VE J-123456789', + '12345678', ], }); test({ diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js new file mode 100644 index 000000000..d771d9198 --- /dev/null +++ b/test/validators/isAfter.test.js @@ -0,0 +1,61 @@ +import test from '../testFunctions'; + +describe('isAfter', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: [{ comparisonDate: 'invalid date' }], + invalid: ['invalid date', '2015-09-17'], + }); + }); + + describe('(legacy syntax)', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: ['invalid date'], + invalid: ['invalid date', '2015-09-17'], + }); + }); + }); +}); diff --git a/test/validators/isISBN.test.js b/test/validators/isISBN.test.js new file mode 100644 index 000000000..99fb2e014 --- /dev/null +++ b/test/validators/isISBN.test.js @@ -0,0 +1,109 @@ +import test from '../testFunctions'; + +describe('isISBN', () => { + it('should validate ISBNs', () => { + test({ + validator: 'isISBN', + args: [{ version: 10 }], + valid: [ + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '1617290858', '1-61729-085-8', '1 61729 085-8', + '0007269706', '0-00-726970-6', '0 00 726970 6', + '3423214120', '3-423-21412-0', '3 423 21412 0', + '340101319X', '3-401-01319-X', '3 401 01319 X', + ], + invalid: [ + '3423214121', '3-423-21412-1', '3 423 21412 1', + '978-3836221191', '9783836221191', + '123456789a', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + args: [{ version: 13 }], + valid: [ + '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', + '9783401013190', '978-3401013190', '978 3401013190', + '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', + ], + invalid: [ + '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '01234567890ab', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + valid: [ + '340101319X', + '9784873113685', + ], + invalid: [ + '3423214121', + '9783836221190', + ], + }); + test({ + validator: 'isISBN', + args: [{ version: 'foo' }], + invalid: [ + '340101319X', + '9784873113685', + ], + }); + }); + + describe('(legacy syntax)', () => { + it('should validate ISBNs', () => { + test({ + validator: 'isISBN', + args: [10], + valid: [ + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '1617290858', '1-61729-085-8', '1 61729 085-8', + '0007269706', '0-00-726970-6', '0 00 726970 6', + '3423214120', '3-423-21412-0', '3 423 21412 0', + '340101319X', '3-401-01319-X', '3 401 01319 X', + ], + invalid: [ + '3423214121', '3-423-21412-1', '3 423 21412 1', + '978-3836221191', '9783836221191', + '123456789a', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + args: [13], + valid: [ + '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', + '9783401013190', '978-3401013190', '978 3401013190', + '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', + ], + invalid: [ + '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '01234567890ab', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + valid: [ + '340101319X', + '9784873113685', + ], + invalid: [ + '3423214121', + '9783836221190', + ], + }); + test({ + validator: 'isISBN', + args: ['foo'], + invalid: [ + '340101319X', + '9784873113685', + ], + }); + }); + }); +});