Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tlux/vx
Elixir schema parser
https://github.com/tlux/vx
elixir elixir-library hex-package schema-validation
Last synced: 5 days ago
JSON representation
Elixir schema parser
- Host: GitHub
- URL: https://github.com/tlux/vx
- Owner: tlux
- License: mit
- Created: 2023-10-18T17:41:10.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-19T21:42:49.000Z (4 months ago)
- Last Synced: 2024-09-19T09:48:45.958Z (about 2 months ago)
- Topics: elixir, elixir-library, hex-package, schema-validation
- Language: Elixir
- Homepage:
- Size: 303 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Vx
[![Build](https://github.com/tlux/vx/actions/workflows/elixir.yml/badge.svg)](https://github.com/tlux/vx/actions/workflows/elixir.yml)
[![Coverage Status](https://coveralls.io/repos/github/tlux/vx/badge.svg?branch=main)](https://coveralls.io/github/tlux/vx?branch=main)
[![Module Version](https://img.shields.io/hexpm/v/vx.svg)](https://hex.pm/packages/vx)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/vx/)
[![License](https://img.shields.io/hexpm/l/vx.svg)](https://github.com/tlux/vx/blob/main/LICENSE.md)
[![Last Updated](https://img.shields.io/github/last-commit/tlux/vx.svg)](https://github.com/tlux/vx/commits/main)The Elixir schema validator.
## Installation
The package can be installed by adding `vx` to your list of dependencies in
`mix.exs`:```elixir
def deps do
[
{:vx, "~> 0.4.0"}
]
end
```## Usage
With Vx, you have the capability to define schemata for validating complex data
effortlessly.First, you need to define your schema.
```elixir
schema = Vx.String.t()
```After that, you can call `Vx.validate/2` or `Vx.validate!/2` to check if a given
values matches:```elixir
Vx.validate(schema, "foo")
# :ok
```When the value does not match, an error is returned (or raised, respectively),
indicating the specific issue.```elixir
Vx.validate(schema, 123)
# {:error, %Vx.Error{...}}
``````elixir
Vx.validate!(schema, 123)
# ** (Vx.Error) must be a string
```Additional constraints can be added to certain types by piping everything
together:```elixir
Vx.Number.t()
|> Vx.Number.gteq(3)
|> Vx.Number.lt(7)
```You can combine multiple types and constraints to validate more complex
schemata:```elixir
Vx.Map.shape(%{
"name" => Vx.String.t(),
"age" => Vx.Number.t(),
"hobbies" =>
Vx.List.t(Vx.String.present())
|> Vx.List.non_empty(),
"type" => Vx.Enum.t(["user", "admin"]),
"addresses" => Vx.List.t(Vx.Struct.t(Address))
})
```## Docs
Take a look at the [documentation](https://hexdocs.pm/vx) to find out available
types and options.