ityonemo

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.

First 10 of 19 Posts Switch mode

ityonemo

ityonemo OP

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

ityonemo OP

updated to version 0.2.1, a minor upgrade which provides the required: "/jsonpointer/to/required/parameter" optional error content if a required filter in your JSONSchema fails.

ityonemo

ityonemo OP

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

egze

egze

Looks very cool. Do I get it correctly that the schema needs to exist at compile time? Any way to make it work with schemas coming from a database, for example?

ityonemo

ityonemo OP

It must exist at compile time. If you need dynamic schemas you’ll probably need JSONschema or ex_json_schema… Unless you have full control (not user input) in which case you can go nuts and make as many modules as you want at runtime

ityonemo

ityonemo OP

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
egze

egze

Thanks. That could work for my case.

ityonemo

ityonemo OP

updated exonerate to version 0.3.2.

A few highlights of changes:

  • faster array items handling
  • allows registering resources for repeated use in functions
  • fixed nested not in unevaluatedItems
  • dump: true now generates code that you can copy-paste into a .ex or .exs file for correct compilation
tt67wq

tt67wq

Does this project have any differences and advantages compared to GitHub - jonasschmidt/ex_json_schema: An Elixir JSON Schema validator · GitHub?

ityonemo

ityonemo OP

It supports all of the JSONschema drafts up to 2020. ExJsonSchema only goes up to draft 7. Also it’s about 3x faster. May or may not be important, but it’s a runtime: false library so your release payload will be much smaller.

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement