validates_zipcode 0.5.4 → 0.6.1

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: ba868b72c89f1dfbe11442ad8bc8d96f4a00c03cbcb41e7f0fb03343f73bcce8
4
+ data.tar.gz: d5494024cda83f6e81ea11019f2e0b06e5ec8fcde5d4e15c380af8b748dd13fa
5
5
  SHA512:
6
- metadata.gz: dc1fde962b62625f8877f34479fe76e4a9a8e9aac7bd9ed598c52ad68c41a5d3ed01031c83c23bd4c4c30a810450b724c53b4bafc5ee51dc5853688fe64d2638
7
- data.tar.gz: ad65f8eec393ce26b63a23c2f82a2e64107e0e869e5ebd7c0b7689e00a9906dd8ba98bc4197364885de332eb49cc68a704b6bd449e3eb46246d0c12f73a6cbca
6
+ metadata.gz: fcce5f5f3da925efa15747fdae9272644bf3f2e6764a7682e418ca94eeb506116df37f3580f32484640625c2acaa0bfaa850dbe7187fbe8142a66bd368eae8b4
7
+ data.tar.gz: 1afb2ee765e1eadbb8dc49a313a0bb4b7af2eb749aa0048141d106934fc9b2508e665b4c2557a59fa0a6dae5810dba30dba51f55ed06a20c68101a6bd19546a5
data/CHANGELOG.md CHANGED
@@ -4,6 +4,25 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ ## 0.6.1
8
+
9
+ Backport of the postcode fixes in 0.7.0 for apps that cannot move to Ruby >= 3.3 and Rails >= 8.0 yet. Requirements are unchanged from 0.6.0.
10
+
11
+ - Accept Nigeria's (NG) 11-character digital postcode, such as `EK-01-A03-FK-01`, alongside the six-digit code. NIPOST launched it in October 2026. Thanks to ~ @Adeniyikayodee
12
+ - Fix the Moldova (MD) postcode pattern: accept the canonical `MD-####` form and bare 4-digit codes, and reject non-`MD` letter prefixes. The previous pattern required any two letters and rejected the hyphenated form — fixes #73
13
+
14
+ ## 0.6.0
15
+
16
+ **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`.
17
+
18
+ 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.
19
+
20
+ - Return `false` when the country code is not a known ISO 3166-1 alpha-2 code, instead of silently returning `true` — fixes #67
21
+
22
+ ## 0.5.3
23
+
24
+ - See commits up to 4e5a3132061d57d220d1616beb15d15a47d301df
25
+
7
26
  ## 0.5.2
8
27
 
9
28
  - Added support for Puerto Rico, thanks to ~ @tahanson
@@ -77,7 +77,7 @@ module ValidatesZipcode
77
77
  MA: /\A\d{5}\z/,
78
78
  NZ: /\A\d{4}\z/,
79
79
  NI: /\A\d{5}\z/,
80
- NG: /\A\d{6}\z/,
80
+ NG: /\A(\d{6}|[A-Z]{2}[- ]?(0[1-9]|[1-9]\d)[- ]?[A-Z0-9]{3}[- ]?[A-Z]{2}[- ]?(0[1-9]|[1-9]\d))\z/i,
81
81
  OM: /\A(PC )?\d{3}\z/i,
82
82
  PK: /\A\d{5}\z/,
83
83
  PY: /\A(\d{4}|\d{6})\z/,
@@ -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,
@@ -203,7 +203,7 @@ module ValidatesZipcode
203
203
  LT: /\A(LT[\- ]?)?\d{5}\z/i,
204
204
  LC: /\A([A-Z\d\s]){3,}\z/i,
205
205
  MC: /\A\d{5}\z/,
206
- MD: /\A(([A-Z]){2})(|\s)\d{4}\z/i,
206
+ MD: /\A(MD[- ]?)?\d{4}\z/i,
207
207
  ME: /\A([A-Z\d\s]){3,}\z/i,
208
208
  MH: /\A\d{5}\z/,
209
209
  MR: /\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.1'
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,
@@ -1065,11 +1065,15 @@ TEST_DATA = {
1065
1065
  ]
1066
1066
  },
1067
1067
  NG: {
1068
- valid: %w[
1069
- 071134
1070
- 025663
1071
- 662612
1072
- 071135
1068
+ valid: [
1069
+ '071134',
1070
+ '025663',
1071
+ '662612',
1072
+ '071135',
1073
+ 'EK-01-A03-FK-01',
1074
+ 'EK 01 A03 FK 01',
1075
+ 'EK01A03FK01',
1076
+ 'ek01a03fk01'
1073
1077
  ],
1074
1078
  invalid: [
1075
1079
  nil,
@@ -1077,7 +1081,10 @@ TEST_DATA = {
1077
1081
  'invalid_zip',
1078
1082
  '12345',
1079
1083
  '123456-123456',
1080
- 'D0D0D0'
1084
+ 'D0D0D0',
1085
+ 'EK-00-A03-FK-01',
1086
+ 'EK-01-A03-FK-00',
1087
+ 'EK-01-A03-FK-011'
1081
1088
  ]
1082
1089
  },
1083
1090
  OM: {
@@ -2168,7 +2175,8 @@ TEST_DATA = {
2168
2175
  '',
2169
2176
  'invalid_zip',
2170
2177
  '1234',
2171
- '123456789'
2178
+ '123456789',
2179
+ '12345'
2172
2180
  ]
2173
2181
  },
2174
2182
  BJ: {
@@ -2838,13 +2846,16 @@ TEST_DATA = {
2838
2846
  },
2839
2847
  MD: {
2840
2848
  valid: %w[
2849
+ 2001
2841
2850
  MD2001
2851
+ MD-2001
2842
2852
  ],
2843
2853
  invalid: [
2844
2854
  nil,
2845
2855
  '',
2846
2856
  'invalid_zip',
2847
- 'MD-2100'
2857
+ 'XY2021',
2858
+ 'MD-202'
2848
2859
  ]
2849
2860
  },
2850
2861
  ME: {
@@ -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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Gil