View Source CozyParams

CI Hex.pm built with Nix

Provides Ecto-like API for casting and validating params.

Why another package?

There are some packages in the community:

NAMEBased on Ecto?Use Ecto-like API?
paramsYESNO
maru_paramsNONO
taramsNONO
...

But, they don't fit my requirements. The package in my dream should:

  • be based on Ecto which is robust and battle-tested.
  • use Ecto-like API, which eliminates friction when working on casting params and modeling data source at the same time.

Installation

Add :cozy_params to the list of dependencies in mix.exs:

def deps do
  [
    {:cozy_params, "<requirement>"}
  ]
end

(Optional) And, it's encouraged to setup .formatter.exs for cozy_params:

[
  import_deps: [
    # ...
    :cozy_params
  ],
  # ...
]

Overview

An example integrating with Phoenix:

defmodule DemoWeb.PageController do
  use DemoWeb, :controller
  import CozyParams

  action_fallback DemoWeb.FallbackController

  defparams :product_search do
    field :name, :string, required: true

    embeds_many :tags do
      field :name, :string, required: true
    end
  end

  def index(conn, params) do
    with {:ok, data} <- product_search(params) do
      # ...
    end
  end
end

defmodule DemoWeb.FallbackController do
  use DemoWeb, :controller

  # ...

  # handle errors for cozy_params
  def call(conn, {:error, params_changeset: %Ecto.Changeset{} = changeset}) do
    messages = CozyParams.get_error_messages(changeset)
    # render messages in HTML, JSON, etc.
  end

  # handle errors for normal changsets from Ecto.
  def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
    # ...
  end

  # ...
end

Visit HexDocs for more details.

License

Apache License 2.0