Jentic OpenAPI Validator
Project description
jentic-openapi-validator
A Python library for validating OpenAPI documents using pluggable validator backends. This package is part of the Jentic OpenAPI Tools ecosystem and provides a flexible, extensible architecture for OpenAPI document validation.
Features
- Pluggable Backend Architecture: Support for multiple validation strategies via entry points
- Multiple Input Formats: Validate OpenAPI documents from file URIs, JSON/YAML strings, or Python dictionaries
- Parallel Execution: Run multiple backends concurrently using
ProcessPoolExecutorfor improved performance - Aggregated Results: Collect diagnostics from all configured backends into a single result
- Type Safety: Full type hints with comprehensive docstrings
- Extensible Design: Easy integration of third-party validator backends
Installation
pip install jentic-openapi-validator
Prerequisites:
- Python 3.11+
Optional Backends:
For advanced validation with Spectral:
pip install jentic-openapi-validator-spectral
For validation with Redocly:
pip install jentic-openapi-validator-redocly
Quick Start
Basic Validation
from jentic.apitools.openapi.validator.core import OpenAPIValidator
# Create validator with default backend
validator = OpenAPIValidator()
# Validate from file URI
result = validator.validate("file:///path/to/openapi.yaml")
print(f"Valid: {result.valid}")
# Check for validation issues
if not result.valid:
for diagnostic in result.diagnostics:
print(f"Error: {diagnostic.message}")
Validate from String
# Validate JSON/YAML string
openapi_json = '{"openapi":"3.1.0","info":{"title":"My API","version":"1.0.0"},"paths":{}}'
result = validator.validate(openapi_json)
if result: # ValidationResult supports boolean context
print("Validation passed!")
Validate from Dictionary
# Validate from dictionary
openapi_doc = {
"openapi": "3.1.0",
"info": {"title": "My API", "version": "1.0.0"},
"paths": {}
}
result = validator.validate(openapi_doc)
print(f"Found {len(result)} issues") # ValidationResult supports len()
Configuration Options
Using Multiple Backends
# Use openapi-spec backend only (default)
validator = OpenAPIValidator()
# Use multiple backends (requires backends to be installed)
validator = OpenAPIValidator(backends=["openapi-spec", "spectral"])
# Results from all backends are aggregated
result = validator.validate(document)
Backend Selection
# Use backend by name
validator = OpenAPIValidator(backends=["openapi-spec"])
# Pass backend instance
from jentic.apitools.openapi.validator.backends.openapi_spec import OpenAPISpecValidatorBackend
backend = OpenAPISpecValidatorBackend()
validator = OpenAPIValidator(backends=[backend])
# Pass backend class
validator = OpenAPIValidator(backends=[OpenAPISpecValidatorBackend])
Custom Parser
from jentic.apitools.openapi.parser.core import OpenAPIParser
# Use a custom parser instance
parser = OpenAPIParser()
validator = OpenAPIValidator(parser=parser)
Parallel Execution
When using multiple backends, you can run them in parallel for improved performance:
# Run backends in parallel using ProcessPoolExecutor
validator = OpenAPIValidator(backends=["default", "openapi-spec", "spectral"])
result = validator.validate(document, parallel=True)
# Limit the number of worker processes
result = validator.validate(document, parallel=True, max_workers=2)
Parallel execution uses ProcessPoolExecutor for true parallelism that bypasses Python's GIL. This is particularly beneficial when using multiple backends, especially I/O-bound backends like Spectral and Redocly that spawn subprocesses.
Notes:
parallel=Falseby default (opt-in)- With a single backend,
parallel=Truehas no effect (runs sequentially) - Diagnostics from all backends are aggregated regardless of execution mode
Working with ValidationResult
The ValidationResult class provides convenient methods for working with validation diagnostics:
result = validator.validate(document)
# Boolean context - True if valid
if result:
print("Valid!")
# Get diagnostic count
print(f"Found {len(result)} issues")
# Check validity
if not result.valid:
print("Validation failed")
# Access all diagnostics
for diagnostic in result.diagnostics:
print(f"{diagnostic.severity}: {diagnostic.message}")
Testing
Run the test suite:
uv run --package jentic-openapi-validator pytest packages/jentic-openapi-validator -v
Integration Tests
The package includes integration tests for backend discovery and validation. Tests requiring external backends (like Spectral) will be automatically skipped if the backend package is not installed or the required CLI is not available.
API Reference
OpenAPIValidator
class OpenAPIValidator:
def __init__(
self,
backends: Sequence[str | BaseValidatorBackend | Type[BaseValidatorBackend]] | None = None,
parser: OpenAPIParser | None = None,
) -> None
Parameters:
backends: Sequence of validator backends to use. Each item can be:str: Name of a backend registered via entry points (e.g., "default", "openapi-spec", "redocly", "spectral")BaseValidatorBackend: Instance of a validator backendType[BaseValidatorBackend]: Class of a validator backend (will be instantiated)- Defaults to
["default"]ifNone
parser: Custom OpenAPIParser instance (optional)
Methods:
validate(document: str | dict, *, base_url: str | None = None, target: str | None = None, parallel: bool = False, max_workers: int | None = None) -> ValidationResult- Validates an OpenAPI document using all configured backends
document: File URI, JSON/YAML string, or dictionarybase_url: Optional base URL for resolving relative referencestarget: Optional target identifier for validation contextparallel: IfTrueand multiple backends are configured, run validation in parallel usingProcessPoolExecutor. Defaults toFalse.max_workers: Maximum number of worker processes for parallel execution. IfNone, defaults to the number of processors on the machine. Only used whenparallel=True.- Returns:
ValidationResultwith aggregated diagnostics
ValidationResult
@dataclass
class ValidationResult:
diagnostics: list[Diagnostic]
valid: bool # Computed automatically
Attributes:
diagnostics: List of all diagnostics from validationvalid:Trueif no diagnostics were found,Falseotherwise
Methods:
__bool__(): Returnsvalidfor use in boolean context__len__(): Returns number of diagnostics__repr__(): Returns string representation
Available Backends
default
Basic validation backend that checks for required OpenAPI fields and structure. Suitable for basic document validation.
openapi-spec
Validation backend using the openapi-spec-validator library for JSON Schema-based validation of OpenAPI documents. CPU-bound validation.
redocly (Optional)
Validation backend using Redocly CLI for comprehensive OpenAPI linting and validation. I/O-bound (spawns Node.js subprocess).
Install: pip install jentic-openapi-validator-redocly
spectral (Optional)
Advanced validation backend using Spectral CLI with comprehensive rule checking. I/O-bound (spawns Node.js subprocess).
Install: pip install jentic-openapi-validator-spectral
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jentic_openapi_validator-1.0.0a49.tar.gz.
File metadata
- Download URL: jentic_openapi_validator-1.0.0a49.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f69e849a7c685dad11d6da173c9859daba8214bbec696562f0b055037623967
|
|
| MD5 |
d56d4c32b81879436dcfc0ad0e1ea89e
|
|
| BLAKE2b-256 |
17b0b301286eb77eabfda590ce1c6298cf0e4b2f87a2624766bc16c5a49fba90
|
Provenance
The following attestation bundles were made for jentic_openapi_validator-1.0.0a49.tar.gz:
Publisher:
release.yml on jentic/jentic-openapi-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_openapi_validator-1.0.0a49.tar.gz -
Subject digest:
9f69e849a7c685dad11d6da173c9859daba8214bbec696562f0b055037623967 - Sigstore transparency entry: 1198362895
- Sigstore integration time:
-
Permalink:
jentic/jentic-openapi-tools@67007e0dc6806b8c2bcbd58241f7997cd386ad84 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@67007e0dc6806b8c2bcbd58241f7997cd386ad84 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file jentic_openapi_validator-1.0.0a49-py3-none-any.whl.
File metadata
- Download URL: jentic_openapi_validator-1.0.0a49-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d486ae7df6949d3824112271361554ed97918c76629b7f9e4025d4d1e2a10145
|
|
| MD5 |
002cdae2d87a556fa736cf7b63208638
|
|
| BLAKE2b-256 |
c03a64c043a291854c7984ffeb2a77e051c452b4f028f565714a8bd5e4cf953b
|
Provenance
The following attestation bundles were made for jentic_openapi_validator-1.0.0a49-py3-none-any.whl:
Publisher:
release.yml on jentic/jentic-openapi-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jentic_openapi_validator-1.0.0a49-py3-none-any.whl -
Subject digest:
d486ae7df6949d3824112271361554ed97918c76629b7f9e4025d4d1e2a10145 - Sigstore transparency entry: 1198362961
- Sigstore integration time:
-
Permalink:
jentic/jentic-openapi-tools@67007e0dc6806b8c2bcbd58241f7997cd386ad84 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jentic
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@67007e0dc6806b8c2bcbd58241f7997cd386ad84 -
Trigger Event:
workflow_dispatch
-
Statement type: