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

Commit ce9959c

Browse files
feat(rules): Added Locale rule
1 parent 634830c commit ce9959c

5 files changed

Lines changed: 319 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
163163
- [Latitude](docs/latitude.md)
164164
- [LeapDate](docs/leap-date.md)
165165
- [LeapYear](docs/leap-year.md)
166+
- [Locale](docs/locale.md)
166167
- [Longitude](docs/longitude.md)
167168
- [Lowercase](docs/lowercase.md)
168169
- [Luhn](docs/luhn.md)

docs/locale.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Locale
2+
3+
Validates if the given input is a locale.
4+
5+
Valid values:
6+
7+
```js
8+
validator.locale().validate('en_US');
9+
validator.locale().validate('pt_BR');
10+
validator.locale().validate('fr_FR');
11+
```
12+
13+
Invalid values:
14+
15+
```js
16+
validator.locale().validate('invalid_locale');
17+
validator.locale().validate(null);
18+
validator.locale().validate(undefined);
19+
```

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export * from './language-code';
8181
export * from './latitude';
8282
export * from './leap-date';
8383
export * from './leap-year';
84+
export * from './locale';
8485
export * from './longitude';
8586
export * from './lowercase';
8687
export * from './luhn';

src/rules/locale.ts

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
import { Validatable } from './../validatable';
2+
import { AbstractWrapper } from './abstract-wrapper';
3+
import { In } from './in';
4+
5+
export class Locale extends AbstractWrapper {
6+
7+
/**
8+
* Get Validatable.
9+
*/
10+
protected getValidatable(input: any): Validatable {
11+
const searcher: string[] = [
12+
'aa_DJ',
13+
'aa_ER',
14+
'aa_ET',
15+
'af_ZA',
16+
'am_ET',
17+
'an_ES',
18+
'ar_AE',
19+
'ar_BH',
20+
'ar_DZ',
21+
'ar_EG',
22+
'ar_IN',
23+
'ar_IQ',
24+
'ar_JO',
25+
'ar_KW',
26+
'ar_LB',
27+
'ar_LY',
28+
'ar_MA',
29+
'ar_OM',
30+
'ar_QA',
31+
'ar_SA',
32+
'ar_SD',
33+
'ar_SY',
34+
'ar_TN',
35+
'ar_YE',
36+
'as_IN',
37+
'ast_ES',
38+
'az_AZ',
39+
'be_BY',
40+
'ber_DZ',
41+
'ber_MA',
42+
'bg_BG',
43+
'bn_BD',
44+
'bn_IN',
45+
'bo_CN',
46+
'bo_IN',
47+
'br_FR',
48+
'bs_BA',
49+
'byn_ER',
50+
'ca_AD',
51+
'ca_ES',
52+
'ca_FR',
53+
'ca_IT',
54+
'crh_UA',
55+
'cs_CZ',
56+
'csb_PL',
57+
'cy_GB',
58+
'da_DK',
59+
'de_AT',
60+
'de_BE',
61+
'de_CH',
62+
'de_DE',
63+
'de_LI',
64+
'de_LU',
65+
'dv_MV',
66+
'dz_BT',
67+
'el_CY',
68+
'el_GR',
69+
'en_AG',
70+
'en_AU',
71+
'en_BW',
72+
'en_CA',
73+
'en_DK',
74+
'en_GB',
75+
'en_HK',
76+
'en_IE',
77+
'en_IN',
78+
'en_NG',
79+
'en_NZ',
80+
'en_PH',
81+
'en_SG',
82+
'en_US',
83+
'en_ZA',
84+
'en_ZW',
85+
'eo',
86+
'es_AR',
87+
'es_BO',
88+
'es_CL',
89+
'es_CO',
90+
'es_CR',
91+
'es_DO',
92+
'es_EC',
93+
'es_ES',
94+
'es_GT',
95+
'es_HN',
96+
'es_MX',
97+
'es_NI',
98+
'es_PA',
99+
'es_PE',
100+
'es_PR',
101+
'es_PY',
102+
'es_SV',
103+
'es_US',
104+
'es_UY',
105+
'es_VE',
106+
'et_EE',
107+
'eu_ES',
108+
'eu_FR',
109+
'fa_IR',
110+
'fi_FI',
111+
'fil_PH',
112+
'fo_FO',
113+
'fr_BE',
114+
'fr_CA',
115+
'fr_CH',
116+
'fr_FR',
117+
'fr_LU',
118+
'fur_IT',
119+
'fy_DE',
120+
'fy_NL',
121+
'ga_IE',
122+
'gd_GB',
123+
'gez_ER',
124+
'gez_ET',
125+
'gl_ES',
126+
'gu_IN',
127+
'gv_GB',
128+
'ha_NG',
129+
'he_IL',
130+
'hi_IN',
131+
'hne_IN',
132+
'hr_HR',
133+
'hsb_DE',
134+
'ht_HT',
135+
'hu_HU',
136+
'hy_AM',
137+
'ia',
138+
'id_ID',
139+
'ig_NG',
140+
'ik_CA',
141+
'is_IS',
142+
'it_CH',
143+
'it_IT',
144+
'iu_CA',
145+
'iw_IL',
146+
'ja_JP',
147+
'ka_GE',
148+
'kk_KZ',
149+
'kl_GL',
150+
'km_KH',
151+
'kn_IN',
152+
'ko_KR',
153+
'ks_IN',
154+
'ku_TR',
155+
'kw_GB',
156+
'ky_KG',
157+
'lg_UG',
158+
'li_BE',
159+
'li_NL',
160+
'lo_LA',
161+
'lt_LT',
162+
'lv_LV',
163+
'mai_IN',
164+
'mg_MG',
165+
'mi_NZ',
166+
'mk_MK',
167+
'ml_IN',
168+
'mn_MN',
169+
'mr_IN',
170+
'ms_MY',
171+
'mt_MT',
172+
'my_MM',
173+
'nb_NO',
174+
'nds_DE',
175+
'nds_NL',
176+
'ne_NP',
177+
'nl_AW',
178+
'nl_BE',
179+
'nl_NL',
180+
'nn_NO',
181+
'no_NO',
182+
'nr_ZA',
183+
'nso_ZA',
184+
'oc_FR',
185+
'om_ET',
186+
'om_KE',
187+
'or_IN',
188+
'pa_IN',
189+
'pa_PK',
190+
'pap_AN',
191+
'pl_PL',
192+
'ps_AF',
193+
'pt_BR',
194+
'pt_PT',
195+
'ro_RO',
196+
'ru_RU',
197+
'ru_UA',
198+
'rw_RW',
199+
'sa_IN',
200+
'sc_IT',
201+
'sd_IN',
202+
'se_NO',
203+
'shs_CA',
204+
'si_LK',
205+
'sid_ET',
206+
'sk_SK',
207+
'sl_SI',
208+
'so_DJ',
209+
'so_ET',
210+
'so_KE',
211+
'so_SO',
212+
'sq_AL',
213+
'sr_CS',
214+
'sr_ME',
215+
'sr_RS',
216+
'sr_YU',
217+
'ss_ZA',
218+
'st_ZA',
219+
'sv_FI',
220+
'sv_SE',
221+
'ta_IN',
222+
'te_IN',
223+
'tg_TJ',
224+
'th_TH',
225+
'ti_ER',
226+
'ti_ET',
227+
'tig_ER',
228+
'tk_TM',
229+
'tl_PH',
230+
'tn_ZA',
231+
'tr_CY',
232+
'tr_TR',
233+
'ts_ZA',
234+
'tt_RU',
235+
'ug_CN',
236+
'uk_UA',
237+
'ur_PK',
238+
'uz_UZ',
239+
've_ZA',
240+
'vi_VN',
241+
'wa_BE',
242+
'wo_SN',
243+
'xh_ZA',
244+
'yi_US',
245+
'yo_NG',
246+
'zh_CN',
247+
'zh_HK',
248+
'zh_SG',
249+
'zh_TW',
250+
'zu_ZA'
251+
];
252+
253+
return new In(searcher, false);
254+
}
255+
}
256+
257+
export default Locale;

test/rules/locale.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Locale } from '../../src/rules/locale';
5+
6+
describe('Locale', () => {
7+
8+
let locale: Locale;
9+
10+
beforeEach(() => {
11+
locale = new Locale();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(locale, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(locale.validate('en_US'));
20+
assert.isTrue(locale.validate('pt_BR'));
21+
assert.isTrue(locale.validate('fr_FR'));
22+
});
23+
24+
it('values is not valid', () => {
25+
assert.isFalse(locale.validate('1'));
26+
assert.isFalse(locale.validate(1.0));
27+
assert.isFalse(locale.validate('invalid_locale'));
28+
assert.isFalse(locale.validate(null));
29+
assert.isFalse(locale.validate(undefined));
30+
assert.isFalse(locale.validate(true));
31+
assert.isFalse(locale.validate(false));
32+
assert.isFalse(locale.validate([]));
33+
assert.isFalse(locale.validate({}));
34+
assert.isFalse(locale.validate(new Array('foo')));
35+
assert.isFalse(locale.validate(new Object({foo: 'bar'})));
36+
37+
class Foo {}
38+
assert.isFalse(locale.validate(new Foo()));
39+
});
40+
41+
});

0 commit comments

Comments
 (0)