diff --git a/CHANGELOG.md b/CHANGELOG.md index 8732927..98e2bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.21.0 - August 6, 2024 + +- Canada, Financial Institution number - allow 3 digits +- modulus_checker failures will now return validation method specific error message + ## 1.20.0 - October 13, 2023 - Do not allow all 0 transit numbers in CA diff --git a/data/raw/pseudo_ibans.yml b/data/raw/pseudo_ibans.yml index e38f133..0cecd94 100644 --- a/data/raw/pseudo_ibans.yml +++ b/data/raw/pseudo_ibans.yml @@ -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 diff --git a/data/structures.yml b/data/structures.yml index 5878b57..57a34c0 100644 --- a/data/structures.yml +++ b/data/structures.yml @@ -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 diff --git a/lib/ibandit/iban.rb b/lib/ibandit/iban.rb index af725e6..7a96925 100644 --- a/lib/ibandit/iban.rb +++ b/lib/ibandit/iban.rb @@ -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]) @@ -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, @@ -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]), @@ -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) diff --git a/lib/ibandit/version.rb b/lib/ibandit/version.rb index 9555b16..5438149 100644 --- a/lib/ibandit/version.rb +++ b/lib/ibandit/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Ibandit - VERSION = "1.20.0" + VERSION = "1.21.0" end