Overview

JSON Schema is good at the shape of a document: types, required fields, formats, enumerations. It is awkward at business rules: a rule that compares two fields, checks a value against a policy elsewhere in the document, or picks some of the nodes and requires something of each.

Jsontron adds that layer to JSON Schema in the way Schematron adds it to XML: a rule selects nodes, and assertions are tested on each of them. The expressions are jq, so selecting, defaulting, comparing and building messages need no new language.

  • Structural validation stays with JSON Schema.
  • The rules stay in the schema, next to the data they describe.
  • Every expression is checked when the schema is built, not on the first document.

This section holds for every implementation. How you add the keywords to your JSON Schema library and read the errors is in the section of your language.

The model

Idea In Jsontron
The nodes to check context: a jq filter over the value the schema object applies to. Each output is a context node.
What must hold test: a jq filter run with . set to each context node.
The text of a failure message: plain text with \(...) interpolations, or a jq string expression.
The whole document $root, bound in every expression, wherever the schema object is.
Values used by several tests let: named jq expressions, bound as $name.

A test is truthy when it produces at least one value and none of its values is false or null. An assert fails when its test is not truthy; a report, the inverse, fails when it is.

How a schema object is evaluated

The Jsontron keywords of one schema object form a single rule set. assert and report are shorthand for a rule whose context is ., and are merged into it (Keywords).

flowchart LR
  I["instance<br>(the value the schema<br>object applies to)"] --> L["schema-level let"]
  L --> C["context of each rule"]
  C -- "each context node" --> RL["rule-level let"]
  RL --> T["test of each assert<br>and report"]
  T -- "fails" --> M["message<br>with its labels"]
  M --> E["one validation error"]
  1. The schema-level let bindings are evaluated, with . set to the instance.
  2. For each rule, in order, context is evaluated on the instance. A context that produces nothing makes the rule vacuously satisfied.
  3. For each context node, the rule's own let bindings are evaluated, then each assert and each report is tested.
  4. Each failing item is one validation error, at the location of the instance.

Every rule runs: unlike classic Schematron, a rule never hides another one. A jq error in a let, context or test stops the rule set and is reported as one error instead of its failures.

The rule set is an assertion like minimum or required: a schema object with a failing rule set is invalid, inside allOf, anyOf, oneOf, not, if and $ref as well.

Where to read

Page What it is
Keywords rules, assert, report and let, their forms, and how they combine.
Messages and labels How a failure's text is built: plain text, interpolation, id, flag and role.
jq in Jsontron $root, truthiness, the part of jq you can rely on in every implementation, and the errors.
Specification The normative text, which every implementation is written from.

Implementations