diff --git a/lib/gc_index_relay_web/controllers/filter_controller.ex b/lib/gc_index_relay_web/controllers/filter_controller.ex index a6a79bc..781717a 100644 --- a/lib/gc_index_relay_web/controllers/filter_controller.ex +++ b/lib/gc_index_relay_web/controllers/filter_controller.ex @@ -74,10 +74,10 @@ defmodule GcIndexRelayWeb.FilterController do @spec validate_required_params(map()) :: {:ok, map()} | {:error, String.t()} def validate_required_params(params) do - if not Map.has_key?(params, "limit") do - {:error, "The filter must specify a limit."} - else + if Map.has_key?(params, "limit") do {:ok, params} + else + {:error, "The filter must specify a limit."} end end @@ -141,11 +141,7 @@ defmodule GcIndexRelayWeb.FilterController do |> Enum.reduce_while({:ok, %{}}, fn {key, value}, {:ok, acc} -> case parse_param(key, value) do {:ok, parsed_value} -> - out_key = - if byte_size(key) == 1 and - ((key >= "a" and key <= "z") or (key >= "A" and key <= "Z")), - do: "#" <> key, - else: key + out_key = parse_tag(key) {:cont, {:ok, Map.put(acc, out_key, parsed_value)}} @@ -155,6 +151,16 @@ defmodule GcIndexRelayWeb.FilterController do end) end + # Parse filter keys that represent tags. Note that only single-letter keys are treated as tags; + # all other keys are passed through unchanged. + @spec parse_tag(String.t()) :: String.t() + defp parse_tag(key) do + if byte_size(key) == 1 and + ((key >= "a" and key <= "z") or (key >= "A" and key <= "Z")), + do: "#" <> key, + else: key + end + # Parse individual parameter based on its key @spec parse_param(String.t(), String.t()) :: {:ok, any()} | {:error, String.t()} defp parse_param("ids", value), do: {:ok, String.split(value, ",")}