@ -243,26 +243,29 @@ If you want to use the exception along with the same default exception handlers
In this example you are just printing the error with a very expressive message, but you get the idea. You can use the exception and then just reuse the default exception handlers.
In this example you are just printing the error with a very expressive message, but you get the idea. You can use the exception and then just reuse the default exception handlers.
### Handling Validation for Path Parameters
### Validating Path Parameters
In real-world applications, you may need to validate input values beyond basic type checking.
FastAPI allows you to validate path parameters using Pydantic validators, similar to query parameter validation.
For example, ensuring that an ID is a positive number:
For example, to ensure an ID follows a specific format:
```python
```python
from fastapi import FastAPI, HTTPException
from typing import Annotated
from fastapi import FastAPI
from pydantic import AfterValidator
app = FastAPI()
app = FastAPI()
@app.get("/users/{user_id}")
def check_valid_id(id: str) -> str:
def get_user(user_id: int):
if not id.startswith(("isbn-", "imdb-")):
if user_id <= 0:
raise ValueError('Invalid ID format, it must start with "isbn-" or "imdb-"')