Metadata-Version: 2.4
Name: andvari
Version: 0.1.0
Summary: Deterministic diff of two versions of a JSON Schema, and Pydantic models from one
Author: unpunnyfuns
Author-email: unpunnyfuns <unpunnyfuns@gmail.com>
License-Expression: MIT
License-File: LICENSE
Requires-Dist: datamodel-code-generator==0.106.0
Requires-Python: >=3.11
Project-URL: Repository, https://github.com/unpunnyfuns/andvari
Description-Content-Type: text/markdown

# andvari

Deterministic diff of two versions of a JSON Schema, and Pydantic models from one.

`andvari diff` lists what changed between two versions of a schema, with a severity for each change. `andvari models` writes Pydantic v2 models for a schema.

> Andvari was a dwarf who lived beneath a waterfall, often in the shape of a pike, and kept a hoard of gold. Loki caught him in a net and took all of it, down to the last ring. Andvari could not stop him, so he laid a curse on the ring: it would be the death of whoever owned it.
>
> After *Reginsmál* and the *Prose Edda*.

## Purpose

Use it for a schema that someone else publishes and may change without notice. Keep a copy of the schema in your repository. In CI, fetch the current schema and run andvari against the copy: any difference fails the build and prints what changed. To accept a change, replace the copy, regenerate the models, fix what no longer fits and commit.

Both commands are deterministic. They use no network and no clock, so the output depends only on the input files.

## Usage

Run `andvari diff <old.json> <new.json>`:

```text
$ cat old.json
{
  "type": "object",
  "required": ["id"],
  "properties": {
    "id": {"type": "integer"},
    "status": {"enum": ["open", "closed"]},
    "note": {"type": "string", "maxLength": 200}
  }
}
$ cat new.json
{
  "type": "object",
  "required": ["id", "owner"],
  "properties": {
    "id": {"type": "number", "description": "Record id"},
    "status": {"enum": ["open", "closed", "archived"]},
    "note": {"type": "string", "maxLength": 100},
    "owner": {"type": "string"}
  }
}
$ andvari diff old.json new.json
old  26b8ae66c96141b28fa168da316f2953dee395b30c3ff7539884218f2dcb8591
new  87d61a59f8abf565a1c13296cb1bb8e50d8793f9a65a6cd79182aa8ad5d813f0
breaking  /note  maxLength  maxLength changed
breaking  /owner  -  required property added
warning  /status  enum  enum loosened
compatible  /id  type  type loosened
annotation  /id  description  description added
5 changes: 2 breaking, 1 warning, 1 compatible, 1 annotation
```

The fingerprints are the SHA-256 of each schema's canonical JSON (sorted keys, no whitespace), so key order and formatting do not count as changes.

| Exit code | Meaning                                                                    |
| --------- | -------------------------------------------------------------------------- |
| 0         | `diff`: same fingerprint, prints `unchanged`. `models`: file written       |
| 1         | The schemas differ                                                         |
| 2         | Unreadable file, invalid JSON, schema not an object, or a `$ref` that cannot be inlined (`diff`); the generator failed (`models`); wrong arguments |

Run `andvari models <schema.json> <out.py> [--class-name NAME]` to write Pydantic v2 models. The same schema and class name always give the same file; remote `$ref`s are not fetched.

```text
$ cat schema.json
{
  "type": "object",
  "required": ["id"],
  "properties": {
    "id": {"type": "integer"},
    "status": {"enum": ["open", "closed"]}
  }
}
$ andvari models schema.json models.py --class-name Record
$ cat models.py
# generated by andvari from a JSON Schema; do not edit

from __future__ import annotations

from enum import Enum

from pydantic import BaseModel


class Status(Enum):
    open = 'open'
    closed = 'closed'


class Record(BaseModel):
    id: int
    status: Status | None = None
```

## Severity

| Change                                                    | Severity                                                       |
| --------------------------------------------------------- | -------------------------------------------------------------- |
| Path removed                                              | breaking                                                       |
| Path added                                                | breaking if required or array items, otherwise warning         |
| `title`, `description`, `examples`, `$comment`, `default` | annotation                                                     |
| `required`                                                | breaking when it becomes true, compatible when false           |
| `type`                                                    | compatible when widened (`number` covers `integer`) or removed, otherwise breaking |
| `enum`                                                    | breaking when added or a value is gone, warning when values are only added, compatible when removed |
| `minimum`, `minLength`, `minItems`, ... and the `max` set | breaking when added or tightened, compatible when loosened or removed; a non-numeric value is breaking |
| Any other keyword                                         | breaking when added, removed or changed                        |

## What is compared

Local `$ref`s (`#/...`) are inlined first; a remote or unresolvable `$ref` is an error. A `$ref` with sibling keywords becomes `allOf: [target, siblings]` when the two share a keyword, otherwise the siblings are merged into the target. `properties` and `items` are followed and each node gets a path (`/`, `/owner`, `/tags/*`). Every other keyword on a node is compared as a whole value.

## Limits

- It compares what the schemas say. It does not decide which data is valid.
- A change inside a combinator or conditional (`oneOf`, `anyOf`, `allOf`, `if`, ...) is reported as "changed", whatever its effect.
- Generated models can be looser than the schema, because the generator cannot express every constraint.
- A change to a definition nothing references changes the fingerprint but lists no change; the command still exits 1.

## Python

```python
from andvari import diff, fingerprint

old = {"type": "object", "properties": {"id": {"type": "integer"}}}
new = {"type": "object", "properties": {"id": {"type": "string"}}}

print(fingerprint(old) == fingerprint(new))
for change in diff(old, new):
    print(change.severity, change.path, change.keyword, change.reason)
```

## Setup and tests

```sh
uv sync
uv run pytest
```
