README.md

# 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`:

```elixir
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`.

* `include` accepts a list where only `company` and `profile` are accepted as
  values.
* `filter` accepts a map with the keys `name` and `only_active`, of which `name`
  can be any value and `only_active` must be boolean. If none was given
  `only_active` will 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:

```elixir
%{
  "include" => ["company", "profile"],
  "filter" => %{"name" => "john", "only_active" => false}
}
```

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `plug_validate_query` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:plug_validate_query, "~> 0.1.0"}
  ]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/plug_validate_query](https://hexdocs.pm/plug_validate_query).