diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index feab2fa77..799e41348 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -14,6 +14,9 @@ A clear and concise description of what the bug is. **Examples** If applicable, add screenshots to help explain your problem. +**Reproductions** +If applicable, provide a reproduction on platforms like [runkit](npm.runkit.com/validator) + **Additional context** Validator.js version: Node.js version: diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index bf7c05a0e..ba6041d49 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -5,8 +5,11 @@ feat(validatorName): brief title of what has been done + + ## Checklist - [ ] PR contains only changes related; no stray files, etc. - [ ] README updated (where applicable) - [ ] Tests written (where applicable) +- [ ] References provided in PR (where applicable) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 21686d855..e0024eff3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,25 +6,24 @@ on: branches: [master] jobs: test: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest strategy: matrix: - node-version: [14, 12, 10, 8, 6] + node-version: [22, 20, 18, 16, 14, 12, 10, 8] name: Run tests on Node.js ${{ matrix.node-version }} steps: - name: Setup Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2-beta + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - check-latest: true - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install dependencies - run: npm install + run: npm install --legacy-peer-deps - name: Run tests run: npm test - - if: matrix.node-version == 14 + - if: matrix.node-version == 22 name: Send coverage info to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v5 with: - file: ./coverage/cobertura-coverage.xml + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 1d346dec1..e89a9b51e 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -38,11 +38,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -53,7 +53,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v3 # ℹ️ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -67,4 +67,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index b4b62f1b9..008a86ce6 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -4,21 +4,23 @@ on: types: [created] jobs: publish: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write steps: - - name: Setup Node.js 14 - uses: actions/setup-node@v2-beta + - name: Setup Node.js 22 + uses: actions/setup-node@v4 with: - node-version: 14 - check-latest: true + node-version: 22 registry-url: https://registry.npmjs.org/ - name: Checkout Repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install Dependencies - run: npm install + run: npm install --legacy-peer-deps - name: Run Tests run: npm test - name: Publish Package to NPM Registry - run: npm publish + run: npm publish --provenance env: NODE_AUTH_TOKEN: ${{secrets.NPM_SECRET}} diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b063ff2a..25192b24a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,281 @@ -## 13.7.0 +# 13.15.20 + +### Fixes, New Locales and Enhancements + +- [#2556](https://github.com/validatorjs/validator.js/pull/2556) `isMobilePhone`: add `ar-QA` locale @WardKhaddour +- [#2576](https://github.com/validatorjs/validator.js/pull/2576) `isAlpha`/`isAlphanuneric`: add Indic locales (`ta-IN`, `te-IN`, `kn-IN`, `ml-IN`, `gu-IN`, `pa-IN`, `or-IN`) @avadootharajesh +- [#2574](https://github.com/validatorjs/validator.js/pull/2574) `isBase64`: improve padding regex @KrayzeeKev +- [#2584](https://github.com/validatorjs/validator.js/pull/2584) `isVAT`: improve `FR` locale @iamAmer +- [#2608](https://github.com/validatorjs/validator.js/pull/2608) `isURL`: improve protocol detection. Resolves CVE-2025-56200 @theofidry +- **Doc fixes and others:** + - [#2563](https://github.com/validatorjs/validator.js/pull/2563) @stoneLeaf + - [#2581](https://github.com/validatorjs/validator.js/pull/2581) @camillobruni + +# 13.15.15 + +### Fixes, New Locales and Enhancements + +- `isMobilePhone` + - [#2514](https://github.com/validatorjs/validator.js/pull/2514) improve `el-CY` locale @rezk2ll + - [#2512](https://github.com/validatorjs/validator.js/pull/2512) improve `pt-AO` locale @renaldodev + - [#2502](https://github.com/validatorjs/validator.js/pull/2502) improve `ar-OM` locale @tomcastro +- [#2089](https://github.com/validatorjs/validator.js/pull/2089) `isIP`: allow usage of option object @pixelbucket-dev +- [#2526](https://github.com/validatorjs/validator.js/pull/2526) `isPassportNumber`: improve `CA` locale @evanbechtol +- [#2491](https://github.com/validatorjs/validator.js/pull/2491) `isBase64`: improve validation based on RFC4648 @aseyfpour +- [#2479](https://github.com/validatorjs/validator.js/pull/2479) `isPostalCode`: improve `FR` locale @Rajput-Balram +- [#2088](https://github.com/validatorjs/validator.js/pull/2088) `isBefore`: allow usage of option object @pixelbucket-dev +- [#2346](https://github.com/validatorjs/validator.js/pull/2346) `isRgbColor`: allow second digit in rgba alpha value @controlol +- [#2453](https://github.com/validatorjs/validator.js/pull/2453) `isIP`: improve IPv6 regex @ShreySinha02 +- [#2052](https://github.com/validatorjs/validator.js/pull/2052) `isPostalCode`: add `PK` locale @mateeni-dev +- [#2529](https://github.com/validatorjs/validator.js/pull/2529) `isPostalCode`: improve `TW` locale @Crocsx +- [#2550](https://github.com/validatorjs/validator.js/pull/2550) `isPassportNumber`: improve `US` locale @yitzchak-schechter +- [#2553](https://github.com/validatorjs/validator.js/pull/2553) `isUUID`: add `loose` option @bc-m +- [#2551](https://github.com/validatorjs/validator.js/pull/2551) `isPostalCode`: add `BD` locale @tanvirrb +- [#2555](https://github.com/validatorjs/validator.js/pull/2555) `isLicensePlate`: improve `pt-PT` locale @castrosu +- **Doc fixes and others:** + - [#2372](https://github.com/validatorjs/validator.js/pull/2372) @EmersonRabelo + - [#2538](https://github.com/validatorjs/validator.js/pull/2538) @WikiRik + - [#2539](https://github.com/validatorjs/validator.js/pull/2539) @WikiRik + - [#2540](https://github.com/validatorjs/validator.js/pull/2540) @WikiRik + - [#2549](https://github.com/validatorjs/validator.js/pull/2549) @WikiRik + - [#2537](https://github.com/validatorjs/validator.js/pull/2537) @sgress454 + +# 13.15.0 + +### New Features / Validators + +- [#2399](https://github.com/validatorjs/validator.js/pull/2399) `isISO31661Numeric` @RobinvanderVliet +- [#2294](https://github.com/validatorjs/validator.js/pull/2294) `isULID` @arafatkn +- [#2215](https://github.com/validatorjs/validator.js/pull/2215) `isISO15924` @xDivisionByZerox + +### Fixes, New Locales and Enhancements + +- `isMobilePhone` + - [#2395](https://github.com/validatorjs/validator.js/pull/2395) add `es-GT` locale @ignaciosuarezquilis + - [#1971](https://github.com/validatorjs/validator.js/pull/1971) improve `en-GB` locale @ihmpavel + - [#2359](https://github.com/validatorjs/validator.js/pull/2359) improve `uk-UA` locale @arttiger + - [#2350](https://github.com/validatorjs/validator.js/pull/2350) improve `ky-KG` locale @sadraliev + - [#2482](https://github.com/validatorjs/validator.js/pull/2482) improve `en-ZM` locale @sonikishan + - [#2362](https://github.com/validatorjs/validator.js/pull/2362) improve `en-GH` locale @NanaAb-116 + - [#2500](https://github.com/validatorjs/validator.js/pull/2500) add `mk-MK` locale @eshward95 + - [#2534](https://github.com/validatorjs/validator.js/pull/2534) improve `sq-AL` locale @nichoola +- [#2406](https://github.com/validatorjs/validator.js/pull/2406) `isBtcAddress` support all address formats and testnets @madoke +- [#2339](https://github.com/validatorjs/validator.js/pull/2339) `isIBAN` improve `VG` regex @ST-DDT +- [#2332](https://github.com/validatorjs/validator.js/pull/2332) `isISO4217` update currency codes @cbodtorf +- [#2291](https://github.com/validatorjs/validator.js/pull/2291) `isIdentityCard` add `PK` locale @Daniyal-Qureshi +- [#2414](https://github.com/validatorjs/validator.js/pull/2414) `isEmail` fix blacklist_chars @keshavlingala +- [#2416](https://github.com/validatorjs/validator.js/pull/2416) `isInt`/`isFloat` handle undefined and null values @Daniyal-Qureshi +- [#2415](https://github.com/validatorjs/validator.js/pull/2415) `isPostalCode` add `CO` locale @jorgevrgs +- [#2404](https://github.com/validatorjs/validator.js/pull/2404) `isPassportNumber` export `passportNumberLocales` @derekparnell +- [#2029](https://github.com/validatorjs/validator.js/pull/2029) `isRgbColor` add `allowSpaces` option @a-h-i +- [#2421](https://github.com/validatorjs/validator.js/pull/2421) `isUUID` require valid variant field and require RFC9562 UUID in version `all` @broofa +- [#2439](https://github.com/validatorjs/validator.js/pull/2439) `isURL` add `max_allowed_length` option @pinkiesky +- [#2437](https://github.com/validatorjs/validator.js/pull/2437) `isEmail` reject starting with double quotes @code0emperor +- [#2333](https://github.com/validatorjs/validator.js/pull/2333) `isLicensePlate` add `en-SG` locale @Sabarinathan07 +- [#2441](https://github.com/validatorjs/validator.js/pull/2441) `normalizeEmail` add `yandex_convert_yandexru` option @AayushGH +- [#2443](https://github.com/validatorjs/validator.js/pull/2443) `isDate` return false instead of Error in certain cases @pano9000 +- [#2474](https://github.com/validatorjs/validator.js/pull/2474) `isLength` add `discreteLengths` option @Suven-p +- [#2481](https://github.com/validatorjs/validator.js/pull/2481) `isDate` disallow mismatching length in `strictMode` @sonikishan +- [#2492](https://github.com/validatorjs/validator.js/pull/2492) `isISO6346` set check digit to 0 if remainder is 10 @joelcuy +- [#2493](https://github.com/validatorjs/validator.js/pull/2493) `isPostalCode` improve `BR` locale @ticmaisdev +- [#2494](https://github.com/validatorjs/validator.js/pull/2494) `isEmail` allow regexp in `host_whitelist` and `host_blacklist` @weikangchia +- [#2518](https://github.com/validatorjs/validator.js/pull/2518) `isIBAN` improve `IE`/`PS` regex @Tarasz57 +- **Doc fixes and others:** + - [#2402](https://github.com/validatorjs/validator.js/pull/2402) @BibhushanKarki + - [#2394](https://github.com/validatorjs/validator.js/pull/2394) @RobinvanderVliet + - [#1732](https://github.com/validatorjs/validator.js/pull/1732) @alguerocode + - [#2413](https://github.com/validatorjs/validator.js/pull/2413) @rubiin + - [#2408](https://github.com/validatorjs/validator.js/pull/2408) @profnandaa + - [#2411](https://github.com/validatorjs/validator.js/pull/2411) @rubiin + - [#2325](https://github.com/validatorjs/validator.js/pull/2325) @ovarn + - [#2418](https://github.com/validatorjs/validator.js/pull/2418) @ihmpavel + - [#2323](https://github.com/validatorjs/validator.js/pull/2323) @ovarn + - [#2423](https://github.com/validatorjs/validator.js/pull/2423) @rubiin + - [#2409](https://github.com/validatorjs/validator.js/pull/2409) @profnandaa + - [#2442](https://github.com/validatorjs/validator.js/pull/2442) @pano9000 + +# 13.12.0 + +### New Features / Validators + +- [#2143](https://github.com/validatorjs/validator.js/pull/2143) `isAbaRouting` @songyuew + +### Fixes, New Locales and Enhancements + +- [#2207](https://github.com/validatorjs/validator.js/pull/2207) `isLicensePlate` add Pakistani `en-PK` locale @anasshakil +- [#2208](https://github.com/validatorjs/validator.js/issues/2208) `isPort` fix invalid leading zeros @anasshakil +- [#2224](https://github.com/validatorjs/validator.js/pull/2224) `isTaxID` added Argentina `es-AR` locale @estefrare +- [#2257](https://github.com/validatorjs/validator.js/pull/2257) `isDate` timezone offset fix @tomaspanek +- [#2265](https://github.com/validatorjs/validator.js/pull/2265) `isPassportNumber` added `ZA` locale @GMorris-professional +- `isMobilePhone`: + - [#2267](https://github.com/validatorjs/validator.js/pull/2267) added `en-MW` locale @SimranSiddiqui + - [#2140](https://github.com/validatorjs/validator.js/pull/2140) fix `am-AM` locale @AlexKrupko +- [#2271](https://github.com/validatorjs/validator.js/pull/2271) `isPostalAddress` fix `NL` locale @RobinvanderVliet +- [#2273](https://github.com/validatorjs/validator.js/pull/2273) `isISO4217` add `SLE` currency @urg +- [#2278](https://github.com/validatorjs/validator.js/pull/2278) `isStrongPassword` fix symbolRegex to include `\` @nandavikas +- [#2279](https://github.com/validatorjs/validator.js/pull/2279) `isVAT` fixed `KZ` locale @MatthieuLemoine +- [#2285](https://github.com/validatorjs/validator.js/pull/2285) `isAlpha`, `isAlphanumeric` added `eo` locale @RobinvanderVliet +- [#2320](https://github.com/validatorjs/validator.js/pull/2320) `isIBAN` add Algeria `DZ` locale @thibault-lr +- [#2343](https://github.com/validatorjs/validator.js/pull/2343) `isVAT`improve `AU` locale @matthewberryman +- [#2345](https://github.com/validatorjs/validator.js/pull/2345) `isUUID` add support for v7 @ruscon +- [#2358](https://github.com/validatorjs/validator.js/pull/2358) `isTaxID` add Ukraine `uk-UA` locale @arttiger +- [#2381](https://github.com/validatorjs/validator.js/pull/2381) `isDate` disallow hiphen before year @Sumit-tech-joshi +- **Doc fixes and others:** + - [#2276](https://github.com/validatorjs/validator.js/pull/2276) @meyfa + - [#2341](https://github.com/validatorjs/validator.js/pull/2341) @WikiRik + - [#2364](https://github.com/validatorjs/validator.js/pull/2364) @rubiin + - [#2368](https://github.com/validatorjs/validator.js/pull/2368) @ZhulinskiiDanil + - [#2371](https://github.com/validatorjs/validator.js/pull/2371) @devmanbud + - [#2386](https://github.com/validatorjs/validator.js/pull/2386) @alinaghale88 + +# 13.11.0 + +### New Features / Validators + +- [#2144](https://github.com/validatorjs/validator.js/pull/2144) `isFreightContainerID`: for shipping containers IDs @songyuew +- [#2188](https://github.com/validatorjs/validator.js/pull/2188) `isMailtoURI` @uksarkar + +### Fixes, New Locales and Enhancements + +- [#2025](https://github.com/validatorjs/validator.js/pull/2025) `isIBAN` add `MA` locale @lroudge +- [#2117](https://github.com/validatorjs/validator.js/pull/2117) `isCreditCard` refactor @pano9000 +- [#2189](https://github.com/validatorjs/validator.js/pull/2189) `isLocale` add support for more language tags @kwahome +- [#2203](https://github.com/validatorjs/validator.js/pull/2203) `isVAT` for `CU` @jimmyorpheus +- [#2217](https://github.com/validatorjs/validator.js/pull/2217) `isJWT` @Prathamesh061 +- [#2222](https://github.com/validatorjs/validator.js/pull/2222) `IsFQDN` test enhancements @aalekhpatel07 +- [#2226](https://github.com/validatorjs/validator.js/pull/2226) `isAlpha`, `isAlphanumeric` for `kk-KZ` @BekStar7 +- [#2229](https://github.com/validatorjs/validator.js/pull/2229) `isEmail` support `allow_underscores` @guspower +- [#2231](https://github.com/validatorjs/validator.js/pull/2231) `isDate` enhance Date declaration compatibility across multiple environments @CiprianS +- [#2235](https://github.com/validatorjs/validator.js/pull/2235) `isIBAN` add white and blacklist options to the isIBAN validator @edilson +- [#2237](https://github.com/validatorjs/validator.js/pull/2237) `isEmail` do not allow non-breaking space in user part @jeremy21212121 +- `isMobilePhone`: + - [#2175](https://github.com/validatorjs/validator.js/pull/2175) `so-SO` @ohersi + - [#2176](https://github.com/validatorjs/validator.js/pull/2176) `fr-CF` @cheboi + - [#2197](https://github.com/validatorjs/validator.js/pull/2197) `es-CU` @klaframboise + - [#2202](https://github.com/validatorjs/validator.js/pull/2202) `pl-PL` @czerwony03 + - [#2209](https://github.com/validatorjs/validator.js/pull/2209) `fr-WF` @aidos42 + - [#2246](https://github.com/validatorjs/validator.js/pull/2246) `ar-SD` @Hussienma -### New Features +# 13.9.0 -- [#1706](https://github.com/validatorjs/validator.js/pull/1706) `isISO4217`, currency code validator @jpaya17 +### New Features / Validators + +- [#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 @@ -39,26 +312,31 @@ ### New and Improved Locales - `isAlpha`, `isAlphanumeric`: + - [#1716](https://github.com/validatorjs/validator.js/pull/1716) `hi-IN` @MiKr13 - [#1837](https://github.com/validatorjs/validator.js/pull/1837) `fi-FI` @Marcholio - `isPassportNumber`: + - [#1656](https://github.com/validatorjs/validator.js/pull/1656) `ID` @rubiin - [#1714](https://github.com/validatorjs/validator.js/pull/1714) `CN` @anirudhgiri - [#1809](https://github.com/validatorjs/validator.js/pull/1809) `PL` @Ronqn - [#1810](https://github.com/validatorjs/validator.js/pull/1810) `RU` @Theta-Dev - `isPostalCode`: + - [#1788](https://github.com/validatorjs/validator.js/pull/1788) `LK` @nimanthadilz - `isIdentityCard`: + - [#1657](https://github.com/validatorjs/validator.js/pull/1657) `TH` @tithanayut - [#1745](https://github.com/validatorjs/validator.js/pull/1745) `PL` @wiktorwojcik112 @fedeci @tux-tn - [#1786](https://github.com/validatorjs/validator.js/pull/1786) `LK` @nimanthadilz @tux-tn - [#1838](https://github.com/validatorjs/validator.js/pull/1838) `FI` @Marcholio - `isMobilePhone`: - - [#1679](https://github.com/validatorjs/validator.js/pull/1679) `de-DE` @AnnaMariaJansen + + - [#1679](https://github.com/validatorjs/validator.js/pull/1679) `de-DE` @AnnaMariaJansen - [#1689](https://github.com/validatorjs/validator.js/pull/1689) `vi-VN` @luisrivas - [#1695](https://github.com/validatorjs/validator.js/pull/1695) [#1682](https://github.com/validatorjs/validator.js/pull/1682) `zh-CN` @laulujan @yisibl - [#1734](https://github.com/validatorjs/validator.js/pull/1734) `es-VE` @islasjuanp @@ -81,6 +359,7 @@ - [#1846](https://github.com/validatorjs/validator.js/pull/1846) `tg-TJ` @mgnss - `isLicensePlate`: + - [#1565](https://github.com/validatorjs/validator.js/pull/1565) `cs-CZ` @filiptronicek - [#1790](https://github.com/validatorjs/validator.js/pull/1790) `fi-FI` @Marcholio @@ -90,9 +369,11 @@ #### 13.6.1 - **New features**: + - [#1495](https://github.com/validatorjs/validator.js/pull/1495) `isLicensePlate` @firlus - **Fixes and Enhancements**: + - [#1651](https://github.com/validatorjs/validator.js/pull/1651) fix ReDOS vulnerabilities in `isHSL` and `isEmail` @tux-tn - [#1644](https://github.com/validatorjs/validator.js/pull/1644) `isURL`: Allow URLs to have only a username in the userinfo subcomponent @jbuchmann-coosto - [#1633](https://github.com/validatorjs/validator.js/pull/1633) `isISIN`: optimization @bmacnaughton @@ -134,12 +415,14 @@ #### ~~13.5.0~~ 13.5.1 - **New features**: + - `isVAT` [#1463](https://github.com/validatorjs/validator.js/pull/1463) @ CodingNagger - `isTaxID` [#1446](https://github.com/validatorjs/validator.js/pull/1446) @tplessas - `isBase58` [#1445](https://github.com/validatorjs/validator.js/pull/1445) @ezkemboi - `isStrongPassword` [#1348](https://github.com/validatorjs/validator.js/pull/1348) @door-bell - **Fixes and Enhancements**: + - [#1486](https://github.com/validatorjs/validator.js/pull/1486) `isISO8601`: add `strictSeparator` @brostone51 - [#1474](https://github.com/validatorjs/validator.js/pull/1474) `isFQDN`: make more strict @CristhianMotoche - [#1469](https://github.com/validatorjs/validator.js/pull/1469) `isFQDN`: `allow_underscore` option @gibson042 @@ -183,10 +466,11 @@ - **New features**: - None - **Fixes and chores**: + - [#1425](https://github.com/validatorjs/validator.js/pull/1425) fix validation for _userinfo_ part for `isURL` @heanzyzabala - - [#1419](https://github.com/validatorjs/validator.js/pull/1419) fix `isBase32` and `isBase64` to validate empty strings properly @AberDerBart - - [#1408](https://github.com/validatorjs/validator.js/pull/1408) tests for `isTaxId` @dspinellis - - [#1397](https://github.com/validatorjs/validator.js/pull/1397) added `validate_length` option for `isURL` @tomgrossman + - [#1419](https://github.com/validatorjs/validator.js/pull/1419) fix `isBase32` and `isBase64` to validate empty strings properly @AberDerBart + - [#1408](https://github.com/validatorjs/validator.js/pull/1408) tests for `isTaxId` @dspinellis + - [#1397](https://github.com/validatorjs/validator.js/pull/1397) added `validate_length` option for `isURL` @tomgrossman - [#1383](https://github.com/validatorjs/validator.js/pull/1383) [#1428](https://github.com/validatorjs/validator.js/pull/1428) doc typos @0xflotus @timgates42 - [#1376](https://github.com/validatorjs/validator.js/pull/1376) add missing tests and switch to Coverall @tux-tn - [#1373](https://github.com/validatorjs/validator.js/pull/1373) improve code coverage @ezkemboi @@ -213,7 +497,6 @@ - `isIdentityCard`: - [#1384](https://github.com/validatorjs/validator.js/pull/1384) `IT` @lorenzodb1 - #### 13.1.1 - Hotfix for a regex incompatibility in some browsers @@ -245,24 +528,24 @@ ([#1338](https://github.com/validatorjs/validator.js/pull/1338)) - New and improved locales ([#1112](https://github.com/validatorjs/validator.js/pull/1112), - [#1167](https://github.com/validatorjs/validator.js/pull/1167), - [#1198](https://github.com/validatorjs/validator.js/pull/1198), - [#1199](https://github.com/validatorjs/validator.js/pull/1199), - [#1273](https://github.com/validatorjs/validator.js/pull/1273), - [#1279](https://github.com/validatorjs/validator.js/pull/1279), - [#1281](https://github.com/validatorjs/validator.js/pull/1281), - [#1293](https://github.com/validatorjs/validator.js/pull/1293), - [#1294](https://github.com/validatorjs/validator.js/pull/1294), - [#1311](https://github.com/validatorjs/validator.js/pull/1311), - [#1312](https://github.com/validatorjs/validator.js/pull/1312), - [#1313](https://github.com/validatorjs/validator.js/pull/1313), - [#1314](https://github.com/validatorjs/validator.js/pull/1314), - [#1315](https://github.com/validatorjs/validator.js/pull/1315), - [#1317](https://github.com/validatorjs/validator.js/pull/1317), - [#1322](https://github.com/validatorjs/validator.js/pull/1322), - [#1324](https://github.com/validatorjs/validator.js/pull/1324), - [#1330](https://github.com/validatorjs/validator.js/pull/1330), - [#1337](https://github.com/validatorjs/validator.js/pull/1337)) + [#1167](https://github.com/validatorjs/validator.js/pull/1167), + [#1198](https://github.com/validatorjs/validator.js/pull/1198), + [#1199](https://github.com/validatorjs/validator.js/pull/1199), + [#1273](https://github.com/validatorjs/validator.js/pull/1273), + [#1279](https://github.com/validatorjs/validator.js/pull/1279), + [#1281](https://github.com/validatorjs/validator.js/pull/1281), + [#1293](https://github.com/validatorjs/validator.js/pull/1293), + [#1294](https://github.com/validatorjs/validator.js/pull/1294), + [#1311](https://github.com/validatorjs/validator.js/pull/1311), + [#1312](https://github.com/validatorjs/validator.js/pull/1312), + [#1313](https://github.com/validatorjs/validator.js/pull/1313), + [#1314](https://github.com/validatorjs/validator.js/pull/1314), + [#1315](https://github.com/validatorjs/validator.js/pull/1315), + [#1317](https://github.com/validatorjs/validator.js/pull/1317), + [#1322](https://github.com/validatorjs/validator.js/pull/1322), + [#1324](https://github.com/validatorjs/validator.js/pull/1324), + [#1330](https://github.com/validatorjs/validator.js/pull/1330), + [#1337](https://github.com/validatorjs/validator.js/pull/1337)) #### 13.0.0 @@ -295,7 +578,7 @@ ([#1267](https://github.com/validatorjs/validator.js/pull/1267)) - New and improved locales ([#1238](https://github.com/validatorjs/validator.js/pull/1238), - [#1265](https://github.com/validatorjs/validator.js/pull/1265)) + [#1265](https://github.com/validatorjs/validator.js/pull/1265)) #### 12.2.0 @@ -305,10 +588,10 @@ ([#1227](https://github.com/validatorjs/validator.js/pull/1227)) - New and improved locales ([#1200](https://github.com/validatorjs/validator.js/pull/1200), - [#1207](https://github.com/validatorjs/validator.js/pull/1207), - [#1213](https://github.com/validatorjs/validator.js/pull/1213), - [#1217](https://github.com/validatorjs/validator.js/pull/1217), - [#1234](https://github.com/validatorjs/validator.js/pull/1234)) + [#1207](https://github.com/validatorjs/validator.js/pull/1207), + [#1213](https://github.com/validatorjs/validator.js/pull/1213), + [#1217](https://github.com/validatorjs/validator.js/pull/1217), + [#1234](https://github.com/validatorjs/validator.js/pull/1234)) #### 12.1.0 @@ -318,9 +601,9 @@ ([#1160](https://github.com/validatorjs/validator.js/pull/1160)) - New and improved locales ([#1162](https://github.com/validatorjs/validator.js/pull/1162), - [#1183](https://github.com/validatorjs/validator.js/pull/1183), - [#1187](https://github.com/validatorjs/validator.js/pull/1187), - [#1191](https://github.com/validatorjs/validator.js/pull/1191)) + [#1183](https://github.com/validatorjs/validator.js/pull/1183), + [#1187](https://github.com/validatorjs/validator.js/pull/1187), + [#1191](https://github.com/validatorjs/validator.js/pull/1191)) #### 12.0.0 @@ -342,18 +625,18 @@ ([#1074](https://github.com/validatorjs/validator.js/pull/1074)) - New and improved locales ([#1059](https://github.com/validatorjs/validator.js/pull/1059), - [#1060](https://github.com/validatorjs/validator.js/pull/1060), - [#1069](https://github.com/validatorjs/validator.js/pull/1069), - [#1073](https://github.com/validatorjs/validator.js/pull/1073), - [#1082](https://github.com/validatorjs/validator.js/pull/1082), - [#1092](https://github.com/validatorjs/validator.js/pull/1092), - [#1121](https://github.com/validatorjs/validator.js/pull/1121), - [#1125](https://github.com/validatorjs/validator.js/pull/1125), - [#1132](https://github.com/validatorjs/validator.js/pull/1132), - [#1152](https://github.com/validatorjs/validator.js/pull/1152), - [#1165](https://github.com/validatorjs/validator.js/pull/1165), - [#1166](https://github.com/validatorjs/validator.js/pull/1166), - [#1174](https://github.com/validatorjs/validator.js/pull/1174)) + [#1060](https://github.com/validatorjs/validator.js/pull/1060), + [#1069](https://github.com/validatorjs/validator.js/pull/1069), + [#1073](https://github.com/validatorjs/validator.js/pull/1073), + [#1082](https://github.com/validatorjs/validator.js/pull/1082), + [#1092](https://github.com/validatorjs/validator.js/pull/1092), + [#1121](https://github.com/validatorjs/validator.js/pull/1121), + [#1125](https://github.com/validatorjs/validator.js/pull/1125), + [#1132](https://github.com/validatorjs/validator.js/pull/1132), + [#1152](https://github.com/validatorjs/validator.js/pull/1152), + [#1165](https://github.com/validatorjs/validator.js/pull/1165), + [#1166](https://github.com/validatorjs/validator.js/pull/1166), + [#1174](https://github.com/validatorjs/validator.js/pull/1174)) #### 11.1.0 @@ -361,15 +644,15 @@ ([#1024](https://github.com/validatorjs/validator.js/pull/1024)) - New and improved locales ([#1035](https://github.com/validatorjs/validator.js/pull/1035), - [#1040](https://github.com/validatorjs/validator.js/pull/1040), - [#1041](https://github.com/validatorjs/validator.js/pull/1041), - [#1048](https://github.com/validatorjs/validator.js/pull/1048), - [#1049](https://github.com/validatorjs/validator.js/pull/1049), - [#1052](https://github.com/validatorjs/validator.js/pull/1052), - [#1054](https://github.com/validatorjs/validator.js/pull/1054), - [#1055](https://github.com/validatorjs/validator.js/pull/1055), - [#1056](https://github.com/validatorjs/validator.js/pull/1056), - [#1057](https://github.com/validatorjs/validator.js/pull/1057)) + [#1040](https://github.com/validatorjs/validator.js/pull/1040), + [#1041](https://github.com/validatorjs/validator.js/pull/1041), + [#1048](https://github.com/validatorjs/validator.js/pull/1048), + [#1049](https://github.com/validatorjs/validator.js/pull/1049), + [#1052](https://github.com/validatorjs/validator.js/pull/1052), + [#1054](https://github.com/validatorjs/validator.js/pull/1054), + [#1055](https://github.com/validatorjs/validator.js/pull/1055), + [#1056](https://github.com/validatorjs/validator.js/pull/1056), + [#1057](https://github.com/validatorjs/validator.js/pull/1057)) #### 11.0.0 @@ -383,11 +666,11 @@ ([0277eb](https://github.com/validatorjs/validator.js/commit/0277eb00d245a3479af52adf7d927d4036895650)) - New and improved locales ([#999](https://github.com/validatorjs/validator.js/pull/999), - [#1010](https://github.com/validatorjs/validator.js/pull/1010), - [#1017](https://github.com/validatorjs/validator.js/pull/1017), - [#1022](https://github.com/validatorjs/validator.js/pull/1022), - [#1031](https://github.com/validatorjs/validator.js/pull/1031), - [#1032](https://github.com/validatorjs/validator.js/pull/1032)) + [#1010](https://github.com/validatorjs/validator.js/pull/1010), + [#1017](https://github.com/validatorjs/validator.js/pull/1017), + [#1022](https://github.com/validatorjs/validator.js/pull/1022), + [#1031](https://github.com/validatorjs/validator.js/pull/1031), + [#1032](https://github.com/validatorjs/validator.js/pull/1032)) #### 10.11.0 @@ -402,9 +685,9 @@ ([#932](https://github.com/validatorjs/validator.js/pull/932)) - New and improved locales ([#931](https://github.com/validatorjs/validator.js/pull/931), - [#933](https://github.com/validatorjs/validator.js/pull/933), - [#947](https://github.com/validatorjs/validator.js/pull/947), - [#950](https://github.com/validatorjs/validator.js/pull/950)) + [#933](https://github.com/validatorjs/validator.js/pull/933), + [#947](https://github.com/validatorjs/validator.js/pull/947), + [#950](https://github.com/validatorjs/validator.js/pull/950)) #### 10.9.0 @@ -416,11 +699,11 @@ ([#906](https://github.com/validatorjs/validator.js/pull/906)) - New and improved locales ([#899](https://github.com/validatorjs/validator.js/pull/899), - [#904](https://github.com/validatorjs/validator.js/pull/904), - [#913](https://github.com/validatorjs/validator.js/pull/913), - [#916](https://github.com/validatorjs/validator.js/pull/916), - [#925](https://github.com/validatorjs/validator.js/pull/925), - [#928](https://github.com/validatorjs/validator.js/pull/928)) + [#904](https://github.com/validatorjs/validator.js/pull/904), + [#913](https://github.com/validatorjs/validator.js/pull/913), + [#916](https://github.com/validatorjs/validator.js/pull/916), + [#925](https://github.com/validatorjs/validator.js/pull/925), + [#928](https://github.com/validatorjs/validator.js/pull/928)) #### 10.8.0 @@ -430,7 +713,7 @@ ([#895](https://github.com/validatorjs/validator.js/pull/895)) - Locales are now exported ([#890](https://github.com/validatorjs/validator.js/pull/890), - [#892](https://github.com/validatorjs/validator.js/pull/892)) + [#892](https://github.com/validatorjs/validator.js/pull/892)) - New locale ([#896](https://github.com/validatorjs/validator.js/pull/896)) @@ -456,7 +739,7 @@ ([#880](https://github.com/validatorjs/validator.js/pull/880)) - New and improved locales ([#878](https://github.com/validatorjs/validator.js/pull/878), - [#879](https://github.com/validatorjs/validator.js/pull/879)) + [#879](https://github.com/validatorjs/validator.js/pull/879)) #### 10.5.0 @@ -472,14 +755,14 @@ ([#860](https://github.com/validatorjs/validator.js/issues/860)) - New and improved locales ([#801](https://github.com/validatorjs/validator.js/pull/801), - [#856](https://github.com/validatorjs/validator.js/pull/856), - [#859](https://github.com/validatorjs/validator.js/issues/859), - [#861](https://github.com/validatorjs/validator.js/pull/861), - [#862](https://github.com/validatorjs/validator.js/pull/862), - [#863](https://github.com/validatorjs/validator.js/pull/863), - [#864](https://github.com/validatorjs/validator.js/pull/864), - [#870](https://github.com/validatorjs/validator.js/pull/870), - [#872](https://github.com/validatorjs/validator.js/pull/872)) + [#856](https://github.com/validatorjs/validator.js/pull/856), + [#859](https://github.com/validatorjs/validator.js/issues/859), + [#861](https://github.com/validatorjs/validator.js/pull/861), + [#862](https://github.com/validatorjs/validator.js/pull/862), + [#863](https://github.com/validatorjs/validator.js/pull/863), + [#864](https://github.com/validatorjs/validator.js/pull/864), + [#870](https://github.com/validatorjs/validator.js/pull/870), + [#872](https://github.com/validatorjs/validator.js/pull/872)) #### 10.4.0 @@ -496,8 +779,8 @@ ([#832](https://github.com/validatorjs/validator.js/pull/832)) - New locales ([#831](https://github.com/validatorjs/validator.js/pull/831), - [#835](https://github.com/validatorjs/validator.js/pull/835), - [#836](https://github.com/validatorjs/validator.js/pull/836)) + [#835](https://github.com/validatorjs/validator.js/pull/835), + [#836](https://github.com/validatorjs/validator.js/pull/836)) #### 10.2.0 @@ -545,10 +828,10 @@ - New and improved locales ([#763](https://github.com/validatorjs/validator.js/pull/763), - [#768](https://github.com/validatorjs/validator.js/pull/768), - [#774](https://github.com/validatorjs/validator.js/pull/774), - [#777](https://github.com/validatorjs/validator.js/pull/777), - [#779](https://github.com/validatorjs/validator.js/pull/779)) + [#768](https://github.com/validatorjs/validator.js/pull/768), + [#774](https://github.com/validatorjs/validator.js/pull/774), + [#777](https://github.com/validatorjs/validator.js/pull/777), + [#779](https://github.com/validatorjs/validator.js/pull/779)) #### 9.2.0 @@ -556,8 +839,8 @@ ([#760](https://github.com/validatorjs/validator.js/pull/760)) - New and improved locales ([#753](https://github.com/validatorjs/validator.js/pull/753), - [#755](https://github.com/validatorjs/validator.js/pull/755), - [#764](https://github.com/validatorjs/validator.js/pull/764)) + [#755](https://github.com/validatorjs/validator.js/pull/755), + [#764](https://github.com/validatorjs/validator.js/pull/764)) #### 9.1.2 @@ -568,7 +851,7 @@ - Locale fixes ([#738](https://github.com/validatorjs/validator.js/pull/738), - [#739](https://github.com/validatorjs/validator.js/pull/739)) + [#739](https://github.com/validatorjs/validator.js/pull/739)) #### 9.1.0 @@ -576,7 +859,7 @@ ([#734](https://github.com/validatorjs/validator.js/pull/734)) - New locales ([#735](https://github.com/validatorjs/validator.js/pull/735), - [#737](https://github.com/validatorjs/validator.js/pull/737)) + [#737](https://github.com/validatorjs/validator.js/pull/737)) #### 9.0.0 @@ -597,10 +880,10 @@ ([#713](https://github.com/validatorjs/validator.js/pull/713)) - New and improved locales ([#700](https://github.com/validatorjs/validator.js/pull/700), - [#701](https://github.com/validatorjs/validator.js/pull/701), - [#714](https://github.com/validatorjs/validator.js/pull/714), - [#715](https://github.com/validatorjs/validator.js/pull/715), - [#718](https://github.com/validatorjs/validator.js/pull/718)) + [#701](https://github.com/validatorjs/validator.js/pull/701), + [#714](https://github.com/validatorjs/validator.js/pull/714), + [#715](https://github.com/validatorjs/validator.js/pull/715), + [#718](https://github.com/validatorjs/validator.js/pull/718)) #### 8.1.0 @@ -636,7 +919,7 @@ ([#677](https://github.com/validatorjs/validator.js/pull/677)) - New locales ([#673](https://github.com/validatorjs/validator.js/pull/673), - [#676](https://github.com/validatorjs/validator.js/pull/676)) + [#676](https://github.com/validatorjs/validator.js/pull/676)) #### 7.1.0 @@ -649,9 +932,9 @@ ([#655](https://github.com/validatorjs/validator.js/issues/655)) - New locales ([#647](https://github.com/validatorjs/validator.js/pull/647), - [#667](https://github.com/validatorjs/validator.js/pull/667), - [#667](https://github.com/validatorjs/validator.js/pull/667), - [#671](https://github.com/validatorjs/validator.js/pull/671)) + [#667](https://github.com/validatorjs/validator.js/pull/667), + [#667](https://github.com/validatorjs/validator.js/pull/667), + [#671](https://github.com/validatorjs/validator.js/pull/671)) #### 7.0.0 @@ -663,9 +946,9 @@ ([#618](https://github.com/validatorjs/validator.js/issues/618)) - New locales ([#616](https://github.com/validatorjs/validator.js/pull/616), - [#622](https://github.com/validatorjs/validator.js/pull/622), - [#627](https://github.com/validatorjs/validator.js/pull/627), - [#630](https://github.com/validatorjs/validator.js/pull/630)) + [#622](https://github.com/validatorjs/validator.js/pull/622), + [#627](https://github.com/validatorjs/validator.js/pull/627), + [#630](https://github.com/validatorjs/validator.js/pull/630)) #### 6.2.1 @@ -717,7 +1000,7 @@ ([#576](https://github.com/validatorjs/validator.js/pull/576)) - New locales ([#575](https://github.com/validatorjs/validator.js/pull/575), - [#552](https://github.com/validatorjs/validator.js/issues/552)) + [#552](https://github.com/validatorjs/validator.js/issues/552)) #### 5.6.0 @@ -859,7 +1142,7 @@ ([#471](https://github.com/validatorjs/validator.js/pull/471)) - Tweak Greek and Chinese mobile phone validation ([#467](https://github.com/validatorjs/validator.js/pull/467), - [#468](https://github.com/validatorjs/validator.js/pull/468)) + [#468](https://github.com/validatorjs/validator.js/pull/468)) - Fixed a bug in `isDate()` when validating ISO 8601 dates without a timezone ([#472](https://github.com/validatorjs/validator.js/issues/472)) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..c7da04163 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,37 @@ +# Contributing to validator.js +Welcome to validator.js repository!! We appreciate your interest in contributing to this open library and for helping our community grow. + +## How to Contribute +### Code Contribution +In general, we follow the "fork-and-pull" Git workflow. + +1. [Fork](https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-a-project) the repository on GitHub +2. Clone the project to your local machine +3. Work on your fork + * Make your changes and additions + - Most of your changes should be focused on src/ and test/ folders and/or [README.md](https://github.com/validatorjs/validator.js/blob/master/README.md). + - Files such as validator.js, validator.min.js and files in lib/ folder are autogenerated when running tests (npm test) and need not to be changed **manually**. + * Change or add tests if needed + * Run tests and make sure they pass + * Add changes to README.md if needed +4. Commit changes to your own branch +5. **Make sure** you merge the latest from "upstream" and resolve conflicts if there is any +6. Repeat step 3(3) above +7. Push your work back up to your fork +8. Submit a Pull request so that we can review your changes + +#### Run Tests +Tests are using mocha. To run the tests use: + +```sh +$ npm test +``` + +### Financial Contribution +We welcome financial contributions on our [open collective](https://opencollective.com/validatorjs). + +You can opt to become a [backer](https://opencollective.com/validatorjs#backer) or a [sponsor](https://opencollective.com/validatorjs#sponsor) and help our project sustain over time. + +Thank you to the people who have already contributed: + + \ No newline at end of file diff --git a/README.md b/README.md index 8977228c8..37fbd5e3d 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ # validator.js -# Changes made - 1707074 [![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) +[![License](https://img.shields.io/badge/License-MIT-red.svg)](https://github.com/alguerocode/validator.js/blob/master/LICENSE) [![Gitter][gitter-image]][gitter-url] -[![Disclose a vulnerability][huntr-image]][huntr-url] A library of string validators and sanitizers. @@ -22,7 +21,13 @@ Passing anything other than a string will result in an error. ### Server-side usage -Install the library with `npm install validator` +Install the `validator` package as: + +```sh +npm i validator +yarn add validator +pnpm i validator +``` #### No ES6 @@ -73,106 +78,102 @@ CDN ``` -## Contributors - -[Become a backer](https://opencollective.com/validatorjs#backer) - -[Become a sponsor](https://opencollective.com/validatorjs#sponsor) - -Thank you to the people who have already contributed: - - - ## Validators 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.
`minOccurrences`: 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', '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', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. -**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', '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', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque. +**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', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', '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', 'bg-BG', 'bn', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. -**isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html). -**isBase58(str)** | check if a string is base58 encoded. -**isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) -**isBefore(str [, date])** | check if the string is a date that's before the specified date. -**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, padding: true }`
when `urlSafe` is true default value for `padding` is false and it tests the given base64 encoded string is [url safe][Base64 URL Safe]. +**isBefore(str [, options])** | check if the string is a date that is before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.

