mirror of https://github.com/tiangolo/fastapi.git
📝 Move Query params - str validations to better name
This commit is contained in:
parent
1f5762703d
commit
d79534b015
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 84 KiB |
|
|
@ -3,7 +3,7 @@
|
||||||
Let's take this application as example:
|
Let's take this application as example:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial001.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
The query parameter `q` is of type `str`, and by default is `None`, so it is optional.
|
The query parameter `q` is of type `str`, and by default is `None`, so it is optional.
|
||||||
|
|
@ -18,7 +18,7 @@ We are going to enforce that even though `q` is optional, whenever it is provide
|
||||||
To achieve that, first import `Query` from `fastapi`:
|
To achieve that, first import `Query` from `fastapi`:
|
||||||
|
|
||||||
```Python hl_lines="1"
|
```Python hl_lines="1"
|
||||||
{!./tutorial/src/query-params-schema/tutorial002.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Use `Query` as the default value
|
## Use `Query` as the default value
|
||||||
|
|
@ -26,7 +26,7 @@ To achieve that, first import `Query` from `fastapi`:
|
||||||
And now use it as the default value of your parameter, setting the parameter `max_length` to 50:
|
And now use it as the default value of your parameter, setting the parameter `max_length` to 50:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial002.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
As we have to replace the default value `None` with `Query(None)`, the first parameter to `Query` serves the same purpose of defining that default value.
|
As we have to replace the default value `None` with `Query(None)`, the first parameter to `Query` serves the same purpose of defining that default value.
|
||||||
|
|
@ -59,7 +59,7 @@ This will validate the data, show a clear error when the data is not valid, and
|
||||||
You can also add a parameter `min_length`:
|
You can also add a parameter `min_length`:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial003.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Add regular expressions
|
## Add regular expressions
|
||||||
|
|
@ -67,7 +67,7 @@ You can also add a parameter `min_length`:
|
||||||
You can define a <abbr title="A regular expression, regex or regexp is a sequence of characters that define a search pattern for strings.">regular expression</abbr> that the parameter should match:
|
You can define a <abbr title="A regular expression, regex or regexp is a sequence of characters that define a search pattern for strings.">regular expression</abbr> that the parameter should match:
|
||||||
|
|
||||||
```Python hl_lines="8"
|
```Python hl_lines="8"
|
||||||
{!./tutorial/src/query-params-schema/tutorial004.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
This specific regular expression checks that the received parameter value:
|
This specific regular expression checks that the received parameter value:
|
||||||
|
|
@ -87,7 +87,7 @@ The same way that you can pass `None` as the first argument to be used as the de
|
||||||
Let's say that you want to declare the `q` query parameter to have a `min_length` of `3`, and to have a default value of `"fixedquery"`:
|
Let's say that you want to declare the `q` query parameter to have a `min_length` of `3`, and to have a default value of `"fixedquery"`:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial005.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! note
|
!!! note
|
||||||
|
|
@ -116,7 +116,7 @@ q: str = Query(None, min_length=3)
|
||||||
So, when you need to declare a value as required while using `Query`, you can use `...` as the first argument:
|
So, when you need to declare a value as required while using `Query`, you can use `...` as the first argument:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial006.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial006.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! info
|
!!! info
|
||||||
|
|
@ -133,13 +133,13 @@ That information will be included in the generated OpenAPI and used by the docum
|
||||||
You can add a `title`:
|
You can add a `title`:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial007.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial007.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
And a `description`:
|
And a `description`:
|
||||||
|
|
||||||
```Python hl_lines="11"
|
```Python hl_lines="11"
|
||||||
{!./tutorial/src/query-params-schema/tutorial008.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial008.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Alias parameters
|
## Alias parameters
|
||||||
|
|
@ -161,7 +161,7 @@ But you still need it to be exactly `item-query`...
|
||||||
Then you can declare an `alias`, and that alias is what will be used to find the parameter value:
|
Then you can declare an `alias`, and that alias is what will be used to find the parameter value:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/query-params-schema/tutorial009.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial009.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Deprecating parameters
|
## Deprecating parameters
|
||||||
|
|
@ -173,9 +173,9 @@ You have to leave it there a while because there are clients using it, but you w
|
||||||
Then pass the parameter `deprecated=True` to `Query`:
|
Then pass the parameter `deprecated=True` to `Query`:
|
||||||
|
|
||||||
```Python hl_lines="16"
|
```Python hl_lines="16"
|
||||||
{!./tutorial/src/query-params-schema/tutorial010.py!}
|
{!./tutorial/src/query-params-str-validations/tutorial010.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
The docs will show it like this:
|
The docs will show it like this:
|
||||||
|
|
||||||
<img src="/img/tutorial/query-params-schema/image01.png">
|
<img src="/img/tutorial/query-params-str-validations/image01.png">
|
||||||
Loading…
Reference in New Issue