JediHelpers.ChangesetHelpers (jedi_helpers v0.2.2)

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 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.

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.

Example

changeset
|> trim_whitespace(:username, max: 50, 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.