brdocs v0.2.0 BrDocs.Changeset View Source

BrDocs validations to use with Ecto.Changeset

Although BrDocs can be used as standalone lib, trying to use BrDocs.Changeset without Ecto installed will raise an exception.

Link to this section Summary

Functions

Validates a changeset against BrDocs validation rules. Returns an Ecto.Changeset

Link to this section Functions

Link to this function

validate_doc(changeset, field, kind, custom_message \\ nil) View Source
validate_doc(Ecto.Changeset.t(), atom(), atom(), String.t() | nil) ::
  Ecto.Changeset.t()

Validates a changeset against BrDocs validation rules. Returns an Ecto.Changeset.

Arguments are:

  • field - is the changeset field to be validated
  • kind - is the BrDocs.BrDoc document kind which will identify the validation rule. It must be one of :cpf, :cnpj.

Examples

defmodule User do
use Ecto.Schema
use BrDocs.Changeset

import Ecto.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