brdocs
Elixir client to generate, validate and format different Brazilian docs.
BrDocs it’s not Ecto specific, but has Ecto support. #ftw
Currently supported docs: CPF, CNPJ
This README follows master branch, which may not be the currently published version.
Here are the docs for the latest published version of BrDocs.
Installation
To install in all environments (useful for generating seed data in dev/prod):
In mix.exs, add the BrDocs dependency:
def deps do
# Get the latest from hex.pm. Works with Ecto +2.x
[
{:brdocs, "~> 0.1"}
]
endOverview
Check out the docs for more details.
Generating valid (but fake) docs:
iex> BrDocs.generate(:cpf)
%BrDocs.BrDoc{kind: :cpf, value: "12345678909"}
iex> BrDocs.generate(:cpf, formatted: true)
%BrDocs.BrDoc{kind: :cpf, value: "123.456.789-09"}
iex> BrDocs.generate(:cnpj)
%BrDocs.BrDoc{kind: :cnpj, value: "11444777000161"}
iex> BrDocs.generate(:cnpj, formatted: true)
%BrDocs.BrDoc{kind: :cnpj, value: "11.444.777/0001-61"}Format docs:
iex> BrDocs.format("12345678909", :cpf)
%BrDocs.BrDoc{kind: :cpf, value: "123.456.789-09"}
iex> BrDocs.format("11444777000161", :cnpj)
%BrDocs.BrDoc{kind: :cnpj, value: "11.444.777/0001-61"}Validate docs:
iex> BrDocs.validate(%BrDocs.BrDoc{kind: :cpf, value: "12345678909"})
true
iex> BrDocs.validate(%BrDocs.BrDoc{kind: :cpf, value: "12345678900"})
false
iex> BrDocs.validate(%BrDocs.BrDoc{kind: :cnpj, value: "11444777000161"})
true
iex> BrDocs.validate(%BrDocs.BrDoc{kind: :cnpj, value: "11444777000160"})
falseEcto Support
There are validation functions to make it easier to validate your Brazilian docs using Ecto.
validate_docvalidates a Brazilian doc using theEcto.Changesetjust like allEcto.Changesetcommon validations.
Maybe, more to come.
Usage:
defmodule User do
use Ecto.Schema
use BrDocs.Changeset
import Ecto.Changeset
schema "users" do
field :name
field :brazilian_doc
end
def changeset(user, params \\\\ %{}) do
user
|> cast(params, [:name, :brazilian_doc])
|> validate_required([:name, :brazilian_doc])
|> validate_doc(:brazilian_doc, :cpf)
|> unique_constraint(:brazilian_doc)
end
endContributing
Before opening a pull request, please open an issue first.
$ git clone https://github.com/emaiax/brdocs.git
$ cd brdocs
$ mix deps.get
$ mix format --check-formatted
$ mix credo --strict
$ mix testOnce you’ve made your additions and everything passes, go ahead and open a PR!