Describe the bug
when validating query strings with @validate(query=MyModel), the code can fail with a key error before reaching actual pydantic validation if query parameters not on the model are supplied.
This will lead to a KeyError instead of a pydantic ValidationError, making it hard to deal with. In our case, we pretty format ValidationError before returning errors to the client, but KeyError will just be a generic 500 error. And we don't want to catch all KeyErrors, as there could be KeyErrors unrelated to validation as well.
Expected behavior
A pydantic ValidationError to be created (if the Model has extra = "forbid") or validation to pass (if the model has extra = "ignore")
Environment (please complete the following information):
OS: Arch Linux
Browser curl/requests
Version 23.12.0
Additional context
We use schemathesis to test our API, which will create many negative test cases like passing random arguments, and it expects those to fail with a 4xx error, but the issue above causes 500 errors, failing schemathesis.
Describe the bug when validating query strings with
@validate(query=MyModel)
, the code can fail with a key error before reaching actual pydantic validation if query parameters not on the model are supplied.Specifically, https://github.com/sanic-org/sanic-ext/blob/main/sanic_ext/extras/validation/clean.py#L6 fails because
data
contains a key that isn't inhints
, as themodel
doesn't have that key that was sent indata
.This will lead to a KeyError instead of a pydantic ValidationError, making it hard to deal with. In our case, we pretty format ValidationError before returning errors to the client, but KeyError will just be a generic 500 error. And we don't want to catch all KeyErrors, as there could be KeyErrors unrelated to validation as well.
Expected behavior A pydantic ValidationError to be created (if the Model has
extra = "forbid"
) or validation to pass (if the model hasextra = "ignore"
)Environment (please complete the following information):
Additional context We use schemathesis to test our API, which will create many negative test cases like passing random arguments, and it expects those to fail with a 4xx error, but the issue above causes 500 errors, failing schemathesis.