Class: ValidatesCountrycode::Validator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validates_countrycode.rb

Overview

rubocop:disable ClassLength Validates a country-code against existing country codes, such as US, BE, DK etc.

Author:

  • Frederik Spang Thomsen

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.country_codesActiveSupport::HashWithIndifferentAccess

Returns matching country_codes

Examples:

Using country codes with symbol key

CountryCodeValidator.country_codes[:DK] # => "DENMARK"

Using country codes with string key

CountryCodeValidator.country_codes['DK'] # => "DENMARK"

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    with KEY => COUNTRY



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/validates_countrycode.rb', line 25

def self.country_codes
  {
    AF: 'AFGHANISTAN',
    AX: 'ÅLAND ISLANDS',
    AL: 'ALBANIA',
    DZ: 'ALGERIA',
    AS: 'AMERICAN SAMOA',
    AD: 'ANDORRA',
    AO: 'ANGOLA',
    AI: 'ANGUILLA',
    AQ: 'ANTARCTICA',
    AG: 'ANTIGUA AND BARBUDA',
    AR: 'ARGENTINA',
    AM: 'ARMENIA',
    AW: 'ARUBA',
    AU: 'AUSTRALIA',
    AT: 'AUSTRIA',
    AZ: 'AZERBAIJAN',
    BS: 'BAHAMAS',
    BH: 'BAHRAIN',
    BD: 'BANGLADESH',
    BB: 'BARBADOS',
    BY: 'BELARUS',
    BE: 'BELGIUM',
    BZ: 'BELIZE',
    BJ: 'BENIN',
    BM: 'BERMUDA',
    BT: 'BHUTAN',
    BO: 'BOLIVIA, PLURINATIONAL STATE OF',
    BQ: 'BONAIRE, SINT EUSTATIUS AND SABA',
    BA: 'BOSNIA AND HERZEGOVINA',
    BW: 'BOTSWANA',
    BV: 'BOUVET ISLAND',
    BR: 'BRAZIL',
    IO: 'BRITISH INDIAN OCEAN TERRITORY',
    BN: 'BRUNEI DARUSSALAM',
    BG: 'BULGARIA',
    BF: 'BURKINA FASO',
    BI: 'BURUNDI',
    KH: 'CAMBODIA',
    CM: 'CAMEROON',
    CA: 'CANADA',
    CV: 'CAPE VERDE',
    KY: 'CAYMAN ISLANDS',
    CF: 'CENTRAL AFRICAN REPUBLIC',
    TD: 'CHAD',
    CL: 'CHILE',
    CN: 'CHINA',
    CX: 'CHRISTMAS ISLAND',
    CC: 'COCOS (KEELING) ISLANDS',
    CO: 'COLOMBIA',
    KM: 'COMOROS',
    CG: 'CONGO',
    CD: 'CONGO, THE DEMOCRATIC REPUBLIC OF THE',
    CK: 'COOK ISLANDS',
    CR: 'COSTA RICA',
    CI: "CÔTE D'IVOIRE",
    HR: 'CROATIA',
    CU: 'CUBA',
    CW: 'CURAÇAO',
    CY: 'CYPRUS',
    CZ: 'CZECH REPUBLIC',
    DK: 'DENMARK',
    DJ: 'DJIBOUTI',
    DM: 'DOMINICA',
    DO: 'DOMINICAN REPUBLIC',
    EC: 'ECUADOR',
    EG: 'EGYPT',
    SV: 'EL SALVADOR',
    GQ: 'EQUATORIAL GUINEA',
    ER: 'ERITREA',
    EE: 'ESTONIA',
    ET: 'ETHIOPIA',
    FK: 'FALKLAND ISLANDS (MALVINAS)',
    FO: 'FAROE ISLANDS',
    FJ: 'FIJI',
    FI: 'FINLAND',
    FR: 'FRANCE',
    GF: 'FRENCH GUIANA',
    PF: 'FRENCH POLYNESIA',
    TF: 'FRENCH SOUTHERN TERRITORIES',
    GA: 'GABON',
    GM: 'GAMBIA',
    GE: 'GEORGIA',
    DE: 'GERMANY',
    GH: 'GHANA',
    GI: 'GIBRALTAR',
    GR: 'GREECE',
    GL: 'GREENLAND',
    GD: 'GRENADA',
    GP: 'GUADELOUPE',
    GU: 'GUAM',
    GT: 'GUATEMALA',
    GG: 'GUERNSEY',
    GN: 'GUINEA',
    GW: 'GUINEA-BISSAU',
    GY: 'GUYANA',
    HT: 'HAITI',
    HM: 'HEARD ISLAND AND MCDONALD ISLANDS',
    VA: 'HOLY SEE (VATICAN CITY STATE)',
    HN: 'HONDURAS',
    HK: 'HONG KONG',
    HU: 'HUNGARY',
    IS: 'ICELAND',
    IN: 'INDIA',
    ID: 'INDONESIA',
    IR: 'IRAN, ISLAMIC REPUBLIC OF',
    IQ: 'IRAQ',
    IE: 'IRELAND',
    IM: 'ISLE OF MAN',
    IL: 'ISRAEL',
    IT: 'ITALY',
    JM: 'JAMAICA',
    JP: 'JAPAN',
    JE: 'JERSEY',
    JO: 'JORDAN',
    KZ: 'KAZAKHSTAN',
    KE: 'KENYA',
    KI: 'KIRIBATI',
    KP: "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF",
    KR: 'KOREA, REPUBLIC OF',
    KW: 'KUWAIT',
    KG: 'KYRGYZSTAN',
    LA: "LAO PEOPLE'S DEMOCRATIC REPUBLIC",
    LV: 'LATVIA',
    LB: 'LEBANON',
    LS: 'LESOTHO',
    LR: 'LIBERIA',
    LY: 'LIBYA',
    LI: 'LIECHTENSTEIN',
    LT: 'LITHUANIA',
    LU: 'LUXEMBOURG',
    MO: 'MACAO',
    MK: 'MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF',
    MG: 'MADAGASCAR',
    MW: 'MALAWI',
    MY: 'MALAYSIA',
    MV: 'MALDIVES',
    ML: 'MALI',
    MT: 'MALTA',
    MH: 'MARSHALL ISLANDS',
    MQ: 'MARTINIQUE',
    MR: 'MAURITANIA',
    MU: 'MAURITIUS',
    YT: 'MAYOTTE',
    MX: 'MEXICO',
    FM: 'MICRONESIA, FEDERATED STATES OF',
    MD: 'MOLDOVA, REPUBLIC OF',
    MC: 'MONACO',
    MN: 'MONGOLIA',
    ME: 'MONTENEGRO',
    MS: 'MONTSERRAT',
    MA: 'MOROCCO',
    MZ: 'MOZAMBIQUE',
    MM: 'MYANMAR',
    NA: 'NAMIBIA',
    NR: 'NAURU',
    NP: 'NEPAL',
    NL: 'NETHERLANDS',
    NC: 'NEW CALEDONIA',
    NZ: 'NEW ZEALAND',
    NI: 'NICARAGUA',
    NE: 'NIGER',
    NG: 'NIGERIA',
    NU: 'NIUE',
    NF: 'NORFOLK ISLAND',
    MP: 'NORTHERN MARIANA ISLANDS',
    NO: 'NORWAY',
    OM: 'OMAN',
    PK: 'PAKISTAN',
    PW: 'PALAU',
    PS: 'PALESTINE, STATE OF',
    PA: 'PANAMA',
    PG: 'PAPUA NEW GUINEA',
    PY: 'PARAGUAY',
    PE: 'PERU',
    PH: 'PHILIPPINES',
    PN: 'PITCAIRN',
    PL: 'POLAND',
    PT: 'PORTUGAL',
    PR: 'PUERTO RICO',
    QA: 'QATAR',
    RE: 'RÉUNION',
    RO: 'ROMANIA',
    RU: 'RUSSIAN FEDERATION',
    RW: 'RWANDA',
    BL: 'SAINT BARTHÉLEMY',
    SH: 'SAINT HELENA, ASCENSION AND TRISTAN DA CUNHA',
    KN: 'SAINT KITTS AND NEVIS',
    LC: 'SAINT LUCIA',
    MF: 'SAINT MARTIN (FRENCH PART)',
    PM: 'SAINT PIERRE AND MIQUELON',
    VC: 'SAINT VINCENT AND THE GRENADINES',
    WS: 'SAMOA',
    SM: 'SAN MARINO',
    ST: 'SAO TOME AND PRINCIPE',
    SA: 'SAUDI ARABIA',
    SN: 'SENEGAL',
    RS: 'SERBIA',
    SC: 'SEYCHELLES',
    SL: 'SIERRA LEONE',
    SG: 'SINGAPORE',
    SX: 'SINT MAARTEN (DUTCH PART)',
    SK: 'SLOVAKIA',
    SI: 'SLOVENIA',
    SB: 'SOLOMON ISLANDS',
    SO: 'SOMALIA',
    ZA: 'SOUTH AFRICA',
    GS: 'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS',
    SS: 'SOUTH SUDAN',
    ES: 'SPAIN',
    LK: 'SRI LANKA',
    SD: 'SUDAN',
    SR: 'SURINAME',
    SJ: 'SVALBARD AND JAN MAYEN',
    SZ: 'SWAZILAND',
    SE: 'SWEDEN',
    CH: 'SWITZERLAND',
    SY: 'SYRIAN ARAB REPUBLIC',
    TW: 'TAIWAN, PROVINCE OF CHINA',
    TJ: 'TAJIKISTAN',
    TZ: 'TANZANIA, UNITED REPUBLIC OF',
    TH: 'THAILAND',
    TL: 'TIMOR-LESTE',
    TG: 'TOGO',
    TK: 'TOKELAU',
    TO: 'TONGA',
    TT: 'TRINIDAD AND TOBAGO',
    TN: 'TUNISIA',
    TR: 'TURKEY',
    TM: 'TURKMENISTAN',
    TC: 'TURKS AND CAICOS ISLANDS',
    TV: 'TUVALU',
    UG: 'UGANDA',
    UA: 'UKRAINE',
    AE: 'UNITED ARAB EMIRATES',
    GB: 'UNITED KINGDOM',
    US: 'UNITED STATES',
    UM: 'UNITED STATES MINOR OUTLYING ISLANDS',
    UY: 'URUGUAY',
    UZ: 'UZBEKISTAN',
    VU: 'VANUATU',
    VE: 'VENEZUELA, BOLIVARIAN REPUBLIC OF',
    VN: 'VIETNAM',
    VG: 'VIRGIN ISLANDS, BRITISH',
    VI: 'VIRGIN ISLANDS, U.S.',
    WF: 'WALLIS AND FUTUNA',
    EH: 'WESTERN SAHARA',
    YE: 'YEMEN',
    ZM: 'ZAMBIA',
    ZW: 'ZIMBABWE'
  }.with_indifferent_access
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Adds



12
13
14
15
16
# File 'lib/validates_countrycode.rb', line 12

def validate_each(record, attribute, value)
  unless self.class.country_codes[value.to_s.upcase].present?
    record.errors[attribute] << record.errors.generate_message(attribute, :invalid)
  end
end