Files
pydantic/docs/examples/validation_decorator_async.py
T
Samuel Colvin 6914410f38 Validation Decorator (#1179)
* starting validation_decorator

* correct skip_pre_38

* fix coverage and type hints

* mypy tests and move to class based decorator

* 3.6 fix, prevent duplicate github actions

* correct py 3.6 check

* better errors

* cleaner field names for args and kwargs

* add change and comments

* starting docs

* back to 3.7 for docs

* docs

* bump

* add async example and fix print indents

* allow type annotations as strings

* python 3.8 in docs
2020-02-05 17:27:12 +00:00

28 lines
719 B
Python

class Connection:
async def execute(self, sql, *args):
return 'testing@example.com'
conn = Connection()
# ignore-above
import asyncio
from pydantic import PositiveInt, ValidationError, validate_arguments
@validate_arguments
async def get_user_email(user_id: PositiveInt):
# `conn` is some fictional connection to a database
email = await conn.execute('select email from users where id=$1', user_id)
if email is None:
raise RuntimeError('user not found')
else:
return email
async def main():
email = await get_user_email(123)
print(email)
try:
await get_user_email(-4)
except ValidationError as exc:
print(exc.errors())
asyncio.run(main())