Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

0026 — jlens-schema reads JSON Schema itself, into shapes, and infers its own

Status: Accepted. Recorded in 0.34, after the fact: the reader and the inference date from the first jlens-schema (October 2026), and the references grew in 0.33 (ADR 0024, ADR 0025).

Context

jlens-schema does two things with JSON Schema. It infers a schema from sample documents (Schema::infer, jlens schema infer), and it reads one, inferred, written by hand or generated by another tool, for the language service: completion, hover, the diagnostics, the type of the result and what an expression uses. Rust has mature JSON Schema crates, and ADR 0001 only covers implementing JSONata itself. So the question is whether jlens reinvents them.

What the language service needs from a schema is not what a validator gives:

  • A query without a document. The typing asks, for a path that an expression builds, which fields and which types a value can have there. No document is at hand when someone types customer. in an editor. The load therefore turns the schema into Schema, a tree of Shapes (types, fields with required or their presence, items, enum, format, ranges, descriptions, the fields an object leaves open, references by name to definitions), which Schema::shape_at and the typing walk.
  • A reading that keeps the warnings sound. A warning says that the schema does not allow something, so a keyword that allows more must be read, and one that only narrows may be left out. The schema guide classifies every keyword of JSON Schema 2020-12, the drafts before it and OpenAPI 3.0 that way (the keywords jlens reads): read, left out because it only narrows or only describes, or reported. A reported keyword ($dynamicRef, $recursiveRef, a type that names no JSON type) leaves its place unknown, and the load lists it beside the schema.
  • Combinators read as sets of values. allOf is merged into one shape. Roadmap 0.34 #11 reads anyOf and oneOf as one branch or another: a field is required only where every branch requires it, and a branch that gives no type leaves the type unknown. A validator needs neither: it checks each branch against the document.
  • A load that does not fail. A $ref that cannot be followed leaves its place unknown and is reported with its place (ADR 0025), and the load is bounded by the size of its documents, whoever wrote them.

Inference has needs of its own. It records more than the schema a GenSON-style tool writes (types, properties, required, items): the fraction of objects that had a field that is not required (x-jlens-presence), string formats, number ranges, small enumerations (x-jlens-enum-inferred), minItems: 1 for arrays never seen empty (x-jlens-min-items-inferred) and examples. It observes samples into the same Shape the load builds, so an inferred schema, written and read again, answers the same.

The crates checked

On crates.io, on 2026-11-12. “Last release” is the date of the version given.

