Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 4fbef07

Browse files
feat(rules): Added CountryCode rule
1 parent 4efca5e commit 4fbef07

7 files changed

Lines changed: 378 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
9393
- [Cntrl](docs/cntrl.md)
9494
- [Consonant](docs/consonant.md)
9595
- [Contains](docs/contains.md)
96+
- [CountryCode](docs/country-code.md)
9697
- [Cpf](docs/cpf.md)
9798
- [CreditCard](docs/credit-card.md)
9899
- [CurrencyCode](docs/currency-code.md)

awesome-validator.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/country-code.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CountryCode
2+
3+
Validates an ISO country code.
4+
5+
Valid values:
6+
7+
```js
8+
validator.countryCode().validate('BR');
9+
validator.countryCode('alpha-2').validate('BR');
10+
validator.countryCode('alpha-3').validate('BRA');
11+
validator.countryCode('numeric').validate('076');
12+
```
13+
14+
Invalid values:
15+
16+
```js
17+
validator.countryCode().validate('foo');
18+
validator.countryCode('alpha-2').validate('076');
19+
validator.countryCode('alpha-3').validate('BR');
20+
validator.countryCode('numeric').validate('BRA');
21+
```
22+
23+
You can choose between:
24+
25+
- alpha-2
26+
- alpha-3
27+
- numeric

src/rules/country-code.ts

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
import { AbstractService } from './abstract-service';
2+
import { In } from './in';
3+
4+
export class CountryCode extends AbstractService<string, string[]> {
5+
6+
/**
7+
* Services.
8+
*/
9+
protected get services(): Map<string, string[]> {
10+
11+
/**
12+
* List extracted from http://download.geonames.org/export/dump/countryInfo.txt
13+
*/
14+
const list: string[][] = [
15+
['AD', 'AND', '020'], // Andorra
16+
['AE', 'ARE', '784'], // United Arab Emirates
17+
['AF', 'AFG', '004'], // Afghanistan
18+
['AG', 'ATG', '028'], // Antigua and Barbuda
19+
['AI', 'AIA', '660'], // Anguilla
20+
['AL', 'ALB', '008'], // Albania
21+
['AM', 'ARM', '051'], // Armenia
22+
['AN', 'ANT', '530'], // Netherlands Antilles
23+
['AO', 'AGO', '024'], // Angola
24+
['AQ', 'ATA', '010'], // Antarctica
25+
['AR', 'ARG', '032'], // Argentina
26+
['AS', 'ASM', '016'], // American Samoa
27+
['AT', 'AUT', '040'], // Austria
28+
['AU', 'AUS', '036'], // Australia
29+
['AW', 'ABW', '533'], // Aruba
30+
['AX', 'ALA', '248'], // Aland Islands
31+
['AZ', 'AZE', '031'], // Azerbaijan
32+
['BA', 'BIH', '070'], // Bosnia and Herzegovina
33+
['BB', 'BRB', '052'], // Barbados
34+
['BD', 'BGD', '050'], // Bangladesh
35+
['BE', 'BEL', '056'], // Belgium
36+
['BF', 'BFA', '854'], // Burkina Faso
37+
['BG', 'BGR', '100'], // Bulgaria
38+
['BH', 'BHR', '048'], // Bahrain
39+
['BI', 'BDI', '108'], // Burundi
40+
['BJ', 'BEN', '204'], // Benin
41+
['BL', 'BLM', '652'], // Saint Barthelemy
42+
['BM', 'BMU', '060'], // Bermuda
43+
['BN', 'BRN', '096'], // Brunei
44+
['BO', 'BOL', '068'], // Bolivia
45+
['BQ', 'BES', '535'], // Bonaire, Saint Eustatius and Saba
46+
['BR', 'BRA', '076'], // Brazil
47+
['BS', 'BHS', '044'], // Bahamas
48+
['BT', 'BTN', '064'], // Bhutan
49+
['BV', 'BVT', '074'], // Bouvet Island
50+
['BW', 'BWA', '072'], // Botswana
51+
['BY', 'BLR', '112'], // Belarus
52+
['BZ', 'BLZ', '084'], // Belize
53+
['CA', 'CAN', '124'], // Canada
54+
['CC', 'CCK', '166'], // Cocos Islands
55+
['CD', 'COD', '180'], // Democratic Republic of the Congo
56+
['CF', 'CAF', '140'], // Central African Republic
57+
['CG', 'COG', '178'], // Republic of the Congo
58+
['CH', 'CHE', '756'], // Switzerland
59+
['CI', 'CIV', '384'], // Ivory Coast
60+
['CK', 'COK', '184'], // Cook Islands
61+
['CL', 'CHL', '152'], // Chile
62+
['CM', 'CMR', '120'], // Cameroon
63+
['CN', 'CHN', '156'], // China
64+
['CO', 'COL', '170'], // Colombia
65+
['CR', 'CRI', '188'], // Costa Rica
66+
['CS', 'SCG', '891'], // Serbia and Montenegro
67+
['CU', 'CUB', '192'], // Cuba
68+
['CV', 'CPV', '132'], // Cape Verde
69+
['CW', 'CUW', '531'], // Curacao
70+
['CX', 'CXR', '162'], // Christmas Island
71+
['CY', 'CYP', '196'], // Cyprus
72+
['CZ', 'CZE', '203'], // Czech Republic
73+
['DE', 'DEU', '276'], // Germany
74+
['DJ', 'DJI', '262'], // Djibouti
75+
['DK', 'DNK', '208'], // Denmark
76+
['DM', 'DMA', '212'], // Dominica
77+
['DO', 'DOM', '214'], // Dominican Republic
78+
['DZ', 'DZA', '012'], // Algeria
79+
['EC', 'ECU', '218'], // Ecuador
80+
['EE', 'EST', '233'], // Estonia
81+
['EG', 'EGY', '818'], // Egypt
82+
['EH', 'ESH', '732'], // Western Sahara
83+
['ER', 'ERI', '232'], // Eritrea
84+
['ES', 'ESP', '724'], // Spain
85+
['ET', 'ETH', '231'], // Ethiopia
86+
['FI', 'FIN', '246'], // Finland
87+
['FJ', 'FJI', '242'], // Fiji
88+
['FK', 'FLK', '238'], // Falkland Islands
89+
['FM', 'FSM', '583'], // Micronesia
90+
['FO', 'FRO', '234'], // Faroe Islands
91+
['FR', 'FRA', '250'], // France
92+
['GA', 'GAB', '266'], // Gabon
93+
['GB', 'GBR', '826'], // United Kingdom
94+
['GD', 'GRD', '308'], // Grenada
95+
['GE', 'GEO', '268'], // Georgia
96+
['GF', 'GUF', '254'], // French Guiana
97+
['GG', 'GGY', '831'], // Guernsey
98+
['GH', 'GHA', '288'], // Ghana
99+
['GI', 'GIB', '292'], // Gibraltar
100+
['GL', 'GRL', '304'], // Greenland
101+
['GM', 'GMB', '270'], // Gambia
102+
['GN', 'GIN', '324'], // Guinea
103+
['GP', 'GLP', '312'], // Guadeloupe
104+
['GQ', 'GNQ', '226'], // Equatorial Guinea
105+
['GR', 'GRC', '300'], // Greece
106+
['GS', 'SGS', '239'], // South Georgia and the South Sandwich Islands
107+
['GT', 'GTM', '320'], // Guatemala
108+
['GU', 'GUM', '316'], // Guam
109+
['GW', 'GNB', '624'], // Guinea-Bissau
110+
['GY', 'GUY', '328'], // Guyana
111+
['HK', 'HKG', '344'], // Hong Kong
112+
['HM', 'HMD', '334'], // Heard Island and McDonald Islands
113+
['HN', 'HND', '340'], // Honduras
114+
['HR', 'HRV', '191'], // Croatia
115+
['HT', 'HTI', '332'], // Haiti
116+
['HU', 'HUN', '348'], // Hungary
117+
['ID', 'IDN', '360'], // Indonesia
118+
['IE', 'IRL', '372'], // Ireland
119+
['IL', 'ISR', '376'], // Israel
120+
['IM', 'IMN', '833'], // Isle of Man
121+
['IN', 'IND', '356'], // India
122+
['IO', 'IOT', '086'], // British Indian Ocean Territory
123+
['IQ', 'IRQ', '368'], // Iraq
124+
['IR', 'IRN', '364'], // Iran
125+
['IS', 'ISL', '352'], // Iceland
126+
['IT', 'ITA', '380'], // Italy
127+
['JE', 'JEY', '832'], // Jersey
128+
['JM', 'JAM', '388'], // Jamaica
129+
['JO', 'JOR', '400'], // Jordan
130+
['JP', 'JPN', '392'], // Japan
131+
['KE', 'KEN', '404'], // Kenya
132+
['KG', 'KGZ', '417'], // Kyrgyzstan
133+
['KH', 'KHM', '116'], // Cambodia
134+
['KI', 'KIR', '296'], // Kiribati
135+
['KM', 'COM', '174'], // Comoros
136+
['KN', 'KNA', '659'], // Saint Kitts and Nevis
137+
['KP', 'PRK', '408'], // North Korea
138+
['KR', 'KOR', '410'], // South Korea
139+
['KW', 'KWT', '414'], // Kuwait
140+
['KY', 'CYM', '136'], // Cayman Islands
141+
['KZ', 'KAZ', '398'], // Kazakhstan
142+
['LA', 'LAO', '418'], // Laos
143+
['LB', 'LBN', '422'], // Lebanon
144+
['LC', 'LCA', '662'], // Saint Lucia
145+
['LI', 'LIE', '438'], // Liechtenstein
146+
['LK', 'LKA', '144'], // Sri Lanka
147+
['LR', 'LBR', '430'], // Liberia
148+
['LS', 'LSO', '426'], // Lesotho
149+
['LT', 'LTU', '440'], // Lithuania
150+
['LU', 'LUX', '442'], // Luxembourg
151+
['LV', 'LVA', '428'], // Latvia
152+
['LY', 'LBY', '434'], // Libya
153+
['MA', 'MAR', '504'], // Morocco
154+
['MC', 'MCO', '492'], // Monaco
155+
['MD', 'MDA', '498'], // Moldova
156+
['ME', 'MNE', '499'], // Montenegro
157+
['MF', 'MAF', '663'], // Saint Martin
158+
['MG', 'MDG', '450'], // Madagascar
159+
['MH', 'MHL', '584'], // Marshall Islands
160+
['MK', 'MKD', '807'], // Macedonia
161+
['ML', 'MLI', '466'], // Mali
162+
['MM', 'MMR', '104'], // Myanmar
163+
['MN', 'MNG', '496'], // Mongolia
164+
['MO', 'MAC', '446'], // Macao
165+
['MP', 'MNP', '580'], // Northern Mariana Islands
166+
['MQ', 'MTQ', '474'], // Martinique
167+
['MR', 'MRT', '478'], // Mauritania
168+
['MS', 'MSR', '500'], // Montserrat
169+
['MT', 'MLT', '470'], // Malta
170+
['MU', 'MUS', '480'], // Mauritius
171+
['MV', 'MDV', '462'], // Maldives
172+
['MW', 'MWI', '454'], // Malawi
173+
['MX', 'MEX', '484'], // Mexico
174+
['MY', 'MYS', '458'], // Malaysia
175+
['MZ', 'MOZ', '508'], // Mozambique
176+
['NA', 'NAM', '516'], // Namibia
177+
['NC', 'NCL', '540'], // New Caledonia
178+
['NE', 'NER', '562'], // Niger
179+
['NF', 'NFK', '574'], // Norfolk Island
180+
['NG', 'NGA', '566'], // Nigeria
181+
['NI', 'NIC', '558'], // Nicaragua
182+
['NL', 'NLD', '528'], // Netherlands
183+
['NO', 'NOR', '578'], // Norway
184+
['NP', 'NPL', '524'], // Nepal
185+
['NR', 'NRU', '520'], // Nauru
186+
['NU', 'NIU', '570'], // Niue
187+
['NZ', 'NZL', '554'], // New Zealand
188+
['OM', 'OMN', '512'], // Oman
189+
['PA', 'PAN', '591'], // Panama
190+
['PE', 'PER', '604'], // Peru
191+
['PF', 'PYF', '258'], // French Polynesia
192+
['PG', 'PNG', '598'], // Papua New Guinea
193+
['PH', 'PHL', '608'], // Philippines
194+
['PK', 'PAK', '586'], // Pakistan
195+
['PL', 'POL', '616'], // Poland
196+
['PM', 'SPM', '666'], // Saint Pierre and Miquelon
197+
['PN', 'PCN', '612'], // Pitcairn
198+
['PR', 'PRI', '630'], // Puerto Rico
199+
['PS', 'PSE', '275'], // Palestinian Territory
200+
['PT', 'PRT', '620'], // Portugal
201+
['PW', 'PLW', '585'], // Palau
202+
['PY', 'PRY', '600'], // Paraguay
203+
['QA', 'QAT', '634'], // Qatar
204+
['RE', 'REU', '638'], // Reunion
205+
['RO', 'ROU', '642'], // Romania
206+
['RS', 'SRB', '688'], // Serbia
207+
['RU', 'RUS', '643'], // Russia
208+
['RW', 'RWA', '646'], // Rwanda
209+
['SA', 'SAU', '682'], // Saudi Arabia
210+
['SB', 'SLB', '090'], // Solomon Islands
211+
['SC', 'SYC', '690'], // Seychelles
212+
['SD', 'SDN', '729'], // Sudan
213+
['SE', 'SWE', '752'], // Sweden
214+
['SG', 'SGP', '702'], // Singapore
215+
['SH', 'SHN', '654'], // Saint Helena
216+
['SI', 'SVN', '705'], // Slovenia
217+
['SJ', 'SJM', '744'], // Svalbard and Jan Mayen
218+
['SK', 'SVK', '703'], // Slovakia
219+
['SL', 'SLE', '694'], // Sierra Leone
220+
['SM', 'SMR', '674'], // San Marino
221+
['SN', 'SEN', '686'], // Senegal
222+
['SO', 'SOM', '706'], // Somalia
223+
['SR', 'SUR', '740'], // Suriname
224+
['SS', 'SSD', '728'], // South Sudan
225+
['ST', 'STP', '678'], // Sao Tome and Principe
226+
['SV', 'SLV', '222'], // El Salvador
227+
['SX', 'SXM', '534'], // Sint Maarten
228+
['SY', 'SYR', '760'], // Syria
229+
['SZ', 'SWZ', '748'], // Swaziland
230+
['TC', 'TCA', '796'], // Turks and Caicos Islands
231+
['TD', 'TCD', '148'], // Chad
232+
['TF', 'ATF', '260'], // French Southern Territories
233+
['TG', 'TGO', '768'], // Togo
234+
['TH', 'THA', '764'], // Thailand
235+
['TJ', 'TJK', '762'], // Tajikistan
236+
['TK', 'TKL', '772'], // Tokelau
237+
['TL', 'TLS', '626'], // East Timor
238+
['TM', 'TKM', '795'], // Turkmenistan
239+
['TN', 'TUN', '788'], // Tunisia
240+
['TO', 'TON', '776'], // Tonga
241+
['TR', 'TUR', '792'], // Turkey
242+
['TT', 'TTO', '780'], // Trinidad and Tobago
243+
['TV', 'TUV', '798'], // Tuvalu
244+
['TW', 'TWN', '158'], // Taiwan
245+
['TZ', 'TZA', '834'], // Tanzania
246+
['UA', 'UKR', '804'], // Ukraine
247+
['UG', 'UGA', '800'], // Uganda
248+
['UM', 'UMI', '581'], // United States Minor Outlying Islands
249+
['US', 'USA', '840'], // United States
250+
['UY', 'URY', '858'], // Uruguay
251+
['UZ', 'UZB', '860'], // Uzbekistan
252+
['VA', 'VAT', '336'], // Vatican
253+
['VC', 'VCT', '670'], // Saint Vincent and the Grenadines
254+
['VE', 'VEN', '862'], // Venezuela
255+
['VG', 'VGB', '092'], // British Virgin Islands
256+
['VI', 'VIR', '850'], // U.S. Virgin Islands
257+
['VN', 'VNM', '704'], // Vietnam
258+
['VU', 'VUT', '548'], // Vanuatu
259+
['WF', 'WLF', '876'], // Wallis and Futuna
260+
['WS', 'WSM', '882'], // Samoa
261+
['XK', 'XKX', '0'], // Kosovo
262+
['YE', 'YEM', '887'], // Yemen
263+
['YT', 'MYT', '175'], // Mayotte
264+
['ZA', 'ZAF', '710'], // South Africa
265+
['ZM', 'ZMB', '894'], // Zambia
266+
['ZW', 'ZWE', '716'] // Zimbabwe
267+
];
268+
269+
return new Map([
270+
['alpha-2', list.map((value: string[]) => value[0])],
271+
['alpha-3', list.map((value: string[]) => value[1])],
272+
['numeric', list.map((value: string[]) => value[2])]
273+
]);
274+
}
275+
276+
/**
277+
* Validate Service.
278+
*/
279+
protected validateService(service: string[], input: any): boolean {
280+
return new In(service, false).validate(input);
281+
}
282+
}

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from './cnpj';
1414
export * from './cntrl';
1515
export * from './consonant';
1616
export * from './contains';
17+
export * from './country-code';
1718
export * from './cpf';
1819
export * from './credit-card';
1920
export * from './currency-code';

test/rules/country-code.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { CountryCode } from '../../src/rules/country-code';
5+
6+
describe('CountryCode', () => {
7+
8+
let countryCode: CountryCode;
9+
10+
beforeEach(() => {
11+
countryCode = new CountryCode();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(countryCode, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(new CountryCode('alpha-2').validate('BR'));
20+
assert.isTrue(new CountryCode('alpha-3').validate('BRA'));
21+
assert.isTrue(new CountryCode('numeric').validate('076'));
22+
assert.isTrue(new CountryCode('alpha-2').validate('DE'));
23+
assert.isTrue(new CountryCode('alpha-3').validate('DEU'));
24+
assert.isTrue(new CountryCode('numeric').validate('276'));
25+
assert.isTrue(new CountryCode('alpha-2').validate('US'));
26+
assert.isTrue(new CountryCode('alpha-3').validate('USA'));
27+
assert.isTrue(new CountryCode('numeric').validate('840'));
28+
29+
assert.isTrue(countryCode.validate('BR'));
30+
assert.isTrue(countryCode.validate('BRA'));
31+
assert.isTrue(countryCode.validate('076'));
32+
assert.isTrue(countryCode.validate('DE'));
33+
assert.isTrue(countryCode.validate('DEU'));
34+
assert.isTrue(countryCode.validate('276'));
35+
assert.isTrue(countryCode.validate('US'));
36+
assert.isTrue(countryCode.validate('USA'));
37+
assert.isTrue(countryCode.validate('840'));
38+
});
39+
40+
it('values is not valid', () => {
41+
assert.isFalse(new CountryCode('alpha-2').validate('USA'));
42+
assert.isFalse(new CountryCode('alpha-3').validate('US'));
43+
assert.isFalse(new CountryCode('numeric').validate('000'));
44+
45+
assert.isFalse(countryCode.validate('text'));
46+
assert.isFalse(countryCode.validate(''));
47+
assert.isFalse(countryCode.validate('foo'));
48+
assert.isFalse(countryCode.validate(1.0));
49+
assert.isFalse(countryCode.validate('wrongtld'));
50+
assert.isFalse(countryCode.validate(null));
51+
assert.isFalse(countryCode.validate(undefined));
52+
assert.isFalse(countryCode.validate(true));
53+
assert.isFalse(countryCode.validate(false));
54+
assert.isFalse(countryCode.validate([]));
55+
assert.isFalse(countryCode.validate({}));
56+
assert.isFalse(countryCode.validate(new Array('foo')));
57+
assert.isFalse(countryCode.validate(new Object({foo: 'bar'})));
58+
59+
class Foo {}
60+
assert.isFalse(countryCode.validate(new Foo()));
61+
});
62+
63+
});

test/rules/currency-code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert } from 'chai';
33
import { AbstractRule } from '../../src/rules/abstract-rule';
44
import { CurrencyCode } from '../../src/rules/currency-code';
55

6-
describe('Tld', () => {
6+
describe('CurrencyCode', () => {
77

88
let currencyCode: CurrencyCode;
99

0 commit comments

Comments
 (0)