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.
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.
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.
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
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
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.
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.
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.