aiohttp-validator


Nameaiohttp-validator JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/dapper91/aiohttp-validator
Summaryaiohttp simple pydantic validator
upload_time2023-09-28 14:19:08
maintainer
docs_urlNone
authorDmitry Pershin
requires_python>=3.9
licenseUnlicense
keywords aiohttp pydantic validation http asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # aiohttp-validator

[![Downloads][download-badge]][download-url]
[![License][licence-badge]][licence-url]
[![Python Versions][python-version-badge]][python-version-url]
[![Build status][build-badge]][build-url]
[![Code coverage][coverage-badge]][coverage-url]

[download-badge]: https://static.pepy.tech/personalized-badge/aiohttp-validator?period=month&units=international_system&left_color=grey&right_color=orange&left_text=Downloads/month
[download-url]: https://pepy.tech/project/aiohttp-validator
[licence-badge]: https://img.shields.io/badge/license-Unlicense-blue.svg
[licence-url]: https://github.com/dapper91/aiohttp-validator/blob/master/LICENSE
[python-version-badge]: https://img.shields.io/pypi/pyversions/aiohttp-validator.svg
[python-version-url]: https://pypi.org/project/aiohttp-validator

[build-badge]: https://github.com/dapper91/aiohttp-validator/actions/workflows/test.yml/badge.svg?branch=master
[build-url]: https://github.com/dapper91/aiohttp-validator/actions/workflows/test.yml
[coverage-badge]: https://codecov.io/gh/dapper91/aiohttp-validator/branch/master/graph/badge.svg
[coverage-url]: https://codecov.io/gh/dapper91/aiohttp-validator

aiohttp simple pydantic http request validator


## Installation

```shell
pip install aiohttp-validator
```


## A Simple Example

```py
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:

```py
@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.

