CleanArchitecture.Contract (clean_architecture v0.1.1)

Default functions for input pattern necessary to perform an action.

Link to this section Summary

Functions

Validates required only when the attribute is present to the input.

Link to this section Functions

Link to this function

validate_required_if_attribute_is_present(changeset, fields, attrs)

Validates required only when the attribute is present to the input.

This ables us to update something partially, when the attribute cannot be nil or empty but the key is not required to be present at the input to perform the action.

The following examples explore a scenario when last_name attribute is required if attribute is present.

examples

Examples

iex> validate_required_if_attribute_is_present(%Ecto.Changeset{}, [:last_name], %{name: "Foo"})
%Ecto.Changeset{valid?: true}

iex> validate_required_if_attribute_is_present(%Ecto.Changeset{}, [:last_name], %{last_name: "Bar"})
%Ecto.Changeset{valid?: true}

iex> validate_required_if_attribute_is_present(%Ecto.Changeset{}, [:last_name], %{name: "Foo", last_name: "Bar"})
%Ecto.Changeset{valid?: true}

iex> validate_required_if_attribute_is_present(%Ecto.Changeset{}, [:last_name], %{name: "Foo", last_name: nil})
%Ecto.Changeset{valid?: false}

iex> validate_required_if_attribute_is_present(%Ecto.Changeset{}, [:last_name], %{name: "Foo", last_name: ""})
%Ecto.Changeset{valid?: false}