Repository: validatorjs/validator.js
Pull request #2399 (2024-05-09 11:10:02)

feat(isISO31661Numeric): Add validation for ISO 3166-1 numeric country codes
The ISO 3166-1 standard contains three sets of country codes: two-letter, three-letter, and three-digit. Currently, we have a validator for two-letter codes (`isISO31661Alpha2`) and three-letter codes (`isISO31661Alpha3`), but not for three-digit codes. This pull request addresses that.

## References
- https://en.wikipedia.org/wiki/ISO_3166-1
- https://en.wikipedia.org/wiki/ISO_3166-1_numeric
- https://www.iso.org/obp/ui/#search/code/

## Checklist

- [x] PR contains only changes related; no stray files, etc.
- [x] README updated (where applicable)
- [x] Tests written (where applicable)
- [x] References provided in PR (where applicable)



Patch:
diff --git a/README.md b/README.md
index 5b8bb7d..4cf1476 100644
--- a/README.md
+++ b/README.md
@@ -126,6 +126,7 @@ Validator                               | Description
 **isISO8601(str [, options])**          | check if the string is a valid [ISO 8601][ISO 8601] date. <br/>`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid.
 **isISO31661Alpha2(str)**               | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code.
 **isISO31661Alpha3(str)**               | check if the string is a valid [ISO 3166-1 alpha-3][ISO 3166-1 alpha-3] officially assigned country code.
+**isISO31661Numeric(str)**              | check if the string is a valid [ISO 3166-1 numeric][ISO 3166-1 numeric] officially assigned country code.
 **isISO4217(str)**                      | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code.
 **isISRC(str)**                         | check if the string is an [ISRC][ISRC].
 **isISSN(str [, options])**             | check if the string is an [ISSN][ISSN].<br/><br/>`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
@@ -261,6 +262,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601
 [ISO 3166-1 alpha-2]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
 [ISO 3166-1 alpha-3]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
+[ISO 3166-1 numeric]: https://en.wikipedia.org/wiki/ISO_3166-1_numeric
 [ISO 4217]: https://en.wikipedia.org/wiki/ISO_4217
 [ISRC]: https://en.wikipedia.org/wiki/International_Standard_Recording_Code
 [ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number
diff --git a/src/index.js b/src/index.js
index ca0651d..bba3557 100644
--- a/src/index.js
+++ b/src/index.js
@@ -95,6 +95,7 @@ import isISO8601 from './lib/isISO8601';
 import isRFC3339 from './lib/isRFC3339';
 import isISO31661Alpha2 from './lib/isISO31661Alpha2';
 import isISO31661Alpha3 from './lib/isISO31661Alpha3';
+import isISO31661Numeric from './lib/isISO31661Numeric';
 import isISO4217 from './lib/isISO4217';
 
 import isBase32 from './lib/isBase32';
@@ -210,6 +211,7 @@ const validator = {
   isRFC3339,
   isISO31661Alpha2,
   isISO31661Alpha3,
+  isISO31661Numeric,
   isISO4217,
   isBase32,
   isBase58,
diff --git a/src/lib/isISO31661Numeric.js b/src/lib/isISO31661Numeric.js
new file mode 100644
index 0000000..8b32a8d
--- /dev/null
+++ b/src/lib/isISO31661Numeric.js
@@ -0,0 +1,26 @@
+import assertString from './util/assertString';
+
+// from https://en.wikipedia.org/wiki/ISO_3166-1_numeric
+const validISO31661NumericCountriesCodes = new Set([
+  '004', '008', '010', '012', '016', '020', '024', '028', '031', '032', '036', '040', '044', '048', '050', '051',
+  '052', '056', '060', '064', '068', '070', '072', '074', '076', '084', '086', '090', '092', '096', '100', '104',
+  '108', '112', '116', '120', '124', '132', '136', '140', '144', '148', '152', '156', '158', '162', '166', '170',
+  '174', '175', '178', '180', '184', '188', '191', '192', '196', '203', '204', '208', '212', '214', '218', '222',
+  '226', '231', '232', '233', '234', '238', '239', '242', '246', '248', '250', '254', '258', '260', '262', '266',
+  '268', '270', '275', '276', '288', '292', '296', '300', '304', '308', '312', '316', '320', '324', '328', '332',
+  '334', '336', '340', '344', '348', '352', '356', '360', '364', '368', '372', '376', '380', '384', '388', '392',
+  '398', '400', '404', '408', '410', '414', '417', '418', '422', '426', '428', '430', '434', '438', '440', '442',
+  '446', '450', '454', '458', '462', '466', '470', '474', '478', '480', '484', '492', '496', '498', '499', '500',
+  '504', '508', '512', '516', '520', '524', '528', '531', '533', '534', '535', '540', '548', '554', '558', '562',
+  '566', '570', '574', '578', '580', '581', '583', '584', '585', '586', '591', '598', '600', '604', '608', '612',
+  '616', '620', '624', '626', '630', '634', '638', '642', '643', '646', '652', '654', '659', '660', '662', '663',
+  '666', '670', '674', '678', '682', '686', '688', '690', '694', '702', '703', '704', '705', '706', '710', '716',
+  '724', '728', '729', '732', '740', '744', '748', '752', '756', '760', '762', '764', '768', '772', '776', '780',
+  '784', '788', '792', '795', '796', '798', '800', '804', '807', '818', '826', '831', '832', '833', '834', '840',
+  '850', '854', '858', '860', '862', '876', '882', '887', '894',
+]);
+
+export default function isISO31661Numeric(str) {
+  assertString(str);
+  return validISO31661NumericCountriesCodes.has(str);
+}


Test patch:
diff --git a/test/validators.test.js b/test/validators.test.js
index 3928a1c..6b603ff 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -11546,6 +11546,37 @@ describe('Validators', () => {
     });
   });
 
+  it('should validate ISO 3166-1 numeric country codes', () => {
+    // from https://en.wikipedia.org/wiki/ISO_3166-1_numeric
+    test({
+      validator: 'isISO31661Numeric',
+      valid: [
+        '076',
+        '208',
+        '276',
+        '348',
+        '380',
+        '410',
+        '440',
+        '528',
+        '554',
+        '826',
+      ],
+      invalid: [
+        '',
+        'NL',
+        'NLD',
+        '002',
+        '197',
+        '249',
+        '569',
+        '810',
+        '900',
+        '999',
+      ],
+    });
+  });
+
   it('should validate ISO 4217 corrency codes', () => {
     // from https://en.wikipedia.org/wiki/ISO_4217
     test({