Skip to content

exfoundry/ecto_trim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • :single_line (default) — trims leading/trailing whitespace and collapses all internal whitespace to single spaces
  • :multi_line — trims leading/trailing whitespace and collapses 3+ consecutive newlines to 2, preserving paragraph breaks

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages