diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b4e532908..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: [20, 18, 16, 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 --legacy-peer-deps + run: npm install --legacy-peer-deps - name: Run tests run: npm test - - if: matrix.node-version == 20 + - 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 2c6af7763..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@v2 + 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@v2 + 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@v2 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index ccc202ca2..008a86ce6 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -4,21 +4,20 @@ 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 18 - uses: actions/setup-node@v3 + - name: Setup Node.js 22 + uses: actions/setup-node@v4 with: - node-version: 18 - check-latest: true + node-version: 22 registry-url: https://registry.npmjs.org/ - name: Checkout Repository - uses: actions/checkout@v3 + 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 581908fdd..25192b24a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,99 @@ +# 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 @@ -37,30 +133,28 @@ ### 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 +- [#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 +- [#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 +- [#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 - - + - [#2197](https://github.com/validatorjs/validator.js/pull/2197) `es-CU` @klaframboise + - [#2202](https://github.com/validatorjs/validator.js/pull/2202) `pl-PL` @czerwony03 + - [#2209](https://github.com/validatorjs/validator.js/pull/2209) `fr-WF` @aidos42 + - [#2246](https://github.com/validatorjs/validator.js/pull/2246) `ar-SD` @Hussienma # 13.9.0 @@ -90,6 +184,7 @@ - [#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 @@ -105,18 +200,22 @@ ### 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 @@ -151,6 +250,7 @@ - [#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 @@ -159,16 +259,17 @@ - [#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 @@ -211,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 @@ -253,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 @@ -262,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 @@ -306,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 @@ -355,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 @@ -385,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 @@ -417,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 @@ -467,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 @@ -477,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 @@ -490,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 @@ -514,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 @@ -533,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 @@ -555,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 @@ -574,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 @@ -588,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 @@ -602,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)) @@ -628,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 @@ -644,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 @@ -668,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 @@ -717,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 @@ -728,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 @@ -740,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 @@ -748,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 @@ -769,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 @@ -808,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 @@ -821,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 @@ -835,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 @@ -889,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 @@ -1031,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/README.md b/README.md index a3fb7debd..37fbd5e3d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ [![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. @@ -85,17 +84,17 @@ 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. **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', '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', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. -**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**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 the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32]. **isBase58(str)** | check if the string is base58 encoded. -**isBase64(str [, options])** | check if the string is base64 encoded. `options` is optional and defaults to `{ urlSafe: false }`
when `urlSafe` is true it tests the given base64 encoded string is [url safe][Base64 URL Safe]. -**isBefore(str [, date])** | check if the string is a date that is before the specified date. +**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. @@ -107,11 +106,11 @@ Validator | Description **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

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

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', '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 and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. +**isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings 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 }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). +**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. @@ -124,13 +123,14 @@ Validator | Description **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 [, options])** | check if the string is an [ISBN][ISBN].

`options` is an object that has no default.
**Options:**
`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested. **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). **isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601][ISO 8601] date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. +**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. @@ -140,35 +140,36 @@ Validator | Description **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 the string matches the format of a country's license plate.

`locale` is one of `['cs-CZ', 'de-DE', 'de-LI', 'en-IN', 'en-PK', 'es-AR', 'hu-HU', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `'any'`. +**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. **isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm). -**isMACAddress(str [, options])** | check if the string is a MAC address.

`options` is an object which defaults to `{ no_separators: false }`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g. '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64. +**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][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', '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`. +**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` 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', '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']`. +**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 locales match. Locale list is `validator.isPostalCodeLocales`. +**isPostalCode(str, locale)** | check if the string is a postal code.

`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', '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 [, 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. +**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(str)** | check if the string is of type slug. **isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.
Default options:
`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }` -**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].

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

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

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

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

`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format. +**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 }`.

