如何在OpenAPI 3.0中的对象中至少需要一个属性?

发布于 2025-02-03 21:37:44 字数 1352 浏览 1 评论 0原文

我有此OpenAPI架构:

components:
  schemas:
    User:
      type: object
      required:
        - username
        - email
        - description
        - profile_icon
      properties:
        username
          type: string
        email
          type: string
          format: email
        description
          type: string
        profile_icon
          type: string

所有属性都是必需的。这是put/user/profile

我将将其更改为补丁/用户/配置文件。 用户仅发送参数仅要求更改。 系统验证至少需要一个参数。

我如何描述需要一个[用户名,电子邮件,描述,profile_icon]?

可接受的示例请求:

{
  "username": "Will Smith"
}
{
  "email": "[email protected]",
  "profile_icon": "aaaaa.png"
}

不可接受的示例(错误):

{}

anyof注释器已接近,但似乎仅适用于$ ref schemas,而不是属性。

htttps://swagger.io/docs.io/docs/specification/data--data-------模型/单一单独/

I have this OpenAPI schema:

components:
  schemas:
    User:
      type: object
      required:
        - username
        - email
        - description
        - profile_icon
      properties:
        username
          type: string
        email
          type: string
          format: email
        description
          type: string
        profile_icon
          type: string

All of properties are required. This is for PUT /user/profile.

I will change this to PATCH /user/profile.
Users send parameters just they only request to change.
System validates that at least one parameter is required.

How can I describe one of [username, email, description, profile_icon] is required?

Acceptable example requests:

{
  "username": "Will Smith"
}
{
  "email": "[email protected]",
  "profile_icon": "aaaaa.png"
}

Not acceptable example (error):

{}

The anyOf annotator is close but it seems to be only for $ref schemas, not properties.

https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-02-10 21:37:44

在对象中至少需要1个属性的最简单方法是使用minproperties:1

    User:
      type: object
      minProperties: 1   # <--------
      properties:
        username:
          type: string
        ...

The easiest way to require at least 1 property in an object is to use minProperties: 1.

    User:
      type: object
      minProperties: 1   # <--------
      properties:
        username:
          type: string
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文