**Options:**
`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now). +**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 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', 'eo', '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, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings or regexp, 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 or regexp, 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', 'eo', '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 }`.

`require_tld` - If set to false the validator will not check if the domain includes a TLD.
`allow_underscores` - if set to true, the validator will allow underscores in the domain.
`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.
`allow_numeric_tld` - if set to true, the validator will allow the TLD of the domain to be made up solely of numbers.
`allow_wildcard` - if set to true, the validator will allow domains starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
`ignore_max_length` - if set to true, the validator will not check for the standard max length of a domain.
+**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isFullWidth(str)** | check if the string contains any full-width chars. **isHalfWidth(str)** | check if the string contains any half-width chars. -**isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['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, [, options])** | check if the string is an IBAN (International Bank Account Number).

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

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK', 'PK']` 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). +**isIP(str [, options])** | check if the string is an IP address (version 4 or 6).

`options` is an object that defaults to `{ version: '' }`.

**Options:**
`version`: defines which IP version to compare to. Accepted values: `4`, `6`, `'4'`, `'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). -**isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. -**isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. -**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. -**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. -**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. -**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. +**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. +**isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code. +**isISO8601(str [, options])** | check if the string is a valid [ISO 8601][ISO 8601] date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. +**isISO15924(str)** | check if the string is a valid [ISO 15924][ISO 15924] officially assigned script code. +**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. +**isISO31661Numeric(str)** | check if the string is a valid [ISO 3166-1 numeric][ISO 3166-1 numeric] 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-BR', 'pt-PT', 'sq-AL', 'sv-SE', 'en-IN', 'hi-IN', 'gu-IN', 'as-IN', 'bn-IN', 'kn-IN', 'ml-IN', 'mr-IN', 'or-IN', 'pa-IN', 'sa-IN', 'ta-IN', 'te-IN', 'kok-IN']` or `any`) -**isLocale(str)** | check if the string is a locale +**isLength(str [, options])** | check if the string's length falls in a range and equal to any of the integers of the `discreteLengths` array if provided.

`options` is an object which defaults to `{ min: 0, max: undefined, discreteLengths: 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', 'en-SG', 'en-PK', '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. -**isLuhnValid(str)** | check if the string passes the luhn check. -**isMACAddress(str [, options])** | check if the string is a MAC address.

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

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

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

`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`. +**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-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-MW', '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-GT','es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'mk-MK', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to `'any'`. If 'any' or a falsey value is used, function will check if any of the locales match).

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

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

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

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. +**isPostalCode(str, locale)** | check if the string is a postal code.

`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', 'BD', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CO', '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', 'PK', '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 [,options])** | check if the string is a rgb or rgba color.

`options` is an object with the following properties

`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.

`allowSpaces` defaults to `true`, which prohibits whitespace. If set to false, whitespace between color values is allowed, such as `rgb(255, 255, 255)` or even `rgba(255, 128, 0, 0.7)`. **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-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]` -**isURL(str [, options])** | check if the string is an URL.

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

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
require_port - if set as true isURL will check if port is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
allow_fragments - if set as false isURL will return false if fragments are present.
allow_query_components - if set as false isURL will return false if query components are present.
validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length). -**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5). +**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'`.

`hourFormat` 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', 'withSeconds', withOptionalSeconds`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format, `'withOptionalSeconds'` will validate `'HH:MM'` and `'HH:MM:SS'` formats. +**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-AR', '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', 'uk-UA']`. +**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 }`.

