Class: SEPA::CreditorIdentifierValidator

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#valid?(creditor_identifier) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/sepa_king/validator.rb', line 45

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



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

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