ExJsonschema

View Source

Fast, safe JSON Schema validation for Elixir

ExJsonschema validates JSON against schemas using a battle-tested Rust engine. It's designed to be simple to use while providing the performance and reliability you need for production applications.

Why ExJsonschema?

  • Zero Setup - Add to your deps, it just works (no Rust toolchain needed)
  • Blazing Fast - Rust-powered validation that scales with your traffic
  • Battle-Tested - Built on the proven jsonschema Rust crate
  • Great Errors - Clear, actionable validation error messages
  • Spec Compliant - Supports JSON Schema draft-07, 2019-09, and 2020-12

Installation

def deps do
  [{:ex_jsonschema, "~> 0.1.1"}]
end

That's it! No Rust toolchain needed.

Quick Start

# Define your schema
schema = ~s({
  "type": "object", 
  "properties": {
    "name": {"type": "string"},
    "age": {"type": "number", "minimum": 0}
  },
  "required": ["name"]
})

# Compile once, validate many times
{:ok, validator} = ExJsonschema.compile(schema)

# Validate your data
:ok = ExJsonschema.validate(validator, ~s({"name": "Alice", "age": 30}))

# Get helpful errors
{:error, errors} = ExJsonschema.validate(validator, ~s({"age": -5}))
# => [%{message: "\"name\" is a required property", instance_path: ""}]

That's it! Compile your schema once, then validate as much data as you need.

More Features

ExJsonschema includes everything you need for production use:

  • Performance Profiles - :strict, :lenient, and :performance presets
  • Flexible Output - Choose between :basic, :detailed, and :verbose error formats
  • Schema Caching - Automatic caching for schemas with $id fields
  • Stream Processing - Works great with Elixir's Stream module
  • Format Validation - Email, URI, date-time, and more built-in formats
# Performance tuned for your use case
opts = ExJsonschema.Options.new(:performance)
ExJsonschema.validate(validator, data, opts)

# Just need a boolean?
ExJsonschema.valid?(validator, data)

Documentation

For detailed usage, check out the guides:

Full API Documentation

Benchmarking

Test performance on your system:

mix benchmark

License

MIT - see LICENSE file for details.