Skip to content

Commit

Permalink
BIC validation moved from BIC model to an independent validator
Browse files Browse the repository at this point in the history
  • Loading branch information
leio10 committed Nov 27, 2017
1 parent a0bee1f commit 5f7af50
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
iban_bic (1.3.0)
iban_bic (1.4.0)
rails (~> 5.1)
regexp-examples (~> 1.3)

Expand Down
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 0 additions & 28 deletions leio.plan.md

This file was deleted.

19 changes: 19 additions & 0 deletions lib/active_model/validations/bic_validator.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/iban_bic/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "active_model/validations/bic_validator"
require "active_model/validations/iban_validator"

module IbanBic
Expand Down
8 changes: 1 addition & 7 deletions lib/iban_bic/models/bic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/iban_bic/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module IbanBic
VERSION = "1.3.0"
VERSION = "1.4.0"
end
12 changes: 6 additions & 6 deletions spec/test_app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
iban_bic (1.2.0)
iban_bic (1.3.0)
rails (~> 5.1)
regexp-examples (~> 1.3)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5f7af50

Please sign in to comment.