View Source FatUtils.Changeset (FatEcto v1.0.0)
Provides utility functions for validating Ecto changesets.
This module includes functions for validating XOR conditions, requiring specific fields, and comparing datetime fields.
Summary
Functions
Adds a custom error to the changeset.
Makes a field required if another field is present in the changeset.
Validates that at least one of the specified OR keys is present in the changeset.
Validates that only one of the specified keys is present in the changeset.
Validates that a start date/time is before an end date/time.
Validates that a start date/time is before or equal to an end date/time.
Validates that only one of the specified XOR keys is present in the changeset.
Functions
@spec add_custom_error(Ecto.Changeset.t(), atom(), String.t()) :: Ecto.Changeset.t()
Adds a custom error to the changeset.
Parameters
changeset
: The Ecto changeset.error_message_title
: The field or title for the error.error_message
: The error message (default: "is invalid").
Examples
iex> changeset = cast(%User{}, %{}, [])
iex> FatUtils.Changeset.add_custom_error(changeset, :field, "custom error")
#Ecto.Changeset<...>
@spec require_field_if_present( Ecto.Changeset.t(), keyword() ) :: Ecto.Changeset.t()
Makes a field required if another field is present in the changeset.
Parameters
changeset
: The Ecto changeset.if_change_key
: The key to check for presence.require_key
: The key to make required.
Examples
iex> changeset = cast(%User{}, %{field1: "value1"}, [:field1, :field2])
iex> FatUtils.Changeset.require_field_if_present(changeset, if_change_key: :field1, require_key: :field2)
#Ecto.Changeset<...>
@spec validate_at_least_one_field(Ecto.Changeset.t(), map(), [atom()], keyword()) :: Ecto.Changeset.t()
Validates that at least one of the specified OR keys is present in the changeset.
Parameters
changeset
: The Ecto changeset.record
: The original record (currently unused).or_keys
: A list of keys to validate.options
: Additional options (currently unused).
Examples
iex> changeset = cast(%User{}, %{}, [:field1, :field2])
iex> FatUtils.Changeset.validate_at_least_one_field(changeset, %User{}, [:field1, :field2])
#Ecto.Changeset<...>
@spec validate_only_one_field(Ecto.Changeset.t(), map(), [atom()], keyword()) :: Ecto.Changeset.t()
Validates that only one of the specified keys is present in the changeset.
Parameters
changeset
: The Ecto changeset.record
: The original record (currently unused).single_keys
: A list of keys to validate.options
: Additional options (currently unused).
Examples
iex> changeset = cast(%User{}, %{field1: "value1"}, [:field1, :field2])
iex> FatUtils.Changeset.validate_only_one_field(changeset, %User{}, [:field1, :field2])
#Ecto.Changeset<...>
@spec validate_start_before_end(Ecto.Changeset.t(), atom(), atom(), keyword()) :: Ecto.Changeset.t()
Validates that a start date/time is before an end date/time.
Parameters
changeset
: The Ecto changeset.start_date_key
: The key for the start date/time.end_date_key
: The key for the end date/time.options
: Options to customize the error message or specify comparison type (:time
or:datetime
).
Examples
iex> changeset = cast(%User{}, %{start_time: ~T[10:00:00], end_time: ~T[09:00:00]}, [:start_time, :end_time])
iex> FatUtils.Changeset.validate_start_before_end(changeset, :start_time, :end_time, compare_type: :time)
#Ecto.Changeset<...>
@spec validate_start_before_or_equal_end( Ecto.Changeset.t(), atom(), atom(), keyword() ) :: Ecto.Changeset.t()
Validates that a start date/time is before or equal to an end date/time.
Parameters
changeset
: The Ecto changeset.start_date_key
: The key for the start date/time.end_date_key
: The key for the end date/time.options
: Options to customize the error message or specify comparison type (:time
or:datetime
).
Examples
iex> changeset = cast(%User{}, %{start_time: ~T[10:00:00], end_time: ~T[10:00:00]}, [:start_time, :end_time])
iex> FatUtils.Changeset.validate_start_before_or_equal_end(changeset, :start_time, :end_time, compare_type: :time)
#Ecto.Changeset<...>
@spec validate_xor_fields(Ecto.Changeset.t(), map(), [atom()], keyword()) :: Ecto.Changeset.t()
Validates that only one of the specified XOR keys is present in the changeset.
Parameters
changeset
: The Ecto changeset.record
: The original record (used to check if all XOR keys are empty).xor_keys
: A list of keys to validate as XOR.options
: Additional options (currently unused).
Examples
iex> changeset = cast(%User{}, %{field1: "value1", field2: "value2"}, [:field1, :field2])
iex> FatUtils.Changeset.validate_xor_fields(changeset, %User{}, [:field1, :field2])
#Ecto.Changeset<...>