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.

https://hexdocs.pm/exonerate/Exonerate.html

16 Likes

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.

9 Likes

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.

2 Likes

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/lib-bench.md at master · E-xyza/Exonerate · GitHub

5 Likes

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?

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

1 Like

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
3 Likes

Thanks. That could work for my case.

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
2 Likes

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

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.

1 Like

Very nice job, thank u!

The code exudes beauty and elegance, truly a masterpiece of art!

1 Like

ex_json_schema has a function resolve/1 which also does validation of the schema against it’s meta schema.

@ityonemo is there a built-in way to validate the schema against in meta schema in exonerate (I tried to find it but without success)? I’m basically fishing for ideas how to go about doing meta validations in the most sensible way. I’m thinking of having a mix task that will load the json schema, validate it and return errors if there are any. That way it will be a tiny bit easier to write the json schema before it is committed to a repo.

Any tips most welcome!

I don’t do this currently since I support multiple jsonschema versions at the same time, and some versions validate differently than others. I would recommend running through a validator first. But I could consider building it out.