FastAPI 允许你为参数声明额外的信息和更精细的校验方式。

    以上一章,查询参数的例子为基础,我们加入一些更精细的验证方式。

    1. from fastapi import FastAPI
    2. from typing import Optional
    3. @app.get("/books/")
    4. def books(book_type: str, limit: int = 1, size: int = 10, book_name: Optional[str] = None, sort: bool = False):
    5. return {
    6. "book_type": book_type,
    7. "book_name": book_name,
    8. "sort": sort,
    9. "limit": limit,
    10. "size": size
    11. }