-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,97 @@ | ||
defmodule Brcpfcnpj.Changeset do | ||
@moduledoc """ | ||
Define funções para serem utilizadas em conjunto com a API de changeset do Ecto. | ||
""" | ||
if Code.ensure_compiled(Ecto) do | ||
defmodule Brcpfcnpj.Changeset do | ||
@moduledoc """ | ||
Define funções para serem utilizadas em conjunto com a API de changeset do Ecto. | ||
""" | ||
|
||
@type t :: %{ | ||
changes: %{required(atom()) => term()}, | ||
errors: [{atom(), error()}], | ||
valid?: boolean() | ||
} | ||
@type error :: {atom, error_message} | ||
@type error_message :: String.t() | {String.t(), Keyword.t()} | ||
@type changeset :: Ecto.Changeset.t() | ||
@type error :: {atom, error_message} | ||
@type error_message :: String.t() | {String.t(), Keyword.t()} | ||
|
||
@doc """ | ||
Valida se essa mudança é um cnpj válido. Aceita um ou mais fields | ||
@doc """ | ||
Valida se essa mudança é um cnpj válido. Aceita um ou mais fields | ||
## Options | ||
## Options | ||
* `:message` - A mensagem em caso de erro, o default é "Invalid Cnpj" | ||
* `:message` - A mensagem em caso de erro, o default é "Invalid Cnpj" | ||
## Examples | ||
## Examples | ||
validate_cnpj(changeset, :cnpj) | ||
validate_cnpj(changeset, :cnpj) | ||
validate_cnpj(changeset, [:cnpj, :other_cnpj]) | ||
validate_cnpj(changeset, [:cnpj, :other_cnpj]) | ||
""" | ||
@spec validate_cnpj(t, atom | list, Keyword.t()) :: t | ||
def validate_cnpj(changeset, field), do: validate_cnpj(changeset, field, []) | ||
""" | ||
@spec validate_cnpj(changeset(), atom() | list(), Keyword.t()) :: changeset() | ||
def validate_cnpj(changeset, field), do: validate_cnpj(changeset, field, []) | ||
|
||
def validate_cnpj(changeset, field, opts) when is_atom(field) do | ||
validate(changeset, field, fn value -> | ||
if Brcpfcnpj.cnpj_valid?(%Cnpj{number: value}) do | ||
[] | ||
else | ||
[{field, message(opts, {"Invalid Cnpj", validation: :cnpj})}] | ||
end | ||
end) | ||
end | ||
def validate_cnpj(changeset, field, opts) when is_atom(field) do | ||
validate(changeset, field, fn value -> | ||
if Brcpfcnpj.cnpj_valid?(%Cnpj{number: value}) do | ||
[] | ||
else | ||
[{field, message(opts, {"Invalid Cnpj", validation: :cnpj})}] | ||
end | ||
end) | ||
end | ||
|
||
def validate_cnpj(changeset, fields, opts) when is_list(fields) do | ||
Enum.reduce(fields, changeset, fn field, acc_changeset -> | ||
validate_cnpj(acc_changeset, field, opts) | ||
end) | ||
end | ||
def validate_cnpj(changeset, fields, opts) when is_list(fields) do | ||
Enum.reduce(fields, changeset, fn field, acc_changeset -> | ||
validate_cnpj(acc_changeset, field, opts) | ||
end) | ||
end | ||
|
||
@doc """ | ||
Valida se essa mudança é um cpf válido. Aceita um ou mais fields | ||
@doc """ | ||
Valida se essa mudança é um cpf válido. Aceita um ou mais fields | ||
## Options | ||
## Options | ||
* `:message` - A mensagem em caso de erro, o default é "Invalid Cpf" | ||
* `:message` - A mensagem em caso de erro, o default é "Invalid Cpf" | ||
## Examples | ||
## Examples | ||
validate_cpf(changeset, :cpf) | ||
validate_cpf(changeset, :cpf) | ||
validate_cpf(changeset, [:cpf, :cnpj]) | ||
validate_cpf(changeset, [:cpf, :cnpj]) | ||
""" | ||
@spec validate_cpf(t, atom | list, Keyword.t()) :: t | ||
def validate_cpf(changeset, field), do: validate_cpf(changeset, field, []) | ||
""" | ||
@spec validate_cpf(changeset(), atom() | list(), Keyword.t()) :: changeset() | ||
def validate_cpf(changeset, field), do: validate_cpf(changeset, field, []) | ||
|
||
def validate_cpf(changeset, field, opts) when is_atom(field) do | ||
validate(changeset, field, fn value -> | ||
if Brcpfcnpj.cpf_valid?(%Cpf{number: value}) do | ||
[] | ||
else | ||
[{field, message(opts, {"Invalid Cpf", validation: :cpf})}] | ||
end | ||
end) | ||
end | ||
def validate_cpf(changeset, field, opts) when is_atom(field) do | ||
validate(changeset, field, fn value -> | ||
if Brcpfcnpj.cpf_valid?(%Cpf{number: value}) do | ||
[] | ||
else | ||
[{field, message(opts, {"Invalid Cpf", validation: :cpf})}] | ||
end | ||
end) | ||
end | ||
|
||
def validate_cpf(changeset, fields, opts) when is_list(fields) do | ||
Enum.reduce(fields, changeset, fn field, acc_changeset -> | ||
validate_cpf(acc_changeset, field, opts) | ||
end) | ||
end | ||
def validate_cpf(changeset, fields, opts) when is_list(fields) do | ||
Enum.reduce(fields, changeset, fn field, acc_changeset -> | ||
validate_cpf(acc_changeset, field, opts) | ||
end) | ||
end | ||
|
||
defp validate(changeset, field, validator) do | ||
%{changes: changes, errors: errors} = changeset | ||
defp validate(changeset, field, validator) do | ||
%{changes: changes, errors: errors} = changeset | ||
|
||
value = Map.get(changes, field) | ||
new = if is_nil(value), do: [], else: validator.(value) | ||
value = Map.get(changes, field) | ||
new = if is_nil(value), do: [], else: validator.(value) | ||
|
||
case new do | ||
[] -> changeset | ||
[_ | _] -> %{changeset | errors: new ++ errors, valid?: false} | ||
case new do | ||
[] -> changeset | ||
[_ | _] -> %{changeset | errors: new ++ errors, valid?: false} | ||
end | ||
end | ||
end | ||
|
||
defp message(opts, default) do | ||
message = Keyword.get(opts, :message, default) | ||
format_message(message) | ||
end | ||
defp message(opts, default) do | ||
message = Keyword.get(opts, :message, default) | ||
format_message(message) | ||
end | ||
|
||
defp format_message(msg = {_, _}), do: msg | ||
defp format_message(msg) when is_binary(msg), do: {msg, []} | ||
defp format_message(msg = {_, _}), do: msg | ||
defp format_message(msg) when is_binary(msg), do: {msg, []} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
%{ | ||
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, | ||
"credo": {:hex, :credo, "1.1.5", "caec7a3cadd2e58609d7ee25b3931b129e739e070539ad1a0cd7efeeb47014f4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.7", "6287f8f2cb45df8584317a4be1075b8c9b8a69de8eeb82b4d9e6c761cf2664cd", [:mix], [{:erlex, ">= 0.2.5", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"earmark": {:hex, :earmark, "1.4.1", "07bb382826ee8d08d575a1981f971ed41bd5d7e86b917fd012a93c51b5d28727", [:mix], [], "hexpm"}, | ||
"erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm"}, | ||
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"inch_ex": {:hex, :inch_ex, "2.0.0", "24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de", [:mix], [{:bunt, "~> 0.2", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, | ||
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm"}, | ||
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, | ||
"credo": {:hex, :credo, "1.1.5", "caec7a3cadd2e58609d7ee25b3931b129e739e070539ad1a0cd7efeeb47014f4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d0bbd3222607ccaaac5c0340f7f525c627ae4d7aee6c8c8c108922620c5b6446"}, | ||
"decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"}, | ||
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.7", "6287f8f2cb45df8584317a4be1075b8c9b8a69de8eeb82b4d9e6c761cf2664cd", [:mix], [{:erlex, ">= 0.2.5", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "506294d6c543e4e5282d4852aead19ace8a35bedeb043f9256a06a6336827122"}, | ||
"earmark": {:hex, :earmark, "1.4.1", "07bb382826ee8d08d575a1981f971ed41bd5d7e86b917fd012a93c51b5d28727", [:mix], [], "hexpm", "cdfa03374331187c7b9e86d971423a19138dc1cf9902b26923a657c789673876"}, | ||
"ecto": {:hex, :ecto, "3.4.2", "6890af71025769bd27ef62b1ed1925cfe23f7f0460bcb3041da4b705215ff23e", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3959b8a83e086202a4bd86b4b5e6e71f9f1840813de14a57d502d3fc2ef7132"}, | ||
"erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm", "756d3e19b056339af674b715fdd752c5dac468cf9d0e2d1a03abf4574e99fbf8"}, | ||
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f1155337ae17ff7a1255217b4c1ceefcd1860b7ceb1a1874031e7a861b052e39"}, | ||
"inch_ex": {:hex, :inch_ex, "2.0.0", "24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de", [:mix], [{:bunt, "~> 0.2", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "96d0ec5ecac8cf63142d02f16b7ab7152cf0f0f1a185a80161b758383c9399a8"}, | ||
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"}, | ||
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"}, | ||
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"}, | ||
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm", "00e3ebdc821fb3a36957320d49e8f4bfa310d73ea31c90e5f925dc75e030da8f"}, | ||
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, | ||
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, | ||
} |