Class: CnpjValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/cnpj_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/cnpj_validator.rb', line 2

def validate_each(record, attribute, value)
  return if value.blank?

  unless CNPJ.new(value).valid?
    default_message = I18n.t('activemodel.errors.messages.cnpj', 'invalid CNPJ', default: 'invalid CNPJ')
    record.errors[attribute] << (options[:message] || default_message)
  end
end