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.
In mix.exs
, add the BrDocs
dependency:
def deps do
# Get the latest from hex.pm. Works with Ecto +2.x
[
{:brdocs, "~> 0.1"}
]
end
Check out the docs for more details.
Generating valid (but fake) docs:
iex> BrDocs.generate(:cpf)
%BrDocs.Doc{kind: :cpf, value: "12345678909"}
iex> BrDocs.generate(:cpf, formatted: true)
%BrDocs.Doc{kind: :cpf, value: "123.456.789-09"}
iex> BrDocs.generate(:cnpj)
%BrDocs.Doc{kind: :cnpj, value: "11444777000161"}
iex> BrDocs.generate(:cnpj, formatted: true)
%BrDocs.Doc{kind: :cnpj, value: "11.444.777/0001-61"}
Format docs:
iex> BrDocs.format("12345678909", :cpf)
%BrDocs.Doc{kind: :cpf, value: "123.456.789-09"}
iex> BrDocs.format("11444777000161", :cnpj)
%BrDocs.Doc{kind: :cnpj, value: "11.444.777/0001-61"}
Validate docs:
iex> BrDocs.validate(%BrDocs.Doc{kind: :cpf, value: "12345678909"})
true
iex> BrDocs.validate(%BrDocs.Doc{kind: :cpf, value: "12345678900"})
false
iex> BrDocs.validate(%BrDocs.Doc{kind: :cnpj, value: "11444777000161"})
true
iex> BrDocs.validate(%BrDocs.Doc{kind: :cnpj, value: "11444777000160"})
false
There is a validation functions to makes it easy to validate your Brazilian docs w/ Ecto.
validate_doc
validates a Brazilian doc in yourEcto.Changeset
.
Maybe, more to come.
Usage:
defmodule User do
use Ecto.Schema
import Ecto.Changeset
import BrDocs.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
end
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 test
Once you've made your additions and everything passes, go ahead and open a PR!