JediHelpers.ChangesetHelpers (jedi_helpers v0.2.3)

Provides helper functions for trimming whitespace and validating string fields in Ecto changesets. Particularly useful for ensuring uniqueness and formatting of string inputs before applying database constraints.

Summary

Functions

Trims leading and trailing whitespace from one or more string fields in the changeset. Ensures consistency and helps maintain uniqueness constraints (e.g., on citext fields).

Functions

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

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

Trims leading and trailing whitespace from one or more string fields in the changeset. Ensures consistency and helps maintain uniqueness constraints (e.g., on citext fields).

Options

  • :max (integer): Maximum allowed length after trimming. If exceeded, a validation error is added. Default is 255.
  • :enforce_unique (boolean): When set to true, adds a unique_constraint/3 to the field. Default is false.

Examples

changeset
|> trim_whitespace(:username, max: 50, enforce_unique: true)

changeset
|> trim_whitespace(:username, enforce_unique: true)

Parameters

  • changeset (Ecto.Changeset.t()): The changeset containing the field(s) to be processed.
  • field (atom() or [atom()]): The field(s) to trim.
  • opts (keyword()): Options for trimming and validation.

Returns

  • An updated Ecto.Changeset.t() with trimmed values and optional validations.