This commit is contained in:
Ibrahim Pelumi Lasisi 2025-12-16 13:08:42 -08:00 committed by GitHub
commit 26001c07d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View File

@ -20,6 +20,13 @@ As it is discouraged, the interactive docs with Swagger UI won't show the docume
## Import Pydantic's `BaseModel` { #import-pydantics-basemodel }
/// note
`BaseModel` is the main class from Pydantic used for creating data models.
FastAPI needs this import to validate request bodies and generate documentation.
///
First, you need to import `BaseModel` from `pydantic`:
{* ../../docs_src/body/tutorial001_py310.py hl[2] *}

View File

@ -166,6 +166,40 @@ Because the available values for the *path parameter* are predefined, the intera
<img src="/img/tutorial/path-params/image03.png">
### Common Validation Errors with Enums { #common-validation-errors-with-enums }
When using an `Enum` for a path parameter, FastAPI will validate the value automatically.
For example, if your `ModelName` enum only allows:
- `"alexnet"`
- `"resnet"`
- `"lenet"`
And the client sends an invalid value:
```
GET /models/vgg16
```
FastAPI returns a clear validation error:
```
{
"detail": [
{
"type": "enum",
"loc": ["path", "model_name"],
"msg": "Input should be 'alexnet', 'resnet' or 'lenet'",
"input": "vgg16"
}
]
}
```
This helps catch invalid requests before they reach your logic and keeps your API behavior predictable.
### Working with Python *enumerations* { #working-with-python-enumerations }
The value of the *path parameter* will be an *enumeration member*.