CNPJ is an acronym for "Cadastro Nacional da Pessoa Jurídica," it's a identifier number associated to companies that the Brazilian government maintains. With this number, it is possible to check or retrieve information about a company.
This library provides a validation that checks if the number is a valid CNPJ number. The CPF has check digit algorithm is similar to ISBN 10, you can check the details in Portuguese here.
If available in Hex, the package can be installed
by adding cnpj
to your list of dependencies in mix.exs
:
def deps do
[
{:cnpj, "~> 0.2.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/cnpj.
You can verify if a CNPJ is valid by calling the function CNPJ.valid?/1
:
CNPJ.valid?(13_118_061_000_108)
# => true
CNPJ.valid?(13_118_061_000_107)
# => false
The CNPJ.parse/1
and CNPJ.parse!/1
returns you the CNPJ value wrapped in a custom type with explicit digits.
CNPJ.parse("70947414000108")
# => {:ok, %CNPJ{digits: {7, 0, 9, 4, 7, 4, 1, 4, 0, 0, 0, 1, 0, 8}}}
CNPJ.parse("70947414000109")
# => {:error, %CNPJ.ParsingError{reason: :invalid_verifier}}
CNPJ.parse!("70947414000108")
# => %CNPJ{digits: {7, 0, 9, 4, 7, 4, 1, 4, 0, 0, 0, 1, 0, 8}}
CNPJ.parse!("70947414000109")
# => ** (CNPJ.ParsingError) invalid_verifier
Create valid CNPJ and in sequence call CNPJ.format/1
:
iex> 70947414000108 |> CNPJ.parse!() |> CNPJ.format()
"70.947.414/0001-08"
iex> "70947414000108" |> CNPJ.parse!() |> CNPJ.format()
"70.947.414/0001-08"
The CNPJ.format/1
expects the CNPJ type.