ityonemo
Exonerate - JSONschema validator for elixir
This one has been sitting in a dark corner of my github for several years, I started it before I knew how to publish to hex.pm but I found a need for it at my current job, so I rewrote chunks of it over the past couple of weeks and brought it up to date with the most current version of JSONSchema. I’ve used the validator (in its old form, as a github pull) in both of my previous elixir jobs, but now that I can say I’m “proud of it” it have put it on hex.pm for everyone to use.
This library injects JSONSchema validation code into your module. It’s inspired by EEx and NimbleParsec. The library is validated against the official JSONSchema test set.
Most Liked
ityonemo
updated to version 0.2.0, which now supports (and is tested against) most of draft 4, 6, 7, 2019, and 2020, also produces better error messages for anyOf, and oneOf filters, as well as “ref traces” for if your schema validation jumps around several references.
ityonemo
updated exonerate to version 0.3.0, almost complete support for JSONSchema draft 2020-09.
A few highlights of changes:
- completely changed macro architecture
- respects ids as URIs, has correct id resolution
- able to obtain remoteRfs
- optional RFC/standards-correct parsing of all "standardized format"s as set forth in the JSONSchema standard.
Benchmarks (generally ~3-5x faster than ExJsonSchema/JsonXema) are shown here: Exonerate/bench/reports/lib-bench.md at master · E-xyza/Exonerate · GitHub
ityonemo
I should probably give a concrete example. Supposing the schemas stored in the database are yours and not users’ you could do this:
defmodule MyApp.Validators do
def make_from_db do
Db.Schemas.get_all()
|> Enum.map(&to_module/1)
|> Enum.each(&Code.compile_quoted/1)
:ok
end
defp to_module(schema) do
module_name = :"validator_#{schema.name}"
module_schema = Jason.encode!(schema.json) # assuming it's JSONB column
common_options = [...]
quote do
defmodule unquote(module_name) do
require Exonerate
Exonerate.function_from_string(:def, :validate, unquote(module_schema), unquote(common_options))
end
end
end
end
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








