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}                 # ISO country code
  [0-9]{2}                    # Check digits
  [A-Za-z0-9]{3}              # Creditor business code
  [A-Za-z0-9+?/:().,'-]{1,28} # National identifier
\z}x

Instance Method Summary collapse

Instance Method Details

#valid?(creditor_identifier) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
# File 'lib/sepa_king/validator.rb', line 50

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

#validate(record) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/sepa_king/validator.rb', line 41

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

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