View Source CozyParams.PhoenixController (cozy_params v0.1.0)

Providing macros for integrating with Phoenix controllers.

Inspired by elixir-maru/maru_params.

examples

Examples

Extend just a controller:

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

  params :index do
    field :name, :string, required: true
  end

  def index(conn, params) do
    # + when external params is valid, `params` will be converted as a map with atom keys.
    # + when external params is invalid, an `{:error, %Ecto.Changeset{}}` will be returned,
    #   which allows developers to handle it in the fallback controller.
  end
end

Extend all controllers:

defmodule DemoWeb do
  # ...
  def controller do
    quote do
      use Phoenix.Controller, namespace: DemoWeb
      # ...

      use CozyParams.PhoenixController
    end
  end
end

defmodule DemoWeb.PageController do
  use DemoWeb, :controller

  params :index do
    field :name, :string, required: true
  end

  def index(conn, params) do
    # + when external params is valid, `params` will be converted as a map with atom keys.
    # + when external params is invalid, an `{:error, %Ecto.Changeset{}}` will be returned,
    #   which allows developers to handle it in the fallback controller.
  end
end

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

Link to this section Summary

Functions

Defines params for a given action.

Link to this section Functions

Link to this macro

params(action, list)

View Source (since 0.1.0) (macro)

Defines params for a given action.