Marcus

Marcus

Xema and JsonXema - Schema validatiors

JsonXema is a JSON Schema validator with support for draft 04, 06, and 07. JsonXema based on elixir schema validator Xema.

https://github.com/hrzndhrn/json_xema

Xema is a schema validator inspired by JSON Schema. In Xema schemas can be created in Elixir and any data structure can be validated.

https://github.com/hrzndhrn/xema

Feedback, critic, improvements and PRs are very welcome, thanks!

Most Liked

dimitarvp

dimitarvp

I’d be interested in seeing a side-by-side comparison between Xema and Ecto’s validation.

Marcus

Marcus

Xema 0.9.0 is released. The new version comes with some breaking changes, see changelog.

New features

  • convert data with Xema.cast
  • nice error messages

Example:

iex> defmodule Person do
...>   use Xema
...>
...>   xema do
...>     map(
...>       keys: :atoms,
...>       properties: %{
...>         name: string(),
...>         age: integer(minimum: 0),
...>         fav: atom(enum: [:erlan, :elixir, :js, :rust, :go])
...>       }
...>     )
...>   end
...>
...>   def new(args), do: cast!(args)
...> end
iex>
iex> person = Person.new(name: "Joe", age: 24, fav: :elixir) 
%{
  age: 24,
  fav: :elixir,
  name: "Joe"
}
iex> json = person |> Jason.encode!() |> Jason.decode()
%{
  "age" => 24,
  "fav" => "elixir",
  "name" => "Joe"
}
iex> Person.cast!(json)
%{
  age: 24,
  fav: :elixir,
  name: "Joe"
}
iex> {:error, error} = Person.validate(%{name: 42, age: -1, fav: :php})
iex> Exception.message(error) 
"""
Value -1 is less than minimum value of 0, at [:age].
Value :php is not defined in enum, at [:fav].
Expected :string, got 42, at [:name].\
"""

More examples can be found at section Cast and Examples - Cast JSON.

repromancer

repromancer

Thanks for working on this! I wanted to try JSON Schema with my new personal project, and was disappointed to see the top Google result is a few specifications out-of-date. Glad I searched here on the forum!

Where Next?

Popular in Announcing Top

aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
sabiwara
Dune is a sandbox for Elixir and aims to safely evaluate user-provided code. You can try it out using this basic Elixir playground made ...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10187 141
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement