Sha256: 430c6e2ba89b159d0a42c9d7ac5789ffd2ffd56a1b0cd4d7060ae52096161969

Contents?: true

Size: 851 Bytes

Versions: 1

Compression:

Stored size: 851 Bytes

Contents

# frozen_string_literal: true

module BraDocumentValidation
  class CNPJValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      return record.errors.add(attribute, error_message(:invalid_format)) unless valid_format?(value)

      record.errors.add(attribute, error_message(:invalid_verification_digit)) unless valid_verification_digit?(value)
    end

    private

    def valid_format?(document)
      BraDocuments::Matcher.match?(document.to_s, kind: :cnpj, mode: document_format)
    end

    def valid_verification_digit?(document)
      BraDocuments::CNPJGenerator.valid_verification_digit?(document: document.to_s)
    end

    def document_format
      options[:formatted] ? :formatted : :raw
    end

    def error_message(default_message)
      options.fetch(:message, default_message)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bra_document_validation-1.0.2 lib/bra_document_validation/cnpj_validator.rb