Jump.CredoChecks.AvoidModifyingStructBeforeChangeset (Jump.CredoChecks v0.7.1)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Modifying a struct before passing it into a changeset function means the new value is never tracked as a change.

This can occur via %{struct | field: value}, Map.put/3, struct/2, or a helper function that does the same.

Changing the struct this way bakes the new value into the changeset's base data. Since changeset validation only looks at changes, this change bypasses validation entirely. What happens next depends on how you persist the changeset:

  • On insert, Ecto writes every field of the struct (with changes overlaid atop the base data), so the unvalidated value gets written to the database.
  • On update, Ecto writes only the changes, so the value is silently dropped, but the struct returned from Repo.update/2 still contains it, so it looks like the value was saved.

(Casting or putting that same field in the changeset doesn't help. If the new value differs from the one on the struct, it overwrites yours, so setting it on the struct was pointless. If it's the same, Ecto sees no change at all, with the consequences above.)

Instead, route the value through the changeset, so that it's tracked as a change and validated.

# ❌ Bad — Repo.update/2 never saves account_id, though the returned user has it
%{existing_user | account_id: account_id}
|> User.changeset(params)
|> Repo.update()

# ✅ Good
existing_user
|> User.changeset(params, account_id)
|> Repo.update()

# ❌ Bad — name is saved without validation (like length, uniqueness, etc.)
%User{}
|> Map.put(:name, name)
|> User.changeset(params)
|> Repo.insert()

# ✅ Good
%User{}
|> User.changeset(Map.put(params, :name, name))
|> Repo.insert()

This check considers a "changeset function" to be any function with changeset as a word in its name (like changeset, registration_changeset, or changeset_for_registration), plus Ecto.Changeset.cast/4 and Ecto.Changeset.change/2. It flags a modified struct passed to one of these functions directly, through a pipe, through a helper function defined in the same file whose return value is a modified struct, or through a variable bound to a modified struct earlier in the same scope.

Cases this check does not flag:

  • Building a new struct (%User{account_id: account_id} or struct(User, attrs)), since that's the conventional way to set trusted fields like foreign keys on insert. This includes struct/2 called on a variable with module in its name (like struct(schema_module, attrs)), since that variable presumably holds a module rather than a struct.
  • A modified value passed as the only argument to a changeset function (like attrs |> Map.put(:account_id, account_id) |> User.changeset()), since that function conventionally takes a plain map of attributes rather than a struct when creating a new record.
  • A modified value held in a variable named map, attrs, attributes, params, or parameters (like attrs |> Map.put(:account_id, account_id) |> Rubric.changeset(opts)), since by convention that's a plain map of attributes rather than a struct.
  • A struct modified within a changeset function (like filling in a default before calling cast/4). This is somewhat suspect, but we'll assume the changeset function author knows what they're doing.
  • A changeset passed only to Phoenix.Component.to_form/2 or Ecto.Changeset.apply_changes/1. Since these are temporary values (not persisted), the modified struct here is less risky.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.