Sha256: d79a7d270e06ac2186a2c3542caa75231f0a5ff5eb731f952c889d733bee65d2

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8
module SPS
  class IBANValidator < ActiveModel::Validator
    # IBAN2007Identifier (taken from schema)
    REGEX = /\A[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}\z/

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

      unless IBANTools::IBAN.valid?(value) && value.match(REGEX)
        record.errors.add(field_name, :invalid, message: options[:message])
      end
    end
  end

  class BICValidator < ActiveModel::Validator
    # AnyBICIdentifier (taken from schema)
    REGEX = /\A[A-Z]{6,6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3,3}){0,1}\z/

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

      if value
        unless value.to_s.match(REGEX)
          record.errors.add(field_name, :invalid, message: options[:message])
        end
      end
    end
  end

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

    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

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sps_king-0.5.0 lib/sps_king/validator.rb
sps_king-0.4.0 lib/sps_king/validator.rb
sps_king-0.3.1 lib/sps_king/validator.rb
sps_king-0.3.0 lib/sps_king/validator.rb
sps_king-0.2.0 lib/sps_king/validator.rb
sps_king-0.1.1 lib/sps_king/validator.rb
sps_king-0.1.0 lib/sps_king/validator.rb