CzechBankAccount Build Status

Czech bank accounts validations with official Czech National Bank algorithm.

Installation

Add this line to your application's Gemfile:

gem 'czech_bank_account'

And then execute:

$ bundle

Or install it yourself as:

$ gem install czech_bank_account

Usage

Initialize CzechBankAccount::Account and call validate on it:

number = '36-6420840257' # bank account number with optional prefix but without bank code
bank_code = '0301' # separate bank code
 = CzechBankAccount::Account.new(number, bank_code)

.validate # returns array with error symbols
#=> [:prefix_doesnt_pass_checksum, :unknown_bank_code]

Or use a shortcut which returns errors directly:

CzechBankAccount.validate('35-6420840257', '9999') # returns array with error symbols
#=> [:unknown_bank_code]

Possible error symbols

  1. :number_is_empty
  2. :bank_code_is_empty
  3. :number_includes_not_allowed_characters
  4. :number_prefix_is_over_length_limit
  5. :number_is_over_or_under_length_limit
  6. :prefix_doesnt_pass_checksum
  7. :number_doesnt_pass_checksum
  8. :unknown_bank_code

Usage in Rails

Prepare your own custom validation as per Rails guide

For example ActiveModel::Validator could look like following:

# app/validators/czech_bank_account_validator.rb
class CzechBankAccountValidator < ActiveModel::Validator
  def validate(record)
    errors = CzechBankAccount.validate(record., record.bank_code)
    errors.each { |e| record.errors.add(:base, e) }
  end
end

Then in model:

class Invoice < ApplicationRecord
  validates_with CzechBankAccountValidator
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Masa331/czech_bank_account.

License

The gem is available as open source under the terms of the MIT License.

Official docs for account validation algorithm