Skip to content

Commit 609bd78

Browse files
authored
Merge pull request #809 from neilime/patch-1
Add ISO_3166-1_alpha-3 validator
2 parents cf7483e + 3ecd6d7 commit 609bd78

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Validator | Description
9191
**isISO8601(str)** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
9292
**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.
9393
**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code.
94+
**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code.
9495
**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code).
9596
**isIn(str, values)** | check if the string is in a array of allowed values.
9697
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`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).

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import isCurrency from './lib/isCurrency';
6868
import isISO8601 from './lib/isISO8601';
6969
import isRFC3339 from './lib/isRFC3339';
7070
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
71+
import isISO31661Alpha3 from './lib/isISO31661Alpha3';
7172

7273
import isBase64 from './lib/isBase64';
7374
import isDataURI from './lib/isDataURI';
@@ -147,6 +148,7 @@ const validator = {
147148
isISO8601,
148149
isRFC3339,
149150
isISO31661Alpha2,
151+
isISO31661Alpha3,
150152
isBase64,
151153
isDataURI,
152154
isMimeType,

src/lib/isISO31661Alpha3.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import assertString from './util/assertString';
2+
3+
// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
4+
const validISO31661Alpha3CountriesCodes = [
5+
'AFG', 'ALA', 'ALB', 'DZA', 'ASM', 'AND', 'AGO', 'AIA', 'ATA', 'ATG', 'ARG', 'ARM', 'ABW', 'AUS', 'AUT', 'AZE',
6+
'BHS', 'BHR', 'BGD', 'BRB', 'BLR', 'BEL', 'BLZ', 'BEN', 'BMU', 'BTN', 'BOL', 'BES', 'BIH', 'BWA', 'BVT', 'BRA',
7+
'IOT', 'BRN', 'BGR', 'BFA', 'BDI', 'KHM', 'CMR', 'CAN', 'CPV', 'CYM', 'CAF', 'TCD', 'CHL', 'CHN', 'CXR', 'CCK',
8+
'COL', 'COM', 'COG', 'COD', 'COK', 'CRI', 'CIV', 'HRV', 'CUB', 'CUW', 'CYP', 'CZE', 'DNK', 'DJI', 'DMA', 'DOM',
9+
'ECU', 'EGY', 'SLV', 'GNQ', 'ERI', 'EST', 'ETH', 'FLK', 'FRO', 'FJI', 'FIN', 'FRA', 'GUF', 'PYF', 'ATF', 'GAB',
10+
'GMB', 'GEO', 'DEU', 'GHA', 'GIB', 'GRC', 'GRL', 'GRD', 'GLP', 'GUM', 'GTM', 'GGY', 'GIN', 'GNB', 'GUY', 'HTI',
11+
'HMD', 'VAT', 'HND', 'HKG', 'HUN', 'ISL', 'IND', 'IDN', 'IRN', 'IRQ', 'IRL', 'IMN', 'ISR', 'ITA', 'JAM', 'JPN',
12+
'JEY', 'JOR', 'KAZ', 'KEN', 'KIR', 'PRK', 'KOR', 'KWT', 'KGZ', 'LAO', 'LVA', 'LBN', 'LSO', 'LBR', 'LBY', 'LIE',
13+
'LTU', 'LUX', 'MAC', 'MKD', 'MDG', 'MWI', 'MYS', 'MDV', 'MLI', 'MLT', 'MHL', 'MTQ', 'MRT', 'MUS', 'MYT', 'MEX',
14+
'FSM', 'MDA', 'MCO', 'MNG', 'MNE', 'MSR', 'MAR', 'MOZ', 'MMR', 'NAM', 'NRU', 'NPL', 'NLD', 'NCL', 'NZL', 'NIC',
15+
'NER', 'NGA', 'NIU', 'NFK', 'MNP', 'NOR', 'OMN', 'PAK', 'PLW', 'PSE', 'PAN', 'PNG', 'PRY', 'PER', 'PHL', 'PCN',
16+
'POL', 'PRT', 'PRI', 'QAT', 'REU', 'ROU', 'RUS', 'RWA', 'BLM', 'SHN', 'KNA', 'LCA', 'MAF', 'SPM', 'VCT', 'WSM',
17+
'SMR', 'STP', 'SAU', 'SEN', 'SRB', 'SYC', 'SLE', 'SGP', 'SXM', 'SVK', 'SVN', 'SLB', 'SOM', 'ZAF', 'SGS', 'SSD',
18+
'ESP', 'LKA', 'SDN', 'SUR', 'SJM', 'SWZ', 'SWE', 'CHE', 'SYR', 'TWN', 'TJK', 'TZA', 'THA', 'TLS', 'TGO', 'TKL',
19+
'TON', 'TTO', 'TUN', 'TUR', 'TKM', 'TCA', 'TUV', 'UGA', 'UKR', 'ARE', 'GBR', 'USA', 'UMI', 'URY', 'UZB', 'VUT',
20+
'VEN', 'VNM', 'VGB', 'VIR', 'WLF', 'ESH', 'YEM', 'ZMB', 'ZWE',
21+
];
22+
23+
export default function isISO31661Alpha3(str) {
24+
assertString(str);
25+
return validISO31661Alpha3CountriesCodes.includes(str.toUpperCase());
26+
}

test/validators.js

+24
Original file line numberDiff line numberDiff line change
@@ -5180,6 +5180,30 @@ describe('Validators', function () {
51805180
});
51815181
});
51825182

5183+
it('should validate ISO 3166-1 alpha 3 country codes', function () {
5184+
// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
5185+
test({
5186+
validator: 'isISO31661Alpha3',
5187+
valid: [
5188+
'ABW',
5189+
'HND',
5190+
'KHM',
5191+
'RWA',
5192+
],
5193+
invalid: [
5194+
'',
5195+
'FR',
5196+
'fR',
5197+
'GB',
5198+
'PT',
5199+
'CM',
5200+
'JP',
5201+
'PM',
5202+
'ZW',
5203+
],
5204+
});
5205+
});
5206+
51835207
it('should validate whitelisted characters', function () {
51845208
test({
51855209
validator: 'isWhitelisted',

0 commit comments

Comments
 (0)