Skip to content

Repository files navigation

ConstructParams

Casting the incoming controller parameters

Installation

If available in Hex, the package can be installed by adding construct_params to your list of dependencies in mix.exs:

defdepsdo[{:construct_params,"~> 0.3.0"}]end

Documentation can be found at https://hexdocs.pm/construct_params.

Usage

ConstractParams provides a macro for controller params casting.

  1. First, you need to create a module that cast the input parameters.
    For casting uses construct library.
defmoduleMyAppWeb.Api.Requests.Users.IndexdouseConstructdofield(:next_page,:string,default: nil)field(:limit,:integer,default: nil)field(:user_id,:integer,default: nil)endend
  1. Then add a decorator to the action with the parameters you want to cast.
defmoduleMyAppWeb.Api.UsersControllerdouseConstructParams.CastDecorator@decoratecast(MyAppWeb.Api.Requests.Users.Index)defindex(conn,params)doendend

In case of a successful casting, the controller action params are replaced with the casted data.
Otherwise the appropriate FallbackController is called with {:error, errors} parameters.

About

Casting the incoming controller parameters

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages