Provides Ecto-like API for casting and validating params.
There are some packages in the community:
NAME | Based on Ecto? | Use Ecto-like API? |
---|---|---|
params | YES | NO |
maru_params | NO | NO |
tarams | NO | NO |
... |
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.
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
],
# ...
]
CozyParams
- provides macros for general usage.CozyParams.Schema
- provides macros for defining schemas directly.
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.
Apache License 2.0