CPF v1.1.0 CPF.Ecto.Changeset View Source

Provide functions to validate CPF field in a changeset.

Link to this section Summary

Functions

Verifies if given field in the changeset has a valid CPF.

Link to this section Types

Link to this section Functions

Link to this function

validate_cpf(changeset, field)

View Source
validate_cpf(changeset(), atom()) :: changeset()

Verifies if given field in the changeset has a valid CPF.

Examples

iex> {%{}, %{cpf: :string}}
...>  |> Ecto.Changeset.cast(%{"cpf" => "aaa"}, [:cpf])
...>  |> CPF.Ecto.Changeset.validate_cpf(:cpf)
...>  |> Map.get(:errors)
[cpf: {"is invalid", [reason: :invalid_format]}]

iex> {%{}, %{cpf: :string}}
...>  |> Ecto.Changeset.cast(%{"cpf" => "429.329.147-40"}, [:cpf])
...>  |> CPF.Ecto.Changeset.validate_cpf(:cpf)
...>  |> Map.get(:errors)
[]

iex> {%{}, %{cpf: :string}}
...>  |> Ecto.Changeset.cast(%{"cpf" => "42932914740"}, [:cpf])
...>  |> CPF.Ecto.Changeset.validate_cpf(:cpf)
...>  |> Map.get(:errors)
[]