pytest-jsonschema is a plugin for pytest designed to facilitate JSON Schema validations within your test suites. This tool enables you to validate JSON files, strings, and Python objects against predefined JSON Schemas, ensuring your data adheres to expected formats.
The package bundles a set of commonly used schemas — for pyproject.toml, package.json, GitHub Actions workflows, docker-compose.yml and more — so you can assert that the configuration files in your own repository are valid, without any network access at test time.
Install pytest-jsonschema from PyPI:
pip install pytest-jsonschemaOr, with uv:
uv add --dev pytest-jsonschemaThe plugin registers itself with pytest automatically; no configuration is required.
The package introduces three pytest fixtures for validating JSON data. Each takes a schema_name from the bundled schemas and returns True when the data is valid.
Validates a file on disk. The format is inferred from the file extension:
from pathlib import Path
def test_package_json_is_valid(schema_validate_file):
path = Path("package.json")
assert schema_validate_file(path=path, schema_name="package")Because the format is inferred, this also works for the TOML and YAML files the bundled schemas cover:
from pathlib import Path
def test_pyproject_is_valid(schema_validate_file):
assert schema_validate_file(path=Path("pyproject.toml"), schema_name="pyproject")
def test_workflow_is_valid(schema_validate_file):
path = Path(".github/workflows/ci.yml")
assert schema_validate_file(path=path, schema_name="github-workflow")Pass file_type explicitly when the extension does not match the content.
Validates a string. There is no filename to infer from, so file_type is required:
from pathlib import Path
def test_package_json_is_valid(schema_validate_string):
data = Path("package.json").read_text()
assert schema_validate_string(data=data, schema_name="package", file_type="json")Warning
If file_type is omitted or is not one of the supported values, the string is
parsed as an empty document rather than raising, and validation will most
likely return True. Always pass file_type to this fixture.
Validates an already-parsed Python object — a dict or a list:
import json
from pathlib import Path
def test_package_json_is_valid(schema_validate):
data = json.loads(Path("package.json").read_text())
assert schema_validate(data=data, schema_name="package")schema_validate_file and schema_validate_string accept the following file_type values:
file_type |
Extensions inferred | Parser |
|---|---|---|
json |
.json |
json (standard library) |
toml |
.toml |
tomllib (standard library) |
yaml |
.yaml, .yml |
ruamel.yaml |
Pass one of these as schema_name:
schema_name |
Validates | Source |
|---|---|---|
ansible |
Ansible playbooks | ansible-lint |
ansible-vars |
Ansible variable files | ansible-lint |
docker-compose |
compose.yml / docker-compose.yml |
compose-spec |
github-action |
action.yml for a composite action |
SchemaStore |
github-funding |
.github/FUNDING.yml |
SchemaStore |
github-issue-config |
.github/ISSUE_TEMPLATE/config.yml |
SchemaStore |
github-issue-forms |
GitHub issue form templates | SchemaStore |
github-workflow |
.github/workflows/*.yml |
SchemaStore |
gitlab-ci |
.gitlab-ci.yml |
GitLab |
package |
package.json |
SchemaStore |
pre-commit-config |
.pre-commit-config.yaml |
SchemaStore |
pre-commit-hooks |
.pre-commit-hooks.yaml |
SchemaStore |
prettierrc |
.prettierrc |
SchemaStore |
pyproject |
pyproject.toml |
SchemaStore |
repository-v1 |
repository.toml, spec 1 |
repoplone |
repository-v2 |
repository.toml, spec 2 |
repoplone |
tsconfig |
tsconfig.json |
SchemaStore |
repository.toml has two specifications, and they are not interchangeable: a file
without a spec_version key, or with spec_version = "1", is spec 1 and declares
packages as [backend.package] / [frontend.package] tables; spec_version = "2"
declares them as a flat [[package]] array. Pick the schema that matches the file:
from pathlib import Path
from pytest_jsonschema.loaders import data_from_file
def test_repository_toml_is_valid(schema_validate_file):
path = Path("repository.toml")
spec = str(data_from_file(path).get("spec_version", "1"))
schema_name = "repository-v2" if spec.startswith("2") else "repository-v1"
assert schema_validate_file(path=path, schema_name=schema_name)The bundled copies are refreshed with make update-schemas.
- Python >= 3.11
- pytest >= 6.2.0
To contribute to pytest-jsonschema, please follow these steps:
-
Clone the repository:
git clone git@github.com:collective/pytest-jsonschema.git
-
Install the package for development:
make install
-
Format the codebase:
make format
-
Run the linters and type checks:
make lint
-
Run the tests:
make testTo stop on the first error and open a pdb session:
uv run pytest -x --pdb
-
Add a change log entry under
news/, named<issue-number>.<type>, wheretypeis one ofbreaking,feature,bugfix,internal,documentationortests. Preview the result withmake changelog.
Run make help to see every available target. Testing is conducted using pytest.
pytest-jsonschema is licensed under the MIT License.