Skip to content

Commit 9ec452a

Browse files
authored
📝 Update docs for Query Params and String Validations, remove obsolete Ellipsis docs (...) (fastapi#13377)
1 parent 2c937aa commit 9ec452a

17 files changed

+20
-294
lines changed

docs/de/docs/tutorial/query-params-str-validations.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,6 @@ Wenn Sie einen Parameter erforderlich machen wollen, während Sie `Query` verwen
315315

316316
{* ../../docs_src/query_params_str_validations/tutorial006_an_py39.py hl[9] *}
317317

318-
### Erforderlich mit Ellipse (`...`)
319-
320-
Es gibt eine Alternative, die explizit deklariert, dass ein Wert erforderlich ist. Sie können als Default das <abbr title='Zeichenfolge, die einen Wert direkt darstellt, etwa 1, "hallowelt", True, None'>Literal</abbr> `...` setzen:
321-
322-
{* ../../docs_src/query_params_str_validations/tutorial006b_an_py39.py hl[9] *}
323-
324-
/// info
325-
326-
Falls Sie das `...` bisher noch nicht gesehen haben: Es ist ein spezieller einzelner Wert, <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">Teil von Python und wird „Ellipsis“ genannt</a> (Deutsch: Ellipse).
327-
328-
Es wird von Pydantic und FastAPI verwendet, um explizit zu deklarieren, dass ein Wert erforderlich ist.
329-
330-
///
331-
332-
Dies wird **FastAPI** wissen lassen, dass dieser Parameter erforderlich ist.
333-
334318
### Erforderlich, kann `None` sein
335319

336320
Sie können deklarieren, dass ein Parameter `None` akzeptiert, aber dennoch erforderlich ist. Das zwingt Clients, den Wert zu senden, selbst wenn er `None` ist.

docs/em/docs/tutorial/query-params-str-validations.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,6 @@ q: Union[str, None] = Query(default=None, min_length=3)
148148

149149
{* ../../docs_src/query_params_str_validations/tutorial006.py hl[7] *}
150150

151-
### ✔ ⏮️ ❕ (`...`)
152-
153-
📤 🎛 🌌 🎯 📣 👈 💲 ✔. 👆 💪 ⚒ `default` 🔢 🔑 💲 `...`:
154-
155-
{* ../../docs_src/query_params_str_validations/tutorial006b.py hl[7] *}
156-
157-
/// info
158-
159-
🚥 👆 🚫 👀 👈 `...` ⏭: ⚫️ 🎁 👁 💲, ⚫️ <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">🍕 🐍 &amp; 🤙 "❕"</a>.
160-
161-
⚫️ ⚙️ Pydantic &amp; FastAPI 🎯 📣 👈 💲 ✔.
162-
163-
///
164-
165-
👉 🔜 ➡️ **FastAPI** 💭 👈 👉 🔢 ✔.
166-
167151
### ✔ ⏮️ `None`
168152

169153
👆 💪 📣 👈 🔢 💪 🚫 `None`, ✋️ 👈 ⚫️ ✔. 👉 🔜 ⚡ 👩‍💻 📨 💲, 🚥 💲 `None`.
@@ -178,18 +162,6 @@ Pydantic, ❔ ⚫️❔ 🏋️ 🌐 💽 🔬 &amp; 🛠️ FastAPI, ✔️
178162

179163
///
180164

181-
### ⚙️ Pydantic `Required` ↩️ ❕ (`...`)
182-
183-
🚥 👆 💭 😬 ⚙️ `...`, 👆 💪 🗄 &amp; ⚙️ `Required` ⚪️➡️ Pydantic:
184-
185-
{* ../../docs_src/query_params_str_validations/tutorial006d.py hl[2,8] *}
186-
187-
/// tip
188-
189-
💭 👈 🌅 💼, 🕐❔ 🕳 🚚, 👆 💪 🎯 🚫 `default` 🔢, 👆 🛎 🚫 ✔️ ⚙️ `...` 🚫 `Required`.
190-
191-
///
192-
193165
## 🔢 🔢 📇 / 💗 💲
194166

195167
🕐❔ 👆 🔬 🔢 🔢 🎯 ⏮️ `Query` 👆 💪 📣 ⚫️ 📨 📇 💲, ⚖️ 🙆‍♀ 🎏 🌌, 📨 💗 💲.

docs/en/docs/tutorial/query-params-str-validations.md

Lines changed: 15 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Let's take this application as example:
66

77
{* ../../docs_src/query_params_str_validations/tutorial001_py310.py hl[7] *}
88

9-
The query parameter `q` is of type `Union[str, None]` (or `str | None` in Python 3.10), that means that it's of type `str` but could also be `None`, and indeed, the default value is `None`, so FastAPI will know it's not required.
9+
The query parameter `q` is of type `str | None`, that means that it's of type `str` but could also be `None`, and indeed, the default value is `None`, so FastAPI will know it's not required.
1010

1111
/// note
1212

1313
FastAPI will know that the value of `q` is not required because of the default value `= None`.
1414

15-
The `Union` in `Union[str, None]` will allow your editor to give you better support and detect errors.
15+
Having `str | None` will allow your editor to give you better support and detect errors.
1616

1717
///
1818

@@ -25,29 +25,9 @@ We are going to enforce that even though `q` is optional, whenever it is provide
2525
To achieve that, first import:
2626

2727
* `Query` from `fastapi`
28-
* `Annotated` from `typing` (or from `typing_extensions` in Python below 3.9)
28+
* `Annotated` from `typing`
2929

30-
//// tab | Python 3.10+
31-
32-
In Python 3.9 or above, `Annotated` is part of the standard library, so you can import it from `typing`.
33-
34-
```Python hl_lines="1 3"
35-
{!> ../../docs_src/query_params_str_validations/tutorial002_an_py310.py!}
36-
```
37-
38-
////
39-
40-
//// tab | Python 3.8+
41-
42-
In versions of Python below Python 3.9 you import `Annotated` from `typing_extensions`.
43-
44-
It will already be installed with FastAPI.
45-
46-
```Python hl_lines="3-4"
47-
{!> ../../docs_src/query_params_str_validations/tutorial002_an.py!}
48-
```
49-
50-
////
30+
{* ../../docs_src/query_params_str_validations/tutorial002_an_py310.py hl[1,3] *}
5131

5232
/// info
5333

@@ -145,54 +125,23 @@ As in this case (without using `Annotated`) we have to replace the default value
145125

146126
So:
147127

148-
```Python
149-
q: Union[str, None] = Query(default=None)
150-
```
151-
152-
...makes the parameter optional, with a default value of `None`, the same as:
153-
154-
```Python
155-
q: Union[str, None] = None
156-
```
157-
158-
And in Python 3.10 and above:
159-
160128
```Python
161129
q: str | None = Query(default=None)
162130
```
163131

164132
...makes the parameter optional, with a default value of `None`, the same as:
165133

166-
```Python
167-
q: str | None = None
168-
```
169-
170-
But the `Query` versions declare it explicitly as being a query parameter.
171-
172-
/// info
173-
174-
Keep in mind that the most important part to make a parameter optional is the part:
175134

176135
```Python
177-
= None
178-
```
179-
180-
or the:
181-
182-
```Python
183-
= Query(default=None)
136+
q: str | None = None
184137
```
185138

186-
as it will use that `None` as the default value, and that way make the parameter **not required**.
187-
188-
The `Union[str, None]` part allows your editor to provide better support, but it is not what tells FastAPI that this parameter is not required.
189-
190-
///
139+
But the `Query` version declares it explicitly as being a query parameter.
191140

192141
Then, we can pass more parameters to `Query`. In this case, the `max_length` parameter that applies to strings:
193142

194143
```Python
195-
q: Union[str, None] = Query(default=None, max_length=50)
144+
q: str | None = Query(default=None, max_length=50)
196145
```
197146

198147
This will validate the data, show a clear error when the data is not valid, and document the parameter in the OpenAPI schema *path operation*.
@@ -201,7 +150,7 @@ This will validate the data, show a clear error when the data is not valid, and
201150

202151
Keep in mind that when using `Query` inside of `Annotated` you cannot use the `default` parameter for `Query`.
203152

204-
Instead use the actual default value of the function parameter. Otherwise, it would be inconsistent.
153+
Instead, use the actual default value of the function parameter. Otherwise, it would be inconsistent.
205154

206155
For example, this is not allowed:
207156

@@ -255,7 +204,7 @@ This specific regular expression pattern checks that the received parameter valu
255204

256205
If you feel lost with all these **"regular expression"** ideas, don't worry. They are a hard topic for many people. You can still do a lot of stuff without needing regular expressions yet.
257206

258-
But whenever you need them and go and learn them, know that you can already use them directly in **FastAPI**.
207+
Now you know that whenever you need them you can use them in **FastAPI**.
259208

260209
### Pydantic v1 `regex` instead of `pattern`
261210

@@ -296,23 +245,15 @@ q: str
296245
instead of:
297246

298247
```Python
299-
q: Union[str, None] = None
248+
q: str | None = None
300249
```
301250

302251
But we are now declaring it with `Query`, for example like:
303252

304253
//// tab | Annotated
305254

306255
```Python
307-
q: Annotated[Union[str, None], Query(min_length=3)] = None
308-
```
309-
310-
////
311-
312-
//// tab | non-Annotated
313-
314-
```Python
315-
q: Union[str, None] = Query(default=None, min_length=3)
256+
q: Annotated[str | None, Query(min_length=3)] = None
316257
```
317258

318259
////
@@ -321,42 +262,14 @@ So, when you need to declare a value as required while using `Query`, you can si
321262

322263
{* ../../docs_src/query_params_str_validations/tutorial006_an_py39.py hl[9] *}
323264

324-
### Required with Ellipsis (`...`)
325-
326-
There's an alternative way to explicitly declare that a value is required. You can set the default to the literal value `...`:
327-
328-
{* ../../docs_src/query_params_str_validations/tutorial006b_an_py39.py hl[9] *}
329-
330-
/// info
331-
332-
If you hadn't seen that `...` before: it is a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">part of Python and is called "Ellipsis"</a>.
333-
334-
It is used by Pydantic and FastAPI to explicitly declare that a value is required.
335-
336-
///
337-
338-
This will let **FastAPI** know that this parameter is required.
339-
340265
### Required, can be `None`
341266

342267
You can declare that a parameter can accept `None`, but that it's still required. This would force clients to send a value, even if the value is `None`.
343268

344-
To do that, you can declare that `None` is a valid type but still use `...` as the default:
269+
To do that, you can declare that `None` is a valid type but simply do not declare a default value:
345270

346271
{* ../../docs_src/query_params_str_validations/tutorial006c_an_py310.py hl[9] *}
347272

348-
/// tip
349-
350-
Pydantic, which is what powers all the data validation and serialization in FastAPI, has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://docs.pydantic.dev/2.3/usage/models/#required-optional-fields" class="external-link" target="_blank">Required fields</a>.
351-
352-
///
353-
354-
/// tip
355-
356-
Remember that in most of the cases, when something is required, you can simply omit the default, so you normally don't have to use `...`.
357-
358-
///
359-
360273
## Query parameter list / multiple values
361274

362275
When you define a query parameter explicitly with `Query` you can also declare it to receive a list of values, or said in another way, to receive multiple values.
@@ -396,7 +309,7 @@ The interactive API docs will update accordingly, to allow multiple values:
396309

397310
### Query parameter list / multiple values with defaults
398311

399-
And you can also define a default `list` of values if none are provided:
312+
You can also define a default `list` of values if none are provided:
400313

401314
{* ../../docs_src/query_params_str_validations/tutorial012_an_py39.py hl[9] *}
402315

@@ -419,15 +332,15 @@ the default of `q` will be: `["foo", "bar"]` and your response will be:
419332

420333
#### Using just `list`
421334

422-
You can also use `list` directly instead of `List[str]` (or `list[str]` in Python 3.9+):
335+
You can also use `list` directly instead of `list[str]`:
423336

424337
{* ../../docs_src/query_params_str_validations/tutorial013_an_py39.py hl[9] *}
425338

426339
/// note
427340

428341
Keep in mind that in this case, FastAPI won't check the contents of the list.
429342

430-
For example, `List[int]` would check (and document) that the contents of the list are integers. But `list` alone wouldn't.
343+
For example, `list[int]` would check (and document) that the contents of the list are integers. But `list` alone wouldn't.
431344

432345
///
433346

docs/es/docs/tutorial/query-params-str-validations.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,6 @@ Así que, cuando necesites declarar un valor como requerido mientras usas `Query
321321

322322
{* ../../docs_src/query_params_str_validations/tutorial006_an_py39.py hl[9] *}
323323

324-
### Requerido con Puntos suspensivos (`...`)
325-
326-
Hay una manera alternativa de declarar explícitamente que un valor es requerido. Puedes establecer el valor por defecto al valor literal `...`:
327-
328-
{* ../../docs_src/query_params_str_validations/tutorial006b_an_py39.py hl[9] *}
329-
330-
/// info | Información
331-
332-
Si no habías visto eso `...` antes: es un valor especial único, es <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">parte de Python y se llama "Ellipsis"</a>.
333-
334-
Se usa por Pydantic y FastAPI para declarar explícitamente que un valor es requerido.
335-
336-
///
337-
338-
Esto le permitirá a **FastAPI** saber que este parámetro es requerido.
339-
340324
### Requerido, puede ser `None`
341325

342326
Puedes declarar que un parámetro puede aceptar `None`, pero que aún así es requerido. Esto obligaría a los clientes a enviar un valor, incluso si el valor es `None`.

docs/ru/docs/tutorial/query-params-str-validations.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,6 @@ q: Union[str, None] = Query(default=None, min_length=3)
291291

292292
{* ../../docs_src/query_params_str_validations/tutorial006_an_py39.py hl[9] *}
293293

294-
### Обязательный параметр с Ellipsis (`...`)
295-
296-
Альтернативный способ указать обязательность параметра запроса - это указать параметр `default` через многоточие `...`:
297-
298-
{* ../../docs_src/query_params_str_validations/tutorial006b_an_py39.py hl[9] *}
299-
300-
/// info | Дополнительная информация
301-
302-
Если вы ранее не сталкивались с `...`: это специальное значение, <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">часть языка Python и называется "Ellipsis"</a>.
303-
304-
Используется в Pydantic и FastAPI для определения, что значение требуется обязательно.
305-
306-
///
307-
308-
Таким образом, **FastAPI** определяет, что параметр является обязательным.
309-
310294
### Обязательный параметр с `None`
311295

312296
Вы можете определить, что параметр может принимать `None`, но всё ещё является обязательным. Это может потребоваться для того, чтобы пользователи явно указали параметр, даже если его значение будет `None`.
@@ -321,18 +305,6 @@ Pydantic, мощь которого используется в FastAPI для
321305

322306
///
323307

324-
### Использование Pydantic's `Required` вместо Ellipsis (`...`)
325-
326-
Если вас смущает `...`, вы можете использовать `Required` из Pydantic:
327-
328-
{* ../../docs_src/query_params_str_validations/tutorial006d_an_py39.py hl[4,10] *}
329-
330-
/// tip | Подсказка
331-
332-
Запомните, когда вам необходимо объявить query-параметр обязательным, вы можете просто не указывать параметр `default`. Таким образом, вам редко придётся использовать `...` или `Required`.
333-
334-
///
335-
336308
## Множество значений для query-параметра
337309

338310
Для query-параметра `Query` можно указать, что он принимает список значений (множество значений).

docs/zh/docs/tutorial/query-params-str-validations.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,6 @@ q: Union[str, None] = Query(default=None, min_length=3)
108108

109109
{* ../../docs_src/query_params_str_validations/tutorial006.py hl[7] *}
110110

111-
### 使用省略号(`...`)声明必需参数
112-
113-
有另一种方法可以显式的声明一个值是必需的,即将默认参数的默认值设为 `...`
114-
115-
{* ../../docs_src/query_params_str_validations/tutorial006b.py hl[7] *}
116-
117-
/// info
118-
119-
如果你之前没见过 `...` 这种用法:它是一个特殊的单独值,它是 <a href="https://docs.python.org/zh-cn/3/library/constants.html#Ellipsis" class="external-link" target="_blank">Python 的一部分并且被称为“Ellipsis”(意为省略号 —— 译者注)</a>。
120-
Pydantic 和 FastAPI 使用它来显式的声明需要一个值。
121-
122-
///
123-
124-
这将使 **FastAPI** 知道此查询参数是必需的。
125-
126111
### 使用`None`声明必需参数
127112

128113
你可以声明一个参数可以接收`None`值,但它仍然是必需的。这将强制客户端发送一个值,即使该值是`None`
@@ -137,18 +122,6 @@ Pydantic 是 FastAPI 中所有数据验证和序列化的核心,当你在没
137122

138123
///
139124

140-
### 使用Pydantic中的`Required`代替省略号(`...`)
141-
142-
如果你觉得使用 `...` 不舒服,你也可以从 Pydantic 导入并使用 `Required`
143-
144-
{* ../../docs_src/query_params_str_validations/tutorial006d.py hl[2,8] *}
145-
146-
/// tip
147-
148-
请记住,在大多数情况下,当你需要某些东西时,可以简单地省略 `default` 参数,因此你通常不必使用 `...``Required`
149-
150-
///
151-
152125
## 查询参数列表 / 多个值
153126

154127
当你使用 `Query` 显式地定义查询参数时,你还可以声明它去接收一组值,或换句话来说,接收多个值。

0 commit comments

Comments
 (0)