`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.
`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.
`protocols` - valid protocols can be modified with this option.
`require_host` - if set to false isURL will not check if host is present in the URL.
`require_port` - if set to true isURL will check if port is present in the URL.
`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.
`allow_fragments` - if set to false isURL will return false if fragments are present.
`allow_query_components` - if set to false isURL will return false if query components are present.
`validate_length` - if set to false isURL will skip string length validation (2083 characters is IE max URL length). -**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5). +**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)** | 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`. @@ -252,6 +253,7 @@ This project is licensed under the [MIT](LICENSE). See the [LICENSE](LICENSE) fi [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 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 854657f4d..106694955 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.12.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" ], diff --git a/src/index.js b/src/index.js index bba35572a..b69c43649 100644 --- a/src/index.js +++ b/src/index.js @@ -22,7 +22,7 @@ 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'; @@ -63,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'; @@ -93,6 +94,7 @@ 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'; @@ -128,7 +130,7 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; -const version = '13.12.0'; +const version = '13.15.20'; const validator = { version, @@ -155,6 +157,7 @@ const validator = { isAlphanumericLocales, isNumeric, isPassportNumber, + passportNumberLocales, isPort, isLowercase, isUppercase, @@ -185,6 +188,7 @@ const validator = { isLength, isLocale, isByteLength, + isULID, isUUID, isMongoId, isAfter, @@ -208,6 +212,7 @@ const validator = { isFreightContainerID, isISO6391, isISO8601, + isISO15924, isRFC3339, isISO31661Alpha2, isISO31661Alpha3, diff --git a/src/lib/alpha.js b/src/lib/alpha.js index 8c37934ff..6f58c9aee 100644 --- a/src/lib/alpha.js +++ b/src/lib/alpha.js @@ -38,6 +38,13 @@ export const alpha = { 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 = { @@ -79,6 +86,13 @@ export const alphanumeric = { 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 = { @@ -128,8 +142,9 @@ for (let locale, i = 0; i < bengaliLocales.length; i++) { export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY']; export const commaDecimal = [ 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'eo', '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', 'kk-KZ', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', + '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/isAfter.js b/src/lib/isAfter.js index e116e77ce..149622a0d 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -3,9 +3,10 @@ import toDate from './toDate'; export default function isAfter(date, options) { // For backwards compatibility: // isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date` - const comparisonDate = options?.comparisonDate || options || Date().toString(); + const 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/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/isDate.js b/src/lib/isDate.js index dc69b3bc1..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]]); @@ -28,6 +28,7 @@ export default function isDate(input, options) { 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,7 +41,7 @@ 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; } 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 9d89f8db3..abe465052 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import checkHost from './util/checkHost'; import isByteLength from './isByteLength'; import isFQDN from './isFQDN'; @@ -60,7 +61,6 @@ function validateDisplayName(display_name) { return true; } - export default function isEmail(str, options) { assertString(str); options = merge(options, default_email_options); @@ -97,11 +97,11 @@ export default function isEmail(str, options) { const domain = parts.pop(); const lower_domain = domain.toLowerCase(); - if (options.host_blacklist.includes(lower_domain)) { + if (options.host_blacklist.length > 0 && checkHost(lower_domain, options.host_blacklist)) { return false; } - if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) { + if (options.host_whitelist.length > 0 && !checkHost(lower_domain, options.host_whitelist)) { return false; } @@ -109,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(); @@ -166,7 +166,7 @@ export default function isEmail(str, options) { if (user.search(new RegExp(`[${options.blacklisted_chars}]+`, 'g')) !== -1) return false; } - if (user[0] === '"') { + if (user[0] === '"' && user[user.length - 1] === '"') { user = user.slice(1, user.length - 1); return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index 643f9729f..84bdc782c 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -1,4 +1,5 @@ import assertString from './util/assertString'; +import isNullOrUndefined from './util/nullUndefinedCheck'; import { decimal } from './alpha'; export default function isFloat(str, options) { @@ -10,10 +11,10 @@ export default function isFloat(str, options) { } 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 28f39be89..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 @@ -39,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}$/, @@ -67,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}$/, @@ -131,7 +132,7 @@ function hasValidIbanFormat(str, options) { return false; } - const isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode); + const isoCountryCodeInWhiteList = includes(options.whitelist, isoCountryCode); if (!isoCountryCodeInWhiteList) { return false; @@ -139,7 +140,7 @@ function hasValidIbanFormat(str, options) { } if (options.blacklist) { - const isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode); + const isoCountryCodeInBlackList = includes(options.blacklist, isoCountryCode); if (isoCountryCodeInBlackList) { return false; 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/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/isISO6346.js b/src/lib/isISO6346.js index 0cb657e7c..2c28c1123 100644 --- a/src/lib/isISO6346.js +++ b/src/lib/isISO6346.js @@ -27,7 +27,8 @@ export function isISO6346(str) { } else sum += str[i] * (2 ** i); } - const checkSumDigit = sum % 11; + let checkSumDigit = sum % 11; + if (checkSumDigit === 10) checkSumDigit = 0; return Number(str[str.length - 1]) === checkSumDigit; } diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 060618fce..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); diff --git a/src/lib/isInt.js b/src/lib/isInt.js index edd67f75a..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]+$/; @@ -12,10 +13,10 @@ export default function isInt(str, options) { 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/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 8476f2f9e..54d80635f 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -7,13 +7,14 @@ const validators = { /^((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), 'sv-SE': str => diff --git a/src/lib/isMimeType.js b/src/lib/isMimeType.js index 4081117af..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 diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index e5fb6bffd..b00391ea6 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -13,13 +13,14 @@ const phones = { '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}$/, @@ -33,7 +34,7 @@ const phones = { 'de-LU': /^(\+352)?((6\d1)\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|6)\d{6})$/, + '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}$/, @@ -41,7 +42,7 @@ const phones = { 'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, '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}$/, @@ -70,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}$/, @@ -119,7 +120,7 @@ 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}$/, @@ -136,7 +137,7 @@ const phones = { '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}$/, + '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}$/, @@ -144,7 +145,7 @@ const phones = { 'sl-SI': /^(\+386\s?|0)(\d{1}\s?\d{3}\s?\d{2}\s?\d{2}|\d{2}\s?\d{3}\s?\d{3})$/, 'sk-SK': /^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/, 'so-SO': /^(\+?252|0)((6[0-9])\d{7}|(7[1-9])\d{7})$/, - 'sq-AL': /^(\+355|0)6[789]\d{6}$/, + '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}$/, @@ -160,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 c1803fb50..c3b842e59 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -16,7 +16,7 @@ const passportRegexByCountryCode = { 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 @@ -65,10 +65,12 @@ const passportRegexByCountryCode = { 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/isPostalCode.js b/src/lib/isPostalCode.js index 99cba290b..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}$/, + 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}$)/, @@ -56,6 +58,8 @@ const patterns = { 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 9458522ab..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 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|1(\.0)?|0(\.0)?)\)$/; +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/isTaxID.js b/src/lib/isTaxID.js index d13229f69..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; @@ -621,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; @@ -890,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++) { diff --git a/src/lib/isTime.js b/src/lib/isTime.js index 076bbfa34..3169864c2 100644 --- a/src/lib/isTime.js +++ b/src/lib/isTime.js @@ -9,10 +9,12 @@ 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$/, }, }; 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 512141df6..9d6040f04 100644 --- a/src/lib/isUUID.js +++ b/src/lib/isUUID.js @@ -1,17 +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, + 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, - all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[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 50fcf52e0..1ec2c5991 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -60,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), 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/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/exports.test.js b/test/exports.test.js index 0bff532ab..a5f458f05 100644 --- a/test/exports.test.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.test.js b/test/sanitizers.test.js index ecb0e128f..e36ba48d3 100644 --- a/test/sanitizers.test.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 index bcd7c15b0..5fc133bec 100644 --- a/test/testFunctions.js +++ b/test/testFunctions.js @@ -2,6 +2,10 @@ 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 || []; @@ -16,7 +20,7 @@ export default function test(options) { } catch (err) { const warning = format( 'validator.%s(%s) passed but should error', - options.validator, args.join(', ') + options.validator, stringifyArgs(args) ); throw new Error(warning); @@ -31,7 +35,7 @@ export default function test(options) { if (validator[options.validator](...args) !== true) { const warning = format( 'validator.%s(%s) failed but should have passed', - options.validator, args.join(', ') + options.validator, stringifyArgs(args) ); throw new Error(warning); @@ -46,7 +50,7 @@ export default function test(options) { if (validator[options.validator](...args) !== false) { const warning = format( 'validator.%s(%s) passed but should have failed', - options.validator, args.join(', ') + options.validator, stringifyArgs(args) ); throw new Error(warning); diff --git a/test/util.test.js b/test/util.test.js index 449cd9ee7..0146a4e2c 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -4,6 +4,8 @@ */ 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', () => { @@ -18,3 +20,45 @@ describe('Util', () => { 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.test.js b/test/validators.test.js index 8037acf37..a3c5f5a5d 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -1,9 +1,7 @@ import assert from 'assert'; import fs from 'fs'; import timezone_mock from 'timezone-mock'; -import { format } from 'util'; import vm from 'vm'; -import validator from '../src/index'; import test from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); @@ -71,6 +69,9 @@ describe('Validators', () => { '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', ], }); }); @@ -322,6 +323,25 @@ 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', @@ -338,6 +358,25 @@ describe('Validators', () => { }); }); + 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', @@ -385,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/', @@ -427,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 */ ], }); }); @@ -439,9 +496,11 @@ describe('Validators', () => { }], valid: [ 'rtmp://foobar.com', + 'rtmp:foobar.com', ], invalid: [ 'http://foobar.com', + 'tel:+15551234567', ], }); }); @@ -494,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', @@ -665,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', @@ -743,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', @@ -787,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', @@ -991,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', @@ -1315,6 +1335,7 @@ describe('Validators', () => { ], }); }); + it('should validate alpha strings', () => { test({ validator: 'isAlpha', @@ -1977,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', @@ -2774,74 +2889,232 @@ describe('Validators', () => { ], }); }); - - it('should error on invalid locale', () => { + it('should validate Tamil alphanumeric strings', () => { test({ validator: 'isAlphanumeric', - args: ['is-NOT'], - error: [ - '1234568960', - 'abc123', + args: ['ta-IN'], + valid: [ + 'தமிழ்', + 'தமிழ்123', + 'அஆஇஈ123', + 'தமிழ்123.45', + '123.45', + 'தமிழ்.', + ], + invalid: [ + 'தமிழ் ', + 'abc', + '', ], }); }); - it('should validate numeric strings', () => { + it('should validate Telugu alphanumeric strings', () => { test({ - validator: 'isNumeric', + validator: 'isAlphanumeric', + args: ['te-IN'], valid: [ - '123', - '00123', - '-00123', - '0', - '-0', - '+123', - '123.123', - '+000000', + 'తెలుగు', + 'తెలుగు123', + 'అఆఇఈ123', + 'తెలుగు123.45', + '123.45', + 'తెలుగు.', ], invalid: [ - ' ', + 'abc', '', - '.', ], }); }); - it('should validate numeric strings without symbols', () => { + it('should validate Kannada alphanumeric strings', () => { test({ - validator: 'isNumeric', - args: [{ - no_symbols: true, - }], + validator: 'isAlphanumeric', + args: ['kn-IN'], valid: [ - '123', - '00123', - '0', + 'ಕನ್ನಡ', + 'ಕನ್ನಡ123', + 'ಅಆಇಈ123', + 'ಕನ್ನಡ123.45', + '123.45', + 'ಕನ್ನಡ.', ], invalid: [ - '-0', - '+000000', + 'abc', '', - '+123', - '123.123', - '-00123', - ' ', - '.', ], }); }); - it('should validate numeric strings with locale', () => { + it('should validate Malayalam alphanumeric strings', () => { test({ - validator: 'isNumeric', - args: [{ - locale: 'fr-FR', - }], + validator: 'isAlphanumeric', + args: ['ml-IN'], valid: [ - '123', - '00123', - '-00123', - '0', + 'മലയാളം', + 'മലയാളം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', + args: ['is-NOT'], + error: [ + '1234568960', + 'abc123', + ], + }); + }); + + it('should validate numeric strings', () => { + test({ + validator: 'isNumeric', + valid: [ + '123', + '00123', + '-00123', + '0', + '-0', + '+123', + '123.123', + '+000000', + ], + invalid: [ + ' ', + '', + '.', + ], + }); + }); + + it('should validate numeric strings without symbols', () => { + test({ + validator: 'isNumeric', + args: [{ + no_symbols: true, + }], + valid: [ + '123', + '00123', + '0', + ], + invalid: [ + '-0', + '+000000', + '', + '+123', + '123.123', + '-00123', + ' ', + '.', + ], + }); + }); + + it('should validate numeric strings with locale', () => { + test({ + validator: 'isNumeric', + args: [{ + locale: 'fr-FR', + }], + valid: [ + '123', + '00123', + '-00123', + '0', '-0', '+123', '123,123', @@ -3029,9 +3302,17 @@ describe('Validators', () => { valid: [ 'GA302922', 'ZE000509', + 'A123456AB', + 'Z556378HG', ], invalid: [ 'AB0123456', + 'AZ556378H', + '556378HCX', + '556378432', + '5563784', + '#B12345FD', + 'A43F12354', ], }); @@ -3682,11 +3963,15 @@ describe('Validators', () => { valid: [ '790369937', '340007237', + 'A90583942', + 'E00007734', ], invalid: [ 'US0123456', '0123456US', '7903699371', + '90583942', + 'E000077341', ], }); @@ -4211,6 +4496,78 @@ describe('Validators', () => { 'a', ], }); + test({ + validator: 'isInt', + args: [{ + min: undefined, + max: undefined, + }], + valid: [ + '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', () => { @@ -4440,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', () => { @@ -4564,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,)', @@ -4574,18 +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', @@ -4597,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)', ], }); }); @@ -5026,12 +5664,42 @@ describe('Validators', () => { valid: ['abc', 'de', 'a', ''], invalid: ['abcd'], }); + test({ + validator: 'isLength', + args: [{ max: 6, discreteLengths: 5 }], + valid: ['abcd', 'vfd', 'ff', '', 'k'], + invalid: ['abcdefgh', 'hfjdksks'], + }); + test({ + validator: 'isLength', + args: [{ min: 2, max: 6, discreteLengths: 5 }], + valid: ['bsa', 'vfvd', 'ff'], + invalid: ['', ' ', 'hfskdunvc'], + }); + test({ + validator: 'isLength', + args: [{ min: 1, discreteLengths: 2 }], + valid: [' ', 'hello', 'bsa'], + invalid: [''], + }); 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'], @@ -5070,18 +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', @@ -5093,12 +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', @@ -5108,12 +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', @@ -5138,13 +5834,14 @@ describe('Validators', () => { 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', @@ -5154,10 +5851,11 @@ describe('Validators', () => { 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', @@ -5180,60 +5878,212 @@ describe('Validators', () => { 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', '934859', 'AAAAAAAA-1111-1111-AAAG-111111111111', - 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', - 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', - '018C544A-D384-7000-BB74-3B1738ABE43C', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + '018C544A-D384-7000-BB74-3B1738ABE43C', + ], + }); + test({ + validator: 'isUUID', + args: [5], + valid: [ + '987FBC97-4BED-5078-AF07-9141BA07C9F3', + '987FBC97-4BED-5078-BF07-9141BA07C9F3', + '987FBC97-4BED-5078-8F07-9141BA07C9F3', + '987FBC97-4BED-5078-9F07-9141BA07C9F3', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + '934859', + '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: [ + '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: [5], + args: ['all'], valid: [ - '987FBC97-4BED-5078-AF07-9141BA07C9F3', - '987FBC97-4BED-5078-BF07-9141BA07C9F3', - '987FBC97-4BED-5078-8F07-9141BA07C9F3', - '987FBC97-4BED-5078-9F07-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', + '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', - '9c858901-8a57-4791-81fe-4c455b099bc9', - 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', - '018C544A-D384-7000-BB74-3B1738ABE43C', ], }); test({ validator: 'isUUID', - args: [6], + args: ['invalid'], valid: [], - 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', - ], - }); - test({ - validator: 'isUUID', - args: [7], - valid: [ - '018C544A-D384-7000-BB74-3B1738ABE43C', - ], 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-3078-CF07-9141BA07C9F3', - '713ae7e3-cb32-45f9-adcb-7c4fa86b90c1', - '625e63f3-58f5-40b7-83a1-a72ad31acffb', - '57b73598-8764-4ad0-a76a-679bb6640eb1', - '9c858901-8a57-4791-81fe-4c455b099bc9', + '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', ], }); }); @@ -5281,41 +6131,6 @@ describe('Validators', () => { }); }); - 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()], - }); - 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: ['2011-08-03'], - valid: ['1999-12-31'], - invalid: ['invalid date'], - }); - test({ - validator: 'isBefore', - args: ['invalid date'], - invalid: ['invalid date', '1999-12-31'], - }); - }); - it('should validate ABA routing number', () => { test({ validator: 'isAbaRouting', @@ -5365,6 +6180,9 @@ describe('Validators', () => { 'MA64011519000001205000534921', 'VG96VPVG0000012345678901', 'DZ580002100001113000000570', + 'IE29AIBK93115212345678', + 'PS92PALS000000000400123456702', + 'PS92PALS00000000040012345670O', ], invalid: [ 'XX22YYY1234567890123', @@ -5373,6 +6191,8 @@ describe('Validators', () => { 'FR7630006000011234567890189😅', 'FR763000600001123456!!🤨7890189@', 'VG46H07Y0223060094359858', + 'IE95TE8270900834048660', + 'PS072435171802145240705922007', ], }); test({ @@ -6649,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({ @@ -7028,6 +7778,7 @@ describe('Validators', () => { locale: 'ar-OM', valid: [ '+96891212121', + '+96871212121', '0096899999999', '93112211', '99099009', @@ -7468,7 +8219,7 @@ describe('Validators', () => { ], }, { - local: 'en-LS', + locale: 'en-LS', valid: [ '+26622123456', '+26628123456', @@ -7599,6 +8350,7 @@ describe('Validators', () => { '0502345671', '0242345671', '0542345671', + '0532345671', '0272345671', '0572345671', '0262345671', @@ -7609,6 +8361,7 @@ describe('Validators', () => { '+233502345671', '+233242345671', '+233542345671', + '+233532345671', '+233272345671', '+233572345671', '+233262345671', @@ -8220,6 +8973,11 @@ describe('Validators', () => { '+3599148725', '96537247', '3596676533', + '+35795123455', + '+35797012204', + '35799123456', + '+35794123456', + '+35796123456', ], invalid: [ '', @@ -8323,6 +9081,8 @@ describe('Validators', () => { '+260966684590', '+260976684590', '260976684590', + '+260779493521', + '+260760010936', ], invalid: [ '12345', @@ -8330,6 +9090,7 @@ describe('Validators', () => { 'Vml2YW11cyBmZXJtZtesting123', '010-38238383', '966684590', + '760010936', ], }, { @@ -9112,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', ], }, { @@ -9810,15 +10600,27 @@ describe('Validators', () => { { 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', ], }, @@ -9839,10 +10641,12 @@ describe('Validators', () => { locale: 'pt-AO', valid: [ '+244911123432', - '+244123091232', + '911123432', + '244911123432', ], invalid: [ '+2449111234321', + '+244811123432', '31234', '31234567', '512345', @@ -10202,9 +11006,41 @@ describe('Validators', () => { '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 = []; @@ -11573,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', @@ -11913,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', @@ -11955,6 +12847,21 @@ describe('Validators', () => { 'Z1A 0B1', ], }, + { + locale: 'CO', + valid: [ + '050034', + '110221', + '441029', + '910001', + ], + invalid: [ + '11001', + '000000', + '109999', + '329999', + ], + }, { locale: 'ES', valid: [ @@ -12013,10 +12920,16 @@ describe('Validators', () => { locale: 'FR', valid: [ '75008', + '44522', + '38499', + '39940', + '01000', + ], + invalid: [ '44 522', - '98025', '38 499', - '39940', + '96000', + '98025', ], }, { @@ -12180,6 +13093,7 @@ describe('Validators', () => { '399', '935', '38842', + '546023', ], }, { @@ -12239,6 +13153,9 @@ describe('Validators', () => { '39100-000', '22040-020', '39400-152', + '39100000', + '22040020', + '39400152', ], invalid: [ '79800A12', @@ -12259,6 +13176,22 @@ describe('Validators', () => { '4144', ], }, + { + locale: 'PK', + valid: [ + '25000', + '44000', + '54810', + '74200', + ], + invalid: [ + '5400', + '540000', + 'NY540', + '540CA', + '540-0', + ], + }, { locale: 'MG', valid: [ @@ -12565,6 +13498,55 @@ describe('Validators', () => { }); }); + 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({ @@ -13202,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', @@ -13241,6 +14192,7 @@ describe('Validators', () => { new Date([2014, 2, 15]), new Date('2014-03-15'), '2020/02/29', + '2020-02-19', ], invalid: [ '', @@ -13256,6 +14208,18 @@ describe('Validators', () => { '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({ @@ -13271,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({ @@ -13286,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({ @@ -13300,6 +14294,22 @@ describe('Validators', () => { '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({ @@ -13315,6 +14325,16 @@ describe('Validators', () => { '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({ @@ -13330,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({ @@ -13348,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({ @@ -13373,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({ @@ -13392,6 +14451,17 @@ 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 @@ -13453,6 +14523,35 @@ describe('Validators', () => { '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' }], @@ -13512,6 +14611,38 @@ describe('Validators', () => { '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({ @@ -13539,16 +14670,42 @@ describe('Validators', () => { 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({ @@ -13859,26 +15016,18 @@ describe('Validators', () => { }); test({ validator: 'isLicensePlate', - args: ['en-PK'], + args: ['en-SG'], valid: [ - 'P 1789', - 'RL745', - 'RIR 5421', - 'KHI 201', - 'LB6571', - 'LHR-786-23', - 'AJGB 816-10', - 'LES 7891 06', - 'IDS 7871', - 'LEH 4607 15', + 'SGX 1234 A', + 'SGX-1234-A', + 'SGB1234Z', ], invalid: [ - 'ajgb 816-10', - ' 278-37', - 'ABZ-27', + 'sg1234a', + 'invalidlicenseplate', + '4578', '', - 'ABC-123-', - 'D 272', + 'GJ054GH4785', ], }); }); @@ -14002,11 +15151,18 @@ describe('Validators', () => { args: ['FR'], valid: [ 'FRAA123456789', - 'AA123456789', + 'FR83404833048', + 'FR40123456789', + 'FRA1123456789', + 'FR1A123456789', ], invalid: [ 'FR AA123456789', '123456789', + 'FRAA123456789A', + 'FR123456789', + 'FR 83404833048', + 'FRaa123456789', ], }); test({ @@ -14835,7 +15991,7 @@ describe('Validators', () => { ], invalid: [ '', - 'somthing', + 'something', 'valid@gmail.com', 'mailto:?subject=okay&subject=444', 'mailto:?subject=something&wrong=888', diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js index d771d9198..f0daf8a17 100644 --- a/test/validators/isAfter.test.js +++ b/test/validators/isAfter.test.js @@ -27,6 +27,21 @@ describe('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)', () => { 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/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', + ], + }); + }); + }); +});