如何在OpenAPI 3.0+中定义支持多选枚举值的Schema
在OpenAPI 3.0+中定义多选枚举数组的正确方式
要实现「值为预设列表中一个或多个值的数组」(即多选枚举),你需要把数组类型声明、元素枚举限制和非空校验结合起来,具体写法如下:
基础实现(允许重复选择)
components: schemas: IngestRules: type: array # 确保数组至少包含1个元素,符合需求 minItems: 1 items: type: string # 列出所有允许的预设值 enum: - rule_a - rule_b - rule_c - rule_d
进阶实现(禁止重复选择)
如果需要避免同一个值被重复选入数组,可以加上uniqueItems: true:
components: schemas: IngestRules: type: array minItems: 1 # 可选配置:确保数组内元素不重复 uniqueItems: true items: type: string enum: - rule_a - rule_b - rule_c - rule_d
关键部分说明
type: array:明确字段为数组类型minItems: 1:强制数组不能为空,满足「包含一个及以上可选值」的要求items.enum:限定数组中每个元素只能是预设列表里的取值,实现枚举约束
内容的提问来源于stack exchange,提问作者Steve G
相关产品推荐
相关产品推荐

