Simple library that helps you validate your API endpoints requests/responses with jsonschema. Requires Python 3.9+ and aiohttp 3.8+.
Installation
Install from PyPI:
pip install aiohttp_validate
Usage
Complete example of validation for text tokenization microservice:
from aiohttp_validate import validate
@validate(
request_schema={
"type": "object",
"properties": {
"text": {"type": "string"},
},
"required": ["text"],
"additionalProperties": False
},
response_schema={
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": {"type": "string"}
}
}
}
)
async def tokenize_text_handler(request, *args):
return tokenize_text(request["text"])
The wrapped handler receives the parsed and validated JSON body as its first argument and the original aiohttp request object as the second:
async def handler(data, request):
pool = request.app["redis_pool"] # the real request is right here
...
To respond with a status code other than 200, return a (data, status) tuple:
async def create_handler(data, request):
return {"id": new_id}, 201
The tuple form is detected by shape (a 2-tuple ending in an int), so if your response data is itself such a tuple, return it as a list instead — JSON has no tuples, the wire format is identical — or return a ready web.json_response.
To also validate string formats (date, email, …), pass a format checker (extra format support follows jsonschema’s rules):
@validate(request_schema=..., format_checker=jsonschema.FormatChecker())
Features
The decorator to (optionally) validate the request to your aiohttp endpoint and it’s response.
Easily integrates with aiohttp_swaggerify to automatically document your endpoints with swagger.
Validation errors are standardized and can be easily parsed by the clients of your service and also human-readable.
Developing
Install with test dependencies and launch tests:
pip install -e .[test] pytest
Credits
That package is influenced by Tornado-JSON written by Hamza Faran Code to parse errors is written by Ruslan Karalkin
Versioning
This software follows Semantic Versioning
License
Free software: 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
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 aiohttp_validate-2.0.tar.gz.
File metadata
- Download URL: aiohttp_validate-2.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c95cdd099b634011ee57e83bccd12a8e758475f97d968a172635b11909119c07
|
|
| MD5 |
313855087f3a17b3e2a1363f04799ad9
|
|
| BLAKE2b-256 |
147f204625eb0f92ce5c852439b636534ffb43bfa5c2a5ba25f3829870c0d292
|
Provenance
The following attestation bundles were made for aiohttp_validate-2.0.tar.gz:
Publisher:
publish.yml on dchaplinsky/aiohttp_validate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiohttp_validate-2.0.tar.gz -
Subject digest:
c95cdd099b634011ee57e83bccd12a8e758475f97d968a172635b11909119c07 - Sigstore transparency entry: 2201787139
- Sigstore integration time:
-
Permalink:
dchaplinsky/aiohttp_validate@479ade8ed7c878c069f574f69536e845c487cfa4 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/dchaplinsky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@479ade8ed7c878c069f574f69536e845c487cfa4 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file aiohttp_validate-2.0-py3-none-any.whl.
File metadata
- Download URL: aiohttp_validate-2.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be9436814e807f61ac0964c98705a665d8828b8966946912ee9e563b14fcc153
|
|
| MD5 |
2e45b61eb074c86163a65f94ebcdfd0e
|
|
| BLAKE2b-256 |
e3939da15770f6c3188e1d04ea02003ffdd8c17caf844524fb22a548e90dd733
|
Provenance
The following attestation bundles were made for aiohttp_validate-2.0-py3-none-any.whl:
Publisher:
publish.yml on dchaplinsky/aiohttp_validate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiohttp_validate-2.0-py3-none-any.whl -
Subject digest:
be9436814e807f61ac0964c98705a665d8828b8966946912ee9e563b14fcc153 - Sigstore transparency entry: 2201787304
- Sigstore integration time:
-
Permalink:
dchaplinsky/aiohttp_validate@479ade8ed7c878c069f574f69536e845c487cfa4 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/dchaplinsky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@479ade8ed7c878c069f574f69536e845c487cfa4 -
Trigger Event:
workflow_dispatch
-
Statement type: