Skip to main content
https://img.shields.io/pypi/v/aiohttp_validate.svg https://github.com/dchaplinsky/aiohttp_validate/actions/workflows/tests.yml/badge.svg

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

aiohttp_validate-2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

aiohttp_validate-2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

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

Hashes for aiohttp_validate-2.0.tar.gz
Algorithm Hash digest
SHA256 c95cdd099b634011ee57e83bccd12a8e758475f97d968a172635b11909119c07
MD5 313855087f3a17b3e2a1363f04799ad9
BLAKE2b-256 147f204625eb0f92ce5c852439b636534ffb43bfa5c2a5ba25f3829870c0d292

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_validate-2.0.tar.gz:

Publisher: publish.yml on dchaplinsky/aiohttp_validate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for aiohttp_validate-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be9436814e807f61ac0964c98705a665d8828b8966946912ee9e563b14fcc153
MD5 2e45b61eb074c86163a65f94ebcdfd0e
BLAKE2b-256 e3939da15770f6c3188e1d04ea02003ffdd8c17caf844524fb22a548e90dd733

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_validate-2.0-py3-none-any.whl:

Publisher: publish.yml on dchaplinsky/aiohttp_validate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page