知识卡片

普通读书笔记卡 · 3460

Schema约束请求载荷

理解等级:初级

OpenAPI 的 Schema 描述请求或响应数据的字段、类型及约束,包括必填、枚举、默认值和数值下界。订单项中 product、size 必填,quantity 可省略且默认值为 1;实现必须在创建订单前检查字段。契约不仅列出字段名,也决定无效或缺失输入如何处理。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《Microservice APIs》第 2 章《A basic API implementation》(源文件:_epub-src/OEBPS/Text/02.htm) - 结论依据:2.1 节把数据模型称为 Schema,明确说明字段存在性与类型,并以订单项必填、可选及默认值示例要求实现先校验载荷。 - 原始内容:Schemas tell us what properties must be included in a payload and what their types are. For example, the OrderItemSchema schema specifies that the product and the size properties are required, but the quantity property is optional. When the quantity property is missing from the payload, the default value is 1 . Our API implementation must therefore enforce the presence of the product and the size properties in the payload before we try to create the order.