View Source CozyParams (cozy_params v0.1.0)

Expose more user friendly API for underlying modules.

Link to this section Summary

Functions

Defines a function for casting and validating params.

Extract error messages from %Ecto.Changeset{}.

Link to this section Functions

Link to this macro

defparams(name, list)

View Source (since 0.1.0) (macro)

Defines a function for casting and validating params.

Essentially, this macro is just a shortcut for using CozyParams.Schema.

Inspired by vic/params.

examples

Examples

defmodule Demo do
  import CozyParams

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

  def search(params) do
    with {:ok, data} <- product_search(params) do
      # process data
    end
  end
end

Above defparams :product_search do: block will:

  1. create a module Demo.CozyParams.ProductSearch automatically.
  2. inject product_search/1 and product_search/2 into current module. And, these two functions will call Demo.CozyParams.ProductSearch.from internally.

For more details of the schema definations in do: block, check out CozyParams.Schema.

defparams can be used in any module, not limited to Phoenix controllers.

In order to demonstrate this point, above example is using an normal Demo module, instead of a Phoenix controller.

For better integration with Phoenix controllers, check out CozyParams.PhoenixController.

Link to this function

get_error_messages(changeset)

View Source (since 0.1.0)

Extract error messages from %Ecto.Changeset{}.