Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

Schemafence banner

Schemafence

Schemafence checks whether a candidate JSON Schema introduces restrictions that can reject instances accepted by a previous schema. It is designed for CI gates around event payloads, configuration formats, persisted documents, and API contracts where a schema change should not silently break existing producers or stored data.

The tool is dependency-free at runtime and deliberately conservative: rules that can be evaluated locally produce explicit breaking findings, while schema constructs whose language inclusion cannot be safely decided by this implementation are surfaced as warnings instead of being ignored.

Architecture

Schemafence architecture

The CLI loads two local schemas, the resolver dereferences only local JSON Pointers, and the comparator recursively dispatches to scalar and structural rule sets. Every result becomes an immutable finding with a stable rule code, JSON Pointer path, severity, and before/after evidence. Unsupported assertions become warnings instead of silently passing compatibility.

Compatibility decision flow

%%{init: {"theme":"base","themeVariables":{"background":"transparent","lineColor":"#64748b","fontFamily":"Inter, ui-sans-serif, system-ui"}}}%%
flowchart LR
  O["old schema"] --> L["load + validate"]
  N["candidate schema"] --> L
  L --> R["resolve local $ref"]
  R --> C["recursive compare"]
  C --> S["scalar rules"]
  C --> T["structural rules"]
  C --> U["unsupported assertion scan"]
  S --> F["findings"]
  T --> F
  U --> F
  F --> B{"breaking?"}
  B -->|yes| X["exit 1"]
  B -->|no| W{"warnings + fail-on-warnings?"}
  W -->|yes| X
  W -->|no| Z["exit 0"]
  classDef input fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px;
  classDef engine fill:#ede9fe,stroke:#7c3aed,color:#5b21b6,stroke-width:2px;
  classDef scalar fill:#fef3c7,stroke:#d97706,color:#92400e,stroke-width:2px;
  classDef structural fill:#dcfce7,stroke:#16a34a,color:#166534,stroke-width:2px;
  classDef warning fill:#fce7f3,stroke:#db2777,color:#9d174d,stroke-width:2px;
  classDef output fill:#f3e8ff,stroke:#a855f7,color:#7e22ce,stroke-width:2px;
  class O,N,L input;
  class R,C engine;
  class S scalar;
  class T structural;
  class U warning;
  class F,B,W,X,Z output;
Loading

Install

From the repository:

python -m pip install .

Or run from a checkout without installing:

PYTHONPATH=src python -m schemafence old.schema.json new.schema.json

Usage

schemafence old.schema.json new.schema.json

Example:

BREAKING REQUIRED_PROPERTIES_ADDED #/required - new schema requires additional properties: request_id
  before: []
  after:  ["request_id"]
BREAKING MIN_LENGTH_INCREASED #/properties/name/minLength - new schema increases minLength
  before: 1
  after:  3
summary: breaking=2 warnings=0

Exit codes are intentionally CI-friendly:

  • 0 — no detected breaking changes; warnings are allowed by default.
  • 1 — one or more breaking findings, or warnings when --fail-on-warnings is set.
  • 2 — invalid input, unsupported $ref form, malformed schema keyword, or file/JSON error.

Machine-readable output:

schemafence old.json new.json --format json

Treat analysis warnings as failures when your compatibility policy requires complete coverage:

schemafence old.json new.json --fail-on-warnings

What is checked

Schemafence evaluates restrictions that shrink the set of accepted JSON instances:

Area Breaking changes detected
Types removing an allowed JSON type or adding a type restriction to an unconstrained location
Constants/enums adding/changing const, adding enum, removing enum values
Numbers tighter minimum/maximum or exclusive bounds; stricter multipleOf
Strings increased minLength, decreased/added maxLength, added/changed pattern
Arrays increased minItems, decreased/added maxItems, enabling uniqueItems, stricter items
Objects newly required properties, tighter property schemas, forbidden former properties, tighter additionalProperties, property-count bounds, added dependentRequired entries
Boolean schemas true/unconstrained becoming false
References local JSON Pointer $ref resolution with missing-target and cycle detection

Object-property handling is intentionally semantic rather than cosmetic. Adding an optional property schema can still be backward-incompatible when the old schema allowed arbitrary values for that property name through additionalProperties; Schemafence compares the old additional-property policy against the new named-property schema.

Local $ref

Local references are supported:

{
  "$defs": {
    "id": {"type": ["string", "integer"]}
  },
  "type": "object",
  "properties": {
    "id": {"$ref": "#/$defs/id"}
  }
}

References are resolved against their own document. Missing pointers, external references, cycles encountered on the comparison path, and $ref objects with sibling keywords are rejected with exit code 2 rather than guessed at.

Analysis boundaries

JSON Schema is expressive enough that exact language inclusion is not generally a small local comparison problem. Schemafence therefore warns when it encounters assertions it does not evaluate, including:

  • allOf, anyOf, oneOf, not
  • if / then / else
  • unevaluatedProperties, unevaluatedItems
  • prefixItems, contains
  • dependentSchemas, patternProperties, propertyNames

format is treated as an annotation and reported as a warning because whether it is assertional depends on the selected vocabulary and validator configuration.

A changed regular expression is treated conservatively as breaking because Schemafence does not attempt regular-language inclusion. This can produce a false positive when the new expression is provably broader. The finding stays explicit so a reviewer can decide whether to waive it.

Schemafence does not fetch network references, infer a JSON Schema draft from remote metaschemas, or execute custom vocabularies. Those choices keep CI deterministic and prevent schema comparison from turning into network execution.

CI example

- name: Check schema compatibility
  run: schemafence schemas/event-v1.json schemas/event-next.json --fail-on-warnings

The repository's own workflow runs the unit suite on Python 3.10 and 3.13, byte-compiles the package, installs it without runtime dependencies, and checks the installed CLI.

Development

PYTHONPATH=src python -m unittest discover -s tests -v
python -m compileall -q src tests
python -m pip wheel . --no-deps --no-build-isolation -w dist

The tests cover type, enum/const, numeric, string, array and object restrictions; additionalProperties; optional-property semantics; dependentRequired; boolean schemas; local references and cycles; warning behavior; CLI JSON output; and exit codes.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages