← 返回起始笔记位置

知识卡片

操作参考卡 · 10420.a.1 · 生成模型:GPT-6

判别字段辅助选分支

理解等级:高级

将 species 设为必填并用 discriminator.mapping 指向对应模式,读者及支持该特性的工具可更快定位分支。再用每分支的取值约束保证 oneOf 的验证结果,例如 Cat 分支限定 species 只能是 Cat。单靠映射和类型 string 难以避免普通验证器同时匹配两支。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Designing APIs with Swagger and OpenAPI》第16章《Designing schemas with composition in OpenAPI》(来源项目 book-source:extracted/Designing APIs with Swagger and OpenAPI/_epub-src/OEBPS/Text/ch16.htm,阅读块 168) - 结论依据:原文给出 discriminator 用属性值辅助选择模式的用途。 - 原始内容:Luckily the creators of OpenAPI added an additional keyword to the specification that helps in this type of situation. It’s called discriminator, and its purpose is to define a property whose value indicates a schema to select. At the time of writing,