validates_zipcode 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fddc5611ea7a76c0566f14ad7fc9957058fb9bc0
4
- data.tar.gz: 19cc15442cda3506264a34fc384ae343913eaa31
3
+ metadata.gz: 18c95b5c356913b61606be8760fc1be5fd392353
4
+ data.tar.gz: 86a84e1e63d45ca9b2966bf151dd1dc35f015342
5
5
  SHA512:
6
- metadata.gz: 2142b4d7f47c25811efaaa88dcd8ee9dd418a8dd7751b0881c52b74184b34872748d755df5cfebe039435e5c8caec0eb32db6feac771dbf3d51bb5e889285811
7
- data.tar.gz: 8e11226752017ecb1da7ffb7a085aa294e26195e891be9e0c351f4be28bad875c698f3a24df3c0456e3d51a272390a8bf5a8bac47ae4e39a5c7aec9d396931e9
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- validates_zipcode (0.0.16)
4
+ validates_zipcode (0.0.18)
5
5
  activemodel (>= 3.2.0)
6
6
 
7
7
  GEM
@@ -49,4 +49,4 @@ DEPENDENCIES
49
49
  validates_zipcode!
50
50
 
51
51
  BUNDLED WITH
52
- 1.13.1
52
+ 1.13.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- validates_zipcode (0.0.16)
4
+ validates_zipcode (0.0.18)
5
5
  activemodel (>= 3.2.0)
6
6
 
7
7
  GEM
@@ -55,4 +55,4 @@ DEPENDENCIES
55
55
  validates_zipcode!
56
56
 
57
57
  BUNDLED WITH
58
- 1.13.1
58
+ 1.13.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- validates_zipcode (0.0.16)
4
+ validates_zipcode (0.0.18)
5
5
  activemodel (>= 3.2.0)
6
6
 
7
7
  GEM
@@ -56,4 +56,4 @@ DEPENDENCIES
56
56
  validates_zipcode!
57
57
 
58
58
  BUNDLED WITH
59
- 1.13.1
59
+ 1.13.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- validates_zipcode (0.0.16)
4
+ validates_zipcode (0.0.18)
5
5
  activemodel (>= 3.2.0)
6
6
 
7
7
  GEM
@@ -56,4 +56,4 @@ DEPENDENCIES
56
56
  validates_zipcode!
57
57
 
58
58
  BUNDLED WITH
59
- 1.13.1
59
+ 1.13.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- validates_zipcode (0.0.16)
4
+ validates_zipcode (0.0.18)
5
5
  activemodel (>= 3.2.0)
6
6
 
7
7
  GEM
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  validates_zipcode!
54
54
 
55
55
  BUNDLED WITH
56
- 1.13.1
56
+ 1.13.6
@@ -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
- AE: /\A([a-zA-Z\d\s]){3,}\z/,
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{6}\z/,
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) { :country_alpha2 }
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 = @country_code || record.send(@country_code_attribute)
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(zipcode: value.to_s, country_alpha2: alpha2).valid?
30
- record.errors.add(attribute, I18n.t('errors.messages.invalid_zipcode', value: value, default: 'Zipcode is invalid'))
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
@@ -1,3 +1,3 @@
1
1
  module ValidatesZipcode
2
- VERSION = "0.0.17"
2
+ VERSION = '0.0.18'
3
3
  end
@@ -3,11 +3,13 @@ module ValidatesZipcode
3
3
  include CldrRegexpCollection
4
4
 
5
5
  def initialize(args = {})
6
- @zipcode = args.fetch(:zipcode)
7
- @country_alpha2 = args.fetch(: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 = "invalid_zip")
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.17
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: 2017-01-04 00:00:00.000000000 Z
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.5.1
143
+ rubygems_version: 2.6.7
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: Localizable zipcode validation for Rails.