Class: ActiveModel::Validations::IbanValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/swiss_bank_validator/validates_iban_format_of.rb

Instance Method Summary collapse

Instance Method Details

#iban_format_valid?(iban) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# 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