View Source Tarams.Utils (Tarams v1.7.1)

Link to this section Summary

Functions

Clean all nil field from params, support nested map and list.

A plug which do srubbing params

Convert all parameter which value is empty string or string with all whitespace to nil. It works with nested map and list too.

Link to this section Functions

@spec clean_nil(any()) :: any()

Clean all nil field from params, support nested map and list.

Example

params = %{"keyword" => nil, "email" => nil, "type" => "customer"}
Tarams.clean_nil(params)
# => %{"type" => "customer"}

params = %{user_ids: [1, 2, nil]}
Tarams.clean_nil(params)
# => %{user_ids: [1, 2]}
Link to this function

plug_scrub(conn, keys \\ [])

View Source

A plug which do srubbing params

Use in Router

defmodule MyApp.Router do
  ...
  plug Tarams.plug_scrub
  ...
end

Use in controller

plug Tarams.plug_scrub when action in [:index, :show]
# or specify which field to scrub
plug Tarams.plug_scrub, ["id", "keyword"] when action in [:index, :show]

Convert all parameter which value is empty string or string with all whitespace to nil. It works with nested map and list too.

Example

params = %{"keyword" => "   ", "email" => "", "type" => "customer"}
Tarams.scrub_param(params)
# => %{"keyword" => nil, "email" => nil, "type" => "customer"}

params = %{user_ids: [1, 2, "", "  "]}
Tarams.scrub_param(params)
# => %{user_ids: [1, 2, nil, nil]}