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 +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/validates_zipcode/cldr_regex_collection.rb +1 -1
- data/lib/validates_zipcode/version.rb +1 -1
- data/lib/validates_zipcode/zipcode.rb +8 -0
- data/spec/spec_helper.rb +5 -4
- data/spec/validates_zipcode_spec.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e698716ffc57c2d3fcdbfcda2dea3817842ed64b10c7739709a7cdad8914789a
|
|
4
|
+
data.tar.gz: a50c8edeeadf3aa499d92260214fd65cc6e2bb1ed34f866133eb5480ecb89d5c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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})
|
|
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,
|
|
@@ -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
|
-
|
|
558
|
-
|
|
559
|
-
|
|
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
|