Hello forum!
I am excited to share Peri, a schema validation library for Elixir, designed to simplify and enhance your data validation processes. Inspired by Clojure’s Plumatic Schema, Peri brings a robust and flexible solution to the Elixir ecosystem.
What is Peri?
Peri is a library that focuses on validating raw maps and supports nested schemas, optional fields, custom validations, and a variety of data types. It provides detailed error reporting to help you debug and handle invalid data effectively.
Key Features:
- Simple and Nested Schema Validation: Easily validate both flat and deeply nested schemas. Define your data structures in a clear and concise manner.
- Optional and Required Fields: Specify which fields are optional and which are required. Ensure your data meets the expected criteria.
- Custom Validations: Implement custom validation functions for complex rules specific to your application.
- Support for Various Data Types: Validate strings, integers, floats, booleans, atoms, tuples, lists, and more.
- Detailed Error Reporting: Receive clear and informative error messages to quickly identify and resolve issues in your data.
Example Usage:
Here’s a quick example to showcase how easy it is to define and validate a schema using Peri:
defmodule MySchemas do
import Peri
defschema :user, %{
name: :string,
age: :integer,
email: {:required, :string},
address: %{
street: :string,
city: :string
},
tags: {:list, :string},
role: {:enum, [:admin, :user, :guest]},
geolocation: {:tuple, [:float, :float]},
rating: {:custom, &validate_rating/1}
}
defp validate_rating(n) when n < 10, do: :ok
defp validate_rating(_), do: {:error, "invalid rating"}
end
user_data = %{name: "John", age: 30, email: "john@example.com", address: %{street: "123 Main St", city: "Somewhere"}, tags: ["science", "funky"], role: :admin, geolocation: {12.2, 34.2}, rating: 9}
case MySchemas.user(user_data) do
{:ok, valid_data} -> IO.puts("Data is valid!")
{:error, errors} -> IO.inspect(errors, label: "Validation errors")
end
In this example, we define a user schema with various fields, including nested structures, required fields, and custom validations. Peri makes it straightforward to ensure that your data conforms to these definitions.
Getting Started:
To start using Peri, add it to your mix.exs
dependencies:
def deps do
[
{:peri, "~> 0.2"}
]
end
Then run mix deps.get
to fetch and compile the dependency.
Documentation and Resources:
- GitHub Repository: Peri on GitHub
- Official Documentation: HexDocs for Peri
Contributing:
I welcome feedback, suggestions, and contributions from the community. If you find any issues or have ideas for improvements, please check out the contributing guidelines on GitHub.
Why Use Peri?
Peri is designed to integrate seamlessly into your Elixir projects, providing a powerful and flexible tool for schema validation. Whether you are dealing with simple data structures or complex nested schemas, Peri offers a clean and efficient solution.
I hope you find Peri useful for your Elixir applications. Feel free to share your experiences, ask questions, and contribute to the project.
Happy coding!