`protocols` - valid protocols can be modified with this option.
`require_tld` - If set to false isURL will not check if the URL's host includes a top-level domain.
`require_protocol` - **RECOMMENDED** if set to true isURL will return false if protocol is not present in the URL. Without this setting, some malicious URLs cannot be distinguishable from a valid URL with authentication information.
`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.
`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.
`allow_underscores` - if set to true, the validator will allow underscores in the URL.
`host_whitelist` - if set to an array of strings or regexp, and the domain matches none of the strings defined in it, the validation fails.
`host_blacklist` - if set to an array of strings or regexp, and the domain matches any of the strings defined in it, the validation fails.
`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.
`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.
`disallow_auth` - if set to true, the validator will fail if the URL contains an authentication component, e.g. `http://username:password@example.com`.
`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.
`max_allowed_length` - if set, isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).
+**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec). +**isUUID(str [, version])** | check if the string is an RFC9562 UUID.
`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, `'all'` or `'loose'`. The `'loose'` option checks if the string is a UUID-like string with hexadecimal values, ignoring RFC9565. **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: `[ 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', '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', 'GB', 'UZ', 'AR', 'BO', 'BR', 'CL', 'CO', 'CR', 'EC', 'SV', 'GT', 'HN', 'MX', 'NI', 'PA', 'PY', 'PE', 'DO', 'UY', 'VE' ]`. -**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 @@ -181,9 +182,9 @@ Here is a list of the sanitizers currently available. 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. +**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`. @@ -191,7 +192,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 @@ -200,37 +201,12 @@ XSS sanitization was removed from the library in [2d5d6999](https://github.com/v For an alternative, have a look at Yahoo's [xss-filters library](https://github.com/yahoo/xss-filters) or at [DOMPurify](https://github.com/cure53/DOMPurify). -## Contributing - -In general, we follow the "fork-and-pull" Git workflow. - -1. Fork the repo on GitHub -2. Clone the project to your own machine -3. Work on your fork - 1. Make your changes and additions - - Most of your changes should be focused on `src/` and `test/` folders and/or `README.md`. - - Files such as `validator.js`, `validator.min.js` and files in `lib/` folder are autogenerated when running tests (`npm test`) and need not to be changed **manually**. - 2. Change or add tests if needed - 3. Run tests and make sure they pass - 4. Add changes to README.md if needed -4. Commit changes to your own branch -5. **Make sure** you merge the latest from "upstream" and resolve conflicts if there is any -6. Repeat step 3(3) above -7. Push your work back up to your fork -8. Submit a Pull request so that we can review your changes - -## Tests - -Tests are using mocha, to run the tests use: - -```sh -$ npm test -``` - ## Maintainers - [chriso](https://github.com/chriso) - **Chris O'Hara** (author) - [profnandaa](https://github.com/profnandaa) - **Anthony Nandaa** +- [rubiin](https://github.com/rubiin) - **Rubin Bhandari** +- [wikirik](https://github.com/wikirik) - **Rik Smale** - [ezkemboi](https://github.com/ezkemboi) - **Ezrqn Kemboi** - [tux-tn](https://github.com/tux-tn) - **Sarhan Aissi** @@ -238,30 +214,13 @@ $ npm test Remember, validating can be troublesome sometimes. See [A list of articles about programming assumptions commonly made that aren't true](https://github.com/jameslk/awesome-falsehoods). -## License (MIT) +## Contributing + +We welcome contributions from the community! If you're interested in contributing to this project, please read our [Contribution Guide](CONTRIBUTING.md) to get started. -``` -Copyright (c) 2018 Chris O'Hara - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -``` +## License + +This project is licensed under the [MIT](LICENSE). See the [LICENSE](LICENSE) file for details. [downloads-image]: http://img.shields.io/npm/dm/validator.svg @@ -283,5 +242,28 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [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 15924]: https://en.wikipedia.org/wiki/ISO_15924 +[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 3166-1 numeric]: https://en.wikipedia.org/wiki/ISO_3166-1_numeric +[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 +[Mailto URI Format]: https://en.wikipedia.org/wiki/Mailto +[MIME Type]: https://en.wikipedia.org/wiki/Media_type +[mongoid]: http://docs.mongodb.org/manual/reference/object-id/ +[RFC 3339]: https://tools.ietf.org/html/rfc3339 +[VAT Number]: https://en.wikipedia.org/wiki/VAT_identification_number diff --git a/SECURITY.md b/SECURITY.md index 72592f135..266d8d844 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -8,4 +8,4 @@ In the case of a confirmed security issue, only the current version of validator **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 +Report the security issue to the Node.js Security Working Group through the [HackerOne program](https://hackerone.com/nodejs-ecosystem) for ecosystem modules on npm, or to [Snyk Security Team](https://snyk.io/vulnerability-disclosure). They will help triage the security issue and work with all involved parties to remediate and release a fix. diff --git a/package.json b/package.json index 7d505205e..106694955 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.7.0", + "version": "13.15.20", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ @@ -9,7 +9,7 @@ "es", "lib", "README.md", - "LICENCE", + "LICENSE", "validator.js", "validator.min.js" ], @@ -51,6 +51,7 @@ "rimraf": "^3.0.0", "rollup": "^0.47.0", "rollup-plugin-babel": "^4.0.1", + "timezone-mock": "^1.3.6", "uglify-js": "^3.0.19" }, "scripts": { @@ -66,7 +67,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 42e1e8b69..b69c43649 100644 --- a/src/index.js +++ b/src/index.js @@ -13,14 +13,16 @@ 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'; +import isAbaRouting from './lib/isAbaRouting'; import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha'; import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric'; import isNumeric from './lib/isNumeric'; -import isPassportNumber from './lib/isPassportNumber'; +import isPassportNumber, { locales as passportNumberLocales } from './lib/isPassportNumber'; import isPort from './lib/isPort'; import isLowercase from './lib/isLowercase'; import isUppercase from './lib/isUppercase'; @@ -61,6 +63,7 @@ import isEmpty from './lib/isEmpty'; import isLength from './lib/isLength'; import isByteLength from './lib/isByteLength'; +import isULID from './lib/isULID'; import isUUID from './lib/isUUID'; import isMongoId from './lib/isMongoId'; @@ -69,7 +72,7 @@ import isBefore from './lib/isBefore'; import isIn from './lib/isIn'; -import isLuhnValid from './lib/isLuhnValid'; +import isLuhnNumber from './lib/isLuhnNumber'; import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; @@ -87,11 +90,14 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; +import { isISO6346, isFreightContainerID } from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; +import isISO15924 from './lib/isISO15924'; import isISO31661Alpha2 from './lib/isISO31661Alpha2'; import isISO31661Alpha3 from './lib/isISO31661Alpha3'; +import isISO31661Numeric from './lib/isISO31661Numeric'; import isISO4217 from './lib/isISO4217'; import isBase32 from './lib/isBase32'; @@ -99,6 +105,7 @@ import isBase58 from './lib/isBase58'; import isBase64 from './lib/isBase64'; import isDataURI from './lib/isDataURI'; import isMagnetURI from './lib/isMagnetURI'; +import isMailtoURI from './lib/isMailtoURI'; import isMimeType from './lib/isMimeType'; @@ -123,7 +130,7 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; -const version = '13.7.0'; +const version = '13.15.20'; const validator = { version, @@ -143,12 +150,14 @@ const validator = { isBoolean, isIBAN, isBIC, + isAbaRouting, isAlpha, isAlphaLocales, isAlphanumeric, isAlphanumericLocales, isNumeric, isPassportNumber, + passportNumberLocales, isPort, isLowercase, isUppercase, @@ -179,12 +188,13 @@ const validator = { isLength, isLocale, isByteLength, + isULID, isUUID, isMongoId, isAfter, isBefore, isIn, - isLuhnValid, + isLuhnNumber, isCreditCard, isIdentityCard, isEAN, @@ -198,17 +208,22 @@ const validator = { isEthereumAddress, isCurrency, isBtcAddress, + isISO6346, + isFreightContainerID, isISO6391, isISO8601, + isISO15924, isRFC3339, isISO31661Alpha2, isISO31661Alpha3, + isISO31661Numeric, isISO4217, isBase32, isBase58, isBase64, isDataURI, isMagnetURI, + isMailtoURI, isMimeType, isLatLong, ltrim, @@ -226,6 +241,7 @@ const validator = { isStrongPassword, isTaxID, isDate, + isTime, isLicensePlate, isVAT, ibanLocales, diff --git a/src/lib/alpha.js b/src/lib/alpha.js index c941c9bd7..6f58c9aee 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, @@ -18,6 +19,7 @@ export const alpha = { 'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i, 'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i, 'ru-RU': /^[А-ЯЁ]+$/i, + 'kk-KZ': /^[А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$/i, 'sl-SI': /^[A-ZČĆĐŠŽ]+$/i, 'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i, 'sr-RS@latin': /^[A-ZČĆŽŠĐ]+$/i, @@ -27,13 +29,22 @@ 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: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣৰৱ৲৳৴৵৶৷৸৹৺৻']+$/, + eo: /^[ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i, 'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i, 'si-LK': /^[\u0D80-\u0DFF]+$/, + 'ta-IN': /^[\u0B80-\u0BFF]+$/i, + 'te-IN': /^[\u0C00-\u0C7F]+$/i, + 'kn-IN': /^[\u0C80-\u0CFF]+$/i, + 'ml-IN': /^[\u0D00-\u0D7F]+$/i, + 'gu-IN': /^[\u0A80-\u0AFF]+$/i, + 'pa-IN': /^[\u0A00-\u0A7F]+$/i, + 'or-IN': /^[\u0B00-\u0B7F]+$/i, }; export const alphanumeric = { @@ -48,6 +59,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, @@ -55,6 +67,7 @@ export const alphanumeric = { 'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i, 'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i, 'ru-RU': /^[0-9А-ЯЁ]+$/i, + 'kk-KZ': /^[0-9А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$/i, 'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i, 'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i, 'sr-RS@latin': /^[0-9A-ZČĆŽŠĐ]+$/i, @@ -63,14 +76,23 @@ 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: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ০১২৩৪৫৬৭৮৯ৰৱ৲৳৴৵৶৷৸৹৺৻']+$/, + eo: /^[0-9ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i, 'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i, 'si-LK': /^[0-9\u0D80-\u0DFF]+$/, + 'ta-IN': /^[0-9\u0B80-\u0BFF.]+$/i, + 'te-IN': /^[0-9\u0C00-\u0C7F.]+$/i, + 'kn-IN': /^[0-9\u0C80-\u0CFF.]+$/i, + 'ml-IN': /^[0-9\u0D00-\u0D7F.]+$/i, + 'gu-IN': /^[0-9\u0A80-\u0AFF.]+$/i, + 'pa-IN': /^[0-9\u0A00-\u0A7F.]+$/i, + 'or-IN': /^[0-9\u0B00-\u0B7F.]+$/i, }; export const decimal = { @@ -119,9 +141,10 @@ for (let locale, i = 0; i < bengaliLocales.length; i++) { // 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', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', + 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'eo', 'es-ES', 'fr-CA', 'fr-FR', + 'gu-IN', 'hi-IN', 'hu-HU', 'id-ID', 'it-IT', 'kk-KZ', 'kn-IN', 'ku-IQ', 'ml-IN', 'nb-NO', + 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sr-RS', + 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'tr-TR', 'uk-UA', 'vi-VN', ]; for (let i = 0; i < dotDecimal.length; i++) { diff --git a/src/lib/contains.js b/src/lib/contains.js index 7be314b04..8e716c4be 100644 --- a/src/lib/contains.js +++ b/src/lib/contains.js @@ -2,14 +2,14 @@ import assertString from './util/assertString'; import toString from './util/toString'; import merge from './util/merge'; -const defaulContainsOptions = { +const defaultContainsOptions = { ignoreCase: false, minOccurrences: 1, }; export default function contains(str, elem, options) { assertString(str); - options = merge(options, defaulContainsOptions); + options = merge(options, defaultContainsOptions); if (options.ignoreCase) { return str.toLowerCase().split(toString(elem).toLowerCase()).length > options.minOccurrences; diff --git a/src/lib/isAbaRouting.js b/src/lib/isAbaRouting.js new file mode 100644 index 000000000..0c6fd7fb2 --- /dev/null +++ b/src/lib/isAbaRouting.js @@ -0,0 +1,20 @@ +import assertString from './util/assertString'; + +// http://www.brainjar.com/js/validation/ +// https://www.aba.com/news-research/research-analysis/routing-number-policy-procedures +// series reserved for future use are excluded +const isRoutingReg = /^(?!(1[3-9])|(20)|(3[3-9])|(4[0-9])|(5[0-9])|(60)|(7[3-9])|(8[1-9])|(9[0-2])|(9[3-9]))[0-9]{9}$/; + +export default function isAbaRouting(str) { + assertString(str); + + if (!isRoutingReg.test(str)) return false; + + let checkSumVal = 0; + for (let i = 0; i < str.length; i++) { + if (i % 3 === 0) checkSumVal += str[i] * 3; + else if (i % 3 === 1) checkSumVal += str[i] * 7; + else checkSumVal += str[i] * 1; + } + return (checkSumVal % 10 === 0); +} diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 47bfb537f..149622a0d 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -1,9 +1,12 @@ -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 = (typeof options === 'object' ? 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/isBase64.js b/src/lib/isBase64.js index 02dead0f4..fd876e4c0 100644 --- a/src/lib/isBase64.js +++ b/src/lib/isBase64.js @@ -1,28 +1,25 @@ import assertString from './util/assertString'; import merge from './util/merge'; -const notBase64 = /[^A-Z0-9+\/=]/i; -const urlSafeBase64 = /^[A-Z0-9_\-]*$/i; - -const defaultBase64Options = { - urlSafe: false, -}; +const base64WithPadding = /^[A-Za-z0-9+/]+={0,2}$/; +const base64WithoutPadding = /^[A-Za-z0-9+/]+$/; +const base64UrlWithPadding = /^[A-Za-z0-9_-]+={0,2}$/; +const base64UrlWithoutPadding = /^[A-Za-z0-9_-]+$/; export default function isBase64(str, options) { assertString(str); - options = merge(options, defaultBase64Options); - const len = str.length; + options = merge(options, { urlSafe: false, padding: !options?.urlSafe }); - if (options.urlSafe) { - return urlSafeBase64.test(str); - } + if (str === '') return true; - if (len % 4 !== 0 || notBase64.test(str)) { - return false; + if (options.padding && str.length % 4 !== 0) return false; + + let regex; + if (options.urlSafe) { + regex = options.padding ? base64UrlWithPadding : base64UrlWithoutPadding; + } else { + regex = options.padding ? base64WithPadding : base64WithoutPadding; } - const firstPaddingChar = str.indexOf('='); - return firstPaddingChar === -1 || - firstPaddingChar === len - 1 || - (firstPaddingChar === len - 2 && str[len - 1] === '='); + return (!options.padding || str.length % 4 === 0) && regex.test(str); } diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 8314f0e14..977d0ad1a 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,9 +1,12 @@ -import assertString from './util/assertString'; import toDate from './toDate'; -export default function isBefore(str, date = String(new Date())) { - assertString(str); - const comparison = toDate(date); - const original = toDate(str); +export default function isBefore(date, options) { + // For backwards compatibility: + // isBefore(str [, date]), i.e. `options` could be used as argument for the legacy `date` + const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); + + const comparison = toDate(comparisonDate); + const original = toDate(date); + return !!(original && comparison && original < comparison); } diff --git a/src/lib/isBoolean.js b/src/lib/isBoolean.js index 9fddc2b48..40ebd3504 100644 --- a/src/lib/isBoolean.js +++ b/src/lib/isBoolean.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import includes from './util/includesArray'; const defaultOptions = { loose: false }; const strictBooleans = ['true', 'false', '1', '0']; @@ -8,8 +9,8 @@ export default function isBoolean(str, options = defaultOptions) { assertString(str); if (options.loose) { - return looseBooleans.includes(str.toLowerCase()); + return includes(looseBooleans, str.toLowerCase()); } - return strictBooleans.includes(str); + return includes(strictBooleans, str); } diff --git a/src/lib/isBtcAddress.js b/src/lib/isBtcAddress.js index 2dfd04651..1a309e682 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}$/; +const bech32 = /^(bc1|tb1|bc1p|tb1p)[ac-hj-np-z02-9]{39,58}$/; +const base58 = /^(1|2|3|m)[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 53c80eb80..938679d39 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -1,15 +1,42 @@ import assertString from './util/assertString'; -import isLuhnValid from './isLuhnValid'; +import isLuhnValid from './isLuhnNumber'; -/* 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}))$/; -/* eslint-enable max-len */ +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})?$/, +}; -export default function isCreditCard(str) { - assertString(str); - const sanitized = str.replace(/[- ]+/g, ''); - if (!creditCard.test(sanitized)) { +const allCards = (() => { + const tmpCardsArray = []; + for (const cardProvider in cards) { + // istanbul ignore else + if (cards.hasOwnProperty(cardProvider)) { + tmpCardsArray.push(cards[cardProvider]); + } + } + return tmpCardsArray; +})(); + +export default function isCreditCard(card, options = {}) { + assertString(card); + 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; + } + } 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.some(cardProvider => cardProvider.test(sanitized))) { + // no specific provider return false; } - return isLuhnValid(str); + return isLuhnValid(card); } diff --git a/src/lib/isDataURI.js b/src/lib/isDataURI.js index 0275d2cc8..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; diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 8b7862e8b..3a1e4afd2 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -12,7 +12,7 @@ function isValidFormat(format) { function zip(date, format) { const zippedArr = [], - len = Math.min(date.length, format.length); + len = Math.max(date.length, format.length); for (let i = 0; i < len; i++) { zippedArr.push([date[i], format[i]]); @@ -22,12 +22,13 @@ function zip(date, format) { } export default function isDate(input, options) { - if (typeof options === 'string') { // Allow backward compatbility for old format isDate(input [, format]) + if (typeof options === 'string') { // Allow backward compatibility for old format isDate(input [, format]) options = merge({ format: options }, default_date_options); } else { options = merge(options, default_date_options); } if (typeof input === 'string' && isValidFormat(options.format)) { + if (options.strictMode && input.length !== options.format.length) return false; const formatDelimiter = options.delimiters .find(delimiter => options.format.indexOf(delimiter) !== -1); const dateDelimiter = options.strictMode @@ -40,14 +41,49 @@ export default function isDate(input, options) { const dateObj = {}; for (const [dateWord, formatWord] of dateAndFormat) { - if (dateWord.length !== formatWord.length) { + if (!dateWord || !formatWord || dateWord.length !== formatWord.length) { return false; } dateObj[formatWord.charAt(0)] = dateWord; } - return new Date(`${dateObj.m}/${dateObj.d}/${dateObj.y}`).getDate() === +dateObj.d; + let fullYear = dateObj.y; + + // Check if the year starts with a hyphen + if (fullYear.startsWith('-')) { + return false; // Hyphen before year is not allowed + } + + if (dateObj.y.length === 2) { + const parsedYear = parseInt(dateObj.y, 10); + + if (isNaN(parsedYear)) { + return false; + } + + const currentYearLastTwoDigits = new Date().getFullYear() % 100; + + if (parsedYear < currentYearLastTwoDigits) { + fullYear = `20${dateObj.y}`; + } else { + fullYear = `19${dateObj.y}`; + } + } + + let month = dateObj.m; + + if (dateObj.m.length === 1) { + month = `0${dateObj.m}`; + } + + let day = dateObj.d; + + if (dateObj.d.length === 1) { + day = `0${dateObj.d}`; + } + + return new Date(`${fullYear}-${month}-${day}T00:00:00.000Z`).getUTCDate() === +dateObj.d; } if (!options.strictMode) { diff --git a/src/lib/isDecimal.js b/src/lib/isDecimal.js index 488668a52..474eff2b0 100644 --- a/src/lib/isDecimal.js +++ b/src/lib/isDecimal.js @@ -1,6 +1,6 @@ import merge from './util/merge'; import assertString from './util/assertString'; -import includes from './util/includes'; +import includes from './util/includesArray'; import { decimal } from './alpha'; function decimalRegExp(options) { diff --git a/src/lib/isEAN.js b/src/lib/isEAN.js index 968c385dd..731932ad3 100644 --- a/src/lib/isEAN.js +++ b/src/lib/isEAN.js @@ -15,9 +15,9 @@ import assertString from './util/assertString'; /** - * Define EAN Lenghts; 8 for EAN-8; 13 for EAN-13; 14 for EAN-14 + * Define EAN Lengths; 8 for EAN-8; 13 for EAN-13; 14 for EAN-14 * and Regular Expression for valid EANs (EAN-8, EAN-13, EAN-14), - * with exact numberic matching of 8 or 13 or 14 digits [0-9] + * with exact numeric matching of 8 or 13 or 14 digits [0-9] */ const LENGTH_EAN_8 = 8; const LENGTH_EAN_14 = 14; diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index c7b7d6e95..abe465052 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -1,18 +1,21 @@ import assertString from './util/assertString'; +import checkHost from './util/checkHost'; -import merge from './util/merge'; import isByteLength from './isByteLength'; import isFQDN from './isFQDN'; import isIP from './isIP'; +import merge from './util/merge'; const default_email_options = { allow_display_name: false, + allow_underscores: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, blacklisted_chars: '', ignore_max_length: false, host_blacklist: [], + host_whitelist: [], }; /* eslint-disable max-len */ @@ -21,7 +24,7 @@ const splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+) 0 && checkHost(lower_domain, options.host_blacklist)) { + return false; + } + + if (options.host_whitelist.length > 0 && !checkHost(lower_domain, options.host_whitelist)) { return false; } @@ -103,11 +109,11 @@ export default function isEmail(str, options) { if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) { /* - Previously we removed dots for gmail addresses before validating. - This was removed because it allows `multiple..dots@gmail.com` - to be reported as valid, but it is not. - Gmail only normalizes single dots, removing them from here is pointless, - should be done in normalizeEmail + Previously we removed dots for gmail addresses before validating. + This was removed because it allows `multiple..dots@gmail.com` + to be reported as valid, but it is not. + Gmail only normalizes single dots, removing them from here is pointless, + should be done in normalizeEmail */ user = user.toLowerCase(); @@ -134,7 +140,11 @@ 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, + allow_underscores: options.allow_underscores, + })) { if (!options.allow_ip_domain) { return false; } @@ -152,7 +162,11 @@ export default function isEmail(str, options) { } } - if (user[0] === '"') { + if (options.blacklisted_chars) { + if (user.search(new RegExp(`[${options.blacklisted_chars}]+`, 'g')) !== -1) return false; + } + + if (user[0] === '"' && user[user.length - 1] === '"') { user = user.slice(1, user.length - 1); return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : @@ -168,9 +182,6 @@ export default function isEmail(str, options) { return false; } } - if (options.blacklisted_chars) { - if (user.search(new RegExp(`[${options.blacklisted_chars}]+`, 'g')) !== -1) return false; - } return true; } diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index 884d1dd6f..eb6928fda 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -7,6 +7,7 @@ const default_fqdn_options = { allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, + ignore_max_length: false, }; export default function isFQDN(str, options) { @@ -48,7 +49,7 @@ export default function isFQDN(str, options) { } return parts.every((part) => { - if (part.length > 63) { + if (part.length > 63 && !options.ignore_max_length) { return false; } diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index e6cced044..84bdc782c 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -1,19 +1,20 @@ import assertString from './util/assertString'; +import isNullOrUndefined from './util/nullUndefinedCheck'; import { decimal } from './alpha'; 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(',', '.')); return float.test(str) && - (!options.hasOwnProperty('min') || value >= options.min) && - (!options.hasOwnProperty('max') || value <= options.max) && - (!options.hasOwnProperty('lt') || value < options.lt) && - (!options.hasOwnProperty('gt') || value > options.gt); + (!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || value >= options.min) && + (!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || value <= options.max) && + (!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || value < options.lt) && + (!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || value > options.gt); } export const locales = Object.keys(decimal); diff --git a/src/lib/isIBAN.js b/src/lib/isIBAN.js index 535a95772..ed0abd6c0 100644 --- a/src/lib/isIBAN.js +++ b/src/lib/isIBAN.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import includes from './util/includesArray'; /** * List of country codes with @@ -24,6 +25,7 @@ const ibanRegexThroughCountryCode = { DE: /^(DE[0-9]{2})\d{18}$/, DK: /^(DK[0-9]{2})\d{14}$/, DO: /^(DO[0-9]{2})[A-Z]{4}\d{20}$/, + DZ: /^(DZ\d{24})$/, EE: /^(EE[0-9]{2})\d{16}$/, EG: /^(EG[0-9]{2})\d{25}$/, ES: /^(ES[0-9]{2})\d{20}$/, @@ -38,7 +40,7 @@ const ibanRegexThroughCountryCode = { GT: /^(GT[0-9]{2})[A-Z0-9]{4}[A-Z0-9]{20}$/, HR: /^(HR[0-9]{2})\d{17}$/, HU: /^(HU[0-9]{2})\d{24}$/, - IE: /^(IE[0-9]{2})[A-Z0-9]{4}\d{14}$/, + IE: /^(IE[0-9]{2})[A-Z]{4}\d{14}$/, IL: /^(IL[0-9]{2})\d{19}$/, IQ: /^(IQ[0-9]{2})[A-Z]{4}\d{15}$/, IR: /^(IR[0-9]{2})0\d{2}0\d{18}$/, @@ -53,6 +55,7 @@ const ibanRegexThroughCountryCode = { LT: /^(LT[0-9]{2})\d{16}$/, LU: /^(LU[0-9]{2})\d{3}[A-Z0-9]{13}$/, LV: /^(LV[0-9]{2})[A-Z]{4}[A-Z0-9]{13}$/, + MA: /^(MA[0-9]{26})$/, MC: /^(MC[0-9]{2})\d{10}[A-Z0-9]{11}\d{2}$/, MD: /^(MD[0-9]{2})[A-Z0-9]{20}$/, ME: /^(ME[0-9]{2})\d{18}$/, @@ -65,7 +68,7 @@ const ibanRegexThroughCountryCode = { NO: /^(NO[0-9]{2})\d{11}$/, PK: /^(PK[0-9]{2})[A-Z0-9]{4}\d{16}$/, PL: /^(PL[0-9]{2})\d{24}$/, - PS: /^(PS[0-9]{2})[A-Z0-9]{4}\d{21}$/, + PS: /^(PS[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/, PT: /^(PT[0-9]{2})\d{21}$/, QA: /^(QA[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/, RO: /^(RO[0-9]{2})[A-Z]{4}[A-Z0-9]{16}$/, @@ -82,10 +85,29 @@ const ibanRegexThroughCountryCode = { TR: /^(TR[0-9]{2})\d{5}[A-Z0-9]{17}$/, UA: /^(UA[0-9]{2})\d{6}[A-Z0-9]{19}$/, VA: /^(VA[0-9]{2})\d{18}$/, - VG: /^(VG[0-9]{2})[A-Z0-9]{4}\d{16}$/, + VG: /^(VG[0-9]{2})[A-Z]{4}\d{16}$/, XK: /^(XK[0-9]{2})\d{16}$/, }; +/** + * Check if the country codes passed are valid using the + * ibanRegexThroughCountryCode as a reference + * + * @param {array} countryCodeArray + * @return {boolean} + */ + +function hasOnlyValidCountryCodes(countryCodeArray) { + const countryCodeArrayFilteredWithObjectIbanCode = countryCodeArray + .filter(countryCode => !(countryCode in ibanRegexThroughCountryCode)); + + if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) { + return false; + } + + return true; +} + /** * Check whether string has correct universal IBAN format * The IBAN consists of up to 34 alphanumeric characters, as follows: @@ -95,14 +117,37 @@ const ibanRegexThroughCountryCode = { * NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z] * * @param {string} str - string under validation + * @param {object} options - object to pass the countries to be either whitelisted or blacklisted * @return {boolean} */ -function hasValidIbanFormat(str) { +function hasValidIbanFormat(str, options) { // Strip white spaces and hyphens const strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase(); const isoCountryCode = strippedStr.slice(0, 2).toUpperCase(); - return (isoCountryCode in ibanRegexThroughCountryCode) && + const isoCountryCodeInIbanRegexCodeObject = isoCountryCode in ibanRegexThroughCountryCode; + + if (options.whitelist) { + if (!hasOnlyValidCountryCodes(options.whitelist)) { + return false; + } + + const isoCountryCodeInWhiteList = includes(options.whitelist, isoCountryCode); + + if (!isoCountryCodeInWhiteList) { + return false; + } + } + + if (options.blacklist) { + const isoCountryCodeInBlackList = includes(options.blacklist, isoCountryCode); + + if (isoCountryCodeInBlackList) { + return false; + } + } + + return (isoCountryCodeInIbanRegexCodeObject) && ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr); } @@ -130,10 +175,10 @@ function hasValidIbanChecksum(str) { return remainder === 1; } -export default function isIBAN(str) { +export default function isIBAN(str, options = {}) { assertString(str); - return hasValidIbanFormat(str) && hasValidIbanChecksum(str); + return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str); } export const locales = Object.keys(ibanRegexThroughCountryCode); diff --git a/src/lib/isIMEI.js b/src/lib/isIMEI.js index fb97d4924..984b4fb88 100644 --- a/src/lib/isIMEI.js +++ b/src/lib/isIMEI.js @@ -1,8 +1,8 @@ import assertString from './util/assertString'; -let imeiRegexWithoutHypens = /^[0-9]{15}$/; -let imeiRegexWithHypens = /^\d{2}-\d{6}-\d{6}-\d{1}$/; +let imeiRegexWithoutHyphens = /^[0-9]{15}$/; +let imeiRegexWithHyphens = /^\d{2}-\d{6}-\d{6}-\d{1}$/; export default function isIMEI(str, options) { @@ -11,10 +11,10 @@ export default function isIMEI(str, options) { // default regex for checking imei is the one without hyphens - let imeiRegex = imeiRegexWithoutHypens; + let imeiRegex = imeiRegexWithoutHyphens; if (options.allow_hyphens) { - imeiRegex = imeiRegexWithHypens; + imeiRegex = imeiRegexWithHyphens; } diff --git a/src/lib/isIP.js b/src/lib/isIP.js index 40ca19aec..da9bb6ca9 100644 --- a/src/lib/isIP.js +++ b/src/lib/isIP.js @@ -42,19 +42,26 @@ const IPv6AddressRegExp = new RegExp('^(' + `(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` + `(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` + `(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` + - ')(%[0-9a-zA-Z-.:]{1,})?$'); + ')(%[0-9a-zA-Z.]{1,})?$'); + +export default function isIP(ipAddress, options = {}) { + assertString(ipAddress); + + // accessing 'arguments' for backwards compatibility: isIP(ipAddress [, version]) + // eslint-disable-next-line prefer-rest-params + const version = (typeof options === 'object' ? options.version : arguments[1]) || ''; -export default function isIP(str, version = '') { - assertString(str); - version = String(version); if (!version) { - return isIP(str, 4) || isIP(str, 6); + return isIP(ipAddress, { version: 4 }) || isIP(ipAddress, { version: 6 }); } - if (version === '4') { - return IPv4AddressRegExp.test(str); + + if (version.toString() === '4') { + return IPv4AddressRegExp.test(ipAddress); } - if (version === '6') { - return IPv6AddressRegExp.test(str); + + if (version.toString() === '6') { + return IPv6AddressRegExp.test(ipAddress); } + 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/isISO15924.js b/src/lib/isISO15924.js new file mode 100644 index 000000000..30f9e4a4f --- /dev/null +++ b/src/lib/isISO15924.js @@ -0,0 +1,37 @@ +import assertString from './util/assertString'; + +// from https://www.unicode.org/iso15924/iso15924-codes.html +const validISO15924Codes = new Set([ + 'Adlm', 'Afak', 'Aghb', 'Ahom', 'Arab', 'Aran', 'Armi', 'Armn', 'Avst', + 'Bali', 'Bamu', 'Bass', 'Batk', 'Beng', 'Bhks', 'Blis', 'Bopo', 'Brah', 'Brai', 'Bugi', 'Buhd', + 'Cakm', 'Cans', 'Cari', 'Cham', 'Cher', 'Chis', 'Chrs', 'Cirt', 'Copt', 'Cpmn', 'Cprt', 'Cyrl', 'Cyrs', + 'Deva', 'Diak', 'Dogr', 'Dsrt', 'Dupl', + 'Egyd', 'Egyh', 'Egyp', 'Elba', 'Elym', 'Ethi', + 'Gara', 'Geok', 'Geor', 'Glag', 'Gong', 'Gonm', 'Goth', 'Gran', 'Grek', 'Gujr', 'Gukh', 'Guru', + 'Hanb', 'Hang', 'Hani', 'Hano', 'Hans', 'Hant', 'Hatr', 'Hebr', 'Hira', 'Hluw', 'Hmng', 'Hmnp', 'Hrkt', 'Hung', + 'Inds', 'Ital', + 'Jamo', 'Java', 'Jpan', 'Jurc', + 'Kali', 'Kana', 'Kawi', 'Khar', 'Khmr', 'Khoj', 'Kitl', 'Kits', 'Knda', 'Kore', 'Kpel', 'Krai', 'Kthi', + 'Lana', 'Laoo', 'Latf', 'Latg', 'Latn', 'Leke', 'Lepc', 'Limb', 'Lina', 'Linb', 'Lisu', 'Loma', 'Lyci', 'Lydi', + 'Mahj', 'Maka', 'Mand', 'Mani', 'Marc', 'Maya', 'Medf', 'Mend', 'Merc', 'Mero', 'Mlym', 'Modi', 'Mong', 'Moon', 'Mroo', 'Mtei', 'Mult', 'Mymr', + 'Nagm', 'Nand', 'Narb', 'Nbat', 'Newa', 'Nkdb', 'Nkgb', 'Nkoo', 'Nshu', + 'Ogam', 'Olck', 'Onao', 'Orkh', 'Orya', 'Osge', 'Osma', 'Ougr', + 'Palm', 'Pauc', 'Pcun', 'Pelm', 'Perm', 'Phag', 'Phli', 'Phlp', 'Phlv', 'Phnx', 'Plrd', 'Piqd', 'Prti', 'Psin', + 'Qaaa', 'Qaab', 'Qaac', 'Qaad', 'Qaae', 'Qaaf', 'Qaag', 'Qaah', 'Qaai', 'Qaaj', 'Qaak', 'Qaal', 'Qaam', 'Qaan', 'Qaao', 'Qaap', 'Qaaq', 'Qaar', 'Qaas', 'Qaat', 'Qaau', 'Qaav', 'Qaaw', 'Qaax', 'Qaay', 'Qaaz', 'Qaba', 'Qabb', 'Qabc', 'Qabd', 'Qabe', 'Qabf', 'Qabg', 'Qabh', 'Qabi', 'Qabj', 'Qabk', 'Qabl', 'Qabm', 'Qabn', 'Qabo', 'Qabp', 'Qabq', 'Qabr', 'Qabs', 'Qabt', 'Qabu', 'Qabv', 'Qabw', 'Qabx', + 'Ranj', 'Rjng', 'Rohg', 'Roro', 'Runr', + 'Samr', 'Sara', 'Sarb', 'Saur', 'Sgnw', 'Shaw', 'Shrd', 'Shui', 'Sidd', 'Sidt', 'Sind', 'Sinh', 'Sogd', 'Sogo', 'Sora', 'Soyo', 'Sund', 'Sunu', 'Sylo', 'Syrc', 'Syre', 'Syrj', 'Syrn', + 'Tagb', 'Takr', 'Tale', 'Talu', 'Taml', 'Tang', 'Tavt', 'Tayo', 'Telu', 'Teng', 'Tfng', 'Tglg', 'Thaa', 'Thai', 'Tibt', 'Tirh', 'Tnsa', 'Todr', 'Tols', 'Toto', 'Tutg', + 'Ugar', + 'Vaii', 'Visp', 'Vith', + 'Wara', 'Wcho', 'Wole', + 'Xpeo', 'Xsux', + 'Yezi', 'Yiii', + 'Zanb', 'Zinh', 'Zmth', 'Zsye', 'Zsym', 'Zxxx', 'Zyyy', 'Zzzz', +]); + +export default function isISO15924(str) { + assertString(str); + return validISO15924Codes.has(str); +} + +export const ScriptCodes = validISO15924Codes; diff --git a/src/lib/isISO31661Numeric.js b/src/lib/isISO31661Numeric.js new file mode 100644 index 000000000..8b32a8d79 --- /dev/null +++ b/src/lib/isISO31661Numeric.js @@ -0,0 +1,26 @@ +import assertString from './util/assertString'; + +// from https://en.wikipedia.org/wiki/ISO_3166-1_numeric +const validISO31661NumericCountriesCodes = new Set([ + '004', '008', '010', '012', '016', '020', '024', '028', '031', '032', '036', '040', '044', '048', '050', '051', + '052', '056', '060', '064', '068', '070', '072', '074', '076', '084', '086', '090', '092', '096', '100', '104', + '108', '112', '116', '120', '124', '132', '136', '140', '144', '148', '152', '156', '158', '162', '166', '170', + '174', '175', '178', '180', '184', '188', '191', '192', '196', '203', '204', '208', '212', '214', '218', '222', + '226', '231', '232', '233', '234', '238', '239', '242', '246', '248', '250', '254', '258', '260', '262', '266', + '268', '270', '275', '276', '288', '292', '296', '300', '304', '308', '312', '316', '320', '324', '328', '332', + '334', '336', '340', '344', '348', '352', '356', '360', '364', '368', '372', '376', '380', '384', '388', '392', + '398', '400', '404', '408', '410', '414', '417', '418', '422', '426', '428', '430', '434', '438', '440', '442', + '446', '450', '454', '458', '462', '466', '470', '474', '478', '480', '484', '492', '496', '498', '499', '500', + '504', '508', '512', '516', '520', '524', '528', '531', '533', '534', '535', '540', '548', '554', '558', '562', + '566', '570', '574', '578', '580', '581', '583', '584', '585', '586', '591', '598', '600', '604', '608', '612', + '616', '620', '624', '626', '630', '634', '638', '642', '643', '646', '652', '654', '659', '660', '662', '663', + '666', '670', '674', '678', '682', '686', '688', '690', '694', '702', '703', '704', '705', '706', '710', '716', + '724', '728', '729', '732', '740', '744', '748', '752', '756', '760', '762', '764', '768', '772', '776', '780', + '784', '788', '792', '795', '796', '798', '800', '804', '807', '818', '826', '831', '832', '833', '834', '840', + '850', '854', '858', '860', '862', '876', '882', '887', '894', +]); + +export default function isISO31661Numeric(str) { + assertString(str); + return validISO31661NumericCountriesCodes.has(str); +} diff --git a/src/lib/isISO4217.js b/src/lib/isISO4217.js index 0738614c9..bbca596a9 100644 --- a/src/lib/isISO4217.js +++ b/src/lib/isISO4217.js @@ -4,12 +4,12 @@ import assertString from './util/assertString'; const validISO4217CurrencyCodes = new Set([ 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', 'BAM', 'BBD', 'BDT', 'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB', 'BOV', 'BRL', 'BSD', 'BTN', 'BWP', 'BYN', 'BZD', - 'CAD', 'CDF', 'CHE', 'CHF', 'CHW', 'CLF', 'CLP', 'CNY', 'COP', 'COU', 'CRC', 'CUC', 'CUP', 'CVE', 'CZK', + 'CAD', 'CDF', 'CHE', 'CHF', 'CHW', 'CLF', 'CLP', 'CNY', 'COP', 'COU', 'CRC', 'CUP', 'CVE', 'CZK', 'DJF', 'DKK', 'DOP', 'DZD', 'EGP', 'ERN', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GHS', 'GIP', 'GMD', 'GNF', 'GTQ', 'GYD', - 'HKD', 'HNL', 'HRK', 'HTG', 'HUF', + 'HKD', 'HNL', 'HTG', 'HUF', 'IDR', 'ILS', 'INR', 'IQD', 'IRR', 'ISK', 'JMD', 'JOD', 'JPY', 'KES', 'KGS', 'KHR', 'KMF', 'KPW', 'KRW', 'KWD', 'KYD', 'KZT', @@ -20,10 +20,10 @@ const validISO4217CurrencyCodes = new Set([ 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', 'PYG', 'QAR', 'RON', 'RSD', 'RUB', 'RWF', - 'SAR', 'SBD', 'SCR', 'SDG', 'SEK', 'SGD', 'SHP', 'SLL', 'SOS', 'SRD', 'SSP', 'STN', 'SVC', 'SYP', 'SZL', + 'SAR', 'SBD', 'SCR', 'SDG', 'SEK', 'SGD', 'SHP', 'SLE', 'SLL', 'SOS', 'SRD', 'SSP', 'STN', 'SVC', 'SYP', 'SZL', 'THB', 'TJS', 'TMT', 'TND', 'TOP', 'TRY', 'TTD', 'TWD', 'TZS', 'UAH', 'UGX', 'USD', 'USN', 'UYI', 'UYU', 'UYW', 'UZS', - 'VES', 'VND', 'VUV', + 'VED', 'VES', 'VND', 'VUV', 'WST', 'XAF', 'XAG', 'XAU', 'XBA', 'XBB', 'XBC', 'XBD', 'XCD', 'XDR', 'XOF', 'XPD', 'XPF', 'XPT', 'XSU', 'XTS', 'XUA', 'XXX', 'YER', diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js new file mode 100644 index 000000000..2c28c1123 --- /dev/null +++ b/src/lib/isISO6346.js @@ -0,0 +1,38 @@ +import assertString from './util/assertString'; + +// https://en.wikipedia.org/wiki/ISO_6346 +// according to ISO6346 standard, checksum digit is mandatory for freight container but recommended +// for other container types (J and Z) +const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/; +const isDigit = /^[0-9]$/; + +export function isISO6346(str) { + assertString(str); + + str = str.toUpperCase(); + + if (!isISO6346Str.test(str)) return false; + + if (str.length === 11) { + let sum = 0; + for (let i = 0; i < str.length - 1; i++) { + if (!isDigit.test(str[i])) { + let convertedCode; + const letterCode = str.charCodeAt(i) - 55; + if (letterCode < 11) convertedCode = letterCode; + else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); + else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); + else convertedCode = 34 + (letterCode % 31); + sum += convertedCode * (2 ** i); + } else sum += str[i] * (2 ** i); + } + + let checkSumDigit = sum % 11; + if (checkSumDigit === 10) checkSumDigit = 0; + return Number(str[str.length - 1]) === checkSumDigit; + } + + return true; +} + +export const isFreightContainerID = isISO6346; diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index d34ddae26..fc37d4a29 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import includes from './util/includesArray'; import isInt from './isInt'; const validators = { @@ -277,7 +278,7 @@ const validators = { const parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; - const checkAddressCode = addressCode => provincesAndCities.includes(addressCode); + const checkAddressCode = addressCode => includes(provincesAndCities, addressCode); const checkBirthDayCode = (birDayCode) => { const yyyy = parseInt(birDayCode.substring(0, 4), 10); @@ -342,6 +343,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, @@ -390,6 +422,16 @@ const validators = { return sum + (Number(number) * (9 - index)); }, 0); }, + PK: (str) => { + // Pakistani National Identity Number CNIC is 13 digits + const CNIC = /^[1-7][0-9]{4}-[0-9]{7}-[1-9]$/; + + // sanitize user input + const sanitized = str.trim(); + + // validate the data structure + return CNIC.test(sanitized); + }, }; export default function isIdentityCard(str, locale) { diff --git a/src/lib/isInt.js b/src/lib/isInt.js index 8047a6969..d5616089a 100644 --- a/src/lib/isInt.js +++ b/src/lib/isInt.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import isNullOrUndefined from './util/nullUndefinedCheck'; const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/; const intLeadingZeroes = /^[-+]?[0-9]+$/; @@ -9,16 +10,13 @@ export default function isInt(str, options) { // Get the regex to use for testing, based on whether // leading zeroes are allowed or not. - let regex = ( - options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? - int : intLeadingZeroes - ); + const regex = options.allow_leading_zeroes === false ? int : intLeadingZeroes; // Check min/max/lt/gt - let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min); - let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max); - let ltCheckPassed = (!options.hasOwnProperty('lt') || str < options.lt); - let gtCheckPassed = (!options.hasOwnProperty('gt') || str > options.gt); + let minCheckPassed = (!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || str >= options.min); + let maxCheckPassed = (!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || str <= options.max); + let ltCheckPassed = (!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || str < options.lt); + let gtCheckPassed = (!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || str > options.gt); return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed; } diff --git a/src/lib/isJSON.js b/src/lib/isJSON.js index d3731e337..5c51dd31c 100644 --- a/src/lib/isJSON.js +++ b/src/lib/isJSON.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import includes from './util/includesArray'; import merge from './util/merge'; const default_json_options = { @@ -15,7 +16,7 @@ export default function isJSON(str, options) { } const obj = JSON.parse(str); - return primitives.includes(obj) || (!!obj && typeof obj === 'object'); + return includes(primitives, obj) || (!!obj && typeof obj === 'object'); } catch (e) { /* ignore */ } return false; } diff --git a/src/lib/isJWT.js b/src/lib/isJWT.js index 1a8896f98..1d0ade5ee 100644 --- a/src/lib/isJWT.js +++ b/src/lib/isJWT.js @@ -7,7 +7,7 @@ export default function isJWT(str) { const dotSplit = str.split('.'); const len = dotSplit.length; - if (len > 3 || len < 2) { + if (len !== 3) { return false; } diff --git a/src/lib/isLatLong.js b/src/lib/isLatLong.js index 5c53c23aa..c4e622753 100644 --- a/src/lib/isLatLong.js +++ b/src/lib/isLatLong.js @@ -1,5 +1,6 @@ import assertString from './util/assertString'; import merge from './util/merge'; +import includes from './util/includesString'; const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; @@ -15,7 +16,7 @@ export default function isLatLong(str, options) { assertString(str); options = merge(options, defaultLatLongOptions); - if (!str.includes(',')) return false; + if (!includes(str, ',')) return false; const pair = str.split(','); if ((pair[0].startsWith('(') && !pair[1].endsWith(')')) || (pair[1].endsWith(')') && !pair[0].startsWith('('))) return false; diff --git a/src/lib/isLength.js b/src/lib/isLength.js index 4ef8b83eb..4d5d52546 100644 --- a/src/lib/isLength.js +++ b/src/lib/isLength.js @@ -5,6 +5,7 @@ export default function isLength(str, options) { assertString(str); let min; let max; + if (typeof (options) === 'object') { min = options.min || 0; max = options.max; @@ -12,8 +13,15 @@ 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 - presentationSequences.length - surrogatePairs.length; - return len >= min && (typeof max === 'undefined' || len <= max); + const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max); + + if (isInsideRange && Array.isArray(options?.discreteLengths)) { + return options.discreteLengths.some(discreteLen => discreteLen === len); + } + + return isInsideRange; } diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index e370e5029..54d80635f 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -2,36 +2,26 @@ 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 => /^((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), + 'en-SG': str => /^[A-Z]{3}[ -]?[\d]{4}[ -]?[A-Z]{1}$/.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), + /^(([A-Z]{2}[ -·]?[0-9]{2}[ -·]?[0-9]{2})|([0-9]{2}[ -·]?[A-Z]{2}[ -·]?[0-9]{2})|([0-9]{2}[ -·]?[0-9]{2}[ -·]?[A-Z]{2})|([A-Z]{2}[ -·]?[0-9]{2}[ -·]?[A-Z]{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()), - 'en-IN': str => /^[A-Z]{2}[ -]?[0-9]{1,2}(?:[ -]?[A-Z])(?:[ -]?[A-Z]*)?[ -]?[0-9]{4}$/.test(str), + 'en-PK': str => /(^[A-Z]{2}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]{3}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]{4}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]((\s|-){0,1})[0-9]{4}((\s|-)[0-9]{2}){0,1}$)/.test(str.trim()), }; -validators['hi-IN'] = validators['en-IN']; -validators['gu-IN'] = validators['en-IN']; -validators['as-IN'] = validators['en-IN']; -validators['bn-IN'] = validators['en-IN']; -validators['kn-IN'] = validators['en-IN']; -validators['ml-IN'] = validators['en-IN']; -validators['mr-IN'] = validators['en-IN']; -validators['or-IN'] = validators['en-IN']; -validators['pa-IN'] = validators['en-IN']; -validators['sa-IN'] = validators['en-IN']; -validators['ta-IN'] = validators['en-IN']; -validators['te-IN'] = validators['en-IN']; -validators['kok-IN'] = validators['en-IN']; - export default function isLicensePlate(str, locale) { assertString(str); if (locale in validators) { diff --git a/src/lib/isLocale.js b/src/lib/isLocale.js index cacac8aec..ec84c8fce 100644 --- a/src/lib/isLocale.js +++ b/src/lib/isLocale.js @@ -1,11 +1,111 @@ import assertString from './util/assertString'; -const localeReg = /^[A-Za-z]{2,4}([_-]([A-Za-z]{4}|[\d]{3}))?([_-]([A-Za-z]{2}|[\d]{3}))?$/; +/* + = 3ALPHA ; selected ISO 639 codes + *2("-" 3ALPHA) ; permanently reserved + */ +const extlang = '([A-Za-z]{3}(-[A-Za-z]{3}){0,2})'; + +/* + = 2*3ALPHA ; shortest ISO 639 code + ["-" extlang] ; sometimes followed by + ; extended language subtags + / 4ALPHA ; or reserved for future use + / 5*8ALPHA ; or registered language subtag + */ +const language = `(([a-zA-Z]{2,3}(-${extlang})?)|([a-zA-Z]{5,8}))`; + +/* + = 4ALPHA ; ISO 15924 code + */ +const script = '([A-Za-z]{4})'; + +/* + = 2ALPHA ; ISO 3166-1 code + / 3DIGIT ; UN M.49 code + */ +const region = '([A-Za-z]{2}|\\d{3})'; + +/* + = 5*8alphanum ; registered variants + / (DIGIT 3alphanum) + */ +const variant = '([A-Za-z0-9]{5,8}|(\\d[A-Z-a-z0-9]{3}))'; + +/* + = DIGIT ; 0 - 9 + / %x41-57 ; A - W + / %x59-5A ; Y - Z + / %x61-77 ; a - w + / %x79-7A ; y - z + */ +const singleton = '(\\d|[A-W]|[Y-Z]|[a-w]|[y-z])'; + +/* + = singleton 1*("-" (2*8alphanum)) + ; Single alphanumerics + ; "x" reserved for private use + */ +const extension = `(${singleton}(-[A-Za-z0-9]{2,8})+)`; + +/* + = "x" 1*("-" (1*8alphanum)) + */ +const privateuse = '(x(-[A-Za-z0-9]{1,8})+)'; + +// irregular tags do not match the 'langtag' production and would not +// otherwise be considered 'well-formed'. These tags are all valid, but +// most are deprecated in favor of more modern subtags or subtag combination + +const irregular = '((en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|' + + '(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|' + + '(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE))'; + +// regular tags match the 'langtag' production, but their subtags are not +// extended language or variant subtags: their meaning is defined by +// their registration and all of these are deprecated in favor of a more +// modern subtag or sequence of subtags + +const regular = '((art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|' + + '(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang))'; + +/* + = irregular ; non-redundant tags registered + / regular ; during the RFC 3066 era + + */ +const grandfathered = `(${irregular}|${regular})`; + +/* + RFC 5646 defines delimitation of subtags via a hyphen: + + "Subtag" refers to a specific section of a tag, delimited by a + hyphen, such as the subtags 'zh', 'Hant', and 'CN' in the tag "zh- + Hant-CN". Examples of subtags in this document are enclosed in + single quotes ('Hant') + + However, we need to add "_" to maintain the existing behaviour. + */ +const delimiter = '(-|_)'; + +/* + = language + ["-" script] + ["-" region] + *("-" variant) + *("-" extension) + ["-" privateuse] + */ +const langtag = `${language}(${delimiter}${script})?(${delimiter}${region})?(${delimiter}${variant})*(${delimiter}${extension})*(${delimiter}${privateuse})?`; + +/* + Regex implementation based on BCP RFC 5646 + Tags for Identifying Languages + https://www.rfc-editor.org/rfc/rfc5646.html + */ +const languageTagRegex = new RegExp(`(^${privateuse}$)|(^${grandfathered}$)|(^${langtag}$)`); export default function isLocale(str) { assertString(str); - if (str === 'en_US_POSIX' || str === 'ca_ES_VALENCIA') { - return true; - } - return localeReg.test(str); + return languageTagRegex.test(str); } diff --git a/src/lib/isLuhnValid.js b/src/lib/isLuhnNumber.js similarity index 93% rename from src/lib/isLuhnValid.js rename to src/lib/isLuhnNumber.js index da205271f..95a066115 100644 --- a/src/lib/isLuhnValid.js +++ b/src/lib/isLuhnNumber.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -export default function isLuhnValid(str) { +export default function isLuhnNumber(str) { assertString(str); const sanitized = str.replace(/[- ]+/g, ''); let sum = 0; diff --git a/src/lib/isMailtoURI.js b/src/lib/isMailtoURI.js new file mode 100644 index 000000000..67748a553 --- /dev/null +++ b/src/lib/isMailtoURI.js @@ -0,0 +1,67 @@ +import trim from './trim'; +import isEmail from './isEmail'; +import assertString from './util/assertString'; + +function parseMailtoQueryString(queryString) { + const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']), + query = { cc: '', bcc: '' }; + let isParseFailed = false; + + const queryParams = queryString.split('&'); + + if (queryParams.length > 4) { + return false; + } + + for (const q of queryParams) { + const [key, value] = q.split('='); + + // checked for invalid and duplicated query params + if (key && !allowedParams.has(key)) { + isParseFailed = true; + break; + } + + if (value && (key === 'cc' || key === 'bcc')) { + query[key] = value; + } + + if (key) { + allowedParams.delete(key); + } + } + + return isParseFailed ? false : query; +} + +export default function isMailtoURI(url, options) { + assertString(url); + + if (url.indexOf('mailto:') !== 0) { + return false; + } + + const [to, queryString = ''] = url.replace('mailto:', '').split('?'); + + if (!to && !queryString) { + return true; + } + + const query = parseMailtoQueryString(queryString); + + if (!query) { + return false; + } + + return `${to},${query.cc},${query.bcc}` + .split(',') + .every((email) => { + email = trim(email, ' '); + + if (email) { + return isEmail(email, options); + } + + return true; + }); +} diff --git a/src/lib/isMimeType.js b/src/lib/isMimeType.js index 1dfa77767..820fa4dc2 100644 --- a/src/lib/isMimeType.js +++ b/src/lib/isMimeType.js @@ -4,18 +4,18 @@ import assertString from './util/assertString'; Checks if the provided string matches to a correct Media type format (MIME type) This function only checks is the string format follows the - etablished rules by the according RFC specifications. + established rules by the according RFC specifications. This function supports 'charset' in textual media types (https://tools.ietf.org/html/rfc6657). This function does not check against all the media types listed by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml) because of lightness purposes : it would require to include - all these MIME types in this librairy, which would weigh it + all these MIME types in this library, which would weigh it significantly. This kind of effort maybe is not worth for the use that - this function has in this entire librairy. + this function has in this entire library. - More informations in the RFC specifications : + More information in the RFC specifications : - https://tools.ietf.org/html/rfc2045 - https://tools.ietf.org/html/rfc2046 - https://tools.ietf.org/html/rfc7231#section-3.1.1.1 @@ -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 3c38d2519..b00391ea6 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -2,7 +2,7 @@ import assertString from './util/assertString'; /* eslint-disable max-len */ const phones = { - 'am-AM': /^(\+?374|0)((10|[9|7][0-9])\d{6}$|[2-4]\d{7}$)/, + 'am-AM': /^(\+?374|0)(33|4[134]|55|77|88|9[13-689])\d{6}$/, 'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/, 'ar-BH': /^(\+?973)?(3|6)\d{7}$/, 'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/, @@ -10,15 +10,17 @@ 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}$/, + 'ar-OM': /^((\+|00)968)?([79][1-9])\d{6}$/, 'ar-PS': /^(\+?970|0)5[6|9](\d{7})$/, 'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, + 'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/, 'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, 'ar-TN': /^(\+?216)?[2459]\d{7}$/, 'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/, + 'ar-QA': /^(\+?974|0)?([3567]\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}$/, @@ -30,17 +32,17 @@ const phones = { '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})$/, - 'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, + '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|7|6|5|4)\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-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-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-GB': /^(\+?44|0)7[1-9]\d{8}$/, '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}$/, + 'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|53|28|55|59)\d{7}$/, 'en-GY': /^(\+592|0)6\d{6}$/, 'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/, 'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/, @@ -48,10 +50,14 @@ const phones = { 'en-IN': /^(\+?91|0)?[6789]\d{9}$/, 'en-JM': /^(\+?876)?\d{7}$/, 'en-KE': /^(\+?254|0)(7|1)\d{8}$/, + 'fr-CF': /^(\+?236| ?)(70|75|77|72|21|22)\d{6}$/, + 'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, 'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, + 'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, '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-MW': /^(\+?265|0)(((77|88|31|99|98|21)\d{7})|(((111)|1)\d{6})|(32000\d{4}))$/, 'en-NA': /^(\+?264|0)(6|8)\d{7}$/, 'en-NG': /^(\+?234|0)?[789]\d{9}$/, 'en-NZ': /^(\+?64|0)[28]\d{7,9}$/, @@ -65,7 +71,7 @@ const phones = { 'en-UG': /^(\+?256|0)?[7]\d{8}$/, 'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/, 'en-ZA': /^(\+?27|0)\d{9}$/, - 'en-ZM': /^(\+?26)?09[567]\d{7}$/, + 'en-ZM': /^(\+?26)?0[79][567]\d{7}$/, 'en-ZW': /^(\+263)[0-9]{9}$/, 'en-BW': /^(\+?267)?(7[1-8]{1})\d{6}$/, 'es-AR': /^\+?549(11|[2368]\d)\d{8}$/, @@ -73,11 +79,12 @@ const phones = { 'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, 'es-CR': /^(\+506)?[2-8]\d{7}$/, - 'es-CU': /^(\+53|0053)?5\d{7}/, + 'es-CU': /^(\+53|0053)?5\d{7}$/, 'es-DO': /^(\+?1)?8[024]9\d{7}$/, - 'es-HN': /^(\+?504)?[9|8]\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-GT': /^(\+?502)?[2|6|7]\d{7}$/, 'es-PE': /^(\+?51)?9\d{8}$/, 'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, 'es-NI': /^(\+?505)\d{7,8}$/, @@ -93,6 +100,7 @@ const phones = { '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}$/, @@ -100,6 +108,7 @@ const phones = { 'fr-MQ': /^(\+?596|0|00596)[67]\d{8}$/, 'fr-PF': /^(\+?689)?8[789]\d{6}$/, 'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/, + 'fr-WF': /^(\+681)?\d{6}$/, 'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/, 'hu-HU': /^(\+?36|06)(20|30|31|50|70)\d{7}$/, 'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/, @@ -111,13 +120,13 @@ const phones = { '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}$/, + 'ky-KG': /^(\+996\s?)?(22[0-9]|50[0-9]|55[0-9]|70[0-9]|75[0-9]|77[0-9]|880|990|995|996|997|998)\s?\d{3}\s?\d{3}$/, 'lt-LT': /^(\+370|8)\d{8}$/, 'lv-LV': /^(\+?371)2\d{7}$/, 'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, 'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, 'my-MM': /^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, - 'ms-MY': /^(\+?6?01){1}(([0145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/, + '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}$/, @@ -125,23 +134,25 @@ const phones = { 'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/, 'nl-AW': /^(\+)?297(56|59|64|73|74|99)\d{5}$/, 'nn-NO': /^(\+?47)?[49]\d{7}$/, - 'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/, + 'pl-PL': /^(\+?48)? ?([5-8]\d|45) ?\d{3} ?\d{2} ?\d{2}$/, 'pt-BR': /^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, 'pt-PT': /^(\+?351)?9[1236]\d{7}$/, - 'pt-AO': /^(\+244)\d{9}$/, - 'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/, + 'pt-AO': /^(\+?244)?9\d{8}$/, + '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})$/, 'sk-SK': /^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/, - 'sq-AL': /^(\+355|0)6[789]\d{6}$/, + 'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, + 'sq-AL': /^(\+355|0)6[2-9]\d{7}$/, 'sr-RS': /^(\+3816|06)[- \d]{5,9}$/, 'sv-SE': /^(\+?46|0)[\s\-]?7[\s\-]?[02369]([\s\-]?\d){7}$/, 'tg-TJ': /^(\+?992)?[5][5]\d{7}$/, 'th-TH': /^(\+66|66|0)\d{9}$/, 'tr-TR': /^(\+?90|0)?5\d{9}$/, 'tk-TM': /^(\+993|993|8)\d{8}$/, - 'uk-UA': /^(\+?38|8)?0\d{9}$/, + 'uk-UA': /^(\+?38)?0(50|6[36-8]|7[357]|9[1-9])\d{7}$/, 'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/, 'vi-VN': /^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/, 'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/, @@ -150,6 +161,7 @@ const phones = { '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})$/, + 'mk-MK': /^(\+?389|0)?((?:2[2-9]\d{6}|(?:3[1-4]|4[2-8])\d{6}|500\d{5}|5[2-9]\d{6}|7[0-9][2-9]\d{5}|8[1-9]\d{6}|800\d{5}|8009\d{4}))$/, }; /* eslint-enable max-len */ diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js index c434a7078..c3b842e59 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -11,11 +11,12 @@ const passportRegexByCountryCode = { AR: /^[A-Z]{3}\d{6}$/, // ARGENTINA AT: /^[A-Z]\d{7}$/, // AUSTRIA AU: /^[A-Z]\d{7}$/, // AUSTRALIA + AZ: /^[A-Z]{1}\d{8}$/, // AZERBAIJAN BE: /^[A-Z]{2}\d{6}$/, // BELGIUM BG: /^\d{9}$/, // BULGARIA BR: /^[A-Z]{2}\d{6}$/, // BRAZIL BY: /^[A-Z]{2}\d{7}$/, // BELARUS - CA: /^[A-Z]{2}\d{6}$/, // CANADA + CA: /^[A-Z]{2}\d{6}$|^[A-Z]\d{6}[A-Z]{2}$/, // CANADA CH: /^[A-Z]\d{7}$/, // SWITZERLAND CN: /^G\d{8}$|^E(?![IO])[A-Z0-9]\d{7}$/, // CHINA [G=Ordinary, E=Electronic] followed by 8-digits, or E followed by any UPPERCASE letter (except I and O) followed by 7 digits CY: /^[A-Z](\d{6}|\d{8})$/, // CYPRUS @@ -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 @@ -48,18 +52,25 @@ const passportRegexByCountryCode = { 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 + US: /^\d{9}$|^[A-Z]\d{8}$/, // UNITED STATES + ZA: /^[TAMD]\d{8}$/, // SOUTH AFRICA }; +export const locales = Object.keys(passportRegexByCountryCode); + /** * Check if str is a valid passport number * relative to provided ISO Country Code. diff --git a/src/lib/isPort.js b/src/lib/isPort.js index 0b316b78c..0a9ddce1d 100644 --- a/src/lib/isPort.js +++ b/src/lib/isPort.js @@ -1,5 +1,5 @@ import isInt from './isInt'; export default function isPort(str) { - return isInt(str, { min: 0, max: 65535 }); + return isInt(str, { allow_leading_zeroes: false, min: 0, max: 65535 }); } diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index cf5b50d25..da3fbdcf8 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -12,13 +12,15 @@ const patterns = { AU: fourDigit, AZ: /^AZ\d{4}$/, BA: /^([7-8]\d{4}$)/, + BD: /^([1-8][0-9]{3}|9[0-4][0-9]{2})$/, BE: fourDigit, BG: fourDigit, - BR: /^\d{5}-\d{3}$/, - BY: /2[1-4]{1}\d{4}$/, + BR: /^\d{5}-?\d{3}$/, + 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}$/, + CO: /^(05|08|11|13|15|17|18|19|20|23|25|27|41|44|47|50|52|54|63|66|68|70|73|76|81|85|86|88|91|94|95|97|99)(\d{4})$/, CZ: /^\d{3}\s?\d{2}$/, DE: fiveDigit, DK: fourDigit, @@ -27,7 +29,7 @@ const patterns = { EE: fiveDigit, ES: /^(5[0-2]{1}|[0-4]{1}\d{1})\d{3}$/, FI: fiveDigit, - FR: /^\d{2}\s?\d{3}$/, + FR: /^(?:(?:0[1-9]|[1-8]\d|9[0-5])\d{3}|97[1-46]\d{2})$/, GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i, GR: /^\d{3}\s?\d{2}$/, HR: /^([1-5]\d{4}$)/, @@ -37,7 +39,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}$/, @@ -52,10 +54,12 @@ const patterns = { MX: fiveDigit, MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, MY: fiveDigit, - NL: /^\d{4}\s?[a-z]{2}$/i, + NL: /^[1-9]\d{3}\s?(?!sa|sd|ss)[a-z]{2}$/i, NO: fourDigit, NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, + // https://www.pakpost.gov.pk/postcodes.php + PK: fiveDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, PT: /^\d{4}\-\d{3}?$/, @@ -68,7 +72,7 @@ const patterns = { SK: /^\d{3}\s?\d{2}$/, TH: fiveDigit, TN: fourDigit, - TW: /^\d{3}(\d{2})?$/, + TW: /^\d{3}(\d{2,3})?$/, UA: fiveDigit, US: /^\d{5}(-\d{4})?$/, ZA: fourDigit, diff --git a/src/lib/isRgbColor.js b/src/lib/isRgbColor.js index e6508e29a..6e2866243 100644 --- a/src/lib/isRgbColor.js +++ b/src/lib/isRgbColor.js @@ -1,12 +1,35 @@ +/* eslint-disable prefer-rest-params */ 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 rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d\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\d?|1(\.0)?|0(\.0)?)\)$/; +const startsWithRgb = /^rgba?/; -export default function isRgbColor(str, includePercentValues = true) { +export default function isRgbColor(str, options) { assertString(str); + // default options to true for percent and false for spaces + let allowSpaces = false; + let includePercentValues = true; + if (typeof options !== 'object') { + if (arguments.length >= 2) { + includePercentValues = arguments[1]; + } + } else { + allowSpaces = options.allowSpaces !== undefined ? options.allowSpaces : allowSpaces; + includePercentValues = options.includePercentValues !== undefined ? + options.includePercentValues : includePercentValues; + } + + if (allowSpaces) { + // make sure it starts with continous rgba? without spaces before stripping + if (!startsWithRgb.test(str)) { + return false; + } + // strip all whitespace + str = str.replace(/\s/g, ''); + } if (!includePercentValues) { return rgbColor.test(str) || rgbaColor.test(str); diff --git a/src/lib/isStrongPassword.js b/src/lib/isStrongPassword.js index 28bb0637f..8fe9223b7 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 933783f44..5e5f8cb5b 100644 --- a/src/lib/isTaxID.js +++ b/src/lib/isTaxID.js @@ -174,24 +174,24 @@ function deDeCheck(tin) { const digits = tin.split('').map(a => parseInt(a, 10)); // Fill array with strings of number positions - let occurences = []; + let occurrences = []; for (let i = 0; i < digits.length - 1; i++) { - occurences.push(''); + occurrences.push(''); for (let j = 0; j < digits.length - 1; j++) { if (digits[i] === digits[j]) { - occurences[i] += j; + occurrences[i] += j; } } } - // Remove digits with one occurence and test for only one duplicate/triplicate - occurences = occurences.filter(a => a.length > 1); - if (occurences.length !== 2 && occurences.length !== 3) { return false; } + // Remove digits with one occurrence and test for only one duplicate/triplicate + occurrences = occurrences.filter(a => a.length > 1); + if (occurrences.length !== 2 && occurrences.length !== 3) { return false; } // In case of triplicate value only two digits are allowed next to each other - if (occurences[0].length === 3) { - const trip_locations = occurences[0].split('').map(a => parseInt(a, 10)); - let recurrent = 0; // Amount of neighbour occurences + if (occurrences[0].length === 3) { + const trip_locations = occurrences[0].split('').map(a => parseInt(a, 10)); + let recurrent = 0; // Amount of neighbor occurrences for (let i = 0; i < trip_locations.length - 1; i++) { if (trip_locations[i] + 1 === trip_locations[i + 1]) { recurrent += 1; @@ -376,6 +376,30 @@ function enUsCheck(tin) { return enUsGetPrefixes().indexOf(tin.slice(0, 2)) !== -1; } +/* + * es-AR validation function + * Clave Única de Identificación Tributaria (CUIT/CUIL) + * Sourced from: + * - https://servicioscf.afip.gob.ar/publico/abc/ABCpaso2.aspx?id_nivel1=3036&id_nivel2=3040&p=Conceptos%20b%C3%A1sicos + * - https://es.wikipedia.org/wiki/Clave_%C3%9Anica_de_Identificaci%C3%B3n_Tributaria + */ + +function esArCheck(tin) { + let accum = 0; + let digits = tin.split(''); + let digit = parseInt(digits.pop(), 10); + for (let i = 0; i < digits.length; i++) { + accum += digits[9 - i] * (2 + (i % 6)); + } + let verif = 11 - (accum % 11); + if (verif === 11) { + verif = 0; + } else if (verif === 10) { + verif = 9; + } + return digit === verif; +} + /* * es-ES validation function * (Documento Nacional de Identidad (DNI) @@ -597,10 +621,10 @@ function huHuCheck(tin) { * and X characters after vowels may only be followed by other X characters. */ function itItNameCheck(name) { - // true at the first occurence of a vowel + // true at the first occurrence of a vowel let vowelflag = false; - // true at the first occurence of an X AFTER vowel + // true at the first occurrence of an X AFTER vowel // (to properly handle last names with X as consonant) let xflag = false; @@ -866,7 +890,7 @@ function plPlCheck(tin) { const date = `${full_year}/${month}/${tin.slice(4, 6)}`; if (!isDate(date, 'YYYY/MM/DD')) { return false; } - // Calculate last digit by mulitplying with odd one-digit numbers except 5 + // Calculate last digit by multiplying with odd one-digit numbers except 5 let checksum = 0; let multiplier = 1; for (let i = 0; i < tin.length - 1; i++) { @@ -1117,6 +1141,21 @@ function svSeCheck(tin) { return algorithms.luhnCheck(tin.replace(/\W/, '')); } +/** + * uk-UA validation function + * Verify TIN validity by calculating check (last) digit (variant of MOD 11) + */ +function ukUaCheck(tin) { + // Calculate check digit + const digits = tin.split('').map(a => parseInt(a, 10)); + const multipliers = [-1, 5, 7, 9, 4, 6, 10, 5, 7]; + let checksum = 0; + for (let i = 0; i < multipliers.length; i++) { + checksum += digits[i] * multipliers[i]; + } + return checksum % 11 === 10 ? digits[9] === 0 : digits[9] === checksum % 11; +} + // Locale lookup objects /* @@ -1137,6 +1176,7 @@ const taxIdFormat = { '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}$/, + 'es-AR': /(20|23|24|27|30|33|34)[0-9]{8}[0-9]/, 'es-ES': /^(\d{0,8}|[XYZKLM]\d{7})[A-HJ-NP-TV-Z]$/i, 'et-EE': /^[1-6]\d{6}(00[1-9]|0[1-9][0-9]|[1-6][0-9]{2}|70[0-9]|710)\d$/, 'fi-FI': /^\d{6}[-+A]\d{3}[0-9A-FHJ-NPR-Y]$/i, @@ -1156,6 +1196,7 @@ 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})$/, + 'uk-UA': /^\d{10}$/, }; // taxIdFormat locale aliases taxIdFormat['lb-LU'] = taxIdFormat['fr-LU']; @@ -1175,6 +1216,7 @@ const taxIdCheck = { 'en-CA': isCanadianSIN, 'en-IE': enIeCheck, 'en-US': enUsCheck, + 'es-AR': esArCheck, 'es-ES': esEsCheck, 'et-EE': etEeCheck, 'fi-FI': fiFiCheck, @@ -1194,6 +1236,7 @@ const taxIdCheck = { 'sk-SK': skSkCheck, 'sl-SI': slSiCheck, 'sv-SE': svSeCheck, + 'uk-UA': ukUaCheck, }; // taxIdCheck locale aliases taxIdCheck['lb-LU'] = taxIdCheck['fr-LU']; diff --git a/src/lib/isTime.js b/src/lib/isTime.js new file mode 100644 index 000000000..3169864c2 --- /dev/null +++ b/src/lib/isTime.js @@ -0,0 +1,25 @@ +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])$/, + withOptionalSeconds: /^([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$/, + withOptionalSeconds: /^(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/isULID.js b/src/lib/isULID.js new file mode 100644 index 000000000..2ace6f471 --- /dev/null +++ b/src/lib/isULID.js @@ -0,0 +1,6 @@ +import assertString from './util/assertString'; + +export default function isULID(str) { + assertString(str); + return /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i.test(str); +} diff --git a/src/lib/isURL.js b/src/lib/isURL.js index 3d2b1df3e..8ae971ed6 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -1,4 +1,6 @@ import assertString from './util/assertString'; +import checkHost from './util/checkHost'; +import includes from './util/includesString'; import isFQDN from './isFQDN'; import isIP from './isIP'; @@ -7,13 +9,28 @@ import merge from './util/merge'; /* options for isURL method -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 -validate_length - if set as false isURL will skip string length validation (IE maximum is 2083) +protocols - valid protocols can be modified with this option. +require_tld - If set to false isURL will not check if the URL's host includes a top-level domain. +require_protocol - if set to true isURL will return false if protocol is not present in the URL. +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. +require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option. +allow_underscores - if set to true, the validator will allow underscores in the URL. +host_whitelist - if set to an array of strings or regexp, and the domain matches none of the strings + defined in it, the validation fails. +host_blacklist - if set to an array of strings or regexp, and the domain matches any of the strings + defined in it, the validation fails. +allow_trailing_dot - if set to true, the validator will allow the domain to end with + a `.` character. +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. +disallow_auth - if set to true, the validator will fail if the URL contains an authentication + component, e.g. `http://username:password@example.com` +validate_length - if set to false isURL will skip string length validation. `max_allowed_length` + will be ignored if this is set as `false`. +max_allowed_length - if set, isURL will not allow URLs longer than the specified value (default is + 2084 that IE maximum URL length). */ @@ -31,24 +48,11 @@ const default_url_options = { allow_fragments: true, allow_query_components: true, validate_length: true, + max_allowed_length: 2084, }; const wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/; -function isRegExp(obj) { - return Object.prototype.toString.call(obj) === '[object RegExp]'; -} - -function checkHost(host, matches) { - for (let i = 0; i < matches.length; i++) { - let match = matches[i]; - if (host === match || (isRegExp(match) && match.test(host))) { - return true; - } - } - return false; -} - export default function isURL(url, options) { assertString(url); if (!url || /[\s<>]/.test(url)) { @@ -59,15 +63,15 @@ export default function isURL(url, options) { } options = merge(options, default_url_options); - if (options.validate_length && url.length >= 2083) { + if (options.validate_length && url.length > options.max_allowed_length) { return false; } - if (!options.allow_fragments && url.includes('#')) { + if (!options.allow_fragments && includes(url, '#')) { return false; } - if (!options.allow_query_components && (url.includes('?') || url.includes('&'))) { + if (!options.allow_query_components && (includes(url, '?') || includes(url, '&'))) { return false; } @@ -79,21 +83,94 @@ export default function isURL(url, options) { split = url.split('?'); url = split.shift(); - split = url.split('://'); - if (split.length > 1) { - protocol = split.shift().toLowerCase(); + // Replaced the 'split("://")' logic with a regex to match the protocol. + // This correctly identifies schemes like `javascript:` which don't use `//`. + // However, we need to be careful not to confuse authentication credentials (user:password@host) + // with protocols. A colon before an @ symbol might be part of auth, not a protocol separator. + const protocol_match = url.match(/^([a-z][a-z0-9+\-.]*):/i); + let had_explicit_protocol = false; + + const cleanUpProtocol = (potential_protocol) => { + had_explicit_protocol = true; + protocol = potential_protocol.toLowerCase(); + if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) { + // The identified protocol is not in the allowed list. return false; } + + // Remove the protocol from the URL string. + return url.substring(protocol_match[0].length); + }; + + if (protocol_match) { + const potential_protocol = protocol_match[1]; + const after_colon = url.substring(protocol_match[0].length); + + // Check if what follows looks like authentication credentials (user:password@host) + // rather than a protocol. This happens when: + // 1. There's no `//` after the colon (protocols like `http://` have this) + // 2. There's an `@` symbol before any `/` + // 3. The part before `@` contains only valid auth characters (alphanumeric, -, _, ., %, :) + const starts_with_slashes = after_colon.slice(0, 2) === '//'; + + if (!starts_with_slashes) { + const first_slash_position = after_colon.indexOf('/'); + const before_slash = first_slash_position === -1 + ? after_colon + : after_colon.substring(0, first_slash_position); + const at_position = before_slash.indexOf('@'); + + if (at_position !== -1) { + const before_at = before_slash.substring(0, at_position); + const valid_auth_regex = /^[a-zA-Z0-9\-_.%:]*$/; + const is_valid_auth = valid_auth_regex.test(before_at); + + if (is_valid_auth) { + // This looks like authentication (e.g., user:password@host), not a protocol + if (options.require_protocol) { + return false; + } + + // Don't consume the colon; let the auth parsing handle it later + } else { + // This looks like a malicious protocol (e.g., javascript:alert();@host) + url = cleanUpProtocol(potential_protocol); + + if (url === false) { + return false; + } + } + } else { + // No @ symbol, this is definitely a protocol + url = cleanUpProtocol(potential_protocol); + + if (url === false) { + return false; + } + } + } else { + // Starts with '//', this is definitely a protocol like http:// + url = cleanUpProtocol(potential_protocol); + + if (url === false) { + return false; + } + } } else if (options.require_protocol) { return false; - } else if (url.slice(0, 2) === '//') { - if (!options.allow_protocol_relative_urls) { + } + + // Handle leading '//' only as protocol-relative when there was NO explicit protocol. + // If there was an explicit protocol, '//' is the normal separator + // and should be stripped unconditionally. + if (url.slice(0, 2) === '//') { + if (!had_explicit_protocol && !options.allow_protocol_relative_urls) { return false; } - split[0] = url.slice(2); + + url = url.slice(2); } - url = split.join('://'); if (url === '') { return false; diff --git a/src/lib/isUUID.js b/src/lib/isUUID.js index c026ca78c..9d6040f04 100644 --- a/src/lib/isUUID.js +++ b/src/lib/isUUID.js @@ -1,16 +1,29 @@ import assertString from './util/assertString'; const uuid = { - 1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i, - 2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i, - 3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i, + 1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, + 2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, + 3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, 4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, 5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, - all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i, + 6: /^[0-9A-F]{8}-[0-9A-F]{4}-6[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, + 7: /^[0-9A-F]{8}-[0-9A-F]{4}-7[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, + 8: /^[0-9A-F]{8}-[0-9A-F]{4}-8[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, + + nil: /^00000000-0000-0000-0000-000000000000$/i, + max: /^ffffffff-ffff-ffff-ffff-ffffffffffff$/i, + loose: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i, + + // From https://github.com/uuidjs/uuid/blob/main/src/regex.js + all: /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/i, }; export default function isUUID(str, version) { assertString(str); - const pattern = uuid[![undefined, null].includes(version) ? version : 'all']; - return !!pattern && pattern.test(str); + + if (version === undefined || version === null) { + version = 'all'; + } + + return version in uuid ? uuid[version].test(str) : false; } diff --git a/src/lib/isVAT.js b/src/lib/isVAT.js index 95593569d..1ec2c5991 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -1,6 +1,37 @@ import assertString from './util/assertString'; import * as algorithms from './util/algorithms'; +const AU = (str) => { + const match = str.match(/^(AU)?(\d{11})$/); + if (!match) { + return false; + } + // @see {@link https://abr.business.gov.au/Help/AbnFormat} + const weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]; + str = str.replace(/^AU/, ''); + const ABN = (parseInt(str.slice(0, 1), 10) - 1).toString() + str.slice(1); + let total = 0; + for (let i = 0; i < 11; i++) { + total += weights[i] * ABN.charAt(i); + } + return (total !== 0 && total % 89 === 0); +}; + +const CH = (str) => { + // @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0} + const hasValidCheckNumber = (digits) => { + const lastDigit = digits.pop(); // used as check number + const weights = [5, 4, 3, 2, 7, 6, 5, 4]; + const calculatedCheckNumber = (11 - (digits.reduce((acc, el, idx) => + acc + (el * weights[idx]), 0) % 11)) % 11; + + return lastDigit === calculatedCheckNumber; + }; + + // @see {@link https://www.estv.admin.ch/estv/de/home/mehrwertsteuer/uid/mwst-uid-nummer.html} + return /^(CHE[- ]?)?(\d{9}|(\d{3}\.\d{3}\.\d{3})|(\d{3} \d{3} \d{3})) ?(TVA|MWST|IVA)?$/.test(str) && hasValidCheckNumber((str.match(/\d/g).map(el => +el))); +}; + const PT = (str) => { const match = str.match(/^(PT)?(\d{9})$/); if (!match) { @@ -29,7 +60,7 @@ export const vatMatchers = { 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), + FR: str => /^(FR)([A-Z0-9]{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), @@ -53,14 +84,14 @@ export const vatMatchers = { */ AL: str => /^(AL)?\w{9}[A-Z]$/.test(str), MK: str => /^(MK)?\d{13}$/.test(str), - AU: str => /^(AU)?\d{11}$/.test(str), + AU, 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), + KZ: str => /^(KZ)?\d{12}$/.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), @@ -69,7 +100,7 @@ export const vatMatchers = { SM: str => /^(SM)?\d{5}$/.test(str), SA: str => /^(SA)?\d{15}$/.test(str), RS: str => /^(RS)?\d{9}$/.test(str), - CH: str => /^(CH)?(\d{6}|\d{9}|(\d{3}.\d{3})|(\d{3}.\d{3}.\d{3}))(TVA|MWST|IVA)$/.test(str), + CH, TR: str => /^(TR)?\d{10}$/.test(str), UA: str => /^(UA)?\d{12}$/.test(str), GB: str => /^GB((\d{3} \d{4} ([0-8][0-9]|9[0-6]))|(\d{9} \d{3})|(((GD[0-4])|(HA[5-9]))[0-9]{2}))$/.test(str), diff --git a/src/lib/normalizeEmail.js b/src/lib/normalizeEmail.js index a163bed88..ceb252f34 100644 --- a/src/lib/normalizeEmail.js +++ b/src/lib/normalizeEmail.js @@ -32,6 +32,8 @@ const default_normalize_email_options = { // The following conversions are specific to Yandex // Lowercases the local part of the Yandex address (known to be case-insensitive) yandex_lowercase: true, + // all yandex domains are equal, this explicitly sets the domain to 'yandex.ru' + yandex_convert_yandexru: true, // The following conversions are specific to iCloud // Lowercases the local part of the iCloud address (known to be case-insensitive) @@ -232,7 +234,7 @@ export default function normalizeEmail(email, options) { if (options.all_lowercase || options.yandex_lowercase) { parts[0] = parts[0].toLowerCase(); } - parts[1] = 'yandex.ru'; // all yandex domains are equal, 1st preferred + parts[1] = options.yandex_convert_yandexru ? 'yandex.ru' : parts[1]; } else if (options.all_lowercase) { // Any other address parts[0] = parts[0].toLowerCase(); 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/src/lib/util/assertString.js b/src/lib/util/assertString.js index 948bcba66..3baa4452f 100644 --- a/src/lib/util/assertString.js +++ b/src/lib/util/assertString.js @@ -1,11 +1,4 @@ export default function assertString(input) { - const isString = typeof input === 'string' || input instanceof String; - - if (!isString) { - let invalidType = typeof input; - if (input === null) invalidType = 'null'; - else if (invalidType === 'object') invalidType = input.constructor.name; - - throw new TypeError(`Expected a string but received a ${invalidType}`); - } + if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`); + if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`); } diff --git a/src/lib/util/checkHost.js b/src/lib/util/checkHost.js new file mode 100644 index 000000000..ed1dddefe --- /dev/null +++ b/src/lib/util/checkHost.js @@ -0,0 +1,13 @@ +function isRegExp(obj) { + return Object.prototype.toString.call(obj) === '[object RegExp]'; +} + +export default function checkHost(host, matches) { + for (let i = 0; i < matches.length; i++) { + let match = matches[i]; + if (host === match || (isRegExp(match) && match.test(host))) { + return true; + } + } + return false; +} diff --git a/src/lib/util/includes.js b/src/lib/util/includesArray.js similarity index 100% rename from src/lib/util/includes.js rename to src/lib/util/includesArray.js diff --git a/src/lib/util/includesString.js b/src/lib/util/includesString.js new file mode 100644 index 000000000..41a50bba8 --- /dev/null +++ b/src/lib/util/includesString.js @@ -0,0 +1,3 @@ +const includes = (str, val) => str.indexOf(val) !== -1; + +export default includes; diff --git a/src/lib/util/nullUndefinedCheck.js b/src/lib/util/nullUndefinedCheck.js new file mode 100644 index 000000000..c45bfbe82 --- /dev/null +++ b/src/lib/util/nullUndefinedCheck.js @@ -0,0 +1,3 @@ +export default function isNullOrUndefined(value) { + return value === null || value === undefined; +} 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 89% rename from test/exports.js rename to test/exports.test.js index 0bff532ab..a5f458f05 100644 --- a/test/exports.js +++ b/test/exports.test.js @@ -6,8 +6,14 @@ import { locales as isAlphanumericLocales } from '../src/lib/isAlphanumeric'; import { locales as isMobilePhoneLocales } from '../src/lib/isMobilePhone'; import { locales as isFloatLocales } from '../src/lib/isFloat'; import { locales as ibanCountryCodes } from '../src/lib/isIBAN'; +import { locales as passportNumberLocales } from '../src/lib/isPassportNumber'; describe('Exports', () => { + it('should export isPassportNumbers\'s supported locales', () => { + assert.ok(passportNumberLocales instanceof Array); + assert.ok(validator.passportNumberLocales instanceof Array); + }); + it('should export validators', () => { assert.strictEqual(typeof validator.isEmail, 'function'); assert.strictEqual(typeof validator.isAlpha, 'function'); diff --git a/test/sanitizers.js b/test/sanitizers.test.js similarity index 94% rename from test/sanitizers.js rename to test/sanitizers.test.js index ecb0e128f..e36ba48d3 100644 --- a/test/sanitizers.js +++ b/test/sanitizers.test.js @@ -481,5 +481,34 @@ describe('Sanitizers', () => { 'my.self@foo.com': 'my.self@foo.com', }, }); + + // Testing yandex_convert_yandexru + test({ + sanitizer: 'normalizeEmail', + args: [{ + yandex_convert_yandexru: false, + }], + expect: { + 'test@yandex.kz': 'test@yandex.kz', + 'test@yandex.ru': 'test@yandex.ru', + 'test@yandex.ua': 'test@yandex.ua', + 'test@yandex.com': 'test@yandex.com', + 'test@yandex.by': 'test@yandex.by', + }, + }); + + test({ + sanitizer: 'normalizeEmail', + args: [{ + yandex_convert_yandexru: true, + }], + expect: { + 'test@yandex.kz': 'test@yandex.ru', + 'test@yandex.ru': 'test@yandex.ru', + 'test@yandex.ua': 'test@yandex.ru', + 'test@yandex.com': 'test@yandex.ru', + 'test@yandex.by': 'test@yandex.ru', + }, + }); }); }); diff --git a/test/testFunctions.js b/test/testFunctions.js new file mode 100644 index 000000000..5fc133bec --- /dev/null +++ b/test/testFunctions.js @@ -0,0 +1,60 @@ +import assert from 'assert'; +import { format } from 'util'; +import validator from '../src/index'; + +function stringifyArgs(argsArr) { + return argsArr.map(arg => JSON.stringify(arg)).join(', '); +} + +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, stringifyArgs(args) + ); + + 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, stringifyArgs(args) + ); + + 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, stringifyArgs(args) + ); + + throw new Error(warning); + } + }); + } +} diff --git a/test/util.js b/test/util.js deleted file mode 100644 index 449cd9ee7..000000000 --- a/test/util.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * All tests that tests any utility. - * Prevent any breaking of functionality - */ -import assert from 'assert'; -import typeOf from '../src/lib/util/typeOf'; - -describe('Util', () => { - it('should validate different typeOf', () => { - assert.strictEqual(typeOf([]), 'array'); - assert.strictEqual(typeOf(null), 'null'); - assert.strictEqual(typeOf({}), 'object'); - assert.strictEqual(typeOf(new Date()), 'date'); - assert.strictEqual(typeOf('ezkemboi'), 'string'); - assert.strictEqual(typeOf(String('kemboi')), 'string'); - assert.strictEqual(typeOf(undefined), 'undefined'); - assert.strictEqual(typeOf(2021), 'number'); - assert.notStrictEqual(typeOf([]), 'object'); - }); -}); diff --git a/test/util.test.js b/test/util.test.js new file mode 100644 index 000000000..0146a4e2c --- /dev/null +++ b/test/util.test.js @@ -0,0 +1,64 @@ +/** + * All tests that tests any utility. + * Prevent any breaking of functionality + */ +import assert from 'assert'; +import typeOf from '../src/lib/util/typeOf'; +import assertString from '../src/lib/util/assertString'; + + +describe('Util', () => { + it('should validate different typeOf', () => { + assert.strictEqual(typeOf([]), 'array'); + assert.strictEqual(typeOf(null), 'null'); + assert.strictEqual(typeOf({}), 'object'); + assert.strictEqual(typeOf(new Date()), 'date'); + assert.strictEqual(typeOf('ezkemboi'), 'string'); + assert.strictEqual(typeOf(String('kemboi')), 'string'); + assert.strictEqual(typeOf(undefined), 'undefined'); + assert.strictEqual(typeOf(2021), 'number'); + assert.notStrictEqual(typeOf([]), 'object'); + }); +}); + +describe('assertString', () => { + it('Should throw an error if argument provided is an undefined', () => { + assert.throws(() => { assertString(); }, TypeError); + }); + + it('Should throw an error if argument provided is a null', () => { + assert.throws(() => { assertString(null); }, TypeError); + }); + + it('Should throw an error if argument provided is a Boolean', () => { + assert.throws(() => { assertString(true); }, TypeError); + }); + + it('Should throw an error if argument provided is a Date', () => { + assert.throws(() => { assertString(new Date()); }, TypeError); + }); + + it('Should throw an error if argument provided is a Number(NaN)', () => { + assert.throws(() => { assertString(NaN); }, TypeError); + }); + + it('Should throw an error if argument provided is a Number', () => { + assert.throws(() => { assertString(2024); }, TypeError); + }); + + it('Should throw an error if argument provided is an Object', () => { + assert.throws(() => { assertString({}); }, TypeError); + }); + + it('Should throw an error if argument provided is an Array', () => { + assert.throws(() => { assertString([]); }, TypeError); + }); + + it('Should not throw an error if the argument is an empty string', () => { + assert.doesNotThrow(() => { assertString(''); }); + }); + + it('Should not throw an error if the argument is a String', () => { + assert.doesNotThrow(() => { assertString('antidisestablishmentarianism'); }); + }); +}); diff --git a/test/validators.js b/test/validators.test.js similarity index 78% rename from test/validators.js rename to test/validators.test.js index 87f73ccd4..a3c5f5a5d 100644 --- a/test/validators.js +++ b/test/validators.test.js @@ -1,62 +1,11 @@ import assert from 'assert'; import fs from 'fs'; -import { format } from 'util'; +import timezone_mock from 'timezone-mock'; 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 +23,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', @@ -86,14 +35,15 @@ describe('Validators', () => { 'invalid.com', '@invalid.com', 'foo@bar.com.', + 'foo@_bar.com', 'somename@gmail.com', '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', @@ -116,6 +66,12 @@ describe('Validators', () => { '"wrong()[]",:;<>@@gmail.com', 'username@domain.com�', 'username@domain.com©', + 'nbsp test@test.com', + 'nbsp_test@te st.com', + 'nbsp_test@test.co m', + '"foobar@gmail.com', + '"foo"bar@gmail.com', + 'foo"bar"@gmail.com', ], }); }); @@ -128,10 +84,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', @@ -139,6 +95,16 @@ describe('Validators', () => { }); }); + it('should validate email addresses with underscores in the domain', () => { + test({ + validator: 'isEmail', + args: [{ allow_underscores: true }], + valid: [ + 'foobar@my_sarisari_store.typepad.com', + ], + invalid: [], + }); + }); it('should validate email addresses without UTF8 characters in local part', () => { test({ @@ -167,6 +133,7 @@ describe('Validators', () => { 'hans.m端ller@test.com', 'z@co.c', 'tüst@invalid.com', + 'nbsp test@test.com', ], }); }); @@ -299,12 +266,15 @@ describe('Validators', () => { it('should not validate email addresses with blacklisted chars in the name', () => { test({ validator: 'isEmail', - args: [{ blacklisted_chars: 'abc' }], + args: [{ blacklisted_chars: 'abc"' }], valid: [ 'emil@gmail.com', ], invalid: [ 'email@gmail.com', + '"foobr"@example.com', + '" foo m端ller "@example.com', + '"foo\@br"@example.com', ], }); }); @@ -328,6 +298,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 +323,60 @@ describe('Validators', () => { }); }); + it('should allow regular expressions in the host blacklist of isEmail', () => { + test({ + validator: 'isEmail', + args: [{ + host_blacklist: ['bar.com', 'foo.com', /\.foo\.com$/], + }], + valid: [ + 'email@foobar.com', + 'email@foo.bar.com', + 'email@qux.com', + ], + invalid: [ + 'email@bar.com', + 'email@foo.com', + 'email@a.b.c.foo.com', + ], + }); + }); + + 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 allow regular expressions in the host whitelist of isEmail', () => { + test({ + validator: 'isEmail', + args: [{ + host_whitelist: ['bar.com', 'foo.com', /\.foo\.com$/], + }], + valid: [ + 'email@bar.com', + 'email@foo.com', + 'email@a.b.c.foo.com', + ], + invalid: [ + 'email@foobar.com', + 'email@foo.bar.com', + 'email@qux.com', + ], + }); + }); + it('should validate URLs', () => { test({ validator: 'isURL', @@ -391,6 +424,12 @@ describe('Validators', () => { 'http://[2010:836B:4179::836B:4179]', 'http://example.com/example.json#/foo/bar', 'http://1337.com', + // TODO: those probably should not be marked as valid URLs; CVE-2025-56200 + /* eslint-disable no-script-url */ + 'javascript:%61%6c%65%72%74%28%31%29@example.com', + 'http://evil-site.com@example.com/', + 'javascript:alert(1)@example.com', + /* eslint-enable no-script-url */ ], invalid: [ 'http://localhost:3000/', @@ -433,6 +472,18 @@ describe('Validators', () => { '////foobar.com', 'http:////foobar.com', 'https://example.com/foo//', + // the following tests are because of CVE-2025-56200 + /* eslint-disable no-script-url */ + "javascript:alert(1);a=';@example.com/alert(1)'", + 'JaVaScRiPt:alert(1)@example.com', + 'javascript:/* comment */alert(1)@example.com', + 'javascript:var a=1; alert(a);@example.com', + 'javascript:alert(1)@user@example.com', + 'javascript:alert(1)@example.com?q=safe', + 'data:text/html,@example.com', + 'vbscript:msgbox("XSS")@example.com', + '//evil-site.com/path@example.com', + /* eslint-enable no-script-url */ ], }); }); @@ -445,9 +496,11 @@ describe('Validators', () => { }], valid: [ 'rtmp://foobar.com', + 'rtmp:foobar.com', ], invalid: [ 'http://foobar.com', + 'tel:+15551234567', ], }); }); @@ -500,6 +553,9 @@ describe('Validators', () => { 'rtmp://foobar.com', 'http://foobar.com', 'test://foobar.com', + // Dangerous! This allows to mark malicious URLs as a valid URL (CVE-2025-56200) + // eslint-disable-next-line no-script-url + 'javascript:alert(1);@example.com', ], invalid: [ 'mailto:test@example.com', @@ -671,6 +727,61 @@ describe('Validators', () => { }); }); + it('should validate authentication strings if a protocol is not required', () => { + test({ + validator: 'isURL', + args: [{ + require_protocol: false, + }], + valid: [ + 'user:pw@foobar.com/', + ], + invalid: [ + 'user:pw,@foobar.com/', + ], + }); + }); + + it('should reject authentication strings if a protocol is required', () => { + test({ + validator: 'isURL', + args: [{ + require_protocol: true, + }], + valid: [ + 'http://user:pw@foobar.com/', + 'https://user:password@example.com', + 'ftp://admin:pass@ftp.example.com/', + ], + invalid: [ + 'user:pw@foobar.com/', + 'user:password@example.com', + 'admin:pass@ftp.example.com/', + ], + }); + }); + + it('should reject invalid protocols when require_valid_protocol is enabled', () => { + test({ + validator: 'isURL', + args: [{ + require_valid_protocol: true, + protocols: ['http', 'https', 'ftp'], + }], + valid: [ + 'http://example.com', + 'https://example.com', + 'ftp://example.com', + ], + invalid: [ + // eslint-disable-next-line no-script-url + 'javascript:alert(1);@example.com', + 'data:text/html,@example.com', + 'file:///etc/passwd@example.com', + ], + }); + }); + it('should let users specify a host whitelist', () => { test({ validator: 'isURL', @@ -749,6 +860,24 @@ describe('Validators', () => { }); }); + it('GHSA-9965-vmph-33xx vulnerability - protocol delimiter parsing difference', () => { + const DOMAIN_WHITELIST = ['example.com']; + + test({ + validator: 'isURL', + args: [{ + protocols: ['https'], + host_whitelist: DOMAIN_WHITELIST, + require_host: false, + }], + valid: [], + invalid: [ + // eslint-disable-next-line no-script-url + "javascript:alert(1);a=';@example.com/alert(1)", + ], + }); + }); + it('should allow rejecting urls containing authentication information', () => { test({ validator: 'isURL', @@ -793,6 +922,21 @@ describe('Validators', () => { }); }); + it('should allow user to configure the maximum URL length', () => { + test({ + validator: 'isURL', + args: [{ max_allowed_length: 20 }], + valid: [ + 'http://foobar.com/12', // 20 characters + 'http://foobar.com/', + ], + invalid: [ + 'http://foobar.com/123', // 21 characters + 'http://foobar.com/1234567890', + ], + }); + }); + it('should validate URLs with port present', () => { test({ validator: 'isURL', @@ -997,136 +1141,6 @@ describe('Validators', () => { }); }); - it('should validate IP addresses', () => { - test({ - validator: 'isIP', - valid: [ - '127.0.0.1', - '0.0.0.0', - '255.255.255.255', - '1.2.3.4', - '::1', - '2001:db8:0000:1:1:1:1:1', - '2001:db8:3:4::192.0.2.33', - '2001:41d0:2:a141::1', - '::ffff:127.0.0.1', - '::0000', - '0000::', - '1::', - '1111:1:1:1:1:1:1:1', - 'fe80::a6db:30ff:fe98:e946', - '::', - '::8', - '::ffff:127.0.0.1', - '::ffff:255.255.255.255', - '::ffff:0:255.255.255.255', - '::2:3:4:5:6:7:8', - '::255.255.255.255', - '0:0:0:0:0:ffff:127.0.0.1', - '1:2:3:4:5:6:7::', - '1:2:3:4:5:6::8', - '1::7:8', - '1:2:3:4:5::7:8', - '1:2:3:4:5::8', - '1::6:7:8', - '1:2:3:4::6:7:8', - '1:2:3:4::8', - '1::5:6:7:8', - '1:2:3::5:6:7:8', - '1:2:3::8', - '1::4:5:6:7:8', - '1:2::4:5:6:7:8', - '1:2::8', - '1::3:4:5:6:7:8', - '1::8', - 'fe80::7:8%eth0', - 'fe80::7:8%1', - '64:ff9b::192.0.2.33', - '0:0:0:0:0:0:10.0.0.1', - ], - invalid: [ - 'abc', - '256.0.0.0', - '0.0.0.256', - '26.0.0.256', - '0200.200.200.200', - '200.0200.200.200', - '200.200.0200.200', - '200.200.200.0200', - '::banana', - 'banana::', - '::1banana', - '::1::', - '1:', - ':1', - ':1:1:1::2', - '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1', - '::11111', - '11111:1:1:1:1:1:1:1', - '2001:db8:0000:1:1:1:1::1', - '0:0:0:0:0:0:ffff:127.0.0.1', - '0:0:0:0:ffff:127.0.0.1', - ], - }); - test({ - validator: 'isIP', - args: [4], - valid: [ - '127.0.0.1', - '0.0.0.0', - '255.255.255.255', - '1.2.3.4', - '255.0.0.1', - '0.0.1.1', - ], - invalid: [ - '::1', - '2001:db8:0000:1:1:1:1:1', - '::ffff:127.0.0.1', - '137.132.10.01', - '0.256.0.256', - '255.256.255.256', - ], - }); - test({ - validator: 'isIP', - args: [6], - valid: [ - '::1', - '2001:db8:0000:1:1:1:1:1', - '::ffff:127.0.0.1', - 'fe80::1234%1', - 'ff08::9abc%10', - 'ff08::9abc%interface10', - 'ff02::5678%pvc1.3', - ], - invalid: [ - '127.0.0.1', - '0.0.0.0', - '255.255.255.255', - '1.2.3.4', - '::ffff:287.0.0.1', - '%', - 'fe80::1234%', - 'fe80::1234%1%3%4', - 'fe80%fe80%', - ], - }); - test({ - validator: 'isIP', - args: [10], - valid: [], - invalid: [ - '127.0.0.1', - '0.0.0.0', - '255.255.255.255', - '1.2.3.4', - '::1', - '2001:db8:0000:1:1:1:1:1', - ], - }); - }); - it('should validate isIPRange', () => { test({ validator: 'isIPRange', @@ -1321,6 +1335,7 @@ describe('Validators', () => { ], }); }); + it('should validate alpha strings', () => { test({ validator: 'isAlpha', @@ -1603,6 +1618,53 @@ describe('Validators', () => { }); }); + it('should validate Japanese alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['ja-JP'], + valid: [ + 'あいうえお', + 'がぎぐげご', + 'ぁぃぅぇぉ', + 'アイウエオ', + 'ァィゥェ', + 'アイウエオ', + '吾輩は猫である', + '臥薪嘗胆', + '新世紀エヴァンゲリオン', + '天国と地獄', + '七人の侍', + 'シン・ウルトラマン', + ], + invalid: [ + 'あいう123', + 'abcあいう', + '1984', + ], + }); + }); + + it('should validate kazakh alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['kk-KZ'], + valid: [ + 'Сәлем', + 'қанағаттандырылмағандықтарыңыздан', + 'Кешіріңіз', + 'Өкінішке', + 'Қайталаңызшы', + 'ағылшынша', + 'түсінбедім', + ], + invalid: [ + 'Кешіріңіз1', + ' Кет бар ', + 'مرحبا العا', + ], + }); + }); + it('should validate Vietnamese alpha strings', () => { test({ validator: 'isAlpha', @@ -1936,6 +1998,100 @@ describe('Validators', () => { }); }); + it('should validate Tamil alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['ta-IN'], + valid: [ + 'அஆஇஈஉஊஎஏஐஒஓஔகஙசஞடணதநபமயரலவழளறனஶஜஷஸஹ', + 'தமிழ்', + ], + invalid: [ + 'தமிழ்123', + 'தமிழ் ', + 'தமிழ்.', + 'abc', + '', + ], + }); + }); + it('should validate Telugu alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['te-IN'], + valid: [ + 'అఆఇఈఉఊఋఌఎఏఐఒఓఔకఖగఘఙచఛజఝఞటఠడఢణతథదధనపఫబభమయరలవశషసహ', + 'తెలుగు', + ], + invalid: ['తెలుగు123', 'తెలుగు.', 'abc', ''], + }); + }); + it('should validate Kannada alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['kn-IN'], + valid: [ + 'ಅಆಇಈಉಊಋಎಏಐಒಓಔಕಖಗಘಙಚಛಜಝಞಟಠಡಢಣತಥದಧನಪಫಬಭಮಯರಲವಶಷಸಹಳ', + 'ಕನ್ನಡ', + ], + invalid: ['ಕನ್ನಡ123', 'ಕನ್ನಡ.', 'abc', ''], + }); + }); + it('should validate Malayalam alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['ml-IN'], + valid: [ + 'അആഇഈഉഊഋഎഏഐഒഓഔകഖഗഘങചഛജഝഞടഠഡഢണതഥദധനപഫബഭമയരലവശഷസഹള', + 'മലയാളം', + ], + invalid: ['മലയാളം123', 'മലയാളം.', 'abc', ''], + }); + }); + it('should validate Gujarati alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['gu-IN'], + valid: [ + 'અઆઇઈઉઊઋએઐઓઔકખગઘચછજઝટઠડઢણતથદધનપફબભમયરલવશષસહળ', + 'ગુજરાતી', + ], + invalid: ['ગુજરાતી123', 'ગુજરાતી.', 'abc', ''], + }); + }); + it('should validate Punjabi alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['pa-IN'], + valid: [ + 'ਅਆਇਈਉਊਏਐਓਔਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵਸ਼ਸਹ', + 'ਪੰਜਾਬੀ', + ], + invalid: ['ਪੰਜਾਬੀ123', 'ਪੰਜਾਬੀ.', 'abc', ''], + }); + }); + it('should validate Odia alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['or-IN'], + valid: [ + 'ଅଆଇଈଉଊଋଌଏଐଓଔକଖଗଘଙଚଛଜଝଞଟଠଡଢଣତଥଦଧନପଫବଭମଯରଲଶଷସହଳ', + 'ଓଡ଼ିଆ', + ], + invalid: ['ଓଡ଼ିଆ123', 'ଓଡ଼ିଆ.', 'abc', ''], + }); + }); + it('should validate Bengali alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['bn-IN'], + valid: [ + 'অআইঈউঊঋএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ', + 'বাংলা', + ], + invalid: ['বাংলা123', 'বাংলা.', 'abc', ''], + }); + }); it('should validate persian alpha strings', () => { test({ validator: 'isAlpha', @@ -1969,13 +2125,34 @@ describe('Validators', () => { }); }); - it('should validate Sinhala alpha strings', () => { + it('should validate Korea alpha strings', () => { test({ validator: 'isAlpha', - args: ['si-LK'], + args: ['ko-KR'], valid: [ - 'චතුර', - 'කචටදබ', + 'ㄱ', + 'ㅑ', + 'ㄱㄴㄷㅏㅕ', + '세종대왕', + '나랏말싸미듕귁에달아문자와로서르사맛디아니할쎄', + ], + invalid: [ + 'abc', + '123', + '흥선대원군 문호개방', + '1592년임진왜란', + '대한민국!', + ], + }); + }); + + it('should validate Sinhala alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['si-LK'], + valid: [ + 'චතුර', + 'කචටදබ', 'ඎඏදාෛපසුගො', ], invalid: [ @@ -1989,6 +2166,25 @@ describe('Validators', () => { }); }); + it('should validate Esperanto alpha strings', () => { + test({ + validator: 'isAlpha', + args: ['eo'], + valid: [ + 'saluton', + 'eĥoŝanĝoĉiuĵaŭde', + 'EĤOŜANĜOĈIUĴAŬDE', + 'Esperanto', + 'LaŭLudovikoZamenhofBongustasFreŝaĈeĥaManĝaĵoKunSpicoj', + ], + invalid: [ + 'qwxyz', + '1887', + 'qwxyz 1887', + ], + }); + }); + it('should error on invalid locale', () => { test({ validator: 'isAlpha', @@ -2389,6 +2585,49 @@ 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 kazakh alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['kk-KZ'], + valid: [ + 'Сәлем777', + '123Бәсе', + 'солай', + 'Жиенсу', + '90тоқсан', + 'жалғыз', + '570бердім', + ], + invalid: [ + ' кешіріңіз ', + 'abcағылшынша', + 'мүмкін!!', + ], + }); + }); + it('should validate kurdish alphanumeric strings', () => { test({ validator: 'isAlphanumeric', @@ -2595,6 +2834,23 @@ 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', @@ -2616,6 +2872,182 @@ describe('Validators', () => { }); }); + it('should validate Esperanto alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['eo'], + valid: [ + 'saluton', + 'eĥoŝanĝoĉiuĵaŭde0123456789', + 'EĤOŜANĜOĈIUĴAŬDE0123456789', + 'Esperanto1887', + 'LaŭLudovikoZamenhofBongustasFreŝaĈeĥaManĝaĵoKunSpicoj', + ], + invalid: [ + 'qwxyz', + 'qwxyz 1887', + ], + }); + }); + it('should validate Tamil alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['ta-IN'], + valid: [ + 'தமிழ்', + 'தமிழ்123', + 'அஆஇஈ123', + 'தமிழ்123.45', + '123.45', + 'தமிழ்.', + ], + invalid: [ + 'தமிழ் ', + 'abc', + '', + ], + }); + }); + + it('should validate Telugu alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['te-IN'], + valid: [ + 'తెలుగు', + 'తెలుగు123', + 'అఆఇఈ123', + 'తెలుగు123.45', + '123.45', + 'తెలుగు.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Kannada alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['kn-IN'], + valid: [ + 'ಕನ್ನಡ', + 'ಕನ್ನಡ123', + 'ಅಆಇಈ123', + 'ಕನ್ನಡ123.45', + '123.45', + 'ಕನ್ನಡ.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Malayalam alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['ml-IN'], + valid: [ + 'മലയാളം', + 'മലയാളം123', + 'അആഇഈ123', + 'മലയാളം123.45', + '123.45', + 'മലയാളം.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Gujarati alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['gu-IN'], + valid: [ + 'ગુજરાતી', + 'ગુજરાતી123', + 'અઆઇઈ123', + 'ગુજરાતી123.45', + '123.45', + 'ગુજરાતી.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Punjabi alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['pa-IN'], + valid: [ + 'ਪੰਜਾਬੀ', + 'ਪੰਜਾਬੀ123', + 'ਅਆਇਈ123', + 'ਪੰਜਾਬੀ123.45', + '123.45', + 'ਪੰਜਾਬੀ.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Odia alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['or-IN'], + valid: [ + 'ଓଡ଼ିଆ', + 'ଓଡ଼ିଆ123', + 'ଅଆଇଈ123', + 'ଓଡ଼ିଆ123.45', + '123.45', + 'ଓଡ଼ିଆ.', + ], + invalid: [ + 'abc', + '', + ], + }); + }); + + it('should validate Bengali alphanumeric strings', () => { + test({ + validator: 'isAlphanumeric', + args: ['bn-IN'], + valid: [ + 'বাংলা', + 'বাংলা১২৩', + 'অআইঈ১২৩', + '১২৩৪৫৬৭৮৯০', + 'বাংলা১২৩', + '১২৩৪৫', + 'বাংলা', + ], + invalid: [ + 'abc', + 'বাংলা123', + '123', + 'বাংলা ১২৩', + 'বাংলা,১২৩', + '১২৩٫৪৫', + '', + ], + }); + }); + it('should error on invalid locale', () => { test({ validator: 'isAlphanumeric', @@ -2736,6 +3168,7 @@ describe('Validators', () => { '', '-1', '65536', + '0080', ], }); }); @@ -2803,6 +3236,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['AZ'], + valid: [ + 'A16175905', + 'A16175958', + ], + invalid: [ + 'AZ1234584', + ], + }); + test({ validator: 'isPassportNumber', args: ['BE'], @@ -2857,9 +3302,17 @@ describe('Validators', () => { valid: [ 'GA302922', 'ZE000509', + 'A123456AB', + 'Z556378HG', ], invalid: [ 'AB0123456', + 'AZ556378H', + '556378HCX', + '556378432', + '5563784', + '#B12345FD', + 'A43F12354', ], }); @@ -3138,6 +3591,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['JM'], + valid: [ + 'A0123456', + ], + invalid: [ + 's0123456', + 'a01234567', + ], + }); + test({ validator: 'isPassportNumber', args: ['JP'], @@ -3165,6 +3630,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'], @@ -3279,6 +3769,55 @@ describe('Validators', () => { 'XR1001R58A', ], }); + test({ + validator: 'isPassportNumber', + args: ['PK'], + valid: [ + 'QZ1791293', + 'XR1001458', + ], + invalid: [ + 'XTR11013R', + 'XR1001R58A', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['PH'], + valid: [ + 'X123456', + 'XY123456', + 'XY1234567', + 'X1234567Y', + ], + invalid: [ + 'XY12345', + 'X12345Z', + 'XY12345Z', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['NZ'], + valid: [ + 'Lf012345', + 'La012345', + 'Ld012345', + 'Lh012345', + 'ea012345', + 'ep012345', + 'n012345', + ], + invalid: [ + 'Lp012345', + 'nd012345', + 'ed012345', + 'eh012345', + 'ef012345', + ], + }); test({ validator: 'isPassportNumber', @@ -3374,6 +3913,22 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['TH'], + valid: [ + 'A123456', + 'B1234567', + 'CD123456', + 'EF1234567', + ], + invalid: [ + '123456', + '1234567', + '010485371AA', + ], + }); + test({ validator: 'isPassportNumber', args: ['TR'], @@ -3408,11 +3963,30 @@ describe('Validators', () => { valid: [ '790369937', '340007237', + 'A90583942', + 'E00007734', ], invalid: [ 'US0123456', '0123456US', '7903699371', + '90583942', + 'E000077341', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['ZA'], + valid: [ + 'T12345678', + 'A12345678', + 'M12345678', + 'D12345678', + ], + invalid: [ + '123456789', + 'Z12345678', ], }); }); @@ -3922,31 +4496,104 @@ describe('Validators', () => { 'a', ], }); - }); - - it('should validate floats', () => { test({ - validator: 'isFloat', + validator: 'isInt', + args: [{ + min: undefined, + max: undefined, + }], valid: [ - '123', - '123.', - '123.123', - '-123.123', - '-0.123', - '+0.123', - '0.123', - '.0', - '-.123', - '+.123', - '01.123', - '-0.22250738585072011e-307', + '143', + '15', + '767777575', ], invalid: [ - '+', - '-', - ' ', - '', + '10.4', + 'bar', + '10a', + 'c44', + ], + }); + test({ + validator: 'isInt', + args: [{ + gt: undefined, + lt: undefined, + }], + valid: [ + '289373466', + '55', + '989', + ], + invalid: [ + '10.4', + 'baz', + '66a', + 'c21', + ], + }); + test({ + validator: 'isInt', + args: [{ + gt: null, + max: null, + }], + valid: [ + '1', + '886', + '84512345', + ], + invalid: [ + '10.4', + 'h', + '1.2', + '+', + ], + }); + test({ + validator: 'isInt', + args: [{ + lt: null, + min: null, + }], + valid: [ + '289373466', + '55', + '989', + ], + invalid: [ + ',', + '+11212+', + 'fail', + '111987234i', + ], + }); + }); + + it('should validate floats', () => { + test({ + validator: 'isFloat', + valid: [ + '123', + '123.', + '123.123', + '-123.123', + '-0.123', + '+0.123', + '0.123', + '.0', + '-.123', + '+.123', + '01.123', + '-0.22250738585072011e-307', + ], + invalid: [ + '+', + '-', + ' ', + '', '.', + ',', 'foo', '20.foo', '2020-01-06T14:31:00.135Z', @@ -4150,6 +4797,87 @@ describe('Validators', () => { 'foo', ], }); + test({ + validator: 'isFloat', + args: [{ + min: undefined, + max: undefined, + }], + valid: [ + '123', + '123.', + '123.123', + '-767.767', + '+111.111', + ], + invalid: [ + 'ab565', + '-,123', + '+,123', + '7866.t', + '123,123', + '123,', + ], + }); + test({ + validator: 'isFloat', + args: [{ + gt: undefined, + lt: undefined, + }], + valid: [ + '14.34343', + '11.1', + '456', + ], + invalid: [ + 'ab565', + '-,123', + '+,123', + '7866.t', + ], + }); + test({ + validator: 'isFloat', + args: [{ + locale: 'ar', + gt: null, + max: null, + }], + valid: [ + '13324٫', + '12321', + '444٫83874', + ], + invalid: [ + '55.55.55', + '1;23', + '+-123', + '1111111l1', + '3.3', + ], + }); + test({ + validator: 'isFloat', + args: [{ + locale: 'ru-RU', + lt: null, + min: null, + }], + valid: [ + '11231554,34343', + '11,1', + '456', + ',311', + ], + invalid: [ + 'ab565', + '-.123', + '+.123', + '7866.t', + '22.3', + ], + }); }); it('should validate hexadecimal strings', () => { @@ -4274,8 +5002,10 @@ describe('Validators', () => { 'rgba(255,255,255,1)', 'rgba(255,255,255,.1)', 'rgba(255,255,255,0.1)', + 'rgba(255,255,255,.12)', 'rgb(5%,5%,5%)', 'rgba(5%,5%,5%,.3)', + 'rgba(5%,5%,5%,.32)', ], invalid: [ 'rgb(0,0,0,)', @@ -4284,16 +5014,63 @@ describe('Validators', () => { 'rgb()', 'rgba(0,0,0)', 'rgba(255,255,255,2)', - 'rgba(255,255,255,.12)', + 'rgba(255,255,255,.123)', 'rgba(255,255,256,0.1)', 'rgb(4,4,5%)', 'rgba(5%,5%,5%)', 'rgba(3,3,3%,.3)', + 'rgba(5%,5%,5%,.321)', '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', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + 'rg ba(0, 251, 22, 0.5)', + 'rgb( 255,255 ,255)', + 'rgba(255, 255, 255, 0.5)', + 'rgba(255, 255, 255, 0.5)', + 'rgb(5%, 5%, 5%)', ], }); + // test empty options object + test({ + validator: 'isRgbColor', + args: [{}], + valid: [ + 'rgb(0,0,0)', + 'rgb(255,255,255)', + 'rgba(0,0,0,0)', + 'rgba(255,255,255,1)', + 'rgba(255,255,255,.1)', + 'rgba(255,255,255,.12)', + 'rgba(255,255,255,0.1)', + 'rgb(5%,5%,5%)', + 'rgba(5%,5%,5%,.3)', + ], + invalid: [ + 'rgb(0,0,0,)', + 'rgb(0,0,)', + 'rgb(0,0,256)', + 'rgb()', + 'rgba(0,0,0)', + 'rgba(255,255,255,2)', + 'rgba(255,255,256,0.1)', + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'rgba(3,3,3%,.3)', + 'rgb(101%,101%,101%)', + 'rgba(3%,3%,101%,0.3)', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + 'rg ba(0, 251, 22, 0.5)', + 'rgb( 255,255 ,255)', + 'rgba(255, 255, 255, 0.5)', + 'rgba(255, 255, 255, 0.5)', + 'rgb(5%, 5%, 5%)', + ], + }); // test where includePercentValues is given as false test({ validator: 'isRgbColor', @@ -4305,6 +5082,159 @@ describe('Validators', () => { invalid: [ 'rgb(4,4,5%)', 'rgba(5%,5%,5%)', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + ], + }); + + // test where includePercentValues is given as false as part of options object + test({ + validator: 'isRgbColor', + args: [{ includePercentValues: false }], + valid: [ + 'rgb(5,5,5)', + 'rgba(5,5,5,.3)', + ], + invalid: [ + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'r g b( 0, 251, 222 )', + 'rgba(255, 255, 255 ,0.2)', + 'r g ba( 0, 251, 222 )', + ], + }); + + // test where include percent is true explciitly + test({ + validator: 'isRgbColor', + args: [true], + valid: [ + 'rgb(5,5,5)', + 'rgba(5,5,5,.3)', + 'rgb(0,0,0)', + 'rgb(255,255,255)', + 'rgba(0,0,0,0)', + 'rgba(255,255,255,1)', + 'rgba(255,255,255,.1)', + 'rgba(255,255,255,.12)', + 'rgba(255,255,255,0.1)', + 'rgb(5%,5%,5%)', + 'rgba(5%,5%,5%,.3)', + 'rgb(5%,5%,5%)', + 'rgba(255,255,255,0.5)', + ], + invalid: [ + 'rgba(255, 255, 255, 0.5)', + 'rgb(5%, 5%, 5%)', + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + 'rgb(0,0,0,)', + 'rgb(0,0,)', + 'rgb(0,0,256)', + 'rgb()', + 'rgba(0,0,0)', + 'rgba(255,255,255,2)', + 'rgba(255,255,256,0.1)', + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'rgba(3,3,3%,.3)', + 'rgb(101%,101%,101%)', + 'rgba(3%,3%,101%,0.3)', + ], + }); + + // test where percent value is false and allowSpaces is true as part of options object + test({ + validator: 'isRgbColor', + args: [{ includePercentValues: false, allowSpaces: true }], + valid: [ + 'rgb(5,5,5)', + 'rgba(5,5,5,.3)', + 'rgba(255,255,255,0.2)', + 'rgba(255, 255, 255 ,0.2)', + ], + invalid: [ + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'rgba(5% ,5%, 5%)', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + 'rgb(0,0,)', + 'rgb()', + 'rgb(4,4,5%)', + 'rgb(5%,5%,5%)', + 'rgba(3,3,3%,.3)', + 'rgb(101%, 101%, 101%)', + 'rgba(3%,3%,101%,0.3)', + ], + + }); + + // test where both are true as part of options object + test({ + validator: 'isRgbColor', + args: [{ includePercentValues: true, allowSpaces: true }], + valid: [ + 'rgb( 5, 5, 5)', + 'rgba(5, 5, 5, .3)', + 'rgb(0, 0, 0)', + 'rgb(255, 255, 255)', + 'rgba(0, 0, 0, 0)', + 'rgba(255, 255, 255, 1)', + 'rgba(255, 255, 255, .1)', + 'rgba(255, 255, 255, 0.1)', + 'rgb(5% ,5% ,5%)', + 'rgba(5%,5%,5%, .3)', + ], + invalid: [ + 'r g b( 0, 251, 222 )', + 'rgb(4,4,5%)', + 'rgb(101%,101%,101%)', + + ], + }); + + // test where allowSpaces is false as part of options object + test({ + validator: 'isRgbColor', + args: [{ includePercentValues: true, allowSpaces: false }], + valid: [ + 'rgb(5,5,5)', + 'rgba(5,5,5,.3)', + 'rgb(0,0,0)', + 'rgb(255,255,255)', + 'rgba(0,0,0,0)', + 'rgba(255,255,255,1)', + 'rgba(255,255,255,.1)', + 'rgba(255,255,255,.12)', + 'rgba(255,255,255,0.1)', + 'rgb(5%,5%,5%)', + 'rgba(5%,5%,5%,.3)', + + ], + invalid: [ + 'rgb( 255,255 ,255)', + 'rgba(255, 255, 255, 0.5)', + 'rgb(5%, 5%, 5%)', + 'rgba(255, 255, 255, 0.5)', + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'r g b( 0, 251, 222 )', + 'r g ba( 0, 251, 222 )', + 'rgb(0,0,0,)', + 'rgb(0,0,)', + 'rgb(0,0,256)', + 'rgb()', + 'rgba(0,0,0)', + 'rgba(255,255,255,2)', + 'rgba(255,255,256,0.1)', + 'rgb(4,4,5%)', + 'rgba(5%,5%,5%)', + 'rgba(3,3,3%,.3)', + 'rgb(101%,101%,101%)', + 'rgba(3%,3%,101%,0.3)', ], }); }); @@ -4496,10 +5426,11 @@ describe('Validators', () => { 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E', - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ', // No signature ], invalid: [ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9', + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTUxNjIzOTAyMn0', + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTYxNjY1Mzg3Mn0.eyJpc3MiOiJodHRwczovL2V4YW1wbGUuY29tIiwiaWF0IjoxNjE2NjUzODcyLCJleHAiOjE2MTY2NTM4ODJ9.a1jLRQkO5TV5y5ERcaPAiM9Xm2gBdRjKrrCpHkGr_8M', '$Zs.ewu.su84', 'ks64$S/9.dy$§kz.3sd73b', ], @@ -4638,16 +5569,51 @@ describe('Validators', () => { 'uz_Latn_UZ', 'en', 'gsw', + 'en-US', 'es_ES', + 'es-419', 'sw_KE', 'am_ET', + 'zh-CHS', 'ca_ES_VALENCIA', 'en_US_POSIX', + 'hak-CN', + 'zh-Hant', + 'zh-Hans', + 'sr-Cyrl', + 'sr-Latn', + 'zh-cmn-Hans-CN', + 'cmn-Hans-CN', + 'zh-yue-HK', + 'yue-HK', + 'zh-Hans-CN', + 'sr-Latn-RS', + 'sl-rozaj', + 'sl-rozaj-biske', + 'sl-nedis', + 'de-CH-1901', + 'sl-IT-nedis', + 'hy-Latn-IT-arevela', + 'i-enochian', + 'en-scotland-fonipa', + 'sl-IT-rozaj-biske-1994', + 'de-CH-x-phonebk', + 'az-Arab-x-AZE-derbend', + 'x-whatever', + 'qaa-Qaaa-QM-x-southern', + 'de-Qaaa', + 'sr-Latn-QM', + 'sr-Qaaa-RS', + 'en-US-u-islamcal', + 'zh-CN-a-myext-x-private', + 'en-a-myext-b-another', ], invalid: [ 'lo_POP', '12', '12_DD', + 'de-419-DE', + 'a-DE', ], }); }); @@ -4700,22 +5666,52 @@ describe('Validators', () => { }); test({ validator: 'isLength', - args: [{ max: 0 }], - valid: [''], - invalid: ['a', 'ab'], + args: [{ max: 6, discreteLengths: 5 }], + valid: ['abcd', 'vfd', 'ff', '', 'k'], + invalid: ['abcdefgh', 'hfjdksks'], }); test({ validator: 'isLength', - valid: ['a', '', 'asds'], + args: [{ min: 2, max: 6, discreteLengths: 5 }], + valid: ['bsa', 'vfvd', 'ff'], + invalid: ['', ' ', 'hfskdunvc'], }); test({ validator: 'isLength', - args: [{ max: 8 }], - valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], + args: [{ min: 1, discreteLengths: 2 }], + valid: [' ', 'hello', 'bsa'], + invalid: [''], }); - }); - - it('should validate strings by byte length', () => { + test({ + validator: 'isLength', + args: [{ max: 0 }], + valid: [''], + invalid: ['a', 'ab'], + }); + test({ + validator: 'isLength', + args: [{ min: 5, max: 10, discreteLengths: [2, 6, 8, 9] }], + valid: ['helloguy', 'shopping', 'validator', 'length'], + invalid: ['abcde', 'abcdefg'], + }); + test({ + validator: 'isLength', + args: [{ discreteLengths: '9' }], + valid: ['a', 'abcd', 'abcdefghijkl'], + invalid: [], + }); + test({ + validator: 'isLength', + valid: ['a', '', 'asds'], + }); + test({ + validator: 'isLength', + args: [{ max: 8 }], + valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], + }); + }); + + it('should validate strings by byte length', () => { test({ validator: 'isByteLength', args: [{ min: 2 }], @@ -4742,17 +5738,44 @@ describe('Validators', () => { }); }); + it('should validate ULIDs', () => { + test({ + validator: 'isULID', + valid: [ + '01HBGW8CWQ5Q6DTT7XP89VV4KT', + '01HBGW8CWR8MZQMBG6FA2QHMDD', + '01HBGW8CWS3MEEK12Y9G7SVW4V', + '01hbgw8cws1tq2njavy9amb0wx', + '01HBGW8cwS43H4jkQ0A4ZRJ7QV', + ], + invalid: [ + '', + '01HBGW-CWS3MEEK1#Y9G7SVW4V', + '91HBGW8CWS3MEEK12Y9G7SVW4V', + '81HBGW8CWS3MEEK12Y9G7SVW4V', + '934859', + '01HBGW8CWS3MEEK12Y9G7SVW4VXXX', + '01UBGW8IWS3MOEK12Y9G7SVW4V', + '01HBGW8CuS43H4JKQ0A4ZRJ7QV', + ], + }); + }); + it('should validate UUIDs', () => { test({ validator: 'isUUID', valid: [ - 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', ], invalid: [ '', 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', 'A987FBC94BED3078CF079141BA07C9F3', '934859', @@ -4764,11 +5787,13 @@ describe('Validators', () => { validator: 'isUUID', args: [undefined], valid: [ - 'A117FBC9-4BED-3078-CF07-9141BA07C9F3', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', 'A117FBC9-4BED-5078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], invalid: [ '', + 'A117FBC9-4BED-3078-CF07-9141BA07C9F3', 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', 'A987FBC94BED3078CF079141BA07C9F3', 'A11AAAAA-1111-1111-AAAG-111111111111', @@ -4778,11 +5803,13 @@ describe('Validators', () => { validator: 'isUUID', args: [null], valid: [ - 'A127FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A127FBC9-4BED-3078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], invalid: [ '', 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A127FBC9-4BED-3078-CF07-9141BA07C9F3', 'A127FBC9-4BED-3078-CF07-9141BA07C9F3xxx', '912859', 'A12AAAAA-1111-1111-AAAG-111111111111', @@ -4800,36 +5827,41 @@ describe('Validators', () => { 'AAAAAAAA-1111-2222-AAAG-111111111111', 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ validator: 'isUUID', args: [2], valid: [ - 'A987FBC9-4BED-2078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-2078-AF07-9141BA07C9F3', ], invalid: [ '', 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', '11111', 'AAAAAAAA-1111-1111-AAAG-111111111111', + 'A987FBC9-4BED-2078-CF07-9141BA07C9F3', 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ validator: 'isUUID', args: [3], valid: [ - 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', ], invalid: [ '', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', '934859', 'AAAAAAAA-1111-1111-AAAG-111111111111', 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ @@ -4848,6 +5880,7 @@ describe('Validators', () => { 'AAAAAAAA-1111-1111-AAAG-111111111111', 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ @@ -4866,18 +5899,191 @@ describe('Validators', () => { 'AAAAAAAA-1111-1111-AAAG-111111111111', '9c858901-8a57-4791-81fe-4c455b099bc9', 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ validator: 'isUUID', args: [6], - valid: [], + valid: [ + '1ef29908-cde1-69d0-be16-bfc8518a95f0', + ], invalid: [ '987FBC97-4BED-1078-AF07-9141BA07C9F3', '987FBC97-4BED-2078-AF07-9141BA07C9F3', '987FBC97-4BED-3078-AF07-9141BA07C9F3', '987FBC97-4BED-4078-AF07-9141BA07C9F3', '987FBC97-4BED-5078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + '987FBC97-4BED-8078-AF07-9141BA07C9F3', + ], + }); + test({ + validator: 'isUUID', + args: [7], + valid: [ + '018C544A-D384-7000-BB74-3B1738ABE43C', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + '934859', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + '713ae7e3-cb32-45f9-adcb-7c4fa86b90c1', + '625e63f3-58f5-40b7-83a1-a72ad31acffb', + '57b73598-8764-4ad0-a76a-679bb6640eb1', + '9c858901-8a57-4791-81fe-4c455b099bc9', + ], + }); + test({ + validator: 'isUUID', + args: [8], + valid: [ + '018C544A-D384-8000-BB74-3B1738ABE43C', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + '934859', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-7078-AF07-9141BA07C9F3', + '713ae7e3-cb32-45f9-adcb-7c4fa86b90c1', + '625e63f3-58f5-40b7-83a1-a72ad31acffb', + '57b73598-8764-4ad0-a76a-679bb6640eb1', + '9c858901-8a57-4791-81fe-4c455b099bc9', + ], + }); + test({ + validator: 'isUUID', + args: ['nil'], + valid: [ + '00000000-0000-0000-0000-000000000000', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '934859', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + 'ffffffff-ffff-ffff-ffff-ffffffffffff', + 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', + ], + }); + test({ + validator: 'isUUID', + args: ['max'], + valid: [ + 'ffffffff-ffff-ffff-ffff-ffffffffffff', + 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '934859', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + '00000000-0000-0000-0000-000000000000', + ], + }); + test({ + validator: 'isUUID', + args: ['loose'], + valid: [ + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', + 'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA', + 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee', + 'EEEEEEEE-EEEE-EEEE-EEEE-EEEEEEEEEEEE', + '99999999-9999-9999-9999-999999999999', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + '934859', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + ], + }); + test({ + validator: 'isUUID', + args: ['all'], + valid: [ + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + '00000000-0000-0000-0000-000000000000', + 'ffffffff-ffff-ffff-ffff-ffffffffffff', + 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '934859', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + ], + }); + test({ + validator: 'isUUID', + args: ['invalid'], + valid: [], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '934859', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + '9deb20fe-a6e0-355c-81ea-288b009e4f6d', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-6078-AF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-8078-AF07-9141BA07C9F3', + '00000000-0000-0000-0000-000000000000', + 'ffffffff-ffff-ffff-ffff-ffffffffffff', + 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', ], }); }); @@ -4925,69 +6131,115 @@ 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'], - }); + it('should validate ABA routing number', () => { 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'], + validator: 'isAbaRouting', + valid: [ + '322070381', + '011103093', + '263170175', + '124303065', + ], + invalid: [ + '426317017', + '789456124', + '603558459', + 'qwerty', + '12430306', + '382070381', + ], }); }); - it('should validate dates against an end date', () => { - test({ - validator: 'isBefore', - args: ['08/04/2011'], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - args: [new Date(2011, 7, 4).toString()], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); + it('should validate IBAN', () => { test({ - validator: 'isBefore', + validator: 'isIBAN', valid: [ - '2000-08-04', - new Date(0).toString(), - new Date(Date.now() - 86400000).toString(), + 'SC52BAHL01031234567890123456USD', + 'LC14BOSL123456789012345678901234', + 'MT31MALT01100000000000000000123', + 'SV43ACAT00000000000000123123', + 'EG800002000156789012345180002', + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'CH56 0483 5012 3456 7800 9', + 'GB98 MIDL 0700 9312 3456 78', + 'IL170108000000012612345', + 'IT60X0542811101000000123456', + 'JO71CBJO0000000000001234567890', + 'TR320010009999901234567890', + 'BR1500000000000010932840814P2', + 'LB92000700000000123123456123', + 'IR200170000000339545727003', + 'MZ97123412341234123412341', + 'MA64011519000001205000534921', + 'VG96VPVG0000012345678901', + 'DZ580002100001113000000570', + 'IE29AIBK93115212345678', + 'PS92PALS000000000400123456702', + 'PS92PALS00000000040012345670O', + ], + invalid: [ + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + 'VG46H07Y0223060094359858', + 'IE95TE8270900834048660', + 'PS072435171802145240705922007', ], - invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], }); test({ - validator: 'isBefore', - args: ['2011-08-03'], - valid: ['1999-12-31'], - invalid: ['invalid date'], + validator: 'isIBAN', + args: [{ whitelist: ['DK', 'GB'] }], + valid: [ + 'DK5000400440116243', + 'GB29NWBK60161331926819', + ], + invalid: [ + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + ], }); test({ - validator: 'isBefore', - args: ['invalid date'], - invalid: ['invalid date', '1999-12-31'], + validator: 'isIBAN', + args: [{ whitelist: ['XX', 'AA'] }], + invalid: [ + 'DK5000400440116243', + 'GB29NWBK60161331926819', + 'BE71 0961 2345 6769', + 'FR76 3000 6000 0112 3456 7890 189', + 'DE91 1000 0000 0123 4567 89', + 'GR96 0810 0010 0000 0123 4567 890', + 'RO09 BCYP 0000 0012 3456 7890', + 'SA44 2000 0001 2345 6789 1234', + 'ES79 2100 0813 6101 2345 6789', + 'XX22YYY1234567890123', + 'FR14 2004 1010 0505 0001 3', + 'FR7630006000011234567890189@', + 'FR7630006000011234567890189😅', + 'FR763000600001123456!!🤨7890189@', + ], }); - }); - - it('should validate IBAN', () => { test({ validator: 'isIBAN', + args: [{ blacklist: ['IT'] }], valid: [ 'SC52BAHL01031234567890123456USD', 'LC14BOSL123456789012345678901234', @@ -5004,7 +6256,6 @@ describe('Validators', () => { 'CH56 0483 5012 3456 7800 9', 'GB98 MIDL 0700 9312 3456 78', 'IL170108000000012612345', - 'IT60X0542811101000000123456', 'JO71CBJO0000000000001234567890', 'TR320010009999901234567890', 'BR1500000000000010932840814P2', @@ -5018,6 +6269,7 @@ describe('Validators', () => { 'FR7630006000011234567890189@', 'FR7630006000011234567890189😅', 'FR763000600001123456!!🤨7890189@', + 'IT60X0542811101000000123456', ], }); }); @@ -5030,6 +6282,7 @@ describe('Validators', () => { 'SBICKEN1', 'SBICKENY', 'SBICKEN1YYP', + 'SBICXKN1YYP', ], invalid: [ 'SBIC23NXXX', @@ -5038,6 +6291,7 @@ describe('Validators', () => { 'SBICKENXX9', 'SBICKEN13458', 'SBICKEN', + 'SBICXK', ], }); }); @@ -5058,78 +6312,393 @@ describe('Validators', () => { }); }); - it('should validate luhn numbers', () => { + 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', + valid: [ + '375556917985515', + '36050234196908', + '4716461583322103', + '4716-2210-5188-5662', + '4929 7226 5379 7141', + '5398228707871527', + '6283875070985593', + '6263892624162870', + '6234917882863855', + '6234698580215388', + '6226050967750613', + '6246281879460688', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '6765780016990268', + '4716989580001715211', + '8171999927660000', + '8171999900000000021', + ], + invalid: [ + 'foo', + 'foo', + '5398228707871528', + '2718760626256571', + '2721465526338453', + '2220175103860763', + '375556917985515999999993', + '899999996234917882863855', + 'prefix6234917882863855', + '623491788middle2863855', + '6234917882863855suffix', + '4716989580001715213', + ], + }); + }); + + + it('should validate credit cards without a proper provider', () => { + test({ + validator: 'isCreditCard', + args: [{ provider: 'Plorf' }], + error: [ + 'foo', + // valid cc # + '375556917985515', + '4716-2210-5188-5662', + '375556917985515999999993', + '6234917882863855suffix', + ], + }); + }); + + + it('should validate AmEx provided credit cards', () => { + test({ + validator: 'isCreditCard', + args: [{ provider: 'AmEx' }], + valid: [ + '375556917985515', + ], + invalid: [ + 'foo', + '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 Diners Club provided credit cards', () => { + test({ + validator: 'isCreditCard', + args: [{ provider: 'DinersClub' }], + valid: [ + '36050234196908', + ], + invalid: [ + '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 Discover provided credit cards', () => { + test({ + validator: 'isCreditCard', + args: [{ provider: 'Discover' }], + valid: [ + '6011111111111117', + '6011000990139424', + ], + invalid: [ + '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 JCB provided credit cards', () => { + test({ + validator: 'isCreditCard', + args: [{ provider: 'JCB' }], + valid: [ + '3530111333300000', + '3566002020360505', + ], + invalid: [ + '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: 'isCreditCard', + args: [{ provider: 'Mastercard' }], + valid: [ + '2222155765072228', + '2225855203075256', + '2718760626256570', + '2720428011723762', + '5398228707871527', + ], + invalid: [ + '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 Union Pay provided credit cards', () => { test({ - validator: 'isLuhnValid', + validator: 'isCreditCard', + args: [{ provider: 'UnionPay' }], valid: [ - '0', - '5421', - '01234567897', - '0123456789012345678906', - '0123456789012345678901234567891', - '123456789012345678906', - '375556917985515', + '6226050967750613', + '6234917882863855', + '6234698580215388', + '6246281879460688', + '6263892624162870', + '6283875070985593', + '6765780016990268', + '8171999927660000', + '8171999900000000021', + ], + invalid: [ + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', '36050234196908', + '375556917985515', + '375556917985515999999993', '4716461583322103', '4716-2210-5188-5662', + '4716989580001715211', '4929 7226 5379 7141', - ], - invalid: [ - '', - '1', - '5422', - 'foo', - 'prefix6234917882863855', - '623491788middle2863855', + '5398228707871527', '6234917882863855suffix', ], }); }); - it('should validate credit cards', () => { + + it('should validate Visa provided credit cards', () => { test({ validator: 'isCreditCard', + args: [{ provider: 'Visa' }], valid: [ - '375556917985515', - '36050234196908', - '4716461583322103', '4716-2210-5188-5662', + '4716461583322103', + '4716989580001715211', '4929 7226 5379 7141', + ], + invalid: [ + 'foo', + '2222155765072228', + '2225855203075256', + '2720428011723762', + '2718760626256570', + '36050234196908', + '375556917985515', + '375556917985515999999993', '5398228707871527', + '6234917882863855suffix', '6283875070985593', '6263892624162870', '6234917882863855', '6234698580215388', '6226050967750613', '6246281879460688', - '2222155765072228', - '2225855203075256', - '2720428011723762', - '2718760626256570', + '6283875070985593', '6765780016990268', - '4716989580001715211', '8171999927660000', '8171999900000000021', ], - invalid: [ - 'foo', - 'foo', - '5398228707871528', - '2718760626256571', - '2721465526338453', - '2220175103860763', - '375556917985515999999993', - '899999996234917882863855', - 'prefix6234917882863855', - '623491788middle2863855', - '6234917882863855suffix', - '4716989580001715213', - ], }); }); + it('should validate identity cards', () => { const fixtures = [ + { + locale: 'PK', + valid: [ + '45504-4185771-3', + '39915-6182971-9', + '21143-6182971-2', + '34543-2323471-1', + '72345-2345678-7', + '63456-8765432-8', + '55672-1234567-5', + '21234-9876543-6', + ], + invalid: [ + '08000-1234567-5', + '74321-87654321-1', + '51234-98765-2', + '00000-0000000-0', + '88888-88888888-0', + '99999-9999999-9', + '11111', + ], + }, + { + locale: 'zh-HK', + valid: [ + 'OV290326[A]', + 'Q803337[0]', + 'Z0977986', + 'W520128(7)', + 'A494866[4]', + 'A494866(4)', + 'Z867821A', + 'ag293013(9)', + 'k348609(5)', + ], + invalid: [ + 'A1234567890', + '98765432', + 'O962472(9)', + 'M4578601', + 'X731324[8]', + 'C503134(5)', + 'RH265886(3)', + ], + }, { locale: 'LK', valid: [ @@ -5522,58 +7091,6 @@ describe('Validators', () => { }); }); - 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', - ], - }); - }); - it('should validate EANs', () => { test({ validator: 'isEAN', @@ -5952,76 +7469,6 @@ describe('Validators', () => { }); }); - 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({ @@ -6064,20 +7511,39 @@ describe('Validators', () => { { locale: 'am-AM', valid: [ - '+37410324123', - '+37422298765', - '+37431276521', - '022698763', - '37491987654', - '+37494567890', + '+37433123456', + '+37441123456', + '+37443123456', + '+37444123456', + '+37455123456', + '+37477123456', + '+37488123456', + '+37491123456', + '+37493123456', + '+37494123456', + '+37495123456', + '+37496123456', + '+37498123456', + '+37499123456', + '055123456', + '37455123456', ], invalid: [ '12345', - '+37411498855', - '+37411498123', + '+37403498855', + '+37416498123', '05614988556', '', '37456789000', + '37486789000', + '+37431312345', + '+37430312345', + '+37460123456', + '+37410324123', + '+37422298765', + '+37431276521', + '022698763', + '+37492123456', ], }, { @@ -6194,9 +7660,11 @@ describe('Validators', () => { '96550000000', '96560000000', '96590000000', + '96541000000', '+96550000000', '+96550000220', '+96551111220', + '+96541000000', ], invalid: [ '+96570000220', @@ -6207,6 +7675,7 @@ describe('Validators', () => { '+9639626626262', '+963332210972', '0114152198', + '+96540000000', ], }, { @@ -6309,6 +7778,7 @@ describe('Validators', () => { locale: 'ar-OM', valid: [ '+96891212121', + '+96871212121', '0096899999999', '93112211', '99099009', @@ -6378,6 +7848,24 @@ describe('Validators', () => { '0114152198', ], }, + { + locale: 'ar-SD', + valid: [ + '0128652312', + '+249919425113', + '249123212345', + '0993212345', + ], + invalid: [ + '12345', + '', + '+249972662622', + '+24946266262', + '+24933221097', + '0614152198', + '096554', + ], + }, { locale: 'ar-TN', valid: [ @@ -6731,7 +8219,7 @@ describe('Validators', () => { ], }, { - local: 'en-LS', + locale: 'en-LS', valid: [ '+26622123456', '+26628123456', @@ -6767,6 +8255,7 @@ describe('Validators', () => { '+4418970973', '', '+1441897465', + '+1441897465 additional invalid string part', ], }, { @@ -6861,6 +8350,7 @@ describe('Validators', () => { '0502345671', '0242345671', '0542345671', + '0532345671', '0272345671', '0572345671', '0262345671', @@ -6871,6 +8361,7 @@ describe('Validators', () => { '+233502345671', '+233242345671', '+233542345671', + '+233532345671', '+233272345671', '+233572345671', '+233262345671', @@ -7004,6 +8495,31 @@ describe('Validators', () => { '+254800723845', ], }, + { + locale: 'fr-CF', + valid: [ + '+23670850000', + '+23675038756', + '+23677859002', + '+23672854202', + '+23621854052', + '+23622854072', + '72234650', + '70045902', + '77934567', + '21456794', + '22452389', + ], + invalid: [ + '+23689032', + '123456789', + '+236723845987', + '022452389', + '+236772345678', + '+236700456794', + + ], + }, { locale: 'en-KI', valid: [ @@ -7101,6 +8617,25 @@ describe('Validators', () => { '+255800723845', ], }, + { + locale: 'en-MW', + valid: [ + '+265994563785', + '+265111785436', + '+265318596857', + '0320008744', + '01256258', + '0882541896', + '+265984563214', + ], + invalid: [ + '58563', + '+2658256258', + '0896328741', + '0708574896', + '+26570857489635', + ], + }, { locale: 'es-PE', valid: [ @@ -7199,6 +8734,24 @@ describe('Validators', () => { '11435213543', ], }, + { + locale: 'fr-CD', + valid: [ + '+243818590432', + '+243893875610', + '243978590234', + '0813346543', + '0820459022', + '+243902590221', + ], + invalid: [ + '243', + '+254818590432', + '+24389032', + '123456789', + '+243700723845', + ], + }, { locale: 'fr-GF', valid: [ @@ -7321,6 +8874,32 @@ describe('Validators', () => { '6898912345', ], }, + { + locale: 'fr-WF', + valid: [ + '+681408500', + '+681499387', + '+681728590', + '+681808542', + '+681828540', + '+681832014', + '408500', + '499387', + '728590', + '808542', + '828540', + '832014', + ], + invalid: [ + '+68189032', + '123456789', + '+681723845987', + '022452389', + '+681772345678', + '+681700456794', + + ], + }, { locale: 'ka-GE', valid: [ @@ -7347,8 +8926,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', @@ -7358,6 +8951,12 @@ describe('Validators', () => { '68129485729', '6589394827', '298RI89572', + '6924567890', + '6964567890', + '6844567890', + '690456789', + '00690456789', + 'not a number', ], }, { @@ -7374,6 +8973,11 @@ describe('Validators', () => { '+3599148725', '96537247', '3596676533', + '+35795123455', + '+35797012204', + '35799123456', + '+35794123456', + '+35796123456', ], invalid: [ '', @@ -7402,6 +9006,9 @@ describe('Validators', () => { '+443003434751', '05073456754', '08001123123', + '07043425232', + '01273884231', + '03332654034', ], }, { @@ -7474,6 +9081,8 @@ describe('Validators', () => { '+260966684590', '+260976684590', '260976684590', + '+260779493521', + '+260760010936', ], invalid: [ '12345', @@ -7481,6 +9090,7 @@ describe('Validators', () => { 'Vml2YW11cyBmZXJtZtesting123', '010-38238383', '966684590', + '760010936', ], }, { @@ -7833,6 +9443,7 @@ describe('Validators', () => { '', 'abc', '+535123457', + '56043029304', ], }, { @@ -7866,6 +9477,10 @@ describe('Validators', () => { '+50489234567', '+50488987896', '+50497567389', + '+50427367389', + '+50422357389', + '+50431257389', + '+50430157389', ], invalid: [ '12345', @@ -8089,6 +9704,7 @@ describe('Validators', () => { '+48 56 6572724', '+48 67 621 5461', '48 67 621 5461', + '+48 45 621 5461', ], invalid: [ '+48 67 621 5461', @@ -8099,6 +9715,7 @@ describe('Validators', () => { '1800-88-8687', '+6019-5830837', '357562855', + '+48 44 621 5461', ], }, { @@ -8205,6 +9822,8 @@ describe('Validators', () => { '088-261987', '1800-88-8687', '088-320000', + '+01112353576', + '+0111419752', ], }, { @@ -8254,15 +9873,44 @@ describe('Validators', () => { { locale: 'ky-KG', valid: [ - '+7 727 123 4567', - '+7 714 2396102', - '77271234567', - '0271234567', - ], - invalid: [ - '02188565377', - '09386932778', - '0938693277vadggjdsaasdgj8', + '+996553033300', + '+996 222 123456', + '+996 500 987654', + '+996 555 111222', + '+996 700 333444', + '+996 770 555666', + '+996 880 777888', + '+996 990 999000', + '+996 995 555666', + '+996 996 555666', + '+996 997 555666', + '+996 998 555666', + ], + invalid: [ + '+996 201 123456', + '+996 312 123456', + '+996 3960 12345', + '+996 3961 12345', + '+996 3962 12345', + '+996 3963 12345', + '+996 3964 12345', + '+996 3965 12345', + '+996 3966 12345', + '+996 3967 12345', + '+996 3968 12345', + '+996 511 123456', + '+996 522 123456', + '+996 561 123456', + '+996 571 123456', + '+996 624 123456', + '+996 623 123456', + '+996 622 123456', + '+996 609 123456', + '+996 100 12345', + '+996 100 1234567', + '996 100 123456', + '0 100 123456', + '0 100 123abc', ], }, { @@ -8442,6 +10090,36 @@ describe('Validators', () => { '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: [ @@ -8468,6 +10146,8 @@ describe('Validators', () => { '740123456', '+40640123456', '+40210123456', + '+0765351689', + '+0711419752', ], }, { @@ -8564,16 +10244,67 @@ describe('Validators', () => { { locale: 'uk-UA', valid: [ - '+380982345679', - '380982345679', - '80982345679', - '0982345679', + '+380501234567', + '+380631234567', + '+380661234567', + '+380671234567', + '+380681234567', + '+380731234567', + '+380751234567', + '+380771234567', + '+380911234567', + '+380921234567', + '+380931234567', + '+380941234567', + '+380951234567', + '+380961234567', + '+380971234567', + '+380981234567', + '+380991234567', + '380501234567', + '380631234567', + '380661234567', + '380671234567', + '380681234567', + '380731234567', + '380751234567', + '380771234567', + '380911234567', + '380921234567', + '380931234567', + '380941234567', + '380951234567', + '380961234567', + '380971234567', + '380981234567', + '380991234567', + '0501234567', + '0631234567', + '0661234567', + '0671234567', + '0681234567', + '0731234567', + '0751234567', + '0771234567', + '0911234567', + '0921234567', + '0931234567', + '0941234567', + '0951234567', + '0961234567', + '0971234567', + '0981234567', + '0991234567', ], invalid: [ '+30982345679', + '+380321234567', + '+380441234567', '982345679', + '80982345679', '+380 98 234 5679', '+380-98-234-5679', + '+380 (98) 234-56-79', '', 'ASDFGJKLmZXJtZtesting123', '123456', @@ -8748,35 +10479,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: [ @@ -8874,18 +10576,51 @@ describe('Validators', () => { 'NotANumber', ], }, + { + locale: 'so-SO', + valid: [ + '+252601234567', + '+252650101010', + '+252794567120', + '252650647388', + '252751234567', + '0601234567', + '0609876543', + ], + invalid: [ + '', + 'not a number', + '+2526012345678', + '25260123456', + '+252705555555', + '+0601234567', + '06945454545', + ], + }, { locale: 'sq-AL', valid: [ - '067123456', - '+35567123456', + '0621234567', + '0661234567', + '0671234567', + '0681234567', + '0691234567', + '+355621234567', + '+355651234567', + '+355661234567', + '+355671234567', + '+355681234567', + '+355691234567', ], invalid: [ '67123456', '06712345', + '067123456', '06712345678', - '065123456', - '057123456', + '0571234567', + '+3556712345', + '+35565123456', + '+35157123456', 'NotANumber', ], }, @@ -8906,10 +10641,12 @@ describe('Validators', () => { locale: 'pt-AO', valid: [ '+244911123432', - '+244123091232', + '911123432', + '244911123432', ], invalid: [ '+2449111234321', + '+244811123432', '31234', '31234567', '512345', @@ -9071,6 +10808,27 @@ describe('Validators', () => { '+12352643456', ], }, + { + locale: 'en-KN', + valid: [ + '+18694699040', + '18694699040', + '+18697652917', + '18697652917', + '18694658472', + '+18696622969', + '+18694882224', + ], + invalid: [ + '', + '+18694238545', + '+1 8694882224', + '8694658472', + '+186946990', + '+1869469904', + '1869469904', + ], + }, { locale: 'en-PK', valid: [ @@ -9115,18 +10873,30 @@ describe('Validators', () => { { locale: 'dv-MV', valid: [ - '+960973256874', - '781246378', - '+960766354789', - '+960912354789', + '+9609112345', + '+9609958973', + '+9607258963', + '+9607958463', + '9609112345', + '9609958973', + '9607212963', + '9607986963', + '9112345', + '9958973', + '7258963', + '7958963', ], invalid: [ '+96059234567', '+96045789', '7812463784', - '+960706985478', - '+960926985478', 'NotANumber', + '+9607112345', + '+9609012345', + '+609012345', + '+96071123456', + '3412345', + '9603412345', ], }, { @@ -9189,6 +10959,88 @@ describe('Validators', () => { '00 +503 1234 5678', ], }, + { + locale: 'en-SS', + valid: [ + '+211928530422', + '+211913384561', + '+211972879174', + '+211952379334', + '0923346543', + '0950459022', + '0970934567', + '211979841238', + '211929843238', + '211959840238', + ], + invalid: [ + '911', + '+211999', + '123456789909', + 'South Sudan', + '21195 840 238', + '+211981234567', + '+211931234567', + '+211901234567', + '+211991234567', + + ], + }, + { + locale: 'es-GT', + valid: [ + '+50221234567', + '+50277654321', + '50226753421', + '50272332468', + '50278984455', + '+50273472492', + '71234567', + '21132398', + ], + invalid: [ + '44', + '+5022712345678', + '1234567899', + '502712345678', + 'This should fail', + '5021931234567', + '+50281234567', + ], + }, + { + locale: 'mk-MK', + valid: [ + '+38923234567', + '38931234567', + '022123456', + '22234567', + '71234567', + '31234567', + '+38923091500', + '80091234', + '81123456', + '54123456', + ], + invalid: [ + '38912345678', + '+389123456789', + '21234567', + '123456789', + '+3891234567', + '700012345', + '510123456', + 'This should fail', + '+389123456', + '389123456', + '80912345', + ], + }, + { + locale: 'ar-QA', + valid: ['+97435551234', '+97455551234', '+97465551234', '+97475551234', '35551234', '55551234', '65551234', '75551234'], + invalid: ['+97445551234', '+97405551234', '+9745555123', '+974555512345', '+97355551234', '+9125551234', '25551234', '+13005551234', '45551234', '95551234', '+9745555abcd', '', '+974'], + }, ]; let allValid = []; @@ -10242,19 +12094,34 @@ describe('Validators', () => { validator: 'isBtcAddress', valid: [ '1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL', + 'mucFNhKMYoBQYUAEsrFVscQ1YaFQPekBpg', '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy', + '2NFUBBRcTJbYc1D4HSCbJhKZp6YCV4PQFpQ', 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq', '14qViLJfdGaP4EeHnDyJbEGQysnCpwk3gd', '35bSzXvRKLpHsHMrzb82f617cV4Srnt7hS', '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhemt', 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', + 'tb1qxhkl607frtvjsy9nlyeg03lf6fsq947pl2pe82', + 'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297', + 'tb1pzpelffrdh9ptpaqnurwx30dlewqv57rcxfeetp86hsssk30p4cws38tr9y', ], invalid: [ + '3J98t1WpEZ73CNmQviecrnyiWrnqh0WNL0', + '3J98t1WpEZ73CNmQviecrnyiWrnqh0WNLo', + '3J98t1WpEZ73CNmQviecrnyiWrnqh0WNLI', + '3J98t1WpEZ73CNmQviecrnyiWrnqh0WNLl', '4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy', '0x56F0B8A998425c53c75C4A303D4eF987533c5597', 'pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g', '17VZNX1SN5NlKa8UQFxwQbFeFc3iqRYhem', 'BC1QW508D6QEJXTDG4Y5R3ZARVAYR0C5XW7KV8F3T4', + 'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3291', + 'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg329b', + 'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg329i', + 'bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg329o', + 'BC1P5D7RJQ7G6RDK2YHZKS9SMLAQTEDR4DEKQ08GE8ZTWAC72SFR9RUSXG3297', + 'TB1PZPELFFRDH9PTPAQNURWX30DLEWQV57RCXFEETP86HSSSK30P4CWS38TR9Y', ], }); }); @@ -10542,6 +12409,28 @@ describe('Validators', () => { }); }); + it('should validate ISO 15924 script codes', () => { + test({ + validator: 'isISO15924', + valid: [ + 'Adlm', + 'Bass', + 'Copt', + 'Dsrt', + 'Egyd', + 'Latn', + 'Zzzz', + ], + invalid: [ + '', + 'arab', + 'zzzz', + 'Qaby', + 'Lati', + ], + }); + }); + it('should validate RFC 3339 dates', () => { test({ validator: 'isRFC3339', @@ -10634,6 +12523,37 @@ describe('Validators', () => { }); }); + it('should validate ISO 3166-1 numeric country codes', () => { + // from https://en.wikipedia.org/wiki/ISO_3166-1_numeric + test({ + validator: 'isISO31661Numeric', + valid: [ + '076', + '208', + '276', + '348', + '380', + '410', + '440', + '528', + '554', + '826', + ], + invalid: [ + '', + 'NL', + 'NLD', + '002', + '197', + '249', + '569', + '810', + '900', + '999', + ], + }); + }); + it('should validate ISO 4217 corrency codes', () => { // from https://en.wikipedia.org/wiki/ISO_4217 test({ @@ -10642,13 +12562,16 @@ describe('Validators', () => { 'AED', 'aed', 'AUD', - 'CUC', + 'CUP', 'EUR', 'GBP', 'LYD', 'MYR', 'SGD', + 'SLE', 'USD', + 'VED', + 'SLE', ], invalid: [ '', @@ -10660,6 +12583,8 @@ describe('Validators', () => { 'RWA', 'EURO', 'euro', + 'HRK', + 'CUC', ], }); }); @@ -10687,6 +12612,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==', @@ -10845,7 +12771,41 @@ describe('Validators', () => { '2017', '0800', ], - }, { + }, + { + locale: 'BD', + valid: [ + '1000', + '1200', + '1300', + '1400', + '1500', + '2000', + '3000', + '4000', + '5000', + '6000', + '7000', + '8000', + '9000', + '9400', + '9499', + ], + invalid: [ + '0999', + '9500', + '10000', + '12345', + '123', + '123456', + 'abcd', + '123a', + 'a123', + '12 34', + '12-34', + ], + }, + { locale: 'BY', valid: [ '225320', @@ -10853,6 +12813,12 @@ describe('Validators', () => { '247710', '231960', ], + invalid: [ + 'test 225320', + '211120 test', + '317543', + '267946', + ], }, { locale: 'CA', @@ -10881,6 +12847,21 @@ describe('Validators', () => { 'Z1A 0B1', ], }, + { + locale: 'CO', + valid: [ + '050034', + '110221', + '441029', + '910001', + ], + invalid: [ + '11001', + '000000', + '109999', + '329999', + ], + }, { locale: 'ES', valid: [ @@ -10939,10 +12920,16 @@ describe('Validators', () => { locale: 'FR', valid: [ '75008', + '44522', + '38499', + '39940', + '01000', + ], + invalid: [ '44 522', - '98025', '38 499', - '39940', + '96000', + '98025', ], }, { @@ -11038,6 +13025,9 @@ describe('Validators', () => { '43516 6456', '123443516 6456', '891123', + 'test 4351666456', + '4351666456 test', + 'test 4351666456 test', ], }, { @@ -11059,6 +13049,13 @@ describe('Validators', () => { '3950IO', '3997 GH', ], + invalid: [ + '1234', + '0603 JV', + '5194SA', + '9164 SD', + '1841SS', + ], }, { locale: 'NP', @@ -11096,6 +13093,7 @@ describe('Validators', () => { '399', '935', '38842', + '546023', ], }, { @@ -11155,6 +13153,9 @@ describe('Validators', () => { '39100-000', '22040-020', '39400-152', + '39100000', + '22040020', + '39400152', ], invalid: [ '79800A12', @@ -11175,6 +13176,22 @@ describe('Validators', () => { '4144', ], }, + { + locale: 'PK', + valid: [ + '25000', + '44000', + '54810', + '74200', + ], + invalid: [ + '5400', + '540000', + 'NY540', + '540CA', + '540-0', + ], + }, { locale: 'MG', valid: [ @@ -11392,6 +13409,7 @@ describe('Validators', () => { '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', @@ -11428,6 +13446,107 @@ describe('Validators', () => { }); }); + + it('should validate ISO6346 shipping containerID', () => { + test({ + validator: 'isISO6346', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + 'QJRZ123456', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + 'AFFU5962593', + ], + }); + }); + it('should validate ISO6346 shipping containerID', () => { + test({ + validator: 'isFreightContainerID', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + 'QJRZ123456', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + 'AFFU5962593', + ], + }); + }); + + it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => { + test({ + validator: 'isISO6346', + valid: [ + 'APZU3789870', + 'TEMU1002030', + 'DFSU1704420', + 'CMAU2221480', + 'SEGU5060260', + 'FCIU8939320', + 'TRHU3495670', + 'MEDU3871410', + 'CMAU2184010', + 'TCLU2265970', + ], + invalid: [ + 'APZU3789871', // Incorrect check digit + 'TEMU1002031', + 'DFSU1704421', + 'CMAU2221481', + 'SEGU5060261', + ], + }); + }); + it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => { + test({ + validator: 'isFreightContainerID', + valid: [ + 'APZU3789870', + 'TEMU1002030', + 'DFSU1704420', + 'CMAU2221480', + 'SEGU5060260', + 'FCIU8939320', + 'TRHU3495670', + 'MEDU3871410', + 'CMAU2184010', + 'TCLU2265970', + ], + invalid: [ + 'APZU3789871', // Incorrect check digit + 'TEMU1002031', + 'DFSU1704421', + 'CMAU2221481', + 'SEGU5060261', + ], + }); + }); + // 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({ @@ -11643,6 +13762,27 @@ describe('Validators', () => { '28-1234567', '96-1234567'], }); + test({ + validator: 'isTaxID', + args: ['es-AR'], + valid: [ + '20271633638', + '23274986069', + '27333234519', + '30678561165', + '33693450239', + '30534868460', + '23111111129', + '34557619099'], + invalid: [ + '20-27163363-8', + '20.27163363.8', + '33693450231', + '69345023', + '693450233123123', + '3369ew50231', + '34557619095'], + }); test({ validator: 'isTaxID', args: ['es-ES'], @@ -11952,6 +14092,19 @@ describe('Validators', () => { '19640823-32333', '1964082332333'], }); + test({ + validator: 'isTaxID', + args: ['uk-UA'], + valid: [ + '3006321856', + '3003102490', + '2164212906'], + invalid: [ + '2565975632', + '256597563287', + 'КС00123456', + '2896235845'], + }); test({ validator: 'isTaxID', valid: [ @@ -12016,6 +14169,8 @@ describe('Validators', () => { 'mxH_+2vs&54_+H3P', '+&DxJ=X7-4L8jRCD', 'etV*p%Nr6w&H%FeF', + '£3.ndSau_7', + 'VaLIDWith\\Symb0l', ], invalid: [ '', @@ -12029,37 +14184,6 @@ describe('Validators', () => { }); }); - 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', @@ -12068,6 +14192,7 @@ describe('Validators', () => { new Date([2014, 2, 15]), new Date('2014-03-15'), '2020/02/29', + '2020-02-19', ], invalid: [ '', @@ -12081,6 +14206,20 @@ describe('Validators', () => { '2019-02-29', // non-leap year '2020-04-31', // invalid date '2020/03-15', // mixed delimiter + '-2020-04-19', + '-2023/05/24', + 'abc-2023/05/24', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', ], }); test({ @@ -12096,6 +14235,21 @@ describe('Validators', () => { '15/7/02', '15-7-02', '15-07/2002', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '15/', + '15/07', + '15/07/', + '15/07/2024/', ], }); test({ @@ -12111,6 +14265,21 @@ describe('Validators', () => { '15/7/02', '15-7-02', '15-07/2002', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '15/', + '15/07', + '15/07/', + '15/07/2024/', ], }); test({ @@ -12124,6 +14293,23 @@ describe('Validators', () => { '15/7/2002', '15-7-2002', '15/07-02', + '30/04/--', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '15/', + '15/07', + '15/07/', + '15/07/2024/', + '15/07/24/', ], }); test({ @@ -12138,6 +14324,17 @@ describe('Validators', () => { '15/7/02', '15-7-02', '5/7-02', + '3/4/aa', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '15/', + '15/07', + '15/07/', + '15/07/2024/', + '15/07/24/', ], }); test({ @@ -12153,6 +14350,16 @@ describe('Validators', () => { '15/7/02', '15-7-02', '15-07/2002', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '15/', + '15/07', + '15/07/', + '15/07/2024/', + '15/07/24/', ], }); test({ @@ -12171,6 +14378,20 @@ describe('Validators', () => { new Date(), new Date([2014, 2, 15]), new Date('2014-03-15'), + '-2020-04-19', + '-2023/05/24', + 'abc-2023/05/24', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', ], }); test({ @@ -12196,6 +14417,21 @@ describe('Validators', () => { '2019/02/29', '2020/04/31', '2020/03-15', + '-2020-04-19', + '-2023/05/24', + 'abc-2023/05/24', + '2024', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01-', + '2024-05-01-abc', + '2024/', + '2024/05', + '2024/05/', + '2024/05/01/', + '2024/05/01/abc', + '2024 05 01 abc', ], }); test({ @@ -12215,25 +14451,261 @@ describe('Validators', () => { new Date([2014, 2, 15]), new Date('2014-03-15'), '29.02.2020', + '02.29.2020.20', + '2024-', + '2024-05', + '2024-05-', + '2024-05-01', + '-2020-04-19', + '-2023/05/24', + 'abc-2023/05/24', + '04.05.2024.', + '04.05.2024.abc', + 'abc.04.05.2024', + ], + }); + // emulating Pacific time zone offset & time + // which could potentially result in UTC conversion issues + timezone_mock.register('US/Pacific'); + test({ + validator: 'isDate', + valid: [ + new Date(2016, 2, 29), + '2017-08-04', + ], + }); + timezone_mock.unregister(); + }); + 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: 'hour24', mode: 'withOptionalSeconds' }], + valid: [ + '23:59:59', + '00:00:00', + '9:50:01', + '00:00', + '23:59', + '9:00', + ], + 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', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour12', mode: 'withOptionalSeconds' }], + valid: [ + '12:59:59 PM', + '2:34:45 AM', + '7:00:00 AM', + '12:59 PM', + '12:59 AM', + '01:00 PM', + '01:00 AM', + '7:00 AM', + ], + invalid: [ + '', + null, + undefined, + 23, + '01:00: 1 PM', + '13:00:', + '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-AA-34', + '12-34-AA', + 'AA-12-AA', + 'AA·12·34', '12·AB·34', + '12·34·AB', + 'AB·34·AB', + 'AA 12 34', + '12 AA 34', + '12 34 AA', 'AB 12 CD', + 'AA1234', + '12AA34', + '1234AA', 'AB12CD', ], invalid: [ '', 'notalicenseplate', + 'AA-AA-00', + '00-AA-AA', + 'AA-AA-AA', + '00-00-00', + 'AA·AA·00', + '00·AA·AA', + 'AA·AA·AA', + '00·00·00', + 'AA AA 00', + '00 AA AA', + 'AA AA AA', + '00 00 00', 'A1-B2-C3', + '1A-2B-3C', 'ABC-1-EF', + 'AB-C1D-EF', + 'AB-C1-DEF', ], }); test({ @@ -12397,6 +14869,76 @@ describe('Validators', () => { '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'], @@ -12472,6 +15014,22 @@ describe('Validators', () => { ], invalid: ['mh04ad0045', 'invalidlicenseplate', '4578', '', 'GJ054GH4785'], }); + test({ + validator: 'isLicensePlate', + args: ['en-SG'], + valid: [ + 'SGX 1234 A', + 'SGX-1234-A', + 'SGB1234Z', + ], + invalid: [ + 'sg1234a', + 'invalidlicenseplate', + '4578', + '', + 'GJ054GH4785', + ], + }); }); it('should validate VAT numbers', () => { test({ @@ -12593,11 +15151,18 @@ describe('Validators', () => { args: ['FR'], valid: [ 'FRAA123456789', - 'AA123456789', + 'FR83404833048', + 'FR40123456789', + 'FRA1123456789', + 'FR1A123456789', ], invalid: [ 'FR AA123456789', '123456789', + 'FRAA123456789A', + 'FR123456789', + 'FR 83404833048', + 'FRaa123456789', ], }); test({ @@ -12850,10 +15415,24 @@ describe('Validators', () => { validator: 'isVAT', args: ['AU'], valid: [ + 'AU53004085616', + '53004085616', + 'AU65613309809', + '65613309809', + 'AU34118972998', + '34118972998', + ], + invalid: [ + 'AU65613309808', + '65613309808', + 'AU55613309809', + '55613309809', + 'AU65613319809', + '65613319809', + 'AU34117972998', + '34117972998', 'AU12345678901', '12345678901', - ], - invalid: [ 'AU 12345678901', '1234567890', ], @@ -12936,11 +15515,11 @@ describe('Validators', () => { validator: 'isVAT', args: ['KZ'], valid: [ - 'KZ123456789', - '123456789', + 'KZ123456789012', + '123456789012', ], invalid: [ - 'KZ 123456789', + 'KZ 123456789012', '12345678', ], }); @@ -13050,18 +15629,30 @@ describe('Validators', () => { validator: 'isVAT', args: ['CH'], valid: [ - 'CH123456TVA', - '123456TVA', - 'CH123456789MWST', - '123456789MWST', - 'CH123.456IVA', - '123.456IVA', - 'CH123.456.789TVA', - '123.456.789TVA', - ], - invalid: [ - 'CH 123456', - '12345', + // strictly valid + 'CHE-116.281.710 MWST', + 'CHE-116.281.710 IVA', + 'CHE-116.281.710 TVA', + // loosely valid presentation variants + 'CHE 116 281 710 IVA', // all separators are spaces + 'CHE-191.398.369MWST', // no space before suffix + 'CHE-116281710 MWST', // no number separators + 'CHE-116281710MWST', // no number separators and no space before suffix + 'CHE105854263MWST', // no separators + 'CHE-116.285.524', // no suffix (vat abbreviation) + 'CHE116281710', // no suffix and separators + '116.281.710 TVA', // no prefix (CHE, ISO-3166-1 Alpha-3) + '116281710MWST', // no prefix and separators + '100.218.485', // no prefix and suffix + '123456788', // no prefix, separators and suffix + ], + invalid: [ + 'CH-116.281.710 MWST', // invalid prefix (should be CHE) + 'CHE-116.281 MWST', // invalid number of digits (should be 9) + 'CHE-123.456.789 MWST', // invalid last digit (should match the calculated check-number 8) + 'CHE-123.356.780 MWST', // invalid check-number (there are no swiss UIDs with the calculated check number 10) + 'CH-116.281.710 VAT', // invalid suffix (should be MWST, IVA or TVA) + 'CHE-116/281/710 IVA', // invalid number separators (should be all dots or all spaces) ], }); test({ @@ -13365,4 +15956,55 @@ describe('Validators', () => { ], }); }); + it('should validate mailto URI', () => { + test({ + validator: 'isMailtoURI', + valid: [ + 'mailto:?subject=something&cc=valid@mail.com', + 'mailto:?subject=something&cc=valid@mail.com,another@mail.com,', + 'mailto:?subject=something&bcc=valid@mail.com', + 'mailto:?subject=something&bcc=valid@mail.com,another@mail.com', + 'mailto:?bcc=valid@mail.com,another@mail.com', + 'mailto:?cc=valid@mail.com,another@mail.com', + 'mailto:?cc=valid@mail.com', + 'mailto:?bcc=valid@mail.com', + 'mailto:?subject=something&body=something else', + 'mailto:?subject=something&body=something else&cc=hello@mail.com,another@mail.com', + 'mailto:?subject=something&body=something else&bcc=hello@mail.com,another@mail.com', + 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com', + 'mailto:hello@mail.com', + 'mailto:info@mail.com?', + 'mailto:hey@mail.com?subject=something', + 'mailto:info@mail.com?subject=something&cc=valid@mail.com', + 'mailto:info@mail.com?subject=something&cc=valid@mail.com,another@mail.com,', + 'mailto:info@mail.com?subject=something&bcc=valid@mail.com', + 'mailto:info@mail.com?subject=something&bcc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?bcc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?cc=valid@mail.com,another@mail.com', + 'mailto:info@mail.com?cc=valid@mail.com', + 'mailto:info@mail.com?bcc=valid@mail.com&', + 'mailto:info@mail.com?subject=something&body=something else', + 'mailto:info@mail.com?subject=something&body=something else&cc=hello@mail.com,another@mail.com', + 'mailto:info@mail.com?subject=something&body=something else&bcc=hello@mail.com,another@mail.com', + 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com', + 'mailto:', + ], + invalid: [ + '', + 'something', + 'valid@gmail.com', + 'mailto:?subject=okay&subject=444', + 'mailto:?subject=something&wrong=888', + 'mailto:somename@gmail.com', + 'mailto:hello@world.com?cc=somename@gmail.com', + 'mailto:hello@world.com?bcc=somename@gmail.com', + 'mailto:hello@world.com?bcc=somename@gmail.com&bcc', + 'mailto:valid@gmail.com?subject=anything&body=nothing&cc=&bcc=&key=', + 'mailto:hello@world.com?cc=somename', + 'mailto:somename', + 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&', + 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&', + ], + }); + }); }); diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js new file mode 100644 index 000000000..f0daf8a17 --- /dev/null +++ b/test/validators/isAfter.test.js @@ -0,0 +1,76 @@ +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'], + }); + test({ + validator: 'isAfter', + args: [], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); + test({ + validator: 'isAfter', + args: [undefined], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); + test({ + validator: 'isAfter', + args: [{ comparisonDate: undefined }], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); + }); + + 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/isBase64.test.js b/test/validators/isBase64.test.js new file mode 100644 index 000000000..c0074343a --- /dev/null +++ b/test/validators/isBase64.test.js @@ -0,0 +1,201 @@ +import { format } from 'util'; +import test from '../testFunctions'; +import validator from '../../src'; + +describe('isBase64', () => { + it('should validate base64 strings with default options', () => { + 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 standard Base64 with padding', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: false, padding: true }], + valid: [ + '', + 'TWFu', + 'TWE=', + 'TQ==', + 'SGVsbG8=', + 'U29mdHdhcmU=', + 'YW55IGNhcm5hbCBwbGVhc3VyZS4=', + ], + invalid: [ + 'TWF', + 'TWE===', + 'SGVsbG8@', + 'SGVsbG8===', + 'SGVsb G8=', + '====', + ], + }); + }); + + it('should validate standard Base64 without padding', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: false, padding: false }], + valid: [ + '', + 'TWFu', + 'TWE', + 'TQ', + 'SGVsbG8', + 'U29mdHdhcmU', + 'YW55IGNhcm5hbCBwbGVhc3VyZS4', + ], + invalid: [ + 'TWE=', + 'TQ===', + 'SGVsbG8@', + 'SGVsbG8===', + 'SGVsb G8', + '====', + ], + }); + }); + + it('should validate Base64url with padding', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: true, padding: true }], + valid: [ + '', + 'SGVsbG8=', + 'U29mdHdhcmU=', + 'YW55IGNhcm5hbCBwbGVhc3VyZS4=', + 'SGVsbG8-', + 'SGVsbG8_', + ], + invalid: [ + 'SGVsbG8===', + 'SGVsbG8@', + 'SGVsb G8=', + '====', + ], + }); + }); + + it('should validate Base64url without padding', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: true, padding: false }], + valid: [ + '', + 'SGVsbG8', + 'U29mdHdhcmU', + 'YW55IGNhcm5hbCBwbGVhc3VyZS4', + 'SGVsbG8-', + 'SGVsbG8_', + ], + invalid: [ + 'SGVsbG8=', + 'SGVsbG8===', + 'SGVsbG8@', + 'SGVsb G8', + '====', + ], + }); + }); + + it('should handle mixed cases correctly', () => { + test({ + validator: 'isBase64', + args: [{ urlSafe: false, padding: true }], + valid: [ + '', + 'TWFu', + 'TWE=', + 'TQ==', + ], + invalid: [ + 'TWE', + 'TQ=', + 'TQ===', + ], + }); + + test({ + validator: 'isBase64', + args: [{ urlSafe: true, padding: false }], + valid: [ + '', + 'SGVsbG8', + 'SGVsbG8-', + 'SGVsbG8_', + ], + invalid: [ + 'SGVsbG8=', + 'SGVsbG8@', + 'SGVsb G8', + ], + }); + }); +}); diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js new file mode 100644 index 000000000..298e5b410 --- /dev/null +++ b/test/validators/isBefore.test.js @@ -0,0 +1,119 @@ +import { describe } from 'mocha'; +import test from '../testFunctions'; + +describe('isBefore', () => { + describe('should validate dates a given end date', () => { + describe('new syntax', () => { + test({ + validator: 'isBefore', + args: [{ comparisonDate: '08/04/2011' }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: new Date(2011, 7, 4).toString() }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: 'invalid date' }], + invalid: ['invalid date', '1999-12-31'], + }); + }); + + describe('legacy syntax', () => { + test({ + validator: 'isBefore', + args: ['08/04/2011'], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [new Date(2011, 7, 4).toString()], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: ['2011-08-03'], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: ['invalid date'], + invalid: ['invalid date', '1999-12-31'], + }); + }); + }); + + describe('should validate dates a default end date', () => { + describe('new syntax', () => { + test({ + validator: 'isBefore', + valid: [ + '2000-08-04', + new Date(0).toString(), + new Date(Date.now() - 86400000).toString(), + ], + invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], + }); + test({ + validator: 'isBefore', + args: undefined, // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [undefined], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: undefined }], // will fall back to the current date + valid: ['1999-06-07'], + }); + }); + + describe('legacy syntax', () => { + test({ + validator: 'isBefore', + valid: [ + '2000-08-04', + new Date(0).toString(), + new Date(Date.now() - 86400000).toString(), + ], + invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], + }); + test({ + validator: 'isBefore', + args: undefined, // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [undefined], // will fall back to the current date + valid: ['1999-06-07'], + }); + }); + }); +}); diff --git a/test/validators/isFQDN.test.js b/test/validators/isFQDN.test.js new file mode 100644 index 000000000..134bab005 --- /dev/null +++ b/test/validators/isFQDN.test.js @@ -0,0 +1,26 @@ +import test from '../testFunctions'; + +describe('isFQDN', () => { + it('should validate domain names.', () => { + test({ + validator: 'isFQDN', + args: [], + valid: [ + 'google.com', + ], + invalid: [ + 'google.l33t', + ], + }); + test({ + validator: 'isFQDN', + args: [{ allow_numeric_tld: true }], + valid: [ + 'google.com', + 'google.l33t', + ], + invalid: [ + ], + }); + }); +}); diff --git a/test/validators/isIP.test.js b/test/validators/isIP.test.js new file mode 100644 index 000000000..62ebbd112 --- /dev/null +++ b/test/validators/isIP.test.js @@ -0,0 +1,304 @@ +import test from '../testFunctions'; + +describe('isIP', () => { + it('should validate IP addresses', () => { + test({ + validator: 'isIP', + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::1', + '2001:db8:0000:1:1:1:1:1', + '2001:db8:3:4::192.0.2.33', + '2001:41d0:2:a141::1', + '::ffff:127.0.0.1', + '::0000', + '0000::', + '1::', + '1111:1:1:1:1:1:1:1', + 'fe80::a6db:30ff:fe98:e946', + '::', + '::8', + '::ffff:127.0.0.1', + '::ffff:255.255.255.255', + '::ffff:0:255.255.255.255', + '::2:3:4:5:6:7:8', + '::255.255.255.255', + '0:0:0:0:0:ffff:127.0.0.1', + '1:2:3:4:5:6:7::', + '1:2:3:4:5:6::8', + '1::7:8', + '1:2:3:4:5::7:8', + '1:2:3:4:5::8', + '1::6:7:8', + '1:2:3:4::6:7:8', + '1:2:3:4::8', + '1::5:6:7:8', + '1:2:3::5:6:7:8', + '1:2:3::8', + '1::4:5:6:7:8', + '1:2::4:5:6:7:8', + '1:2::8', + '1::3:4:5:6:7:8', + '1::8', + 'fe80::7:8%eth0', + 'fe80::7:8%1', + '64:ff9b::192.0.2.33', + '0:0:0:0:0:0:10.0.0.1', + ], + invalid: [ + 'abc', + '256.0.0.0', + '0.0.0.256', + '26.0.0.256', + '0200.200.200.200', + '200.0200.200.200', + '200.200.0200.200', + '200.200.200.0200', + '::banana', + 'banana::', + '::1banana', + '::1::', + '1:', + ':1', + ':1:1:1::2', + '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1', + '::11111', + '11111:1:1:1:1:1:1:1', + '2001:db8:0000:1:1:1:1::1', + '0:0:0:0:0:0:ffff:127.0.0.1', + '0:0:0:0:ffff:127.0.0.1', + 'BC:e4d5:c:e7b9::%40i0nccymtl9cwfKo.5vaeXLSGRMe:EDh2qs5wkhnPws5xQKqafjfAMm6wGFCJ.bVFsZfb', + '1dC:0DF8:62D:3AC::%KTatXocjaFVioS0RTNQl4mA.V151o0RSy.JIu-D-D8.d3171ZWsSJ7PK4YjkJCRN0F', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: 'invalid version' }], + valid: [], + invalid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: null }], + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: undefined }], + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: 4 }], + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '255.0.0.1', + '0.0.1.1', + ], + invalid: [ + '::1', + '2001:db8:0000:1:1:1:1:1', + '::ffff:127.0.0.1', + '137.132.10.01', + '0.256.0.256', + '255.256.255.256', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: 6 }], + valid: [ + '::1', + '2001:db8:0000:1:1:1:1:1', + '::ffff:127.0.0.1', + 'fe80::1234%1', + 'ff08::9abc%10', + 'ff08::9abc%interface10', + 'ff02::5678%pvc1.3', + ], + invalid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::ffff:287.0.0.1', + '%', + 'fe80::1234%', + 'fe80::1234%1%3%4', + 'fe80%fe80%', + ], + }); + + test({ + validator: 'isIP', + args: [{ version: 10 }], + valid: [], + invalid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::1', + '2001:db8:0000:1:1:1:1:1', + ], + }); + }); + + describe('legacy syntax', () => { + it('should validate IP addresses', () => { + test({ + validator: 'isIP', + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::1', + '2001:db8:0000:1:1:1:1:1', + '2001:db8:3:4::192.0.2.33', + '2001:41d0:2:a141::1', + '::ffff:127.0.0.1', + '::0000', + '0000::', + '1::', + '1111:1:1:1:1:1:1:1', + 'fe80::a6db:30ff:fe98:e946', + '::', + '::8', + '::ffff:127.0.0.1', + '::ffff:255.255.255.255', + '::ffff:0:255.255.255.255', + '::2:3:4:5:6:7:8', + '::255.255.255.255', + '0:0:0:0:0:ffff:127.0.0.1', + '1:2:3:4:5:6:7::', + '1:2:3:4:5:6::8', + '1::7:8', + '1:2:3:4:5::7:8', + '1:2:3:4:5::8', + '1::6:7:8', + '1:2:3:4::6:7:8', + '1:2:3:4::8', + '1::5:6:7:8', + '1:2:3::5:6:7:8', + '1:2:3::8', + '1::4:5:6:7:8', + '1:2::4:5:6:7:8', + '1:2::8', + '1::3:4:5:6:7:8', + '1::8', + 'fe80::7:8%eth0', + 'fe80::7:8%1', + '64:ff9b::192.0.2.33', + '0:0:0:0:0:0:10.0.0.1', + ], + invalid: [ + 'abc', + '256.0.0.0', + '0.0.0.256', + '26.0.0.256', + '0200.200.200.200', + '200.0200.200.200', + '200.200.0200.200', + '200.200.200.0200', + '::banana', + 'banana::', + '::1banana', + '::1::', + '1:', + ':1', + ':1:1:1::2', + '1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1', + '::11111', + '11111:1:1:1:1:1:1:1', + '2001:db8:0000:1:1:1:1::1', + '0:0:0:0:0:0:ffff:127.0.0.1', + '0:0:0:0:ffff:127.0.0.1', + ], + }); + test({ + validator: 'isIP', + args: [4], + valid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '255.0.0.1', + '0.0.1.1', + ], + invalid: [ + '::1', + '2001:db8:0000:1:1:1:1:1', + '::ffff:127.0.0.1', + '137.132.10.01', + '0.256.0.256', + '255.256.255.256', + ], + }); + test({ + validator: 'isIP', + args: [6], + valid: [ + '::1', + '2001:db8:0000:1:1:1:1:1', + '::ffff:127.0.0.1', + 'fe80::1234%1', + 'ff08::9abc%10', + 'ff08::9abc%interface10', + 'ff02::5678%pvc1.3', + ], + invalid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::ffff:287.0.0.1', + '%', + 'fe80::1234%', + 'fe80::1234%1%3%4', + 'fe80%fe80%', + ], + }); + test({ + validator: 'isIP', + args: [10], + valid: [], + invalid: [ + '127.0.0.1', + '0.0.0.0', + '255.255.255.255', + '1.2.3.4', + '::1', + '2001:db8:0000:1:1:1:1:1', + ], + }); + }); + }); +}); 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', + ], + }); + }); + }); +});