Skip to content

Kartstig/ecto_changeset_tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ecto Changeset Tools

Don't go schema-writing without em

CircleCI codecov

Installation

If available in Hex, the package can be installed by adding ecto_changeset_tools to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_changeset_tools, "~> 1.0.0"}
  ]
end

Usage

EctoChangesetTools provides some more commonly used abstractions for updating and validating fields for modules that implement Ecto.Schema, but more specifically, modules that utilitize Ecto.Changeset.change/2.

All functions in this module are designed to be pipe friendly. That means you can easily incorporate them into your schemas:

defmodule User do
  use Ecto.Schema
  import EctoChangesetTools.Formatters
  import EctoChangesetTools.Validators

  @spec changeset(__MODULE__.t(), map()) :: Ecto.Changeset.t()
  def changeset(%__MODULE__{} = user, attrs) do
    user
    |> cast(attrs, [
      :email,
      :first_name,
      :last_name,
      :phone_number
    ])
    |> downcase_fields([:email, :first_name, :last_name])
    |> sanitize_phone_field(:phone_number)
    |> validate_phone_field(:phone_number)
    |> validate_required([:email])
    |> unique_constraint(:email)
  end
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_changeset_tools.

About

Handy utilities for working with Ecto.Changeset

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages