Skip to content
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def hello(
nicknames: List[str] = Json(),
date_of_birth: datetime = Json(),
password_expiry: Optional[int] = Json(5),
is_admin: bool = Query(False)
is_admin: bool = Query(False),
user_type: str = Json(alias="type")
):
return "Hello World!"

Expand Down Expand Up @@ -80,8 +81,9 @@ All parameters can have default values, and automatic validation.
* blacklist: str, A string containing forbidden characters for the value
* pattern: str, A regex pattern to test for string matches
* func: Callable -> Union[bool, tuple[bool, str]], A function containing a fully customized logic to validate the value
* datetime_format: str: datetime format string ([datetime format codes](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes))
* comment: str: A string to display as the argument description in generated documentation (if used)
* datetime_format: str, datetime format string ([datetime format codes](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes))
* comment: str, A string to display as the argument description in generated documentation (if used)
* alias: str, An expected parameter name instead of the function name. See `access_type` example for clarification.

`File` has the following options:
* content_types: array of strings, an array of allowed content types.
Expand Down
4 changes: 3 additions & 1 deletion flask_parameter_validation/parameter_types/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __init__(
pattern=None, # str: regexp pattern
func=None, # Callable -> Union[bool, tuple[bool, str]]: function performing a fully customized validation
datetime_format=None, # str: datetime format string (https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes),
comment=None
comment=None, # str: comment for autogenerated documentation
alias=None, # str: alias for parameter name
):
self.default = default
self.min_list_length = min_list_length
Expand All @@ -40,6 +41,7 @@ def __init__(
self.func = func
self.datetime_format = datetime_format
self.comment = comment
self.alias = alias

# Validator
def validate(self, value):
Expand Down
7 changes: 5 additions & 2 deletions flask_parameter_validation/parameter_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ def validate(self, expected_input, all_request_inputs):
Validate that a given expected input exists in the requested input collection
"""
# Extract useful information from expected input
expected_name = expected_input.name
expected_input_type = expected_input.annotation # i.e. str, int etc.
# i.e. Form, Query, Json etc.
expected_delivery_type = expected_input.default

# Check if an alias is given, otherwise use the input name
if expected_delivery_type.alias:
expected_name = expected_delivery_type.alias
else:
expected_name = expected_input.name
# Get input type as string to recognize typing objects, e.g. to convert typing.List to "typing.List"
# Note: We use this str() method, as typing API is too unreliable, see https://stackoverflow.com/a/52664522/7173479
expected_input_type_str = str(expected_input.annotation)
Expand Down