Skip to content

Commit

Permalink
validate that the request parameter exists
Browse files Browse the repository at this point in the history
  • Loading branch information
livioribeiro committed Oct 30, 2023
1 parent b9dbb21 commit ba1ff24
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/selva/web/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)
from selva.web.converter.error import (
MissingFromRequestImplError,
MissingFromRequestParamImplError,
MissingParamConverterImplError,
MissingRequestParamExtractorImplError,
)
from selva.web.converter.from_request import FromRequest
Expand Down Expand Up @@ -289,7 +289,7 @@ async def _params_from_request(
value = await maybe_async(converter.from_str, param)
result[name] = value
else:
raise MissingFromRequestParamImplError(param_type)
raise MissingParamConverterImplError(param_type)
else:
if converter := await self._find_param_converter(
param_type, FromRequest
Expand Down
8 changes: 7 additions & 1 deletion src/selva/web/converter/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, param_type):
self.param_type = param_type


class MissingFromRequestParamImplError(Exception):
class MissingParamConverterImplError(Exception):
def __init__(self, param_type):
super().__init__(
f"no implementation of '{ParamConverter.__name__}' found for type {param_type}"
Expand All @@ -25,3 +25,9 @@ def __init__(self, param_type):
f"no implementation of '{ParamExtractor.__name__}' found for type {param_type}"
)
self.param_type = param_type


class PathParamNotFoundError(Exception):
def __init__(self, name: str):
super().__init__(f"path parameter '{name}' not found")
self.name = name
7 changes: 6 additions & 1 deletion src/selva/web/converter/param_extractor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from asgikit.requests import Request

from selva.web.converter.decorator import register_param_extractor
from selva.web.converter.error import PathParamNotFoundError

__all__ = (
"FromPath",
Expand Down Expand Up @@ -37,7 +38,11 @@ def extract(
else:
name = metadata.name or parameter_name

return request["path_params"][name]
param = request["path_params"].get(name)
if not param:
raise PathParamNotFoundError(name)

return param


class FromQuery(FromRequestParam):
Expand Down
File renamed without changes.

0 comments on commit ba1ff24

Please sign in to comment.