[![Build Status][shield-travis]][travis-ci]
[![Coverage Status][shield-coveralls]][docs]

Easily define [Plug][plug]'s params types and cast incoming params to their types in a plug for `Phoenix.Controller` or `Plug.Router`.

## Features
* Casting incoming params to defined types
* Nulify params if not exists
* Raise exception for required types (types ending with a `!`)
* Supports namespace for params like. (`user: [name: :string]` prepare params from `user[name]`)

## Usage

### Phoenix Framework
Put `use CastParams` inside Phoenix controller or add it into `web.ex`. It will extend controller module with `cast_params/1` macro which allows to define param's types. 

  defmodule AccountController do
    use AppWeb, :controller
    use CastParams
    # Defining params types for all actions
    # :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
    # Defining for create action
    # user[name] - string value
    # user[subscribe] - boolean value
    cast_params user: [name: :string, subscribe: :boolean] when action == :create

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

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

    def create(conn, %{"user" => %{"name" => name, "subscribe" => subscribe}) do

Documentation can be found at [][docs].

### Other Plug.Router Apps
For other applications using `Plug.Router`, call the `cast_params` anytime after calling the `:match` and `:dispatch` plugs:

defmodule MyApp.Router do
  use Plug.Router
  use CastParams
  plug :match
  plug :dispatch

  cast_params(category_id: :integer, terms: :boolean)
  cast_params(name: :string)

  get("/", do: send_resp(conn, 200, "ok"))

## Supported Types
Each type can ending with a `!` to mark the parameter as required.

* *`:boolean`*
* *`:integer`* 
* *`:string`* 
* *`:float`* 
* *`:decimal`*

## Installation

[Available in Hex](, the package can be installed as:

Add `cast_params` to your list of dependencies in mix.exs:

def deps do
    {:cast_params, ">= 0.0.3"} 

## Contribution
Feel free to send your PR with proposals, improvements or corrections 😉.

## License
This software is licensed under [the MIT license](