validates_zipcode 0.5.4 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dabc7eb750e5922949d9caebe9616e87d2c571f6caa64f56f0779106235c2e89
4
- data.tar.gz: 20ff39a51cd9f7d71dbfabef44eaf985c1e77e1df070f78d0fcceccbd052f2da
3
+ metadata.gz: e698716ffc57c2d3fcdbfcda2dea3817842ed64b10c7739709a7cdad8914789a
4
+ data.tar.gz: a50c8edeeadf3aa499d92260214fd65cc6e2bb1ed34f866133eb5480ecb89d5c
5
5
  SHA512:
6
- metadata.gz: dc1fde962b62625f8877f34479fe76e4a9a8e9aac7bd9ed598c52ad68c41a5d3ed01031c83c23bd4c4c30a810450b724c53b4bafc5ee51dc5853688fe64d2638
7
- data.tar.gz: ad65f8eec393ce26b63a23c2f82a2e64107e0e869e5ebd7c0b7689e00a9906dd8ba98bc4197364885de332eb49cc68a704b6bd449e3eb46246d0c12f73a6cbca
6
+ metadata.gz: b50f55f7f3265cf5de4f56292cc4167cd860afa19e28752b830238bb9e622fdf6948f7da0c60dc5d004524a7595029e8cd45da272deb8978c98bf7be080737a6
7
+ data.tar.gz: 7750e6f341ca45e65cfa0c5fde71e2b5c451fe99a7fa910cfd6c6ab33695864baef875c041587fe5d451b32e8700cc8147bf54b2a75ef796e489e6c2abac8706
data/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ ## 0.6.0
8
+
9
+ **Heads up:** This release changes the behaviour of `ValidatesZipcode.valid?` when an unknown or malformed country code is passed. Previously, passing a country code not recognised by the gem (e.g. `'UKXXXXX'`) would return `true` — no regex to match against meant no failure. It will now return `false`.
10
+
11
+ If you were relying on the old behaviour (passing arbitrary country codes and expecting `true`), this is a breaking change for those cases. For everyone else, this is the expected fix — an unrecognised country is not a valid one.
12
+
13
+ - Return `false` when the country code is not a known ISO 3166-1 alpha-2 code, instead of silently returning `true` — fixes #67
14
+
15
+ ## 0.5.3
16
+
17
+ - See commits up to 4e5a3132061d57d220d1616beb15d15a47d301df
18
+
7
19
  ## 0.5.2
8
20
 
9
21
  - Added support for Puerto Rico, thanks to ~ @tahanson
@@ -154,7 +154,7 @@ module ValidatesZipcode
154
154
  BI: /\A([A-Z\d\s]){3,}\z/i,
155
155
  BS: /\A([A-Z\d\s]){3,}\z/i,
156
156
  BZ: /\A([A-Z\d\s]){3,}\z/i,
157
- BR: /\A\d{5}(-?\d{3})?\z/,
157
+ BR: /\A\d{5}(-?\d{3})\z/,
158
158
  BJ: /\A([A-Z\d\s]){3,}\z/i,
159
159
  BT: /\A\d{5}\z/,
160
160
  BQ: /\A([A-Z\d\s]){3,}\z/i,
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ValidatesZipcode
3
- VERSION = '0.5.4'
3
+ VERSION = '0.6.0'
4
4
  end
@@ -12,6 +12,7 @@ module ValidatesZipcode
12
12
 
13
13
  def valid?
14
14
  return true if @excluded_country_codes.include?(@country_alpha2)
15
+ return false unless valid_country_alpha2?
15
16
  return true unless regexp
16
17
 
17
18
  zipcode = @format ? formatted_zip : @zipcode
@@ -31,6 +32,13 @@ module ValidatesZipcode
31
32
  @regexp ||= regexp_for_country_alpha2(@country_alpha2)
32
33
  end
33
34
 
35
+ # ISO 3166-1 alpha-2 codes are exactly 2 uppercase ASCII letters.
36
+ # Anything longer or shorter (e.g. "UKXXXXX", "G", "12") is not a
37
+ # valid country code and should fail validation immediately.
38
+ def valid_country_alpha2?
39
+ @country_alpha2.to_s.match?(/\A[A-Za-z]{2}\z/)
40
+ end
41
+
34
42
  def formatted_zip
35
43
  @formatted_zip ||= Formatter.new(zipcode: @zipcode, country_alpha2: @country_alpha2).format
36
44
  end
data/spec/spec_helper.rb CHANGED
@@ -554,9 +554,9 @@ TEST_DATA = {
554
554
  },
555
555
  KH: {
556
556
  valid: %w[
557
- 07113
558
- 02566
559
- 66261
557
+ 071131
558
+ 025662
559
+ 662613
560
560
  ],
561
561
  invalid: [
562
562
  nil,
@@ -2168,7 +2168,8 @@ TEST_DATA = {
2168
2168
  '',
2169
2169
  'invalid_zip',
2170
2170
  '1234',
2171
- '123456789'
2171
+ '123456789',
2172
+ '12345'
2172
2173
  ]
2173
2174
  },
2174
2175
  BJ: {
@@ -82,6 +82,14 @@ describe ValidatesZipcode, '.valid?' do
82
82
  expect(ValidatesZipcode.valid?('12345', 'ZZ')).to eq(true)
83
83
  end
84
84
 
85
+ it "is false with a malformed country code that is not a valid ISO 3166-1 alpha-2 code" do
86
+ expect(ValidatesZipcode.valid?('Sw1A 2aA', 'UKXXXXX')).to eq(false)
87
+ expect(ValidatesZipcode.valid?('12345', 'USA')).to eq(false) # 3 letters
88
+ expect(ValidatesZipcode.valid?('12345', 'U')).to eq(false) # 1 letter
89
+ expect(ValidatesZipcode.valid?('12345', '12')).to eq(false) # digits
90
+ expect(ValidatesZipcode.valid?('12345', '')).to eq(false) # empty
91
+ end
92
+
85
93
  it "is true with an excluded country code - we don't want those to fail and nil is hairy" do
86
94
  expect(ValidatesZipcode.valid?('XXXX!!!XXXX', 'PA', excluded_country_codes: %w[PA])).to eq(true)
87
95
  end
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.5.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Gil