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
I’d be interested in seeing a side-by-side comparison between Xema and Ecto’s validation.
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
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!
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








