Skip to content

Commit

Permalink
BIC format validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
leio10 committed Nov 13, 2017
1 parent 4c4c22c commit d5a2499
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
10 changes: 5 additions & 5 deletions 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 @@ -82,7 +82,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 @@ -121,7 +121,7 @@ GEM
rainbow (2.2.2)
rake
rake (12.2.1)
regexp-examples (1.4.0)
regexp-examples (1.4.1)
rspec-core (3.7.0)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
Expand Down Expand Up @@ -173,7 +173,7 @@ GEM
equalizer (~> 0.0, >= 0.0.9)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
websocket-extensions (0.1.3)

PLATFORMS
ruby
Expand All @@ -189,4 +189,4 @@ DEPENDENCIES
virtus (~> 1.0)

BUNDLED WITH
1.15.1
1.16.0
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,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.3.0

* Added BIC format validation in BIC model.

#### 1.2.0

* Added `like_pattern_from_parts` method. Not a very performant version.
Expand All @@ -137,7 +141,7 @@ $ bundle exec rails generate iban_bic:install --with-static-data

#### 1.0.1

* Added presence validations in BIC model
* Added presence validations in BIC model.

#### 1.0.0

Expand Down
7 changes: 7 additions & 0 deletions lib/iban_bic/models/bic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class Bic < ActiveRecord::Base

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

after_commit do
IbanBic.clear_cache
end
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.2.0"
VERSION = "1.3.0"
end
4 changes: 2 additions & 2 deletions spec/iban_bic/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
context "when using database entries" do
before do
new_bic = Bic.find_or_initialize_by(country: "ES", bank_code: "0003")
new_bic.bic = "DIFFERENT"
new_bic.bic = "ABCDESMM"
new_bic.save!
IbanBic.clear_cache
end
it { is_expected.to eq("DIFFERENT") }
it { is_expected.to eq("ABCDESMM") }
end

context "when using static bics" do
Expand Down
32 changes: 32 additions & 0 deletions spec/models/bic_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "rails_helper"

describe Bic, :db do
subject(:bic) { Bic.new country: country, bank_code: bank_code, bic: bic_code }
let(:country) { "ES" }
let(:bank_code) { "1234" }
let(:bic_code) { "ABCDESMM123" }

it { is_expected.to be_valid }

context "when bic is downcase" do
let(:bic_code) { "abcdesmm123" }
it { is_expected.to be_valid }
end

context "when bic doesn't include branch code" do
let(:bic_code) { "ABCDESMM" }
it { is_expected.to be_valid }
end

context "when country inside bic doesn't match country" do
let(:country) { "PT" }
it { is_expected.to be_invalid }
end

context "when bic format is invalid" do
let(:bic_code) { "fsd234asd" }
it { is_expected.to be_invalid }
end
end
25 changes: 12 additions & 13 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.0.0)
iban_bic (1.2.0)
rails (~> 5.1)
regexp-examples (~> 1.3)

Expand Down Expand Up @@ -50,19 +50,18 @@ GEM
byebug (9.1.0)
concurrent-ruby (1.0.5)
crass (1.0.2)
erubi (1.6.1)
globalid (0.4.0)
erubi (1.7.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (0.8.6)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
loofah (2.1.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.6.6)
mime-types (>= 1.16, < 4)
mail (2.7.0)
mini_mime (>= 0.1.1)
method_source (0.9.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_mime (0.1.4)
mini_portile2 (2.3.0)
minitest (5.10.3)
nio4r (2.1.0)
Expand Down Expand Up @@ -94,8 +93,8 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.1.0)
regexp-examples (1.3.2)
rake (12.2.1)
regexp-examples (1.4.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -106,7 +105,7 @@ GEM
sqlite3 (1.3.13)
thor (0.20.0)
thread_safe (0.3.6)
tzinfo (1.2.3)
tzinfo (1.2.4)
thread_safe (~> 0.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
Expand All @@ -124,4 +123,4 @@ RUBY VERSION
ruby 2.4.1p111

BUNDLED WITH
1.15.1
1.16.0

0 comments on commit d5a2499

Please sign in to comment.