FastAPI系列:路径参数额外校验Path

路径参数额外校验Path

from  fastapi  import Path

app =  FastAPI()

@app.get('/items/{item_id}')
async  def read_items(item_id: str = Path(default=None, max_length=3, min_length=1, title='the id of item to get')):
    """
    default: 默认值,如果路径参数没有提供,将使用此值。
    alias: 别名,用于在OpenAPI和JSON Schema中替换原始参数名。
    title: 在OpenAPI和JSON Schema中的标题。
    description: 在OpenAPI和JSON Schema中的描述。
    gt, ge, lt, le: 用于数值验证,分别表示大于、大于等于、小于、小于等于。
    min_length, max_length: 用于字符串长度验证,分别表示最小长度和最大长度。
    regex: 正则表达式,用于字符串格式验证。
    example: 示例值,用于在OpenAPI和JSON Schema中展示。
    examples: 多个示例值,用于在OpenAPI和JSON Schema中展示。
    deprecated: 如果为True,表示此参数在OpenAPI和JSON Schema中已被弃用。
    include_in_schema: 如果为False,此参数将不会在OpenAPI和JSON Schema中显示。
    extra: 额外的关键字参数,将直接传递给Pydantic模型字段
    """
    results = {"item_id": item_id}
    return results
posted @ 2024-02-28 17:12  我在路上回头看  阅读(26)  评论(0编辑  收藏  举报