Class: SEPA::IBANValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/sepa_rator/validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid_iban?(value) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/sepa_rator/validator.rb', line 11

def self.valid_iban?(value)
  iban = Ibandit::IBAN.new(value.to_s)
  iban.valid? && value.to_s == iban.iban
end

Instance Method Details

#validate(record) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sepa_rator/validator.rb', line 16

def validate(record)
  field_name = options[:field_name] || :iban
  value = record.public_send(field_name).to_s

  iban = Ibandit::IBAN.new(value)
  unless iban.valid?
    record.errors.add(field_name, :invalid, message: options[:message] || iban_error_message(iban))
    return
  end
  return if value == iban.iban

  record.errors.add(field_name, :invalid,
                    message: options[:message] || 'is invalid (must be uppercase with no spaces)')
end