View Source ChangesetHelpers (Changeset Helpers v0.22.0)

Provides a set of helpers to work with Changesets.

Summary

Functions

Adds an error to the nested changeset.

Returns the nested association in a changeset. This function will first look into the changes and then fails back on data wrapped in a changeset.

Returns the nested association in a changeset at the given index.

This function allows checking if a given field is different between two changesets.

Fetches a nested change from the given changeset.

Same as fetch_change/2 but returns the value or raises if the given nested key was not found.

Fetches the given nested field from changes or from the data.

Same as fetch_field/2 but returns the value or raises if the given nested key was not found.

Puts the given nested association in the changeset through a given list of field names.

Puts the given nested association in the changeset at the given index.

Validates a list of values using the given validator.

validate_not_changed/3 checks if the specified attribute is present in the changeset's parameters. If the attribute is present, an error is added to the changeset's :errors key.

Functions

Link to this function

add_error(changeset, keys, message, extra \\ [])

View Source

Adds an error to the nested changeset.

account_changeset =
  ChangesetHelpers.add_error(account_changeset, [:user, :articles, :error_key], "Some error")
Link to this function

change_assoc(struct_or_changeset, keys)

View Source
Link to this function

change_assoc(struct_or_changeset, keys, changes)

View Source

Returns the nested association in a changeset. This function will first look into the changes and then fails back on data wrapped in a changeset.

Changes may be added to the given changeset through the third argument.

A tuple is returned containing the root changeset, and the changeset of the association.

{account_changeset, address_changeset} =
  change_assoc(account_changeset, [:user, :config, :address], %{street: "Foo street"})
Link to this function

change_assoc(struct_or_changeset, keys, index, changes)

View Source

Returns the nested association in a changeset at the given index.

A tuple is returned containing the root changeset, the changesets of the association and the changeset at the specified index.

See change_assoc(struct_or_changeset, keys, changes).

Link to this function

diff_field(changeset1, changeset2, keys)

View Source

This function allows checking if a given field is different between two changesets.

{street_changed, street1, street2} =
  diff_field(account_changeset, new_account_changeset, [:user, :config, :address, :street])
Link to this function

do_has_change?(changeset, list)

View Source
Link to this function

fetch_change(changeset, list)

View Source

Fetches a nested change from the given changeset.

This function only looks at the :changes field of the given changeset and returns {:ok, value} if the change is present or :error if it's not.

{:ok, street} =
  ChangesetHelpers.fetch_change(account_changeset, [:user, :config, :address, :street])
Link to this function

fetch_change!(changeset, keys)

View Source

Same as fetch_change/2 but returns the value or raises if the given nested key was not found.

street = ChangesetHelpers.fetch_change!(account_changeset, [:user, :config, :address, :street])
Link to this function

fetch_field(changeset, list)

View Source

Fetches the given nested field from changes or from the data.

While fetch_change/2 only looks at the current changes to retrieve a value, this function looks at the changes and then falls back on the data, finally returning :error if no value is available.

For relations, these functions will return the changeset original data with changes applied. To retrieve raw changesets, please use fetch_change/2.

{:changes, street} =
  ChangesetHelpers.fetch_field(account_changeset, [:user, :config, :address, :street])
Link to this function

fetch_field!(changeset, keys)

View Source

Same as fetch_field/2 but returns the value or raises if the given nested key was not found.

street = ChangesetHelpers.fetch_field!(account_changeset, [:user, :config, :address, :street])
Link to this function

field_fails_validation?(changeset, field, validations)

View Source
Link to this function

field_violates_constraint?(changeset, field, constraints)

View Source
Link to this function

has_change?(changeset, keys)

View Source
Link to this function

put_assoc(changeset, keys, value)

View Source

Puts the given nested association in the changeset through a given list of field names.

ChangesetHelpers.put_assoc(account_changeset, [:user, :config, :address], address_changeset)

Instead of giving a Changeset or a schema as the third argument, a function may also be given receiving the nested Changeset(s) to be updated as argument.

ChangesetHelpers.put_assoc(account_changeset, [:user, :articles],
  &(Enum.concat(&1, [%Article{} |> Ecto.Changeset.change()])))

In the code above, we add a new empty Article to the articles association (typically done when we want to add a new article to a form).

Link to this function

put_assoc(changeset, keys, index, value)

View Source

Puts the given nested association in the changeset at the given index.

See put_assoc(changeset, keys, value).

Link to this function

update_assoc_changes(changeset, keys, changes)

View Source
Link to this function

validate_changes(changeset, fields, meta, validator)

View Source

Works like Ecto.Changeset.validate_change/3 but may receive multiple fields.

The validator function receives as argument a keyword list, where the keys are the field names and the values are the change for this field, or the data.any()

If one of the fields is nil, the validator function is not invoked.

Link to this function

validate_comparison(changeset, field1, operator, field2_or_value, opts \\ [])

View Source

Validates the result of the comparison of

  • two fields (where at least one is a change) or
  • a change and a value, where the value is an integer, a Date, a Time, a DateTime or a NaiveDateTime.

Options

  • :error_on_field - specifies on which field to add the error, defaults to the first field
  • :message - a customized message on failure
Link to this function

validate_list(changeset, field, validation_fun, validation_fun_args)

View Source

Validates a list of values using the given validator.

changeset =
  %Appointment{}
  |> Appointment.changeset(%{days_of_week: [1, 3, 8]})
  |> validate_list(:days_of_week, &Ecto.Changeset.validate_inclusion/3, [1..7])

assert [days_of_week: {"is invalid", [validation: :list, index: 2, validator: :validate_inclusion]}] = changeset.errors
assert [days_of_week: {:list, [validator: :validate_inclusion]}] = changeset.validations
Link to this function

validate_not_changed(changeset, field_or_fields)

View Source

validate_not_changed/3 checks if the specified attribute is present in the changeset's parameters. If the attribute is present, an error is added to the changeset's :errors key.