ValidateQuery plug
A Plug that filters arbitrary query parameter keys.
Example
This plug was made primarily for JSON-API like usage in mind. With this spec, a
couple of query parameters are expected, such as include, filter and sort:
defmodule MyApp.UserController do
plug(ValidateQuery,
include: {["company", "profile", :list]},
filter: [name: {:any}, only_active: {:any, :boolean, true}],
)
def index(conn, _params) do
# return response
end
end
In the above example we filter two different query parameters: include and
filter.
includeaccepts a list where onlycompanyandprofileare accepted as values.filteraccepts a map with the keysnameandonly_active, of whichnamecan be any value andonly_activemust be boolean. If none was givenonly_activewill default to true.
The following url is an example of a (full) valid query according to the above spec:
/users?include=company,profile&filter[name]=john&filter[only_active]=false.
Which would results in conn.params like so:
%{
"include" => ["company", "profile"],
"filter" => %{"name" => "john", "only_active" => false}
}Installation
If available in Hex, the package can be installed
by adding plug_validate_query to your list of dependencies in mix.exs:
def deps do
[
{:plug_validate_query, "~> 0.1.0"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/plug_validate_query.