PhoenixApiToolkit.Ecto.Validators.map_if_valid

You're seeing just the function map_if_valid, go back to PhoenixApiToolkit.Ecto.Validators module for more information.
Link to this function

map_if_valid(changeset, then_do, else_do \\ &(&1))

View Source

Specs

map_if_valid(
  Ecto.Changeset.t(),
  (Ecto.Changeset.t() -> any()),
  (Ecto.Changeset.t() -> any())
) :: Ecto.Changeset.t()

If changeset is valid, apply the first function then_do to it, else apply the second function else_do to it, which defaults to the identity function.

Examples

# function then_do is applied to the changeset if it is valid
iex> %Ecto.Changeset{valid?: true} |> map_if_valid(& &1.changes)
%{}

# if the changeset is invalid and else_do is provided, apply it to the changeset
iex> %Ecto.Changeset{valid?: false} |> map_if_valid(& &1.changes, & &1.errors)
[]

# else_do defaults to identity, returning the changeset
iex> %Ecto.Changeset{valid?: false} |> map_if_valid(& &1.changes)
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: nil, valid?: false>