Casting the incoming controller parameters
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"}]endDocumentation can be found at https://hexdocs.pm/construct_params.
ConstractParams provides a macro for controller params casting.
- 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- 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)doendendIn 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.