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

Commit f833241

Browse files
feat(rules): Added SubdivisionCode rule
1 parent 032ee85 commit f833241

261 files changed

Lines changed: 7969 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
147147
- [StringInstance](docs/string-instance.md)
148148
- [StringType](docs/string-type.md)
149149
- [StringVal](docs/string-val.md)
150+
- [SubdivisionCode](docs/subdivision-code.md)
150151
- [SymbolInstance](docs/symbol-instance.md)
151152
- [SymbolType](docs/symbol-type.md)
152153
- [SymbolVal](docs/symbol-val.md)

awesome-validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/subdivision-code.md

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
# Subdivision Code
2+
3+
Validates subdivision country codes according to [ISO 3166-2](http://en.wikipedia.org/wiki/ISO_3166-2).
4+
5+
The `$countryCode` must be a country in [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) format.
6+
7+
Valid values:
8+
9+
```js
10+
validator.subdivisionCode('br').validate('SP');
11+
validator.subdivisionCode('br', false).validate('sp');
12+
validator.subdivisionCode('BR').validate('SP');
13+
validator.subdivisionCode('BR', false).validate('sp');
14+
validator.subdivisionCode('us').validate('NY');
15+
validator.subdivisionCode('us', false).validate('ny');
16+
validator.subdivisionCode('US').validate('NY');
17+
validator.subdivisionCode('US', false).validate('ny');
18+
```
19+
20+
Invalid values:
21+
22+
```js
23+
validator.subdivisionCode('br').validate('sp');
24+
validator.subdivisionCode('br', false).validate('subdivisionCode');
25+
validator.subdivisionCode('BR').validate('sp');
26+
validator.subdivisionCode('BR', false).validate('subdivisionCode');
27+
validator.subdivisionCode('us').validate('ny');
28+
validator.subdivisionCode('us', false).validate('subdivisionCode');
29+
validator.subdivisionCode('US').validate('ny');
30+
validator.subdivisionCode('US', false).validate('subdivisionCode');
31+
```
32+
33+
## Available country codes
34+
35+
- `AD`: Andorra
36+
- `AE`: United Arab Emirates
37+
- `AF`: Afghanistan
38+
- `AG`: Antigua and Barbuda
39+
- `AI`: Anguilla
40+
- `AL`: Albania
41+
- `AM`: Armenia
42+
- `AN`: AN.html
43+
- `AO`: Angola
44+
- `AQ`: Antarctica
45+
- `AR`: Argentina
46+
- `AS`: American Samoa
47+
- `AT`: Austria
48+
- `AU`: Australia
49+
- `AW`: Aruba
50+
- `AX`: Åland
51+
- `AZ`: Azerbaijan
52+
- `BA`: Bosnia and Herzegovina
53+
- `BB`: Barbados
54+
- `BD`: Bangladesh
55+
- `BE`: Belgium
56+
- `BF`: Burkina Faso
57+
- `BG`: Bulgaria
58+
- `BH`: Bahrain
59+
- `BI`: Burundi
60+
- `BJ`: Benin
61+
- `BL`: Saint Barthélemy
62+
- `BM`: Bermuda
63+
- `BN`: Brunei
64+
- `BO`: Bolivia
65+
- `BQ`: Bonaire
66+
- `BR`: Brazil
67+
- `BS`: Bahamas
68+
- `BT`: Bhutan
69+
- `BV`: Bouvet Island
70+
- `BW`: Botswana
71+
- `BY`: Belarus
72+
- `BZ`: Belize
73+
- `CA`: Canada
74+
- `CC`: Cocos [Keeling] Islands
75+
- `CD`: Democratic Republic of the Congo
76+
- `CF`: Central African Republic
77+
- `CG`: Republic of the Congo
78+
- `CH`: Switzerland
79+
- `CI`: Ivory Coast
80+
- `CK`: Cook Islands
81+
- `CL`: Chile
82+
- `CM`: Cameroon
83+
- `CN`: China
84+
- `CO`: Colombia
85+
- `CR`: Costa Rica
86+
- `CS`: CS.html
87+
- `CU`: Cuba
88+
- `CV`: Cape Verde
89+
- `CW`: Curacao
90+
- `CX`: Christmas Island
91+
- `CY`: Cyprus
92+
- `CZ`: Czech Republic
93+
- `DE`: Germany
94+
- `DJ`: Djibouti
95+
- `DK`: Denmark
96+
- `DM`: Dominica
97+
- `DO`: Dominican Republic
98+
- `DZ`: Algeria
99+
- `EC`: Ecuador
100+
- `EE`: Estonia
101+
- `EG`: Egypt
102+
- `EH`: Western Sahara
103+
- `ER`: Eritrea
104+
- `ES`: Spain
105+
- `ET`: Ethiopia
106+
- `FI`: Finland
107+
- `FJ`: Fiji
108+
- `FK`: Falkland Islands
109+
- `FM`: Micronesia
110+
- `FO`: Faroe Islands
111+
- `FR`: France
112+
- `GA`: Gabon
113+
- `GB`: United Kingdom
114+
- `GD`: Grenada
115+
- `GE`: Georgia
116+
- `GF`: French Guiana
117+
- `GG`: Guernsey
118+
- `GH`: Ghana
119+
- `GI`: Gibraltar
120+
- `GL`: Greenland
121+
- `GM`: Gambia
122+
- `GN`: Guinea
123+
- `GP`: Guadeloupe
124+
- `GQ`: Equatorial Guinea
125+
- `GR`: Greece
126+
- `GS`: South Georgia and the South Sandwich Islands
127+
- `GT`: Guatemala
128+
- `GU`: Guam
129+
- `GW`: Guinea-Bissau
130+
- `GY`: Guyana
131+
- `HK`: Hong Kong
132+
- `HM`: Heard Island and McDonald Islands
133+
- `HN`: Honduras
134+
- `HR`: Croatia
135+
- `HT`: Haiti
136+
- `HU`: Hungary
137+
- `ID`: Indonesia
138+
- `IE`: Ireland
139+
- `IL`: Israel
140+
- `IM`: Isle of Man
141+
- `IN`: India
142+
- `IO`: British Indian Ocean Territory
143+
- `IQ`: Iraq
144+
- `IR`: Iran
145+
- `IS`: Iceland
146+
- `IT`: Italy
147+
- `JE`: Jersey
148+
- `JM`: Jamaica
149+
- `JO`: Jordan
150+
- `JP`: Japan
151+
- `KE`: Kenya
152+
- `KG`: Kyrgyzstan
153+
- `KH`: Cambodia
154+
- `KI`: Kiribati
155+
- `KM`: Comoros
156+
- `KN`: Saint Kitts and Nevis
157+
- `KP`: North Korea
158+
- `KR`: South Korea
159+
- `KW`: Kuwait
160+
- `KY`: Cayman Islands
161+
- `KZ`: Kazakhstan
162+
- `LA`: Laos
163+
- `LB`: Lebanon
164+
- `LC`: Saint Lucia
165+
- `LI`: Liechtenstein
166+
- `LK`: Sri Lanka
167+
- `LR`: Liberia
168+
- `LS`: Lesotho
169+
- `LT`: Lithuania
170+
- `LU`: Luxembourg
171+
- `LV`: Latvia
172+
- `LY`: Libya
173+
- `MA`: Morocco
174+
- `MC`: Monaco
175+
- `MD`: Moldova
176+
- `ME`: Montenegro
177+
- `MF`: Saint Martin
178+
- `MG`: Madagascar
179+
- `MH`: Marshall Islands
180+
- `MK`: Macedonia
181+
- `ML`: Mali
182+
- `MM`: Myanmar [Burma]
183+
- `MN`: Mongolia
184+
- `MO`: Macao
185+
- `MP`: Northern Mariana Islands
186+
- `MQ`: Martinique
187+
- `MR`: Mauritania
188+
- `MS`: Montserrat
189+
- `MT`: Malta
190+
- `MU`: Mauritius
191+
- `MV`: Maldives
192+
- `MW`: Malawi
193+
- `MX`: Mexico
194+
- `MY`: Malaysia
195+
- `MZ`: Mozambique
196+
- `NA`: Namibia
197+
- `NC`: New Caledonia
198+
- `NE`: Niger
199+
- `NF`: Norfolk Island
200+
- `NG`: Nigeria
201+
- `NI`: Nicaragua
202+
- `NL`: Netherlands
203+
- `NO`: Norway
204+
- `NP`: Nepal
205+
- `NR`: Nauru
206+
- `NU`: Niue
207+
- `NZ`: New Zealand
208+
- `OM`: Oman
209+
- `PA`: Panama
210+
- `PE`: Peru
211+
- `PF`: French Polynesia
212+
- `PG`: Papua New Guinea
213+
- `PH`: Philippines
214+
- `PK`: Pakistan
215+
- `PL`: Poland
216+
- `PM`: Saint Pierre and Miquelon
217+
- `PN`: Pitcairn Islands
218+
- `PR`: Puerto Rico
219+
- `PS`: Palestine
220+
- `PT`: Portugal
221+
- `PW`: Palau
222+
- `PY`: Paraguay
223+
- `QA`: Qatar
224+
- `RE`: Réunion
225+
- `RO`: Romania
226+
- `RS`: Serbia
227+
- `RU`: Russia
228+
- `RW`: Rwanda
229+
- `SA`: Saudi Arabia
230+
- `SB`: Solomon Islands
231+
- `SC`: Seychelles
232+
- `SD`: Sudan
233+
- `SE`: Sweden
234+
- `SG`: Singapore
235+
- `SH`: Saint Helena
236+
- `SI`: Slovenia
237+
- `SJ`: Svalbard and Jan Mayen
238+
- `SK`: Slovakia
239+
- `SL`: Sierra Leone
240+
- `SM`: San Marino
241+
- `SN`: Senegal
242+
- `SO`: Somalia
243+
- `SR`: Suriname
244+
- `SS`: South Sudan
245+
- `ST`: São Tomé and Príncipe
246+
- `SV`: El Salvador
247+
- `SX`: Sint Maarten
248+
- `SY`: Syria
249+
- `SZ`: Swaziland
250+
- `TC`: Turks and Caicos Islands
251+
- `TD`: Chad
252+
- `TF`: French Southern Territories
253+
- `TG`: Togo
254+
- `TH`: Thailand
255+
- `TJ`: Tajikistan
256+
- `TK`: Tokelau
257+
- `TL`: East Timor
258+
- `TM`: Turkmenistan
259+
- `TN`: Tunisia
260+
- `TO`: Tonga
261+
- `TR`: Turkey
262+
- `TT`: Trinidad and Tobago
263+
- `TV`: Tuvalu
264+
- `TW`: Taiwan
265+
- `TZ`: Tanzania
266+
- `UA`: Ukraine
267+
- `UG`: Uganda
268+
- `UM`: U.S. Minor Outlying Islands
269+
- `US`: United States
270+
- `UY`: Uruguay
271+
- `UZ`: Uzbekistan
272+
- `VA`: Vatican City
273+
- `VC`: Saint Vincent and the Grenadines
274+
- `VE`: Venezuela
275+
- `VG`: British Virgin Islands
276+
- `VI`: U.S. Virgin Islands
277+
- `VN`: Vietnam
278+
- `VU`: Vanuatu
279+
- `WF`: Wallis and Futuna
280+
- `WS`: Samoa
281+
- `XK`: Kosovo
282+
- `YE`: Yemen
283+
- `YT`: Mayotte
284+
- `ZA`: South Africa
285+
- `ZM`: Zambia
286+
- `ZW`: Zimbabwe
287+
288+
All data was extrated from [GeoNames](http://www.geonames.org) which is licensed under a
289+
[Creative Commons Attribution 3.0 License](http://creativecommons.org/licenses/by/3.0).

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export * from './space';
6868
export * from './string-instance';
6969
export * from './string-type';
7070
export * from './string-val';
71+
export * from './subdivision-code';
7172
export * from './symbol-instance';
7273
export * from './symbol-type';
7374
export * from './symbol-val';

src/rules/subdivision-code.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AbstractRule } from './abstract-rule';
2+
import { In } from './in';
3+
4+
import * as subdivisions from './subdivisions';
5+
6+
export class SubdivisionCode extends AbstractRule {
7+
8+
/**
9+
* SubdivisionCode.
10+
*/
11+
public constructor(
12+
public readonly countryCode: string,
13+
public readonly identical: boolean = true
14+
) {
15+
super();
16+
}
17+
18+
/**
19+
* Validate.
20+
*/
21+
public validate(input: any): boolean {
22+
const countryCode: string = this.countryCode.toLocaleUpperCase();
23+
24+
if (!subdivisions[countryCode]) {
25+
return false;
26+
}
27+
28+
return new In(subdivisions[countryCode], false, this.identical).validate(input);
29+
}
30+
}

src/rules/subdivisions/ad.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Andorra subdivision code.
3+
*
4+
* ISO 3166-1 alpha-2: AD
5+
*
6+
* @link http://www.geonames.org/AD/administrative-division-andorra.html
7+
*/
8+
export const AD: string[] = [
9+
'02', // Canillo
10+
'03', // Encamp
11+
'04', // La Massana
12+
'05', // Ordino
13+
'06', // Sant Julia de Lòria
14+
'07', // Andorra la Vella
15+
'08' // Escaldes-Engordany
16+
];

src/rules/subdivisions/ae.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* United Arab Emirates subdivision code.
3+
*
4+
* ISO 3166-1 alpha-2: AE
5+
*
6+
* @link http://www.geonames.org/AE/administrative-division-united-arab-emirates.html
7+
*/
8+
export const AE: string[] = [
9+
'AJ', // 'Ajman
10+
'AZ', // Abu Zaby
11+
'DU', // Dubayy
12+
'FU', // Al Fujayrah
13+
'RK', // R'as al Khaymah
14+
'SH', // Ash Shariqah
15+
'UQ' // Umm al Qaywayn
16+
];

0 commit comments

Comments
 (0)