Skip to content

leontrolski/isjsonschemasubset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

isjsonschemasubset

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

Install

pip install isjsonschemasubset

Currently in 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 [email protected]: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/*

About

Check one JSON schema is a subset of another

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages