Skip to content

Finbits/brazilian_documents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brazilian Documents

CI

Validate, format and valid brazilian CPFs and CNPJs.

Installation

The package can be installed by adding brazilian_documents to your list of dependencies in mix.exs:

def deps do
  [
    {:brazilian_documents, "~> 0.3.1"}
  ]
end

The docs can be found at https://hexdocs.pm/brazilian_documents.

Usage

cpf = BrazilianDocuments.generate_cpf()
# "42111813516"
BrazilianDocuments.valid_cpf?(cpf)
# true
BrazilianDocuments.format_cpf(cpf)
# {:ok, "421.118.135-16"}
cnpj = BrazilianDocuments.generate_cnpj()
# "82767804491812"
BrazilianDocuments.valid_cnpj?(cnpj)
# true
BrazilianDocuments.format_cnpj(cnpj)
# {:ok, "82.767.804/4918-12"}

Ecto Integration

import BrazilianDocuments.Changeset

def changeset(target \\ %__MODULE__{}, attrs) do
  target
  |> cast(attrs, @fields)
  |> validate_required(@required_fields)
  |> validate_cpf(:cpf)
  |> validate_cnpj(:cnpj)
end

You can use the type modules in your schemas

alias BrazilianDocuments.Types.{CNPJ, CPF}

schema "table" do
  field :cpf, CPF
  field :cnpj, CNPJ
end

License

Apache License, Version 2.0 © Finbits