首页 > 其他分享 >[FastAPI-18]Filed请求体校验

[FastAPI-18]Filed请求体校验

时间:2023-03-24 11:01:12浏览次数:36  
标签:item FastAPI price Filed Field 18 import tags

import random

from fastapi import FastAPI
from pydantic import Field, BaseModel
import typing

app = FastAPI()

'''
请求体的每一个字段需要单独校验
name 长度最少3位
price 不少于10
tags 元素不重复,元素个数
{
    "name": "book",
    "description": "python",
    "price": 42.0,
    "tax": 3.2,
    "tags": ["rock","metal","bar"]
}
'''


class ItemTags(BaseModel):
    name: str = Field(min_length=3, example="Tom")
    description: str
    price: float = Field(ge=10, example=18)
    tax: float
    tags: typing.List[str] = Field(min_items=3, max_items=5, unique_items=True)
    # 默认随机值
    discount: int = Field(default_factory=lambda: random.randint(1, 9))


@app.post("/item")
def create_item(item: ItemTags):
    return item

标签:item,FastAPI,price,Filed,Field,18,import,tags
From: https://www.cnblogs.com/leoshi/p/17250749.html

相关文章

  • 2018-D
    2018-D新建数据库test0317,目录为考试目录,并在完成建表后备份1、建表:use[test0317];createtable[STD_INFO]( [std_id]intnotnullprimarykey, [std_name]v......
  • [FastAPI-14]pydantic多个请求体
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''多个请求体{"user":{"username":"string","password":"string"......
  • [FastAPI-13]pydantic请求体接收数据
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''创建继承BaseModel的类,定义模型user路径函数中定义形参user,类型为User通过对象use......
  • [FastAPI-11]Query参数校验
    importtypingfromfastapiimportFastAPI,Queryapp=FastAPI()'''查询参数使用Query校验类似路由转换使用Path校验物品名称最小3位,最大10位default=None参数......
  • 【坚持每日一题10.18】回文排列
    给定一个字符串,编写一个函数判定其是否为某个回文串的排列之一。回文串是指正反两个方向都一样的单词或短语。排列是指字母的重新排列。回文串不一定是字典当中的单词。示例......
  • P4221 [WC2018]州区划分 题解
    题目链接题目描述给出\(n\)个城市,\(m\)条边,一个划分合法当且仅当所有划分中的点集和集合中点之间存在的边集所构成的图不构成欧拉回路且联通。定义一个点集的值为......
  • FastAPI: 极速开发Python Web应用的未来之星
    我在工作中经常使用Flask来开发Web应用。但是随着项目规模的增长,我发现自己需要写越来越多的重复代码,同时Flask并没有提供一个良好的数据验证和文档生成工具。有一天,我听说......
  • 代码随想录Day7-Leetcode454. 四数相加 II,383. 赎金信 ,15. 三数之和 ,18. 四数之和
    454.四数相加II这个第一时间没想出来怎么做的;后面看了题解才发现可以两两分组;绝了/***@param{number[]}nums1*@param{number[]}nums2*@param{number[......
  • [FastAPI-08]Path校验
    fromfastapiimportFastAPI,Pathapp=FastAPI()#Path校验'''限制接口输入的数字大小限制100-1000限制字符串输入的字符数量3-8位'''@app.get("/number/{nu......
  • 变形课 HDU - 1181 (dfs)
    题意:给定多个单词,每个单词的首字母可以到末字母,问能否由'b'到'm'。分析:将每个单词首尾字母建图,dfs('b')将能到的所有字母进行标记,最后检查'm'是否被标记。#include......