Paramsx
Paramsx provides functionally to whitelist and validate parameters
Objective
We dont need wait for ecto raise a error in changeset to see a missing key in params
Installation
def deps do
[
{:paramsx, "~> 0.4.1"}
]
endHandlers
We have a handler to return a json for your, in your module responsible for the action_fallback from phoenix you can do:
defmodule YourModuleOfFalback do
use Paramsx.ErrorHandlerFallback
.
.
.
endUsage
Filter with required and optional params
Example:
iex> Paramsx.filter(%{"foo" => "bar", "other" => "value"}, [required: [:foo], optional: []])
{:ok, %{"foo" => "bar"}}
# You have to explicit say that is list when have it:
iex> Paramsx.filter(%{"foo" => [%{"bar" => "value_bar"}]}, required: [foo_list: [:bar]])
{:ok, %{foo: [%{bar: "value_bar"}]}}
# If some error appear was triggered the return will be:
iex> Paramsx.filter(%{"foo" => "bar", "foo2" => "bar2"}, required: [:foo3])
{:error, %{missing_keys: [:foo3]}} Incoming improvements:
- Scroll through inside nested keyword list to be a better filter;
- Add more types to verify
- Config to use Schema of your application to filter;