View Source CastParams (CastParams v0.0.5)

Plug for casting request params to defined types.

Usage

defmodule AccountController do
  use AppWeb, :controller
  use CastParams

  # define params types
  # :category_id - required integer param (raise CastParams.NotFound if not exists)
  # :weight - float param, set nil if doesn't exists
  cast_params category_id: :integer!, weight: :float

  # defining for show action
  # :name - is required string param
  # :terms - is boolean param
  cast_params name: :string!, terms: :boolean when action == :show

  # received prepared params
  def index(conn, %{"category_id" => category_id, "weight" => weight} = params) do
  end

  # received prepared params
  def show(conn, %{"category_id" => category_id, "terms" => terms, "weight" => weight} = params) do
  end
end

Supported Types

Each type can ending with a ! to mark the parameter as required.

  • :boolean
  • :integer
  • :string
  • :float
  • :decimal

Summary

Types

Options for use CastParams

Functions

Stores a plug to be executed as part of the plug pipeline.

Types

@type options() :: [{:nulify, boolean()}]

Options for use CastParams

Functions

Link to this macro

cast_params(schema)

View Source (macro)
@spec cast_params(CastParams.Schema.t()) :: Macro.t()

Stores a plug to be executed as part of the plug pipeline.