validate-pyproject

0.25 · active · verified Thu Apr 16

validate-pyproject is a Python library and CLI tool designed for validating `pyproject.toml` files against various Python packaging standards (like PEP 517, PEP 518, PEP 621, PEP 639, and PEP 735) using JSON Schema. It is currently at version 0.25 and maintains an active development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to programmatically validate a `pyproject.toml` file content using `validate-pyproject`. It uses `tomli` to parse the TOML string into a Python dictionary, then instantiates `api.Validator` to perform the checks.

import tomli
from validate_pyproject import api, errors
import os

# Example pyproject.toml content
PYPROJECT_TOML_CONTENT = '''
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "my-package"
version = "1.0.0"
description = "A simple Python package"
requires-python = ">=3.8"
keywords = ["packaging", "example"]
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]
authors = [
    {name = "Example Author", email = "author@example.com"},
]
maintainers = [
    {name = "Example Maintainer", email = "maintainer@example.com"},
]
'''

# Parse the TOML string into a dictionary
pyproject_as_dict = tomli.loads(PYPROJECT_TOML_CONTENT)

# Instantiate the validator (can be configured with extra schemas or plugins)
validator = api.Validator()

try:
    # Perform the validation
    validator(pyproject_as_dict)
    print("pyproject.toml is valid!")
except errors.ValidationError as ex:
    print(f"Invalid pyproject.toml: {ex.message}")
    # More detailed errors can be accessed via ex.details
    for error in ex.details:
        print(f"  - {error.message} at {error.json_path}")

view raw JSON →