Packages

A set of validators and matchers, focusing on Brazilian needs, but general too!

Current section

Files

Jump to
xbacon lib changesets cnpj_changeset.ex
Raw

lib/changesets/cnpj_changeset.ex

defmodule XBacon.Changeset.CNPJ do
import Ecto.Changeset
import XBacon.Matchers.CNPJ
@moduledoc """
Defines a set of validations that can be applied on a changeset
using the `XBacon.Matchers.CNPJ` matchers
"""
@doc """
Validates the given changeset looking for the `cnpj` field,
it allows customization of lookup field and error messages.
## Example
struct
|> cast(params, @fields)
|> validate_cnpj
It will look up for a field named :cnpj to acquire the value, if you
do not use this name on field you can customize as second arity.
## Example
struct
|> cast(params, @fields)
|> validate_cnpj(:cadastro_juridico)
"""
def validate_cnpj(changeset, field \\ :cnpj, opts \\ []) do
cnpj = get_field(changeset, field)
case valid? cnpj do
true -> changeset
false ->
add_error(changeset, field, Keyword.get(opts, :message, "CNPJ has invalid format."))
end
end
end