Class: BrRailties::CpfCnpjValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/br_railties/cpf_cnpj_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/validators/br_railties/cpf_cnpj_validator.rb', line 8

def validate_each(record, attribute, value)
  cpf = ::CPF.new(value)
  return if cpf.valid? && value == cpf.stripped

  cnpj = ::CNPJ.new(value)
  return if cnpj.valid? && value == cnpj.stripped

  record.errors.add(
    attribute,
    options[:message] || 'CPF/CNPJ inválido (9 ou 14 caracteres, somente dígitos)'
  )
end