Skip to content

lud/jsv

Repository files navigation

JSV

hex.pm Version Build Status License

JSV is a JSON Schema Validation library for Elixir with full support for the latest JSON Schema specification.

Documentation

API documentation is available on hexdocs.pm.

Supported Dialects

JSV's goal is to support 100% of the latest specification, which is currently 2020-12. Additional support for Draft 7 is also included.

  • Draft 2020-12
  • Draft 7

Installation

def deps do
  [
    {:jsv, "~> 0.10"},
  ]
end

Optional dependencies can be added to support more features:

def deps do
  [
    # Automatically enables enhanced format validation like URI, IRI, email
    # address, JSON-pointer, etc.
    {:abnf_parsec, "~> 1.0"},

    # JSV Supports Decimal and will validate Decimal structs as numbers.
    {:decimal, "~> 2.0"},

    # Decoding schemas resolved via HTTP requires JSON support for Elixir
    # versions prior to 1.18
    {:jason, "~> 1.0"},
    # OR
    {:poison, "~> 6.0 or ~> 5.0"},
  ]
end

Basic Usage

Here is an example of how to use the library:

schema = %{
  type: :object,
  properties: %{
    name: %{type: :string}
  },
  required: [:name]
}

root = JSV.build!(schema)

case JSV.validate(%{"name" => "Alice"}, root) do
  {:ok, data} ->
    {:ok, data}

  # Errors can be converted into JSON-compatible structures for API responses
  # or logging.
  {:error, validation_error} ->
    {:error, JSON.encode!(JSV.normalize_error(validation_error))}
end

JSV offers many additional features! Check the documentation for more details.

Contributing

Pull requests are welcome, provided they include appropriate tests and documentation.

About

Json Schema Validator for Elixir with full spec support

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages