Class: ActiveModel::Validations::BrCpfValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/custom_validations/br_cpf_validator.rb

Constant Summary collapse

BLACK_LIST =
%w(12345678909 11111111111 22222222222 33333333333 44444444444
55555555555 66666666666 77777777777 88888888888 99999999999
00000000000)

Instance Method Summary collapse

Instance Method Details

#valid_cpf?(cpf) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/custom_validations/br_cpf_validator.rb', line 15

def valid_cpf? cpf
  begin
    cpf = (cpf.is_a?(String) ? cpf : cpf.to_s)
  rescue
    return false
  end

  # could be 10 or 11 digits or with mask 999.999.999-99
  if cpf !~ /^\d{10,11}$|\d{3}\.\d{3}\.\d{3}-\d{2}$/
    return false
  end

  cpf = cpf.scan(/\d/).collect(&:to_i)
  cpf.unshift(0) if cpf.length == 10

  # filter black list
  if BLACK_LIST.include? cpf.join
    return false
  end

  # calculate first digit
  sum = (0..8).inject(0) do |sum, i|
    sum + cpf[i] * (10 - i)
  end

  result = sum % 11
  result = result < 2 ? 0 : 11 - result

  if result != cpf[9]
    return false
  end

  # calculate second digit
  sum = (0..8).inject(0) do |sum, i|
    sum + cpf[i] * (11 - i)
  end

  sum += cpf[9] * 2

  result = sum % 11
  result = result < 2 ? 0 : 11 - result

  result == cpf[10]
end

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
# File 'lib/custom_validations/br_cpf_validator.rb', line 10

def validate_each(record, attribute, value)
  return if value.to_s.blank? # it doesn't validate presence
  record.errors.add(attribute, :br_cpf, options) unless valid_cpf?(value.to_s)
end