EctoTrim

Ecto parameterized type that trims and normalizes whitespace on cast and dump.

Why

Users paste text with extra spaces, trailing whitespace, and long runs of blank lines. EctoTrim normalizes whitespace at the Ecto type level — define the field once, skip the manual String.trim/1 in every changeset.

Status

This library is used in several production websites and is considered complete. Updates will only be made for compatibility with new Elixir or Ecto versions.

Installation

def deps do
  [{:ecto_trim, "~> 1.0"}]
end

Usage

schema "companies" do
  field :name, EctoTrim                    # for text inputs
  field :bio, EctoTrim, mode: :multi_line  # for textareas
end

Modes

Examples

# single_line (default)
changeset = cast(%Company{}, %{name: "  Acme   Corp  "}, [:name])
changeset.changes.name
#=> "Acme Corp"

# multi_line
changeset = cast(%Company{}, %{bio: "Line one\n\n\n\nLine two"}, [:bio])
changeset.changes.bio
#=> "Line one\n\nLine two"

License

MIT