Skip to main content

pytest-jsonschema

PyPI Python Version Wheel License Status Tests / QA Contributors Stars

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.

Installation

Install pytest-jsonschema from PyPI:

pip install pytest-jsonschema

Or, with uv:

uv add --dev pytest-jsonschema

The plugin registers itself with pytest automatically; no configuration is required.

Usage

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.

schema_validate_file

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.

schema_validate_string

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")

schema_validate

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")

Supported file formats

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 (tomli below Python 3.12)
yaml .yaml, .yml ruamel.yaml

Available schemas

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.

Requirements

  • Python >= 3.10
  • pytest >= 6.2.0

Contributing

To contribute to pytest-jsonschema, please follow these steps:

  1. Clone the repository:

    git clone git@github.com:collective/pytest-jsonschema.git
    
  2. Install the package for development:

    make install
    
  3. Format the codebase:

    make format
    
  4. Run the linters and type checks:

    make lint
    
  5. Run the tests:

    make test
    

    To stop on the first error and open a pdb session:

    uv run pytest -x --pdb
    
  6. Add a change log entry under news/, named <issue-number>.<type>, where type is one of breaking, feature, bugfix, internal, documentation or tests. Preview the result with make changelog.

Run make help to see every available target. Testing is conducted using pytest.

License

pytest-jsonschema is licensed under the MIT License.

Download files

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

Source Distribution

pytest_jsonschema-1.1.0.tar.gz (232.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_jsonschema-1.1.0-py3-none-any.whl (118.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_jsonschema-1.1.0.tar.gz.

File metadata

  • Download URL: pytest_jsonschema-1.1.0.tar.gz
  • Upload date:
  • Size: 232.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_jsonschema-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3b6372d48b3eecc7ada627f1cd739ae47bda411e66202eb6951e6809fda8fa05
MD5 ee68dc67fb20260a15eb5e4f9e521b3a
BLAKE2b-256 8146a5c553f68a0388ff079f27a89cad32a7abefdeb7e442b644b671064ca023

See more details on using hashes here.

File details

Details for the file pytest_jsonschema-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_jsonschema-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 118.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_jsonschema-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4021053843339c52a25a06dd2a2dff4bc8937ce73c2d8b69de1f04c29c32dbad
MD5 09f9b73a6d6a75863f91a6ef766a62f4
BLAKE2b-256 1b8ef90d574caef0e8560ef527bba686ae315344ac5830571647d303793b692d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page