aiohttp-apispec icon indicating copy to clipboard operation
aiohttp-apispec copied to clipboard

Validate extra keys in data

Open pintofcat opened this issue 3 years ago • 3 comments

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.

pintofcat avatar Jul 21 '20 09:07 pintofcat

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 avatar Jul 21 '20 12:07 maximdanilchenko

@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?

kiforchuk avatar Nov 16 '20 13:11 kiforchuk

should be closed with https://github.com/maximdanilchenko/aiohttp-apispec/pull/112 @maximdanilchenko

pzhang65 avatar Jan 28 '22 15:01 pzhang65