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!

Marcus

Marcus

The Xema library was updated to 0.8.0 and works now with inlined references to speed up validation. That also affects JsonXema see benchmark.

Xema also supports now custom validators. There is an example on the examples page in the online docs.

Marcus

Marcus

The new release of Xema adds the macros field and required to create struct schemas.

iex> defmodule StructA do
...>   use Xema
...>
...>   xema do
...>     field :foo, :integer, minimum: 0
...>   end
...> end
...>
...> defmodule StructB do
...>   use Xema
...>
...>   xema do
...>     field :a, :string, min_length: 3
...>     field :b, StructA
...>     required [:a]
...>   end
...> end
...>
...> data = StructB.cast!(a: "abc", b: %{foo: 5})
...> data.a
"abc"
iex> Map.from_struct(data.b)
%{foo: 5}

A more extensive example can be found at Examples > Struct

Where Next?

Popular in Libraries Top

kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
574 16576 179
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
praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
ericlathrop
I built a silly site for Halloween that uses Phoenix Channels on the backend, and React on the frontend. I had many problems integrating ...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement