You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/parameters.md
+49-5Lines changed: 49 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -19,13 +19,9 @@ app.run()
19
19
20
20
The first argument is the name of the parameter in the query string, **not the argument name**, and the second argument is the type that it should take.
21
21
22
-
!!! danger
23
-
24
-
view.py has not yet implemented type checking on parameters
25
-
26
22
## Body
27
23
28
-
Bodies work the exact same way, but with the `body` decorator instead:
24
+
Bodies work the exact same way as queries, but with the `body` decorator instead:
This is extremely buggy and not yet recommended for general use.
61
+
62
+
## Type Validation
63
+
64
+
view.py will ensure that the type sent to the server is compatible with what you passed to the decorator. For example:
65
+
66
+
```py
67
+
@app.get("/")
68
+
@query("number", int)
69
+
asyncdefindex(number: int):
70
+
# number will always be an int.
71
+
# if it isn't, an error 400 is sent back to the user automatically
72
+
return"..."
73
+
```
74
+
75
+
The following types are supported:
76
+
77
+
-`typing.Any`
78
+
-`str`
79
+
-`int`
80
+
-`bool`
81
+
-`float`
82
+
-`dict` (or `typing.Dict`)
83
+
-`None`
84
+
85
+
You can allow unions by just passing more parameters:
86
+
87
+
```py
88
+
@app.get('/hello')
89
+
@query("name", str, None)
90
+
asyncdefhello(name: str|None):
91
+
ifnot name:
92
+
return"hello world"
93
+
94
+
returnf"hello {name}"
95
+
```
96
+
97
+
You can pass type arguments to a `dict`, which are also validated by the server:
98
+
99
+
```py
100
+
@app.get("/something")
101
+
@body("data", dict[str, int]) # typing.Dict on 3.8 and 3.9
102
+
asyncdefsomething(data: dict[str, int]):
103
+
# data will always be a dictionary of strings and integers
104
+
return"..."
105
+
```
106
+
107
+
The key in a dictionary must always be `str` (i.e. `dict[int, str]` is not allowed), but the value can be any supported type (including other dictionaries!)
0 commit comments