Peri

Peri is a schema validation library for Elixir, inspired by Clojure's Plumatic Schema. It focuses on validating raw maps and supports nested schemas and optional fields. With Peri, you can define schemas and validate data structures in a concise and readable manner.

Features

Installation

To use Peri in your project, add it to your dependencies in mix.exs:

def deps do
[
{:peri, "~> 0.1.0"}
]
end

Then, run mix deps.get to fetch the dependencies.

Usage

Defining Schemas

To define a schema, use the defschema macro. By default, all fields in the schema are optional unless specified as {:required, type}.

defmodule MySchemas do
import Peri
defschema :product, %{
id: {:required, :integer},
name: {:required, :string},
price: :float,
in_stock: :boolean
}
end

Validating Data

You can then use the schema to validate data:

product_data = %{id: 1, name: "Laptop", price: 999.99, in_stock: true}
case MySchemas.product(product_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end

Available Types

Peri supports the following types for schema definitions:

Examples

Simple Schema Validation

defmodule MySchemas do
import Peri
defschema :order, %{
order_id: {:required, :integer},
customer_name: {:required, :string},
total_amount: :float
}
end
order_data = %{order_id: 123, customer_name: "Alice", total_amount: 59.99}
case MySchemas.order(order_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end
invalid_order_data = %{order_id: 123}
case MySchemas.order(invalid_order_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end

Nested Schema Validation

defmodule MySchemas do
import Peri
defschema :user_profile, %{
username: {:required, :string},
email: {:required, :string},
details: %{
age: :integer,
bio: :string
}
}
end
profile_data = %{
username: "bob_smith",
email: "bob@example.com",
details: %{age: 30, bio: "Software developer"}
}
case MySchemas.user_profile(profile_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end
invalid_profile_data = %{
username: "bob_smith",
email: "bob@example.com",
details: %{age: "thirty"}
}
case MySchemas.user_profile(invalid_profile_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end

Comparison: Peri vs. Ecto

While both Peri and Ecto provide mechanisms for working with schemas in Elixir, they serve different purposes and are used in different contexts.

Peri

Ecto

Summary

By understanding the different strengths and use cases of Peri and Ecto, you can choose the right tool for your specific needs, ensuring efficient and effective data handling in your Elixir applications.

Why "Peri"?

The name "Peri" is derived from the prefix "peri-" which means "around" or "surrounding". This reflects the library's purpose of providing a protective layer around your data structures, ensuring they conform to specified schemas. Just as a perimeter protects and defines boundaries, Peri ensures that your data is validated and well-defined according to your schemas.

Contributing

We welcome contributions to Peri! If you have suggestions for improvements or find bugs, please open an issue or submit a pull request on GitHub.

License

Peri is released under the MIT License. See the LICENSE file for more details.


Peri makes it easy to define and validate data structures in Elixir, providing a powerful tool for ensuring data integrity in your applications. Start using Peri today and enjoy simple, reliable schema validation!