Para
Para is an Elixir library that provides structured and declarative way to parse and validate 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 database 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.
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.
Para also allows you to define multiple schemas inside the same module to promote consistency between your schema and controller files, resulting in better code organizations.
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
defmodule Web.UserPara do
use Para
validator :create do
required :name, :string
required :age, :integer
required :email, :string
optional :phone, :string
end
validator :update do
required :name, :string
required :age, :integer
required :email, :string
optional :phone, :string
end
end
You can now use this module as a validator in your controller
defmodule Web.UserController do
use Web, :controller
alias Web.UserPara
def create(conn, params) do
with {:ok, data} <- UserPara.validate(:create, params) do
# ...
end
end
def update(conn, params) do
with {:ok, data} <- UserPara.validate(:update, params) do
# ...
end
end
end
For more advanced usage, please refer to the docs