Class: IsraeliBankAccountValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/israeli/active_model/israeli_bank_account_validator.rb

Overview

Validates that an attribute is a valid Israeli bank account number.

Examples:

Basic usage (accepts domestic or IBAN)

class BankAccount < ApplicationRecord
  validates :account_number, israeli_bank_account: true
end

Domestic format only

class BankAccount < ApplicationRecord
  validates :domestic_account, israeli_bank_account: { format: :domestic }
end

IBAN format only

class BankAccount < ApplicationRecord
  validates :iban, israeli_bank_account: { format: :iban }
end

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/israeli/active_model/israeli_bank_account_validator.rb', line 22

def validate_each(record, attribute, value)
  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

   = options[:format] || :any
  return if Israeli::Validators::BankAccount.valid?(value, format: )

  reason = Israeli::Validators::BankAccount.invalid_reason(value, format: )
  record.errors.add(
    attribute,
    options[:message] || :invalid,
    reason: reason,
    expected_format: 
  )
end