Skip to content

Commit 32395f0

Browse files
committed
Remove README merge conflicts
1 parent 4e2dfbb commit 32395f0

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

README.md

+45-18
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if __name__ == "__main__":
3838
## Usage
3939
To validate parameters with flask-parameter-validation, two conditions must be met.
4040
1. The `@ValidateParameters()` decorator must be applied to the function
41-
2. Type hints ([supported types](#type-hints-and-accepted-input-types)) and a default of a subclass of `Parameter` for the parameters you want to use flask-parameter-validation on
41+
2. Type hints ([supported types](#type-hints-and-accepted-input-types)) and a default of a subclass of `Parameter` must be supplied per parameter flask-parameter-validation parameter
4242

4343

4444
### Enable and customize Validation for a Route with the @ValidateParameters decorator
@@ -49,7 +49,14 @@ The `@ValidateParameters()` decorator takes parameters that alter route validati
4949
| error_handler | `Optional[Response]` | `None` | Overwrite the output format of generated errors, see [Overwriting Default Errors](#overwriting-default-errors) for more |
5050

5151
#### Overwriting Default Errors
52-
By default, the error messages are returned as a JSON response, with the detailed error in the "error" field. However, this can be edited by passing a custom error function into the `ValidateParameters()` decorator. For example:
52+
By default, the error messages are returned as a JSON response, with the detailed error in the "error" field, eg:
53+
```json
54+
{
55+
"error": "Parameter 'age' must be type 'int'"
56+
}
57+
```
58+
59+
However, this can be edited by passing a custom error function into the `ValidateParameters()` decorator. For example:
5360
```py
5461
def error_handler(err):
5562
error_name = type(err)
@@ -61,8 +68,8 @@ def error_handler(err):
6168
"error_message": str(err)
6269
}, 400
6370

64-
@ValidateParameters(error_handler)
6571
@app.route(...)
72+
@ValidateParameters(error_handler)
6673
def api(...)
6774
```
6875

@@ -74,27 +81,47 @@ The `Parameter` class provides a base for validation common among all input type
7481
|---------------|------------------------------------------------------------------------------------------------------------------------|------------------|
7582
| Route | Parameter passed in the pathname of the URL, such as `/users/<int:id>` | All HTTP Methods |
7683
| Form | Parameter in an HTML form or a `FormData` object in the request body, often with `Content-Type: x-www-form-urlencoded` | POST Methods |
77-
| Json | Parameter in the JSON object in the request body, must have header `Content-Type: application/json` | POST Method |
84+
| Json | Parameter in the JSON object in the request body, must have header `Content-Type: application/json` | POST Methods |
7885
| Query | Parameter in the query of the URL, such as /news_article?id=55 | All HTTP Methods |
7986
| File | Parameter is a file uploaded in the request body | POST Method |
87+
| MultiSource | Parameter is in one of the locations provided to the constructor | Dependent on selected locations |
88+
89+
Note: "**POST Methods**" refers to the HTTP methods that send data in the request body, such as POST, PUT, PATCH and DELETE. Although sending data via some methods such as DELETE is not standard, it is supported by Flask and this library.
90+
91+
##### MultiSource Parameters
92+
Using the `MultiSource` parameter type, parameters can be accepted from any combination of `Parameter` subclasses. Example usage is as follows:
93+
94+
```py
95+
@app.route("/")
96+
@app.route("/<v>") # If accepting parameters by Route and another type, a path with and without that Route parameter must be specified
97+
@ValidateParameters()
98+
def multi_source_example(
99+
value: int = MultiSource(Route, Query, Json, min_int=0)
100+
)
101+
```
102+
103+
The above example will accept parameters passed to the route through Route, Query, and JSON Body.
104+
105+
106+
Note: "**POST Methods**" refers to the HTTP methods that send data in the request body, such as POST, PUT, PATCH and DELETE. Although sending data via some methods such as DELETE is not standard, it is supported by Flask and this library.
80107

81108
#### Type Hints and Accepted Input Types
82109
Type Hints allow for inline specification of the input type of a parameter. Some types are only available to certain `Parameter` subclasses.
83110

84-
| Type Hint / Expected Python Type | Notes | `Route` | `Form` | `Json` | `Query` | `File` |
85-
|-----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|---------|--------|--------|---------|--------|
86-
| `str` | | Y | Y | Y | Y | N |
87-
| `int` | | Y | Y | Y | Y | N |
88-
| `bool` | | Y | Y | Y | Y | N |
89-
| `float` | | Y | Y | Y | Y | N |
90-
| `list`/`typing.List` (`typing.List` is [deprecated](https://docs.python.org/3/library/typing.html#typing.List)) | For `Query` inputs, users can pass via either `value=1&value=2&value=3`, or `value=1,2,3`, both will be transformed to a `list`. | N | Y | Y | Y | N |
91-
| `typing.Union` | | Y | Y | Y | Y | N |
92-
| `typing.Optional` | | Y | Y | Y | Y | Y |
93-
| `datetime.datetime` | received as a `str` in ISO-8601 date-time format | Y | Y | Y | Y | N |
94-
| `datetime.date` | received as a `str` in ISO-8601 full-date format | Y | Y | Y | Y | N |
95-
| `datetime.time` | received as a `str` in ISO-8601 partial-time format | Y | Y | Y | Y | N |
96-
| `dict` | | N | N | Y | N | N |
97-
| `FileStorage` | | N | N | N | N | Y |
111+
| Type Hint / Expected Python Type | Notes | `Route` | `Form` | `Json` | `Query` | `File` |
112+
|------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|---------|--------|--------|---------|--------|
113+
| `str` | | Y | Y | Y | Y | N |
114+
| `int` | | Y | Y | Y | Y | N |
115+
| `bool` | | Y | Y | Y | Y | N |
116+
| `float` | | Y | Y | Y | Y | N |
117+
|`list`/`typing.List` (`typing.List` is [deprecated](https://docs.python.org/3/library/typing.html#typing.List)) | For `Query` and `Form` inputs, users can pass via either `value=1&value=2&value=3`, or `value=1,2,3`, both will be transformed to a `list`. | N | Y | Y | Y | N |
118+
| `typing.Union` | Cannot be used inside of `typing.List` | Y | Y | Y | Y | N |
119+
| `typing.Optional` | | Y | Y | Y | Y | Y |
120+
| `datetime.datetime` | Received as a `str` in ISO-8601 date-time format | Y | Y | Y | Y | N |
121+
| `datetime.date` | Received as a `str` in ISO-8601 full-date format | Y | Y | Y | Y | N |
122+
| `datetime.time` | Received as a `str` in ISO-8601 partial-time format | Y | Y | Y | Y | N |
123+
| `dict` | For `Query` and `Form` inputs, users should pass the stringified JSON | N | Y | Y | Y | N |
124+
| `FileStorage` | | N | N | N | N | Y |
98125

99126
These can be used in tandem to describe a parameter to validate: `parameter_name: type_hint = ParameterSubclass()`
100127
- `parameter_name`: The field name itself, such as username

0 commit comments

Comments
 (0)