sunitakawane / json-schema

Native Clojure implementation of jsonschema, including a validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON Schema for Clojure(Script)

Almost all Clojure implementations of json schema validators wrap Java libraries. This is generally a good idea.

However, there are some reasons why a native Clojure implementation can be useful:

  • Java libraries compile jsonschema to object graphs, making them inaccessible to many of the data functions in the Clojure core library.

  • On the front-end, it can be painful to have to convert Clojure data to JavaScript objects simply for the purposes of calling a jsonschema validation such as Ajv.

  • Extensibility: JSON Schema is designed to be extended with additional vocabularies. Clojure has some nice open-for-extension mechanisms.

  • Size: Implementing JSON Schema is not that scary in a language as nice as Clojure. There’s not so much code to read, understand and possibly extend.

Scope

This library implements JSON Schema 'draft7' (draft-handrews-json-schema-validation-01).

Status

Caution
This is a new project, of alpha status. There may be future incompatible changes ahead.

Most core features are working but there is more work yet to do:

  • Annotation gathering

  • Improved Error messages

  • The schema-path and data-path values on failures

  • Relative json-pointer

This library is tested with the official JSON-Schema-Test-Suite.

Currently, 631 of the 633 official optional and non-optional tests pass. Failing tests include support for uri-template and idn-email.

Schemas

A schema is a Clojure map (or boolean) that should be augmented with metadata by calling juxt.jsonschema.schema/schema on the schema data:

(juxt.jsonschema.schema/schema {"type" "object"})

Resolvers

Validation can take an optional options map.

The :resolvers entry should be a collection of resolvers.

  • :juxt.jsonschema.resolve/built-in is the built-in resolver which will resolve schemas contained in the library, such as the draft7 meta-schema.

  • :juxt.jsonschema.resolve/default-resolver is a resolver which takes an argument of a map of URIs (or regexes) to values.

    A value can be a schema (which should be pre-processed with schema metadata by calling juxt.jsonschema.schema/schema).

    A value may also be a function (called with the URI or, in the case of a regex, the result of the regex match):

    {#"http://example.com/schemas/(.*)" (fn [match] {:type "object"
                                                     :path (second match)})}

Alternative implementations

About

Native Clojure implementation of jsonschema, including a validator

License:MIT License


Languages

Language:Clojure 98.8%Language:Makefile 1.2%