Automatic OpenAPI request & response validator
Project description
🚀 AutoAPI Validator
Automatic OpenAPI request & response validator
A Python package that automatically validates API requests & responses using an OpenAPI (Swagger) file.
✨ Features
✔ Load OpenAPI JSON/YAML specifications
✔ Validate API responses against schemas
✔ Clear, detailed error messages
✔ CLI command for quick validation
✔ Framework-independent
📦 Installation
pip install autoapi-validator
Development Installation
git clone https://github.com/santhosh/autoapi-validator.git
cd autoapi-validator
pip install -e .
🔧 Usage
As a Library
from autoapi_validator import load_openapi, validate_response
# Load OpenAPI specification
spec = load_openapi("openapi.yaml")
# Get schema for a specific component
schema = spec["components"]["schemas"]["User"]
# Validate API response
response = {
"id": 1,
"name": "Santhosh"
}
is_valid, message = validate_response(response, schema)
print(is_valid, message)
FastAPI Integration 🚀
NEW! Automatic validation middleware for FastAPI applications:
from fastapi import FastAPI
from autoapi_validator.integrations.fastapi import configure_validation
app = FastAPI()
# Enable automatic response validation
configure_validation(app, validate_responses=True)
# That's it! All responses are now validated against your OpenAPI schema
Installation with FastAPI support:
pip install autoapi-validator[fastapi]
Features:
- ✅ Automatic response validation using FastAPI's generated OpenAPI schema
- ✅ No duplicate schema definitions needed
- ✅ Validation warnings logged for debugging
- ✅ Non-blocking (responses still sent even if validation fails)
Full example:
from fastapi import FastAPI
from pydantic import BaseModel
from autoapi_validator.integrations.fastapi import configure_validation
app = FastAPI()
configure_validation(app)
class User(BaseModel):
id: int
name: str
email: str
@app.get("/users/{user_id}", response_model=User)
async def get_user(user_id: int):
return {"id": user_id, "name": "John", "email": "john@example.com"}
See examples/fastapi_example.py for a complete working example.
Flask Integration 🍶
NEW! Validation decorators for Flask applications:
from flask import Flask
from autoapi_validator.integrations.flask import setup_flask_validation
app = Flask(__name__)
# Setup validation with your OpenAPI spec
validator = setup_flask_validation(app, "openapi.yaml")
@app.post("/users")
@validator.validate_request_decorator("/users", "POST")
def create_user():
# Request is automatically validated!
return jsonify(request.json)
@app.get("/users/<int:user_id>")
@validator.validate_response_decorator("/users/{userId}", "GET")
def get_user(user_id):
# Response is automatically validated!
return jsonify({"id": user_id, "name": "John"})
Installation with Flask support:
pip install autoapi-validator[flask]
Features:
- ✅ Request validation decorators
- ✅ Response validation decorators
- ✅ Flexible per-endpoint validation
- ✅ Full OpenAPI schema support
See examples/flask_example.py for a complete working example.
Django Integration 🎯
NEW! Middleware and decorators for Django applications:
# settings.py
MIDDLEWARE = [
...
'autoapi_validator.integrations.django.ValidationMiddleware',
]
OPENAPI_SPEC_PATH = 'path/to/openapi.yaml'
OPENAPI_VALIDATE_REQUESTS = False # Optional
OPENAPI_VALIDATE_RESPONSES = True # Optional
# views.py
from django.http import JsonResponse
from autoapi_validator.integrations.django import validate_api
@validate_api("/users", "POST")
def create_user(request):
# Request automatically validated!
data = json.loads(request.body)
return JsonResponse({"id": 1, **data})
@validate_api("/users/{id}", "GET")
def get_user(request, id):
# Response automatically validated!
return JsonResponse({"id": id, "name": "John"})
Installation with Django support:
pip install autoapi-validator[django]
Features:
- ✅ Middleware for automatic validation
- ✅ View decorators for per-endpoint control
- ✅ Django & Django REST Framework support
- ✅ Full OpenAPI schema support
See examples/django_example/ for complete examples.
CI/CD Integration 🔄
GitHub Actions Example:
- name: Validate OpenAPI Spec
run: |
pip install autoapi-validator
python -m autoapi_validator openapi.yaml --info
Pre-commit Hook:
repos:
- repo: local
hooks:
- id: validate-openapi
name: Validate OpenAPI Specification
entry: python -m autoapi_validator
args: ['openapi.yaml', '--info']
language: system
See examples/ci_cd/ for complete configurations.
CLI Usage
# Display help
python -m autoapi_validator
# Load and validate OpenAPI spec
python -m autoapi_validator openapi.yaml
# Show spec information
python -m autoapi_validator openapi.yaml --info
📋 Example
If your API response should be:
{
"id": 1,
"name": "Santhosh"
}
But the API returns:
{
"id": "one",
"fullname": "Santhosh"
}
➡️ AutoAPI Validator will detect the error automatically and provide detailed feedback!
🛠️ Requirements
- Python >= 3.8
- pyyaml >= 6.0
- jsonschema >= 4.0.0
- requests >= 2.28.0
🗺️ Roadmap
Version 0.1.0
- ✅ Load OpenAPI JSON/YAML
- ✅ Validate API responses
- ✅ Clear error messages
- ✅ Basic CLI
Version 0.2.0
- ✅ FastAPI integration middleware
- ✅ Automatic response validation
- ✅ FastAPI example application
Version 0.3.0
- ✅ FastAPI request validation
- ✅ Flask integration with decorators
- ✅ Flask example application
- ✅ CI/CD tools (GitHub Actions, pre-commit hooks)
Version 0.4.0 (Current)
- ✅ Django integration with middleware
- ✅ Django view decorators
- ✅ Django example application
Future Versions
- Auto test generation from OpenAPI specs
- Request body validation improvements
- Real-time API monitoring
- Validation reporting and metrics
📄 License
MIT License - see LICENSE file for details
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
👤 Author
Santhosh
⭐ Star this repo if you find it useful!
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 autoapi_validator-0.1.0.tar.gz.
File metadata
- Download URL: autoapi_validator-0.1.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b12764a14edbe527c4e79ee35a405e4632d4753771ca0301253b54c8b14efa0e
|
|
| MD5 |
0c35d8def030cf5bbc00f2f3a67411b8
|
|
| BLAKE2b-256 |
dde22c237ec78dc155edc527e2dc0ad3969fbe1fc78d75f6963fb8694e412176
|
File details
Details for the file autoapi_validator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: autoapi_validator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
905a0f97719b656025f0836c3c17e54f4b13a99fad69db16208f176a9b9ae5f6
|
|
| MD5 |
3e9755cda686f362529a15db883fb7fa
|
|
| BLAKE2b-256 |
b3621ff86782cc657193c4cadd54d19c2fb4dbe3fc86f7ce8a597e11c4096593
|