知识卡片
判别字段辅助选分支
将 species 设为必填并用 discriminator.mapping 指向对应模式,读者及支持该特性的工具可更快定位分支。再用每分支的取值约束保证 oneOf 的验证结果,例如 Cat 分支限定 species 只能是 Cat。单靠映射和类型 string 难以避免普通验证器同时匹配两支。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Designing schemas with composition in OpenAPI 16.5 Adding discriminators in OpenAPI · 查看原书依据
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,
参考来源
- 位置:《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,