要求数组在 Swagger Schema 对象定义中至少包含一个元素 [英] Require array to contain at least one element in Swagger Schema Object definition

查看:12
本文介绍了要求数组在 Swagger Schema 对象定义中至少包含一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 swagger.yaml 中有这样的架构对象定义:

I'm having a Schema Object definition like this in my swagger.yaml:

User:
  type: object
  properties:
    username:
      type: string
      description: the user name
    colors:
      type: array
      items: {
        type: string,
        enum: [ "red", "blue", "green" ]
      }
      description: user must have one or more colors associated
  required:
    - username
    - colors

但是,生成的服务器仍然很乐意接受使用此架构对象作为不包含任何 colors 字段的必需主体参数的 POST 请求.

However, the generated server still happily accepts POST requests using this schema object as required body parameter that do not contain any colors field.

我能否以 color 字段在 User 架构对象中始终需要并且理想情况下还必须包含来自枚举的至少一个或多个项目的方式配置 Swagger?

Can I configure Swagger in a way that the color field is always required in a User schema object and ideally also must contain at least one or more items from the enum?

推荐答案

使用 minItems: 1.此外,您可以在数组中强制执行 uniqueItems.

Use minItems: 1. Additionally you can enforce uniqueItems within the array.

    colors:
      type: array
      minItems: 1
      uniqueItems: true
      items:
        type: string
        enum: [ "red", "blue", "green" ]

这篇关于要求数组在 Swagger Schema 对象定义中至少包含一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