validates_zipcode 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/README.md +7 -6
- data/lib/validates_zipcode/cldr_regex_collection.rb +174 -0
- data/lib/validates_zipcode/validator.rb +4 -14
- data/lib/validates_zipcode/version.rb +1 -1
- data/lib/validates_zipcode.rb +1 -0
- data/validates_zipcode.gemspec +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb84ecd52313d0e077ddae839ea70e3afcb0dfcc
|
4
|
+
data.tar.gz: fe461836cc00d0a52050414377ba3a420e7b9ea6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3e1ad47b5e8fab5075bd2fa101711c7b36658429c18fd1c4d815ca80cdabe911835dd7a21006f7ceb9b4bc8e0f2a7c4c82901eae89860128d9ce0113ce50e86
|
7
|
+
data.tar.gz: d8fb4c72df0d51444465590e3b84efd6e74905efb0261c25743f2944007a2119c1ef2d877c66d636450b3baae79a24c04cf3c064da3e20d97db22cbced92a9f4
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
# ValidatesZipcode
|
1
|
+
# ValidatesZipcode [![Build Status](https://secure.travis-ci.org/dgilperez/validates_zipcode.png)](https://travis-ci.org/dgilperez/validates_zipcode) [![Gem Version](https://badge.fury.io/rb/validates_zipcode.svg)](http://badge.fury.io/rb/validates_zipcode)
|
2
2
|
|
3
3
|
Add zipcode validation support to Rails (ActiveModel), considering different zipcode country formats.
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
``ValidatesZipcode`` currently support **159 countries**. Regex data taken from several sources, mainly from the [CLDR](common/supplemental/postalCodeData.xml) database. Any other country's zipcode will validate without errors.
|
6
|
+
|
7
|
+
``ValidatesZipcode`` works for Rails 3 & 4 and Ruby >= 1.9.3.
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -41,7 +42,7 @@ If you need to localize the error message, just add this to your I18n locale fil
|
|
41
42
|
|
42
43
|
## Contributing
|
43
44
|
|
44
|
-
1. Fork it
|
45
|
+
1. Fork it
|
45
46
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
47
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
48
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -49,11 +50,11 @@ If you need to localize the error message, just add this to your I18n locale fil
|
|
49
50
|
|
50
51
|
## Contributors
|
51
52
|
|
52
|
-
To see the generous people who have contributed code, take a look at the
|
53
|
+
To see the generous people who have contributed code, take a look at the [contributors list](http://github.com/dgilperez/validates_zipcode/contributors).
|
53
54
|
|
54
55
|
## Maintainers
|
55
56
|
|
56
|
-
*
|
57
|
+
* [David Gil](http://github.com/dgilperez)
|
57
58
|
|
58
59
|
## License
|
59
60
|
|
@@ -0,0 +1,174 @@
|
|
1
|
+
# Regex source: http://unicode.org/cldr/trac/browser/trunk/common/supplemental/postalCodeData.xml
|
2
|
+
# 159 countries supported
|
3
|
+
#
|
4
|
+
module ValidatesZipcode
|
5
|
+
module CldrRegexpCollection
|
6
|
+
ZIPCODES_REGEX = {
|
7
|
+
GB: /\AGIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF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3}[ ]?\d{2}\z/,
|
107
|
+
SI: /\A\d{4}\z/,
|
108
|
+
ZA: /\A\d{4}\z/,
|
109
|
+
LK: /\A\d{5}\z/,
|
110
|
+
TJ: /\A\d{6}\z/,
|
111
|
+
TH: /\A\d{5}\z/,
|
112
|
+
TN: /\A\d{4}\z/,
|
113
|
+
TR: /\A\d{5}\z/,
|
114
|
+
TM: /\A\d{6}\z/,
|
115
|
+
UA: /\A\d{5}\z/,
|
116
|
+
UY: /\A\d{5}\z/,
|
117
|
+
UZ: /\A\d{6}\z/,
|
118
|
+
VA: /\A00120\z/,
|
119
|
+
VE: /\A\d{4}\z/,
|
120
|
+
ZM: /\A\d{5}\z/,
|
121
|
+
AS: /\A96799\z/,
|
122
|
+
CC: /\A6799\z/,
|
123
|
+
CK: /\A\d{4}\z/,
|
124
|
+
RS: /\A\d{6}\z/,
|
125
|
+
ME: /\A8\d{4}\z/,
|
126
|
+
CS: /\A\d{5}\z/,
|
127
|
+
YU: /\A\d{5}\z/,
|
128
|
+
CX: /\A6798\z/,
|
129
|
+
ET: /\A\d{4}\z/,
|
130
|
+
FK: /\AFIQQ 1ZZ\z/,
|
131
|
+
NF: /\A2899\z/,
|
132
|
+
FM: /\A(9694[1-4])([ \-]\d{4})?\z/,
|
133
|
+
GF: /\A9[78]3\d{2}\z/,
|
134
|
+
GN: /\A\d{3}\z/,
|
135
|
+
GP: /\A9[78][01]\d{2}\z/,
|
136
|
+
GS: /\ASIQQ 1ZZ\z/,
|
137
|
+
GU: /\A969[123]\d([ \-]\d{4})?\z/,
|
138
|
+
GW: /\A\d{4}\z/,
|
139
|
+
HM: /\A\d{4}\z/,
|
140
|
+
IQ: /\A\d{5}\z/,
|
141
|
+
KG: /\A\d{6}\z/,
|
142
|
+
LR: /\A\d{4}\z/,
|
143
|
+
LS: /\A\d{3}\z/,
|
144
|
+
MG: /\A\d{3}\z/,
|
145
|
+
MH: /\A969[67]\d([ \-]\d{4})?\z/,
|
146
|
+
MN: /\A\d{6}\z/,
|
147
|
+
MP: /\A9695[012]([ \-]\d{4})?\z/,
|
148
|
+
MQ: /\A9[78]2\d{2}\z/,
|
149
|
+
NC: /\A988\d{2}\z/,
|
150
|
+
NE: /\A\d{4}\z/,
|
151
|
+
VI: /\A008(([0-4]\d)|(5[01]))([ \-]\d{4})?\z/,
|
152
|
+
PF: /\A987\d{2}\z/,
|
153
|
+
PG: /\A\d{3}\z/,
|
154
|
+
PM: /\A9[78]5\d{2}\z/,
|
155
|
+
PN: /\APCRN 1ZZ\z/,
|
156
|
+
PW: /\A96940\z/,
|
157
|
+
RE: /\A9[78]4\d{2}\z/,
|
158
|
+
SH: /\A(ASCN|STHL) 1ZZ\z/,
|
159
|
+
SJ: /\A\d{4}\z/,
|
160
|
+
SO: /\A\d{5}\z/,
|
161
|
+
SZ: /\A[HLMS]\d{3}\z/,
|
162
|
+
TC: /\ATKCA 1ZZ\z/,
|
163
|
+
WF: /\A986\d{2}\z/,
|
164
|
+
XK: /\A\d{5}\z/,
|
165
|
+
YT: /\A976\d{2}\z/
|
166
|
+
}
|
167
|
+
|
168
|
+
def regexp_for_country_alpha2(alpha2)
|
169
|
+
alpha2 = alpha2.to_s.upcase.to_sym
|
170
|
+
|
171
|
+
ZIPCODES_REGEX[alpha2]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -3,7 +3,8 @@
|
|
3
3
|
# class User < ActiveModel
|
4
4
|
# validates :postalcode, zipcode: true
|
5
5
|
#
|
6
|
-
# #
|
6
|
+
# # Default country_alpha2 attribute.
|
7
|
+
# # You can change it using :country_code_attribute option
|
7
8
|
# def country_alpha2
|
8
9
|
# country.alpha2
|
9
10
|
# end
|
@@ -15,17 +16,7 @@ require 'active_model/validator'
|
|
15
16
|
|
16
17
|
module ValidatesZipcode
|
17
18
|
class Validator < ActiveModel::EachValidator
|
18
|
-
|
19
|
-
ZIPCODES_REGEX = {
|
20
|
-
ES: /\A\d{5}\z/,
|
21
|
-
AR: /\A([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})\z/,
|
22
|
-
CL: /\A[0-9]{3}[-]?[0-9]{4}\z/,
|
23
|
-
NZ: /\A\d{4}\z/,
|
24
|
-
AU: /\A\d{4}\z/,
|
25
|
-
US: /\A\d{5}(-\d{4})?\z/,
|
26
|
-
CA: /\A[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}\z/
|
27
|
-
# CA: /\A[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d\z/
|
28
|
-
}
|
19
|
+
include CldrRegexpCollection
|
29
20
|
|
30
21
|
def initialize(options)
|
31
22
|
@country_code = options.fetch(:country_code) { }
|
@@ -36,8 +27,7 @@ module ValidatesZipcode
|
|
36
27
|
|
37
28
|
def validate_each(record, attribute, value)
|
38
29
|
alpha2 = @country_code || record.send(@country_code_attribute)
|
39
|
-
|
40
|
-
regexp = ZIPCODES_REGEX[alpha2]
|
30
|
+
regexp = regexp_for_country_alpha2(alpha2)
|
41
31
|
return unless regexp
|
42
32
|
|
43
33
|
unless regexp.match(value.to_s)
|
data/lib/validates_zipcode.rb
CHANGED
data/validates_zipcode.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
+
s.required_ruby_version = '>= 1.9.3'
|
22
|
+
|
21
23
|
s.add_dependency 'activemodel', '>= 3.2.0'
|
22
24
|
|
23
25
|
s.add_development_dependency "bundler", "~> 1.6"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_zipcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Gil
|
@@ -76,11 +76,13 @@ extra_rdoc_files: []
|
|
76
76
|
files:
|
77
77
|
- .gitignore
|
78
78
|
- .rspec
|
79
|
+
- .travis.yml
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|
82
83
|
- Rakefile
|
83
84
|
- lib/validates_zipcode.rb
|
85
|
+
- lib/validates_zipcode/cldr_regex_collection.rb
|
84
86
|
- lib/validates_zipcode/helper_methods.rb
|
85
87
|
- lib/validates_zipcode/railtie.rb
|
86
88
|
- lib/validates_zipcode/validator.rb
|
@@ -100,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
102
|
requirements:
|
101
103
|
- - '>='
|
102
104
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
105
|
+
version: 1.9.3
|
104
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
107
|
requirements:
|
106
108
|
- - '>='
|