Type Safety with JSON Subschema

Andrew Habib, Avraham Shinnar, Martin Hirzel, Michael PradelarXiv 2019

Introduction

JSON has become the ubiquitous standard for data exchange, with JSON Schema providing the necessary structure for validation. However, while tools for runtime validation are plentiful, there is a significant gap in tools capable of reasoning about the relationships between schemas themselves. This paper addresses the JSON Subschema decision problem: given two schemas $s$ and $t$, is $s$ a subschema of $t$ ($s <: t$)? Determining this relationship is critical for ensuring backward compatibility in API evolution, verifying data pipeline integrity, and managing the inherent complexity of JSON Schema's rich feature set, which includes logical connectives, regular expressions, and numeric constraints.

JSON Schema keywords and meta-schemas illustrating the complexity of the subtyping problem.

Methodology

The authors propose jsonsubschema, a tool that employs a three-stage algorithmic pipeline to determine subtyping relationships statically.

  1. Canonicalization: To handle the flexibility of JSON Schema syntax, schemas are first transformed into a standardized "canonical" form. This process makes implicit defaults explicit and decomposes multi-type schemas into logical disjunctions.
  2. Simplification: Rewrite rules are applied to eliminate or push down complex operators like enum, not, and allOf. This stage aims to reduce intersections of schemas into simpler, equivalent forms.
  3. Subschema Checking: The tool utilizes type-directed inference rules. It partitions schemas into type-homogeneous fragments (e.g., string-only or number-only constraints) and dispatches them to specialized checkers. For instance, string constraints are evaluated using regular expression inclusion, while numeric constraints are analyzed through range reasoning and greatest common divisor calculations for multipleOf properties.
The three-stage algorithmic pipeline: canonicalization, simplification, and fragment extraction.

Evaluation and Results

The tool was evaluated against four diverse datasets: the official JSON Schema Test Suite, API evolution pairs from The Washington Post (WP), Kubernetes (K8s) OpenAPI specifications, and machine learning pipeline schemas from IBM's Lale library.

jsonsubschema successfully decided the subschema relationship for 94.31% of the 8,548 real-world schema pairs tested. The tool demonstrated perfect soundness; every decision provided was verified as correct against ground truth. In terms of performance, the check typically completes within seconds, scaling linearly with schema size. Notably, jsonsubschema significantly outperformed existing community tools like issubset, which exhibited a 40% error rate in comparable tests.

Discussion and Limitations

A key insight from this work is that while JSON subtyping is theoretically complex and potentially undecidable, real-world schemas rarely employ the "pathological" features (such as deeply nested negations) that lead to these difficulties. By focusing on canonicalization and type homogeneity, the authors created a practical tool for static analysis.

However, limitations remain. The current implementation does not support recursive schemas (using $ref for self-reference) or the negation of complex object/array types. Additionally, the use of non-regular features in regular expressions can render the subtyping check undecidable. Future work aims to implement on-demand canonicalization for massive schemas and extend support to recursive definitions.

Conclusion

jsonsubschema provides a robust framework for formal subtyping in JSON Schema. By moving validation from runtime to compile-time/static analysis, it enables engineers to catch breaking changes and data mismatches early in the development lifecycle, bringing a new level of type safety to the JSON ecosystem.

Summary updated

BibTeX
@misc{habib2019type,
  author = {Andrew Habib and Avraham Shinnar and Martin Hirzel and Michael Pradel},
  title = {{Type Safety with JSON Subschema}},
  year = {2019},
  eprint = {1911.12651},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1911.12651},
}
Figure