Skip to main content

Check one JSON schema is a subset of another

Project description

isjsonschemasubset

Check one JSONSchema is a subset of another. Plays nicely with Pydantic.

Install

pip install isjsonschemasubset

Currently in an Alpha.

Usage

from pathlib import Path
from isjsonschemasubset import dump, issubset, load

path_to_a = Path(__file__).parent / "a.json"
path_to_b = Path(__file__).parent / "b.json"
dump(A, path_to_a)  # where A is a `type[pydantic.BaseModel]`
dump(B, path_to_b)  # where B is a `type[pydantic.BaseModel]`
json_schema_a = load(path_to_a)
json_schema_b = load(path_to_b)

for error in issubset(json_schema_a, json_schema_b):
    ...

Yields errors if type A is not a subset of B.

In this context, "is a subset of" means that if we were to do:

B(**a.model_dump(mode="json"))

We would not get an error. This is very important when deserializing data from JSON columns in the database and for writing backwards compatible APIs.

See tests/test_schema_versions.py for further example usage. In this case, every time we change Foo (or anything that Foo refers to recursively), we dump a new schema in tests/schemas/Foo and check all of the schemas are backwards compatible.

Dev install

git clone git@github.com:leontrolski/isjsonschemasubset.git
pip install -r requirements-dev.txt
pytest
mypy src tests --strict

Upload to pypi

# bump version
python -m pip install build twine
python -m build
twine check dist/*
twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isjsonschemasubset-0.0.2.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

isjsonschemasubset-0.0.2-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page