CrateVersion, last releaseWhat it isWhy it does not serve
jsonschema0.61.2, 2026-11-12The most used validator. Compiles a schema into a Validator; validates documents, with errors and the annotations of the Output v1 format; fetches remote references, bundles and dereferences schemas, validates against meta-schemasIts questions are about a document. A compiled Validator exposes checks, not the fields and types of a place
referencing0.61.2, 2026-11-12The resolver of jsonschema, from the same repository: a Registry of documents by URI, a Resolver that looks up a reference, per-draft rules, the dynamic scope of $dynamicRef, a Retrieve trait for documents not givenIt could have served the resolution: see below
boon0.6.1, 2025-01-07A validator, drafts 4 to 2020-12; passes the JSON-Schema-Test-Suite but its optional cases; errors as a hierarchyAs jsonschema
valico4.0.0, 2023-05-13A validator and coercer, draft 7; no repository named on crates.ioAs jsonschema, and no release since 2023
jsonschema-valid0.5.2, 2023-11-08A validator, drafts 4, 6 and 7As jsonschema, and no 2019-09 or 2020-12
json_schema_ast0.6.0, 2026-11-12Builds, validates and resolves 2020-12 schemas and OpenAPI 3.1 Schema Objects, with a resolved graph that can be walked; the front end of jsoncompatThe closest to a static reader, but its README rejects the other drafts named by $schema, does not read OpenAPI 3.0’s nullable, and rejects plain-name fragments ($anchor), dynamic references, and remote ones not registered beforehand. It has no notion of a keyword that only narrows
jsonschema-schema0.2.0, 2026-03-06Typed structs for the keywords of 2020-12, with navigate_pointer, which follows $refA typed parse of the keywords, not a reading of them: what each means for a place would remain jlens’s
unbinder0.2.0, 2026-10-14Replaces each $ref by its target, bounded by depth, size and document limits; Documents lists the documents still missing and opens none; a cycle is left as $ref and listedIts output is a schema again, which jlens would still read, and a cycle left as $ref would still be jlens’s to resolve. Its $ids follow json-schema-ref-parser, counting only where the root has one or names a draft, and its README does not mention $anchor
schemars1.2.3, 2026-10-12A schema from Rust types (#[derive(JsonSchema)])The other direction
typify0.8.0, 2026-09-09 (0.10.0-alpha.3, 2026-10-14)Rust types from a schema, as source code at build timeThe other end of the same problem: its README calls JSON Schema “a constraint language designed for validation”, which it reads as one interpretation into Rust types. jlens needs the reading at run time, as data, and keeps what it cannot read unknown
genson-rs0.2.0, 2024-05-26A fast port of GenSON: a schema (draft-04 and later) inferred from documentsThe GenSON schema, without the presence, formats, ranges, enumerations and minItems above. No release since 2024
genson-core1.4.0, 2026-10-26A vendored and extended genson-rs, with normalisation to Avro and Parquet schemasAs genson-rs; its additions are for data pipelines
json-schema-profiler0.0.1, 2026-10-25Counts, at each path of a set of documents, how often a field is present and of which type, then proposes a schema under a strict or an expansive policyThe nearest to jlens’s inference, but experimental, and released after it

The options

  1. Build on a validator (jsonschema, boon). A compiled validator holds checks run against a document, and gives no way to ask what a place may hold. jlens would walk the schema’s JSON itself all the same, and carry a validator it never runs.
  2. Build on a static reading (json_schema_ast, jsonschema-schema, typify’s type space). These read a schema without a document, but each covers fewer drafts or dialects than jlens reads, or ends in Rust source. None tells a keyword that widens from one that narrows, and none leaves a place unknown and reports why. The rules of the guide’s table would be written over them all the same.
  3. Read the schema ourselves, from its JSON values, possibly taking the resolution of $ref from referencing.

Where reuse was possible: referencing

The step from the text of a $ref to its target is the same for a validator and for jlens. referencing does what decision 2 of ADR 0025 describes (base URIs from $id, anchors, JSON Pointers), and more: the rules of each draft, such as draft-04’s id, and the dynamic scope that $dynamicRef and $recursiveRef need, which jlens reports instead of reading. It is maintained and widely used. jlens kept its own resolver, uri.rs (RFC 3986, section 5.2, in about 330 lines with their tests) and the index of $ids and $anchors in the load, because:

  • The part it would replace is small, and the rest stays. What ADR 0024 and ADR 0025 decide is about the walk over the schema: the $refs that lead back to themselves, found as the strongly connected components of the graph of references, become references by name; the budget counts the values each target would copy before any is copied; the report gives the place of each $ref not followed. referencing resolves a reference given to it, so all of that would remain.
  • Its failures are not jlens’s. Its README says that it resolves every external reference eagerly, and RegistryBuilder::prepare fails when a retrieval fails. jlens loads the schema without the document, leaves each place that needs it unknown and lists them, and the CLI asks for the documents and loads again (load_asking). A Retrieve that records what it was asked and returns a placeholder could imitate that, but the placeholder would be read as a schema.
  • Its dependencies. referencing 0.61.2 brings ahash, fluent-uri, getrandom, micromap, parking_lot and percent-encoding, none of which jlens builds today: jlens-schema adds nothing to what the evaluator already depends on. The WebAssembly package, which builds jlens-schema, has a size budget, and the C API ships it in every library of the Java package.

What would change this: reading $dynamicRef and $recursiveRef instead of reporting them, or reading draft-04’s id. Their scope is what referencing already implements, and writing it again would be reinventing it. Then referencing would be weighed again, for the step from the text to the target only, under the walk, the budget and the report.

Decision

jlens-schema reads JSON Schema itself: the load walks the schema’s JSON values, resolves $ref with its own URI resolver, and builds Shapes, following the keyword classification of the schema guide. Inference is its own as well, and observes samples into the same shapes.

jlens does not validate documents against a schema. An application that needs validation uses a validator crate beside jlens, which neither competes with it nor wraps it.

Consequences

  • jlens owns the classification of the keywords, and keeps it current: a new draft or dialect is a change to the guide’s table and to the load, and until then its new keywords are not read.
  • The resolution of $ref, $id and $anchor is tested by jlens’s own tests only. The official JSON-Schema-Test-Suite could test it: the schemas of its reference cases (such as ref.json, refRemote.json and anchor.json), with the documents of its remotes folder given by URI, should load with every $ref followed, and the shape of a place should hold its valid documents. This is a possibility, not a plan.
  • jlens-schema has no dependency on a validator or a resolver: its dependencies are those of the evaluator.
  • When referencing gains what jlens would otherwise write (above), this ADR is to be revisited, not extended in place: a later ADR supersedes it.