aiohttp-apispec
aiohttp-apispec copied to clipboard
Validate extra keys in data
Hey! Is it possible to validate extra keys in data? For example:
class TaskSerializer(Schema):
id = fields.Int()
name = fields.Str()
)
class TaskView(BaseView):
serializer = TaskSerializer()
@request_schema(TaskSerializer)
async def patch(self) -> Response:
data = self.request["data"]
task_id = int(self.request.match_info["id"])
return web.json_response({}, status=200)
If i try to patch with body {"id": 1, "name": "task", "extra_key": 1}
then error will not be raised, how to raise ValidationError
on this case too? Thanks.
Hi! There are no ready to use feature for this. But you can extend your schema validation (read here https://marshmallow.readthedocs.io/en/latest/extending.html) with post/preprocessing and checking for invalid keys.
@maximdanilchenko This won't help.. The problem with the webargs itself: it parses arguments and then calls schema.load(...) for already parsed arguments (there are no extra fields after arguments parsing), so basically webargs library ignores "unknown" schema parameter However, it was changed in 6.0.0b1 -- since that version webargs parsers preserve marshmallow schemas behavior for the additional fields.
Does anything block webargs update in aiohttp_apispec?
should be closed with https://github.com/maximdanilchenko/aiohttp-apispec/pull/112 @maximdanilchenko