Skip to content

Commit

Permalink
[canada] - bank_code_length 4 -> 3
Browse files Browse the repository at this point in the history
  • Loading branch information
piiraa committed Aug 6, 2024
1 parent df14cd8 commit 7dc2c7b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.21.0 - August 6, 2024

- Canada, Financial Institution number - allow 3 digits

## 1.20.0 - October 13, 2023

- Do not allow all 0 transit numbers in CA
Expand Down
7 changes: 5 additions & 2 deletions data/raw/pseudo_ibans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ AU:
:pseudo_iban_account_number_length: 10
:national_id_length: 6
CA:
:bank_code_length: 4
:bank_code_length: !ruby/range
begin: 3
end: 4
excl: false
:branch_code_length: 5
:account_number_length: !ruby/range
begin: 7
end: 12
excl: false
:bank_code_format: "\\d{4}"
:bank_code_format: "\\d{3,4}"
:branch_code_format: "0{0,4}[1-9][0-9]{0,4}"
:account_number_format: "(?!0+\\z)\\d{7,12}"
:national_id_length: 9
Expand Down
7 changes: 5 additions & 2 deletions data/structures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1493,13 +1493,16 @@ AU:
:pseudo_iban_account_number_length: 10
:national_id_length: 6
CA:
:bank_code_length: 4
:bank_code_length: !ruby/range
begin: 3
end: 4
excl: false
:branch_code_length: 5
:account_number_length: !ruby/range
begin: 7
end: 12
excl: false
:bank_code_format: "\\d{4}"
:bank_code_format: "\\d{3,4}"
:branch_code_format: 0{0,4}[1-9][0-9]{0,4}
:account_number_format: "(?!0+\\z)\\d{7,12}"
:national_id_length: 9
Expand Down
30 changes: 19 additions & 11 deletions lib/ibandit/iban.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ def valid_length?

def valid_bank_code_length?
return unless valid_country_code?
return true if structure[:bank_code_length]&.zero?
if structure[:bank_code_length].is_a?(Integer) && structure[:bank_code_length].zero?
return true
end

if swift_bank_code.nil? || swift_bank_code.empty?
@errors[:bank_code] = Ibandit.translate(:is_required)
return false
end

return true if swift_bank_code.length == structure[:bank_code_length]
return true if valid_input_length?(structure[:bank_code_length],
swift_bank_code.length)

@errors[:bank_code] =
Ibandit.translate(:wrong_length, expected: structure[:bank_code_length])
Expand Down Expand Up @@ -197,14 +200,8 @@ def valid_account_number_length?
return false
end

case structure[:account_number_length]
when Range
if structure[:account_number_length].include?(swift_account_number.length)
return true
end
else
return true if swift_account_number.length == structure[:account_number_length]
end
return true if valid_input_length?(structure[:account_number_length],
swift_account_number.length)

@errors[:account_number] =
Ibandit.translate(:wrong_length,
Expand Down Expand Up @@ -240,7 +237,9 @@ def valid_format?

def valid_bank_code_format?
return unless valid_bank_code_length?
return true if structure[:bank_code_length]&.zero?
if structure[:bank_code_length].is_a?(Integer) && structure[:bank_code_length].zero?
return true
end

if swift_bank_code&.match?(
entire_string_regex(structure[:bank_code_format]),
Expand Down Expand Up @@ -509,6 +508,15 @@ def formatted
iban.to_s.gsub(/(.{4})/, '\1 ').strip
end

def valid_input_length?(allowed_length, input_length)
case allowed_length
when Range
allowed_length.include?(input_length)
else
allowed_length == input_length
end
end

def valid_modulus_check_bank_code?
return true if Ibandit.modulus_checker.valid_bank_code?(self)

Expand Down
2 changes: 1 addition & 1 deletion lib/ibandit/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Ibandit
VERSION = "1.20.0"
VERSION = "1.21.0"
end

0 comments on commit 7dc2c7b

Please sign in to comment.