Sha256: 25f5db77f467ff8c58cfb76fc4dc871d54a72d3d8e6247347845b4bba25d59d6

Contents?: true

Size: 848 Bytes

Versions: 1

Compression:

Stored size: 848 Bytes

Contents

# frozen_string_literal: true

module BraDocumentValidation
  class CPFValidator < 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: :cpf, mode: document_format)
    end

    def valid_verification_digit?(document)
      BraDocuments::CPFGenerator.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/cpf_validator.rb