```py
@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)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dapper91/aiohttp-validator",
    "name": "aiohttp-validator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "aiohttp,pydantic,validation,http,asyncio",
    "author": "Dmitry Pershin",
    "author_email": "dapper1291@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/22/af/07328359555d13b3035352e9ca96c635842ff82392cbcedceb86fd606a7b/aiohttp_validator-0.2.0.tar.gz",
    "platform": null,
    "description": "# aiohttp-validator\n\n[![Downloads][download-badge]][download-url]\n[![License][licence-badge]][licence-url]\n[![Python Versions][python-version-badge]][python-version-url]\n[![Build status][build-badge]][build-url]\n[![Code coverage][coverage-badge]][coverage-url]\n\n[download-badge]: https://static.pepy.tech/personalized-badge/aiohttp-validator?period=month&units=international_system&left_color=grey&right_color=orange&left_text=Downloads/month\n[download-url]: https://pepy.tech/project/aiohttp-validator\n[licence-badge]: https://img.shields.io/badge/license-Unlicense-blue.svg\n[licence-url]: https://github.com/dapper91/aiohttp-validator/blob/master/LICENSE\n[python-version-badge]: https://img.shields.io/pypi/pyversions/aiohttp-validator.svg\n[python-version-url]: https://pypi.org/project/aiohttp-validator\n\n[build-badge]: https://github.com/dapper91/aiohttp-validator/actions/workflows/test.yml/badge.svg?branch=master\n[build-url]: https://github.com/dapper91/aiohttp-validator/actions/workflows/test.yml\n[coverage-badge]: https://codecov.io/gh/dapper91/aiohttp-validator/branch/master/graph/badge.svg\n[coverage-url]: https://codecov.io/gh/dapper91/aiohttp-validator\n\naiohttp simple pydantic http request validator\n\n\n## Installation\n\n```shell\npip install aiohttp-validator\n```\n\n\n## A Simple Example\n\n```py\nimport datetime as dt\nfrom typing import Any, Dict, List, TypedDict\nfrom uuid import UUID\n\nimport pydantic\nfrom aiohttp import web\n\nimport aiohttp_validator as validator\n\nroutes = web.RouteTableDef()\n\n\n@routes.get('/posts')\n@validator.validated()\nasync def get_posts(request: web.Request, tags: List[str], limit: pydantic.conint(gt=0, le=100), offset: int = 0):\n    assert isinstance(tags, list)\n    assert isinstance(limit, int)\n    assert isinstance(offset, int)\n    # your code here ...\n\n    return web.Response(status=200)\n\n\nclass RequestHeaders(TypedDict):\n    requestId: int\n\n\nclass User(pydantic.BaseModel):\n    name: str\n    surname: str\n\n\nclass Post(pydantic.BaseModel):\n    title: str\n    text: str\n    timestamp: float\n    author: User\n    tags: List[str] = pydantic.Field(default_factory=list)\n\n\n@routes.post('/posts/{section}/{date}')\n@validator.validated(config=pydantic.ConfigDict(extra='forbid'))\nasync def create_post(request: web.Request, body: Post, headers: RequestHeaders, section: str, date: dt.date):\n    assert isinstance(body, Post)\n    assert isinstance(headers, dict)\n    assert isinstance(date, dt.date)\n    assert isinstance(section, str)\n    # your code here ...\n\n    return web.Response(status=201)\n\n\nclass AuthCookies(pydantic.BaseModel):\n    tokenId: UUID\n\n\n@routes.post('/users')\n@validator.validated(config=pydantic.ConfigDict(extra='forbid'))\nasync def create_user(request: web.Request, body: Dict[str, Any], headers: RequestHeaders, cookies: AuthCookies):\n    assert isinstance(body, dict)\n    assert isinstance(headers, RequestHeaders)\n    assert isinstance(cookies, AuthCookies)\n    # your code here ...\n\n    return web.Response(status=201)\n\napp = web.Application()\napp.add_routes(routes)\n\nweb.run_app(app, port=8080)\n\n```\n\nIf any path or query parameter name are clashes with body, headers or cookies argument\nfor some reason the last can be renamed:\n\n```py\n@routes.post('/{cookies}')\n@validator.validated(cookies_argname='_cookies')\nasync def method(request: web.Request, body: Dict[str, Any], _cookies: AuthCookies, cookies: str):\n    # your code here ...\n\n    return web.Response(status=201)\n```\n\nIf any argname is `None` the corresponding request part will not be passed to the function\nand argname can be used as a path or query parameter.\n\n```py\n@routes.post('/{body}/{headers}')\n@validator.validated(body_argname=None, headers_argname=None, cookies_argname=None)\nasync def method(request: web.Request, body: str, headers: str, cookies: str = ''):\n    # your code here ...\n\n    return web.Response(status=201)\n```\n",
    "bugtrack_url": null,
    "license": "Unlicense",
    "summary": "aiohttp simple pydantic validator",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/dapper91/aiohttp-validator",
        "Repository": "https://github.com/dapper91/aiohttp-validator"
    },
    "split_keywords": [
        "aiohttp",
        "pydantic",
        "validation",
        "http",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d6df286e3b60867bca44c819055d8707bf4bea27ba25e931e2e88885be4973a0",
                "md5": "e3687d90aeaeb6d2e494cd9cff8c2a6c",
                "sha256": "e6ec79e3e0453c1108ed9d553aeb8e324a050c5a445143ad48199fe258648d10"
            },
            "downloads": -1,
            "filename": "aiohttp_validator-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e3687d90aeaeb6d2e494cd9cff8c2a6c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5538,
            "upload_time": "2023-09-28T14:19:07",
            "upload_time_iso_8601": "2023-09-28T14:19:07.085305Z",
            "url": "https://files.pythonhosted.org/packages/d6/df/286e3b60867bca44c819055d8707bf4bea27ba25e931e2e88885be4973a0/aiohttp_validator-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "22af07328359555d13b3035352e9ca96c635842ff82392cbcedceb86fd606a7b",
                "md5": "fc5d8eb0cc5157efb6d21b89630d43d8",
                "sha256": "b7a4393f926b185f76fbd21393266132ab2800c87d421db71ec83573ed704b98"
            },
            "downloads": -1,
            "filename": "aiohttp_validator-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "fc5d8eb0cc5157efb6d21b89630d43d8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 4898,
            "upload_time": "2023-09-28T14:19:08",
            "upload_time_iso_8601": "2023-09-28T14:19:08.675586Z",
            "url": "https://files.pythonhosted.org/packages/22/af/07328359555d13b3035352e9ca96c635842ff82392cbcedceb86fd606a7b/aiohttp_validator-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-28 14:19:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dapper91",
    "github_project": "aiohttp-validator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aiohttp-validator"
}
        
Elapsed time: 0.11955s