diff --git a/Gemfile.lock b/Gemfile.lock index cf7f069..5d86657 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - iban_bic (1.3.0) + iban_bic (1.4.0) rails (~> 5.1) regexp-examples (~> 1.3) diff --git a/README.md b/README.md index 1a8293a..c75cad3 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ When IBAN validation is not enough. [![Dependency Status](https://www.versioneye.com/user/projects/59d393190fb24f0046190d85/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/59d393190fb24f0046190d85?style=flat) ## Features -* IBAN validation (`ActiveModel::EachValidator` and control digits calculator function) and fixing. +* IBAN and BIC validation (using `ActiveModel::EachValidator`, IBAN control digits calculator function). * National account digits control validation (currently only ES and PT are included, others countries can be added). +* IBAN fixing (global and national control digits). * Associated tags to countries (currently only SEPA and FIXED_CHECK tags are available). * BICs mapping from IBAN bank code part: COUNTRY + BANK => BIC code. * Currently, static data only includes some ES banks, PRs are welcomed. @@ -18,7 +19,7 @@ When IBAN validation is not enough. ## Usage -1. Validator: add IBAN validation to your models. +1. IBAN validator: add IBAN validation to your models. ```ruby validates :iban, iban: true @@ -30,35 +31,48 @@ You can also validate that IBAN is from a SEPA country. validates :iban, iban: { tags: [:sepa] } ``` -2. IBAN control digits calculation +2. BIC validator: add BIC validation to your models. + +```ruby +validates :bic, bic: true +``` + +You can specify the country field in the record to enforce its inclusion in the given BIC. + +```ruby +validates :bic, bic: { country: :pais } +``` + + +3. IBAN control digits calculation ```ruby 2.4.1 :001 > IbanBic.calculate_check("ES0000030000300000000000") => 87 ``` -3. IBAN parsing +4. IBAN parsing ```ruby 2.4.1 :001 > IbanBic.parse("ES8700030000300000000000") => {"country"=>"ES", "iban_check"=>"87", "bank"=>"0003", "branch"=>"0000", "check"=>"30", "account"=>"0000000000"} ``` -4. IBAN fixing (IBAN control digits and country control digits, if that code is available) +5. IBAN fixing (IBAN control digits and country control digits, if that code is available) ```ruby 2.4.1 :001 > IbanBic.fix("ES0000030000200000000000") => "ES8700030000300000000000" ``` -5. BIC calculation (bank code must be in the static file or in the database) +6. BIC calculation (bank code must be in the static file or in the database) ```ruby 2.4.1 :001 > IbanBic.calculate_bic("ES8700030000300000000000") => "BDEPESM1XXX" ``` -6. Pattern generation for SQL LIKE queries. +7. Pattern generation for SQL LIKE queries. ```ruby 2.4.1 :001 > IbanBic.like_pattern("ES8700030000300000000000", :country, :bank) @@ -67,7 +81,7 @@ validates :iban, iban: { tags: [:sepa] } => "ES__0003________________" ``` -7. Random IBAN generation +8. Random IBAN generation ```ruby 2.4.1 :001 > require "iban_bic/random" @@ -126,6 +140,10 @@ $ bundle exec rails generate iban_bic:install --with-static-data 4. Customize initializer if needed, adding validations for new countries, or overriding YAML files. ## Changelog +#### 1.4.0 + +* BIC validation moved from BIC model to an independent validator. + #### 1.3.0 * Added BIC format validation in BIC model. diff --git a/leio.plan.md b/leio.plan.md deleted file mode 100644 index 1b6aa53..0000000 --- a/leio.plan.md +++ /dev/null @@ -1,28 +0,0 @@ - - -COD => pais - - pais => parte BIC => DB? - => parte CC => funcion? - - - - - -gema - validateIban - calculateBic - - - modelo - bic - numero banco - codigo - - - migraciĆ³n - modelo - datos - - config - \ No newline at end of file diff --git a/lib/active_model/validations/bic_validator.rb b/lib/active_model/validations/bic_validator.rb new file mode 100644 index 0000000..f5f2dc5 --- /dev/null +++ b/lib/active_model/validations/bic_validator.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "active_model/validations" +require "iban_bic" + +# ActiveModel Rails module. +module ActiveModel + # ActiveModel::Validations Rails module. Contains all the default validators. + module Validations + class BicValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + country_field = options[:country] ? record[options[:country]] : "[A-Z]{2}" + unless /[A-Z]{4}#{country_field}[0-9A-Z]{2,5}/.match? value.upcase + record.errors.add(attribute, :invalid_format) + end + end + end + end +end diff --git a/lib/iban_bic/engine.rb b/lib/iban_bic/engine.rb index 98faf04..8a5ff04 100644 --- a/lib/iban_bic/engine.rb +++ b/lib/iban_bic/engine.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "active_model/validations/bic_validator" require "active_model/validations/iban_validator" module IbanBic diff --git a/lib/iban_bic/models/bic.rb b/lib/iban_bic/models/bic.rb index 4b09439..e4ea34d 100644 --- a/lib/iban_bic/models/bic.rb +++ b/lib/iban_bic/models/bic.rb @@ -4,13 +4,7 @@ class Bic < ActiveRecord::Base self.table_name = IbanBic.configuration.bics_table_name validates :country, :bank_code, :bic, presence: true - - validate do - bic.upcase! - unless /[A-Z]{4}#{country}[0-9A-Z]{2,5}/.match? bic - errors.add(:bic, :invalid_format) - end - end + validates :bic, bic: { country: :country } after_commit do IbanBic.clear_cache diff --git a/lib/iban_bic/version.rb b/lib/iban_bic/version.rb index 56c69b7..fd09f27 100644 --- a/lib/iban_bic/version.rb +++ b/lib/iban_bic/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module IbanBic - VERSION = "1.3.0" + VERSION = "1.4.0" end diff --git a/spec/test_app/Gemfile.lock b/spec/test_app/Gemfile.lock index df46779..f979471 100644 --- a/spec/test_app/Gemfile.lock +++ b/spec/test_app/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - iban_bic (1.2.0) + iban_bic (1.3.0) rails (~> 5.1) regexp-examples (~> 1.3) @@ -49,7 +49,7 @@ GEM builder (3.2.3) byebug (9.1.0) concurrent-ruby (1.0.5) - crass (1.0.2) + crass (1.0.3) erubi (1.7.0) globalid (0.4.1) activesupport (>= 4.2.0) @@ -61,7 +61,7 @@ GEM mail (2.7.0) mini_mime (>= 0.1.1) method_source (0.9.0) - mini_mime (0.1.4) + mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.10.3) nio4r (2.1.0) @@ -93,8 +93,8 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.2.1) - regexp-examples (1.4.0) + rake (12.3.0) + regexp-examples (1.4.1) sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -109,7 +109,7 @@ GEM thread_safe (~> 0.1) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + websocket-extensions (0.1.3) PLATFORMS ruby