README.md

# Para

Para is an Elixir library that provides structured and declarative way of validating HTTP parameters.

Para uses Ecto under the hood and therefore inherits most of its utilities such as changeset and built-in validators.

### Why use Para?

When building API endpoints that deal with a lot of parameters, it is sometimes not enough to just rely on Ecto schema for parsing and validation. A lot of times the HTTP parameters do not always represent the final form of the data that gets sent to the database.

![Pipeline](https://user-images.githubusercontent.com/845515/131730786-61c360bd-43ca-4dbc-a0ce-b3a283eeb3cb.png)

Para is meant to be implemented as part of your data processing pipeline, typically between external source to internal services. Using Para in this way avoids having to contaminate your controllers and internal services with repetitive inputs parsing and validations. It should also make testing a lot easier.

### Usage

Add Para as a dependency in your `mix.exs` file.

```
def deps do
  [{:para, "~> 0.1"}]
end
```

After you are done, run `mix deps.get` in your shell to fetch and compile Para.

### Examples

First, let's define your parameters schema

```elixir
defmodule Web.FooPara do
  use Para

  validator :create do
    required :name, :string
    required :age, :integer
    required :email, :string
    optional :phone, :string
  end

  validator :update do
    optional :name, :string
    optional :age, :integer
    optional :email, :string
    optional :phone, :string
  end
end
```

You can now use this module as a validator in your controller

```elixir
defmodule Web.FooController do
  use Web, :schema
  alias Web.FooPara

  def create(conn, params) do
    with {:ok, data} <- FooPara.validate(:create, params) do
      # ...
    end
  end

  def update(conn, params) do
    with {:ok, data} <- FooPara.validate(:update, params) do
      # ...
    end
  end
end
```

For more advanced usage, please refer to the [docs](https://hexdocs.pm/para/api-reference.html)