ExEssentials.Web.Plugs.NormalizeQueryParams (ExEssentials v0.4.2)
View SourceA plug that normalizes query parameters by converting special string values into their literal Elixir equivalents.
Normalizations applied:
- """" → ""
- "null" / "undefined" → nil
- "[]" → []
- "{}" → %{}
- "true" / "false" → true / false
- Numeric strings like "123" → 123 (integer)
- Decimal strings like "1.23" → 1.23 (float)
- CSV strings like "open,closed" → ["open", "closed"]
- Map-like strings like "{key: value}" → %{"key" => "value"}
Usage
This plug should be placed in your Phoenix endpoint.ex
before Plug.Parsers
to ensure it only affects query parameters, and not body parameters.
plug ExEssentials.Web.Plugs.NormalizeQueryParams
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Jason
With the plug enabled, incoming query parameters like:
/example?status="null"&ids=1,2,3&filters={type: admin, active: true}
Will be transformed into:
%{
"status" => nil,
"ids" => [1, 2, 3],
"filters" => %{"type" => "admin", "active" => true}
}