Repository: validatorjs/validator.js
Pull request #2529 (2025-02-10 03:59:03)

feat(isPostalCode): Allow taiwan 3+3 format

<!--- briefly describe what you have done in this PR --->
Taiwan added in 2020 a 3+3 postal code format.
Even though it's mainly for internal usage, it is still a valid format that might be required in some use cases. Therefore, I believe this library should consider it a valid postal code.

<!--- provide some (credible) references showing the structure of the data to be validated, if applicable --->

[Official Post Website](https://www.post.gov.tw/post/internet/U_english2/index.jsp?ID=3509010116#:~:text=6%20digits.,serial%20number%E2%80%9D%20of%20ordinary%20households.)
[Postal code 5 and 6 format searcher](https://twn.youbianku.com/)
[Default Wiki Page](https://www.wikiwand.com/en/articles/Postal_codes_in_Taiwan)


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b4e5329..af090a9 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, 6]
     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 2c6af77..e89a9b5 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 ccc202c..008a86c 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/README.md b/README.md
index a64ca45..e1661b5 100644
--- a/README.md
+++ b/README.md
@@ -157,7 +157,7 @@ Validator                               | Description
 **isOctal(str)**                        | check if the string is a valid octal number.
 **isPassportNumber(str, countryCode)**    | check if the string is a valid passport number.<br/><br/>`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.<br/><br/>`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', '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', '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.<br/><br/>`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CO', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PK', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.isPostalCodeLocales`.
 **isRFC3339(str)**                      | check if the string is a valid [RFC 3339][RFC 3339] date.
 **isRgbColor(str [,options])**                     | check if the string is a rgb or rgba color.<br/></br>`options` is an object with the following properties<br/><br/>`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.<br/><br/>`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).
diff --git a/package.json b/package.json
index 49ac0f8..2d7a847 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
     "es",
     "lib",
     "README.md",
-    "LICENCE",
+    "LICENSE",
     "validator.js",
     "validator.min.js"
   ],
diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js
index 01ae3fc..5472346 100644
--- a/src/lib/isPostalCode.js
+++ b/src/lib/isPostalCode.js
@@ -57,6 +57,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}?$/,
@@ -69,7 +71,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,


Test patch:
diff --git a/test/validators.test.js b/test/validators.test.js
index d6e948a..72b5591 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -4692,6 +4692,7 @@ describe('Validators', () => {
         '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)',
@@ -4703,7 +4704,6 @@ describe('Validators', () => {
         'rgb()',
         'rgba(0,0,0)',
         'rgba(255,255,255,2)',
-        'rgba(255,255,255,.12)',
         'rgba(255,255,256,0.1)',
         'rgb(4,4,5%)',
         'rgba(5%,5%,5%)',
@@ -4764,6 +4764,7 @@ describe('Validators', () => {
         '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)',
@@ -4783,7 +4784,6 @@ describe('Validators', () => {
         'rgb()',
         'rgba(0,0,0)',
         'rgba(255,255,255,2)',
-        'rgba(255,255,255,.12)',
         'rgba(255,255,256,0.1)',
         'rgb(4,4,5%)',
         'rgba(5%,5%,5%)',
@@ -4856,6 +4856,7 @@ describe('Validators', () => {
         '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)',
@@ -4876,7 +4877,6 @@ describe('Validators', () => {
         'rgb()',
         'rgba(0,0,0)',
         'rgba(255,255,255,2)',
-        'rgba(255,255,255,.12)',
         'rgba(255,255,256,0.1)',
         'rgb(4,4,5%)',
         'rgba(5%,5%,5%)',
@@ -12576,6 +12576,7 @@ describe('Validators', () => {
           '399',
           '935',
           '38842',
+          '546023',
         ],
       },
       {
@@ -12658,6 +12659,22 @@ describe('Validators', () => {
           '4144',
         ],
       },
+      {
+        locale: 'PK',
+        valid: [
+          '25000',
+          '44000',
+          '54810',
+          '74200',
+        ],
+        invalid: [
+          '5400',
+          '540000',
+          'NY540',
+          '540CA',
+          '540-0',
+        ],
+      },
       {
         locale: 'MG',
         valid: [