validates_zipcode 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/gemfiles/rails_3.2.gemfile.lock +2 -2
- data/gemfiles/rails_4.0.gemfile.lock +2 -2
- data/gemfiles/rails_4.1.gemfile.lock +2 -2
- data/gemfiles/rails_4.2.gemfile.lock +2 -2
- data/gemfiles/rails_5.0.gemfile.lock +2 -2
- data/lib/validates_zipcode.rb +2 -2
- data/lib/validates_zipcode/cldr_regex_collection.rb +4 -2
- data/lib/validates_zipcode/validator.rb +11 -5
- data/lib/validates_zipcode/version.rb +1 -1
- data/lib/validates_zipcode/zipcode.rb +4 -2
- data/spec/validates_zipcode_spec.rb +51 -13
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18c95b5c356913b61606be8760fc1be5fd392353
|
4
|
+
data.tar.gz: 86a84e1e63d45ca9b2966bf151dd1dc35f015342
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72be9513ed1857044d0689407283476762f6db7c758d4cef66a871a8baaaf02c774cc2e4bd7236506bea2103fdcf577a6ffd9d8a22dd696c6c8f76da5030eb76
|
7
|
+
data.tar.gz: 14efc7339977773b23bcf1a1727d9d024836ff388fcaf6b81024589e1950a0ae49f56f951398e073df615c51ae3dd2634816f8e0f4489abc62ee2fd52f4f1732
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## 0.0.18
|
6
|
+
|
7
|
+
- Adds support for `excluded_country_codes` in the validation setup.
|
8
|
+
- Remove support for UAE, not reliable sources found, assume not used.
|
9
|
+
|
5
10
|
## 0.0.17
|
6
11
|
|
7
12
|
- Updates support for Luxembourg postal codes ~ @rivsc
|
data/lib/validates_zipcode.rb
CHANGED
@@ -6,7 +6,7 @@ require "validates_zipcode/version"
|
|
6
6
|
require "validates_zipcode/zipcode"
|
7
7
|
|
8
8
|
module ValidatesZipcode
|
9
|
-
def self.valid?(zipcode, country_alpha2)
|
10
|
-
ValidatesZipcode::Zipcode.new(zipcode: zipcode, country_alpha2: country_alpha2).valid?
|
9
|
+
def self.valid?(zipcode, country_alpha2, options = {})
|
10
|
+
ValidatesZipcode::Zipcode.new(options.merge(zipcode: zipcode, country_alpha2: country_alpha2)).valid?
|
11
11
|
end
|
12
12
|
end
|
@@ -139,7 +139,9 @@ module ValidatesZipcode
|
|
139
139
|
WF: /\A986\d{2}\z/,
|
140
140
|
XK: /\A\d{5}\z/,
|
141
141
|
YT: /\A976\d{2}\z/,
|
142
|
-
|
142
|
+
# NOTE: UAE has no postal codes
|
143
|
+
# https://github.com/dgilperez/validates_zipcode/issues/28
|
144
|
+
# AE: /\A([a-zA-Z\d\s]){3,}\z/,
|
143
145
|
AF: /\A\d{4}\z/,
|
144
146
|
AL: /\A\d{4}\z/,
|
145
147
|
AG: /\A([a-zA-Z\d\s]){3,}\z/,
|
@@ -205,7 +207,7 @@ module ValidatesZipcode
|
|
205
207
|
NR: /\A([a-zA-Z\d\s]){3,}\z/,
|
206
208
|
PT: /\A\d{4}([\-]\d{3})?\z/,
|
207
209
|
PS: /\A\d{3}\z/,
|
208
|
-
PA: /\A\d{
|
210
|
+
PA: /\A\d{4}\z/,
|
209
211
|
PE: /\A\d{5}\z/,
|
210
212
|
QA: /\A([a-zA-Z\d\s]){3,}\z/,
|
211
213
|
RW: /\A([a-zA-Z\d\s]){3,}\z/,
|
@@ -17,17 +17,23 @@ require 'active_model/validator'
|
|
17
17
|
module ValidatesZipcode
|
18
18
|
class Validator < ActiveModel::EachValidator
|
19
19
|
def initialize(options)
|
20
|
-
@country_code = options.fetch(:country_code)
|
21
|
-
@country_code_attribute = options.fetch(:country_code_attribute
|
20
|
+
@country_code = options.fetch(:country_code, nil)
|
21
|
+
@country_code_attribute = options.fetch(:country_code_attribute, :country_alpha2)
|
22
|
+
@excluded_country_codes = options.fetch(:excluded_country_codes, [])
|
22
23
|
|
23
24
|
super
|
24
25
|
end
|
25
26
|
|
26
27
|
def validate_each(record, attribute, value)
|
27
|
-
alpha2
|
28
|
+
alpha2 = @country_code || record.send(@country_code_attribute)
|
29
|
+
options = { zipcode: value.to_s,
|
30
|
+
country_alpha2: alpha2,
|
31
|
+
excluded_country_codes: @excluded_country_codes }
|
28
32
|
|
29
|
-
unless ValidatesZipcode::Zipcode.new(
|
30
|
-
record.errors.add(attribute, I18n.t('errors.messages.invalid_zipcode',
|
33
|
+
unless ValidatesZipcode::Zipcode.new(options).valid?
|
34
|
+
record.errors.add(attribute, :invalid_zipcode, message: I18n.t('errors.messages.invalid_zipcode',
|
35
|
+
value: value,
|
36
|
+
default: 'Zipcode is invalid'))
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|
@@ -3,11 +3,13 @@ module ValidatesZipcode
|
|
3
3
|
include CldrRegexpCollection
|
4
4
|
|
5
5
|
def initialize(args = {})
|
6
|
-
@zipcode
|
7
|
-
@country_alpha2
|
6
|
+
@zipcode = args.fetch(:zipcode)
|
7
|
+
@country_alpha2 = args.fetch(:country_alpha2)
|
8
|
+
@excluded_country_codes = args.fetch(:excluded_country_codes, [])
|
8
9
|
end
|
9
10
|
|
10
11
|
def valid?
|
12
|
+
return true if @excluded_country_codes.include?(@country_alpha2)
|
11
13
|
return true unless regexp
|
12
14
|
!!(regexp =~ @zipcode)
|
13
15
|
end
|
@@ -135,7 +135,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
135
135
|
|
136
136
|
it 'does not validate with an invalid zipcode' do
|
137
137
|
record = build_record('2100', 'RS')
|
138
|
-
zipcode_should_be_invalid(record)
|
138
|
+
zipcode_should_be_invalid(record, '2100')
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
@@ -156,7 +156,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
156
156
|
|
157
157
|
it 'does not validate with an invalid zipcode' do
|
158
158
|
record = build_record('21006', 'LT')
|
159
|
-
zipcode_should_be_invalid(record)
|
159
|
+
zipcode_should_be_invalid(record, '21006')
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
@@ -168,7 +168,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
168
168
|
|
169
169
|
it 'does not validate with an invalid zipcode' do
|
170
170
|
record = build_record('MD-2100', 'MD')
|
171
|
-
zipcode_should_be_invalid(record)
|
171
|
+
zipcode_should_be_invalid(record, 'MD-2100')
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
@@ -182,7 +182,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
182
182
|
|
183
183
|
it 'does not validate with an invalid zipcode' do
|
184
184
|
record = build_record('4500-2500', 'PT')
|
185
|
-
zipcode_should_be_invalid(record)
|
185
|
+
zipcode_should_be_invalid(record, '4500-2500')
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
@@ -196,7 +196,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
196
196
|
|
197
197
|
it 'does not validate with an invalid zipcode' do
|
198
198
|
record = build_record('723155', 'BR')
|
199
|
-
zipcode_should_be_invalid(record)
|
199
|
+
zipcode_should_be_invalid(record, '723155')
|
200
200
|
end
|
201
201
|
end
|
202
202
|
|
@@ -210,7 +210,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
210
210
|
|
211
211
|
it 'does not validate with an invalid zipcode' do
|
212
212
|
record = build_record('723155', 'KR')
|
213
|
-
zipcode_should_be_invalid(record)
|
213
|
+
zipcode_should_be_invalid(record, '723155')
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -224,7 +224,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
224
224
|
|
225
225
|
it 'does not validate with an invalid zipcode' do
|
226
226
|
record = build_record('981 32', 'CZ')
|
227
|
-
zipcode_should_be_invalid(record)
|
227
|
+
zipcode_should_be_invalid(record, '981 32')
|
228
228
|
end
|
229
229
|
end
|
230
230
|
|
@@ -238,7 +238,7 @@ describe ValidatesZipcode, '#validate_each' do
|
|
238
238
|
|
239
239
|
it 'does not validate with an invalid zipcode' do
|
240
240
|
record = build_record('120 00', 'SK')
|
241
|
-
zipcode_should_be_invalid(record)
|
241
|
+
zipcode_should_be_invalid(record, '120 00')
|
242
242
|
end
|
243
243
|
end
|
244
244
|
|
@@ -252,7 +252,41 @@ describe ValidatesZipcode, '#validate_each' do
|
|
252
252
|
|
253
253
|
it 'does not validate with an invalid zipcode' do
|
254
254
|
record = build_record('1200', 'IL')
|
255
|
-
zipcode_should_be_invalid(record)
|
255
|
+
zipcode_should_be_invalid(record, '1200')
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
context 'Panama' do
|
260
|
+
it 'validates with a valid zipcode' do
|
261
|
+
['0800', '6369'].each do |zipcode|
|
262
|
+
record = build_record(zipcode, 'PA')
|
263
|
+
zipcode_should_be_valid(record)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'does not validate with an invalid zipcode' do
|
268
|
+
['10800', '369', 'A341'].each do |zipcode|
|
269
|
+
record = build_record(zipcode, 'PA')
|
270
|
+
zipcode_should_be_invalid(record, zipcode)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
context 'Excluded existing country code' do
|
276
|
+
context 'Panama' do
|
277
|
+
it 'validates with a valid zipcode' do
|
278
|
+
['0800', '6369'].each do |zipcode|
|
279
|
+
record = build_record(zipcode, 'PA')
|
280
|
+
zipcode_should_be_valid(record, excluded_country_codes: ['PA'])
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'validates with an invalid zipcode' do
|
285
|
+
['10800', '369', 'A341'].each do |zipcode|
|
286
|
+
record = build_record(zipcode, 'PA')
|
287
|
+
zipcode_should_be_valid(record, excluded_country_codes: ['PA'])
|
288
|
+
end
|
289
|
+
end
|
256
290
|
end
|
257
291
|
end
|
258
292
|
end
|
@@ -272,17 +306,21 @@ describe ValidatesZipcode, '.valid?' do
|
|
272
306
|
it "is true with an unknown country code - we don't have all!" do
|
273
307
|
expect(ValidatesZipcode.valid?('12345', 'ZZ')).to eq(true)
|
274
308
|
end
|
309
|
+
|
310
|
+
it "is true with an excluded country code - we don't want those to fail and nil is hairy" do
|
311
|
+
expect(ValidatesZipcode.valid?('XXXX!!!XXXX', 'PA', excluded_country_codes: ['PA'])).to eq(true)
|
312
|
+
end
|
275
313
|
end
|
276
314
|
end
|
277
315
|
|
278
|
-
def zipcode_should_be_valid(record)
|
279
|
-
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
|
316
|
+
def zipcode_should_be_valid(record, options = {})
|
317
|
+
ValidatesZipcode::Validator.new(options.merge(attributes: :zipcode)).validate(record)
|
280
318
|
|
281
319
|
expect(record.errors).to be_empty
|
282
320
|
end
|
283
321
|
|
284
|
-
def zipcode_should_be_invalid(record, zipcode =
|
285
|
-
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
|
322
|
+
def zipcode_should_be_invalid(record, zipcode, options = {})
|
323
|
+
ValidatesZipcode::Validator.new(options.merge(attributes: :zipcode)).validate(record)
|
286
324
|
|
287
325
|
expect(record.errors.size).to eq 1
|
288
326
|
expect(record.errors.messages[:zipcode]).to include 'Zipcode is invalid'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Gil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
version: '0'
|
141
141
|
requirements: []
|
142
142
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.6.7
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: Localizable zipcode validation for Rails.
|