PhoenixApiToolkit.Ecto.Validators.validate_order_by

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

validate_order_by(changeset, orderable_fields)

View Source

Specs

validate_order_by(Ecto.Changeset.t(), Enum.t()) :: Ecto.Changeset.t()

Validate the value of an order_by query parameter. The format of the parameter is expected to match ~r/^(asc|desc|asc_nulls_last|desc_nulls_last|asc_nulls_first|desc_nulls_first):(\w{1,20})$/ (may be repeated, comma-separated). The supported fields should be passed as a list or MapSet (which performs better) to orderables.

If the change is valid, the original change is replaced with a keyword list of {:field, :direction}, which is supported by PhoenixApiToolkit.Ecto.DynamicFilters.standard_filters/4.

Examples

For the implementation of changeset/1, see Elixir.PhoenixApiToolkit.Ecto.Validators.

@orderables ~w(first_name last_name) |> MapSet.new()

iex> changeset(%{order_by: "asc:last_name"}) |> validate_order_by(@orderables)
#Ecto.Changeset<action: nil, changes: %{order_by: [asc: :last_name]}, errors: [], data: %{}, valid?: true>

iex> changeset(%{order_by: "invalid"}) |> validate_order_by(@orderables)
#Ecto.Changeset<action: nil, changes: %{order_by: []}, errors: [order_by: {"format is asc|desc:field", []}], data: %{}, valid?: false>

iex> changeset(%{order_by: "asc:eye_count"}) |> validate_order_by(@orderables)
#Ecto.Changeset<action: nil, changes: %{order_by: []}, errors: [order_by: {"unknown field eye_count", []}], data: %{}, valid?: false>

iex> changeset(%{order_by: nil}) |> validate_order_by(@orderables)
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: %{}, valid?: true>

iex> changeset(%{order_by: "asc:last_name,desc_nulls_last:first_name"}) |> validate_order_by(@orderables)
#Ecto.Changeset<action: nil, changes: %{order_by: [asc: :last_name, desc_nulls_last: :first_name]}, errors: [], data: %{}, valid?: true>