知识卡片

普通读书笔记卡 · 3462.c

pydantic映射契约约束

理解等级:中级

pydantic 模型可把 API Schema 落到 Python 类型:模型继承 BaseModel,字段用类型提示,枚举约束可选值,受限整数与列表约束下界。接口实现应逐项核对规范中的必填、默认值和范围;写了模型类并不代表契约自动完整覆盖。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《Microservice APIs》第 2 章《A basic API implementation》(源文件:_epub-src/OEBPS/Text/02.htm) - 结论依据:2.4 节把订单 Schema 实现为继承 BaseModel 的类,逐项使用类型提示、枚举、conint、conlist 来表达原规范的类型和范围。 - 原始内容:We define every schema as a class that inherits from pydantic’s BaseModel class , and we specify the type of every attribute using Python type hints. For attributes that can only take on a limited selection of values, we define an enumeration class. ... Finally, we use pydantic’s conlist type to define CreateOrderSchema ’s order property as a list with at least one element.