如何在对象中定义字段在openapi中是唯一的?

How to define a field in an object is unique in openapi?

我有这个 api 文档是用 OpenAPI 3.0.3 编写的

openapi: 3.0.3
info:
  version: '1.0'
  title: 'MyTitle'
  description: Specification for Bear Store

servers:
  - url: https://development.example.com/v1
    description: Development Server

paths:
  '/v1/bears':
    get:
      description: Requests a lists all the bears
      summary: List of bears request
      responses:
        '200':
          description: List of Bears
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: string
                      format: uuid
                    name:
                      type: string

我有一个具有 idname 属性的 Bear 对象。我想定义 id 字段是一个唯一字段。我如何在 OpenAPI 3 中定义它?

OpenAPI 允许您定义字段的类型及其预期格式。但是不能保证它的唯一性。

唯一性是一个业务问题,可能涉及重要的检查。因此,将此类概念形式化不在 OpenAPI/Swagger 的范围内。

但是,您可以添加描述来解释字段值必须唯一的要求,然后在API中进行必要的检查以确保该值客户提供的实际上是唯一的。