When IBAN validation is not enough.
- 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.
- Optional database model to allow apps to dynamically add new BICs mapping.
- Random IBANs generator
- IBAN validator: add IBAN validation to your models.
validates :iban, iban: true
You can also validate that IBAN is from a SEPA country.
validates :iban, iban: { tags: [:sepa] }
- BIC validator: add BIC validation to your models.
validates :bic, bic: true
You can specify the country field in the record to enforce its inclusion in the given BIC.
validates :bic, bic: { country: :pais }
- IBAN control digits calculation
IbanBic.calculate_check("ES0000030000300000000000")
=> 87
- IBAN parsing
IbanBic.parse("ES8700030000300000000000")
=> {"country"=>"ES", "iban_check"=>"87", "bank"=>"0003", "branch"=>"0000", "check"=>"30", "account"=>"0000000000"}
- IBAN fixing (IBAN control digits and country control digits, if that code is available)
IbanBic.fix("ES0000030000200000000000")
=> "ES8700030000300000000000"
- BIC calculation (bank code must be in the static file or in the database)
IbanBic.calculate_bic("ES8700030000300000000000")
=> "BDEPESM1XXX"
- Pattern generation for SQL LIKE queries.
IbanBic.like_pattern("ES8700030000300000000000", :country, :bank)
=> "ES__0003________________"
IbanBic.like_pattern_from_parts(country: "ES", bank: "0003")
=> "ES__0003________________"
- Random IBAN generation
require "iban_bic/random"
=> true
IbanBic.random_iban
=> "MU52BOIR2768144336487102000AWQ"
IbanBic.random_iban country: "ES"
=> "ES6111051493192369291292"
IbanBic.random_iban tags: [:sepa]
=> "FI5584518206233159"
IbanBic.random_iban not_tags: [:sepa]
=> "IL317532867920826062774"
Note: It can't generate a valid IBAN code for some countries where iban check digits are fixed if validation code for that country is not available.
- Add this line to your application's Gemfile
gem "iban_bic"
- Update bundle
$ bundle
- If you will use this gem for BIC calculations or want to add additional country control codes validations, choose from any of the following two options to run the installer:
3.a. Using BICs dynamic data from database
Add a bics
table to your database and an initializer file for configuration:
$ bundle exec rails generate iban_bic:install
$ bundle exec rake db:migrate
Load static data to the created bics
table:
$ bundle exec rake iban_bic:load_data
3.b. Using static data from YAML files
Create an initializer file for configuration:
$ bundle exec rails generate iban_bic:install --with-static-data
- Customize initializer if needed, adding validations for new countries, or overriding YAML files.
- Support for Rails 5.2.
- Fixed validators to avoid failing when values are
nil
.
- Dependencies security update.
- Fixed BIC validation, don't allow less 1 or 2 characters for the office code and don't allow to add extra characters at the begining or at the end of the string.
- BIC validation moved from BIC model to an independent validator.
- Added BIC format validation in BIC model.
- Added
like_pattern_from_parts
method. Not a very performant version. - Fixed portuguese IBAN validation.
- Added
like_pattern
method.
- Added presence validations in BIC model.
-
Added IBAN fixing and random IBAN generator.
-
Changed country checks, they must change check parts to generate the correct IBAN. Comparison against the original IBAN is made by the caller.
- First version.
Issues and PRs are welcomed.
The gem is available as open source under the terms of the MIT License.