Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #92 - @validate decorator not working for class-based views #93

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions sanic_ext/extras/validation/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Callable, Optional, Type, Union

from sanic import Request
from sanic.exceptions import SanicException

from sanic_ext.exceptions import InitError

Expand Down Expand Up @@ -31,7 +32,14 @@ def validate(

def decorator(f):
@wraps(f)
async def decorated_function(request: Request, *args, **kwargs):
async def decorated_function(*args, **kwargs):

if args and isinstance(args[0], Request):
request: Request = args[0]
elif len(args) > 1:
request: Request = args[1]
else:
raise SanicException("Request could not be found")

if schemas["json"]:
await do_validation(
Expand Down Expand Up @@ -66,7 +74,7 @@ async def decorated_function(request: Request, *args, **kwargs):
allow_multiple=True,
allow_coerce=True,
)
retval = f(request, *args, **kwargs)
retval = f(*args, **kwargs)
if isawaitable(retval):
retval = await retval
return retval
Expand Down
28 changes: 28 additions & 0 deletions tests/extra/test_validation_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from typing import List, Optional

import pytest
from sanic import json
from sanic.views import HTTPMethodView

from sanic_ext import validate
from sanic_ext.extras.validation.check import check_data
from sanic_ext.extras.validation.schema import make_schema, parse_hint

Expand Down Expand Up @@ -239,3 +242,28 @@ def test_modeling_union_type():
check_data(models.ModelUnionTypeStrNone, {"foo": None}, schema)
with pytest.raises(TypeError):
check_data(models.ModelUnionTypeStrNone, {"foo": 0}, schema)


def test_validate_decorator(app):
@dataclass
class Pet:
name: str

@app.post("/function")
@validate(json=Pet)
async def handler(_, body: Pet):
return json({"is_pet": isinstance(body, Pet)})

class MethodView(HTTPMethodView, attach=app, uri="/method"):
decorators = [validate(json=Pet)]

async def post(self, _, body: Pet):
return json({"is_pet": isinstance(body, Pet)})

_, response = app.test_client.post("/function", json={"name": "Snoopy"})
assert response.status == 200
assert response.json["is_pet"]

_, response = app.test_client.post("/method", json={"name": "Snoopy"})
assert response.status == 200
assert response.json["is_pet"]