Skip to main content

aiohttp-validator

Downloads License Python Versions Build status Code coverage

aiohttp simple pydantic http request validator

Installation

pip install aiohttp-validator

A Simple Example

import datetime as dt
from typing import Any, Dict, List, TypedDict
from uuid import UUID

import pydantic
from aiohttp import web

import aiohttp_validator as validator

routes = web.RouteTableDef()


@routes.get('/posts')
@validator.validated()
async def get_posts(request: web.Request, tags: List[str], limit: pydantic.conint(gt=0, le=100), offset: int = 0):
    assert isinstance(tags, list)
    assert isinstance(limit, int)
    assert isinstance(offset, int)
    # your code here ...

    return web.Response(status=200)


class RequestHeaders(TypedDict):
    requestId: int


class User(pydantic.BaseModel):
    name: str
    surname: str


class Post(pydantic.BaseModel):
    title: str
    text: str
    timestamp: float
    author: User
    tags: List[str] = pydantic.Field(default_factory=list)


@routes.post('/posts/{section}/{date}')
@validator.validated(config=pydantic.ConfigDict(extra='forbid'))
async def create_post(request: web.Request, body: Post, headers: RequestHeaders, section: str, date: dt.date):
    assert isinstance(body, Post)
    assert isinstance(headers, dict)
    assert isinstance(date, dt.date)
    assert isinstance(section, str)
    # your code here ...

    return web.Response(status=201)


class AuthCookies(pydantic.BaseModel):
    tokenId: UUID


@routes.post('/users')
@validator.validated(config=pydantic.ConfigDict(extra='forbid'))
async def create_user(request: web.Request, body: Dict[str, Any], headers: RequestHeaders, cookies: AuthCookies):
    assert isinstance(body, dict)
    assert isinstance(headers, RequestHeaders)
    assert isinstance(cookies, AuthCookies)
    # your code here ...

    return web.Response(status=201)

app = web.Application()
app.add_routes(routes)

web.run_app(app, port=8080)

If any path or query parameter name are clashes with body, headers or cookies argument for some reason the last can be renamed:

@routes.post('/{cookies}')
@validator.validated(cookies_argname='_cookies')
async def method(request: web.Request, body: Dict[str, Any], _cookies: AuthCookies, cookies: str):
    # your code here ...

    return web.Response(status=201)

If any argname is None the corresponding request part will not be passed to the function and argname can be used as a path or query parameter.

@routes.post('/{body}/{headers}')
@validator.validated(body_argname=None, headers_argname=None, cookies_argname=None)
async def method(request: web.Request, body: str, headers: str, cookies: str = ''):
    # your code here ...

    return web.Response(status=201)

Metadata

Release files for aiohttp-validator 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-validator 0.2.0
File Size Uploaded Provenance Actions
aiohttp_validator-0.2.0.tar.gz 4.9 kB Without provenance Details for aiohttp_validator-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-validator 0.2.0
File Interpreter ABI Platform Actions
aiohttp_validator-0.2.0-py3-none-any.whl Python 3 none any Details for aiohttp_validator-0.2.0-py3-none-any.whl

Total release size: 10.4 kB

Release files / aiohttp_validator-0.2.0.tar.gz

Download URL aiohttp_validator-0.2.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b7a4393f926b185f76fbd21393266132ab2800c87d421db71ec83573ed704b98
BLAKE2b-256 checksum
How to use checksums
22af07328359555d13b3035352e9ca96c635842ff82392cbcedceb86fd606a7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

Release files / aiohttp_validator-0.2.0-py3-none-any.whl

Download URL aiohttp_validator-0.2.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6ec79e3e0453c1108ed9d553aeb8e324a050c5a445143ad48199fe258648d10
BLAKE2b-256 checksum
How to use checksums
d6df286e3b60867bca44c819055d8707bf4bea27ba25e931e2e88885be4973a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1012-azure

Security for aiohttp-validator 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page