Class: SEPA::CreditorIdentifierValidator

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

Constant Summary collapse

REGEX =
%r{\A[a-zA-Z]{2,2}[0-9]{2,2}([A-Za-z0-9]|[+|?|/|\-|:|(|)|.|,|']){3,3}([A-Za-z0-9]|[+|?|/|\-|:|(|)|.|,|']){1,28}\z}.freeze

Instance Method Summary collapse

Instance Method Details

#valid?(creditor_identifier) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/sepa_king/validator.rb', line 38

def valid?(creditor_identifier)
  if ok = creditor_identifier.to_s.match(REGEX) && creditor_identifier[0..1].match(/DE/i)
    # In Germany, the identifier has to be exactly 18 chars long
    ok = creditor_identifier.length == 18
  end
  ok
end

#validate(record) ⇒ Object



31
32
33
34
35
36
# File 'lib/sepa_king/validator.rb', line 31

def validate(record)
  field_name = options[:field_name] || :creditor_identifier
  value = record.send(field_name)

  record.errors.add(field_name, :invalid, message: options[:message]) unless valid?(value)
end