JediHelpers.ChangesetHelpers (jedi_helpers v0.2.1)

Summary

Functions

Trims leading and trailing whitespaces from the specified fields in the changeset. This ensures consistency and helps maintain uniqueness, especially for citext fields.

Functions

trim_whitespace(changeset, keys, opts \\ [])

@spec trim_whitespace(Ecto.Changeset.t(), atom() | [atom()], keyword()) ::
  Ecto.Changeset.t()

Trims leading and trailing whitespaces from the specified fields in the changeset. This ensures consistency and helps maintain uniqueness, especially for citext fields.

You can also specify a max length in the opts to validate that the field's length after trimming does not exceed the specified value. If it exceeds, a validation error is added.

Example

changeset |> trim_whitespace(:username, max: 50) |> unique_constraint(:username)

Parameters:

  • changeset (Ecto.Changeset.t()): The changeset containing the field to be processed.
  • field (atom() or [atom()]): The field(s) to trim.
  • opts (keyword()): Options for the function:
  • :max (integer()): The maximum allowed length after trimming. Adds a validation error if exceeded.

Returns:

  • An updated Ecto.Changeset.t() with the trimmed field and potential validation error if the length exceeds max.