class ActiveModel::Validations::IbanValidator

Public Instance Methods

iban_format_valid?(iban) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 64
def iban_format_valid?(iban)
  iban_numerified = numerify_iban(iban)
  return false if iban_numerified.nil?

  iban_numerified.to_i % 97 == 1
end
numerify_iban(iban) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 71
def numerify_iban(iban)
  return '' if iban.blank?

  numerified = ''
  code = iban.to_s.strip.gsub(/\s+/, '').upcase
  (code[4..] + code[0..3]).each_byte do |byte|
    numerified += case byte
                    # 0..9
                  when 48..57 then byte.chr
                    # 'A'..'Z'
                  when 65..90 then (byte - 55).to_s # 55 = 'A'.ord + 10
                  else
                    return nil
                  end
  end
  numerified
end
validate_each(record, attribute, value) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 7
def validate_each(record, attribute, value)
  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  return unless validate_iban_length(record, attribute, value, options)

  validate_start_with_ch(record, attribute, value, options)
  validate_iban_with_regex(record, attribute, value, options)
  validate_iban_format(record, attribute, value, options)
end
validate_iban_format(record, attribute, value, options) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 42
def validate_iban_format(record, attribute, value, options)
  return if iban_format_valid?(value)

  record.errors.add(
    attribute,
    :iban_format_not_valid,
    message: options[:message],
    value: value
  )
end
validate_iban_length(record, attribute, value, options) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 18
def validate_iban_length(record, attribute, value, options)
  return true if value.length == SwissBankValidator::IBAN_LENGTH

  record.errors.add(
    attribute,
    :invalid_iban_length,
    message: options[:message],
    value: SwissBankValidator::IBAN_LENGTH
  )

  false
end
validate_iban_with_regex(record, attribute, value, options) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 31
def validate_iban_with_regex(record, attribute, value, options)
  return if SwissBankValidator::IBAN_REGEX.match(value).present?

  record.errors.add(
    attribute,
    :space_not_authorized_in_iban,
    message: options[:message],
    value: value
  )
end
validate_start_with_ch(record, attribute, value, options) click to toggle source
# File lib/swiss_bank_validator/validates_iban_format_of.rb, line 53
def validate_start_with_ch(record, attribute, value, options)
  return if value.downcase.start_with?('ch')

  record.errors.add(
    attribute,
    :must_start_with_ch,
    message: options[:message],
    value: value
  )
end