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

Viewed 1570

I have this api documentation written in 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

I have a Bear object that has id, and name properties. I want to define that id field is a unique field. How can I define this in OpenAPI 3?

1 Answers

OpenAPI lets you define a field's type and its expected format. But it's not possible for it to ensure its uniqueness.

Uniqueness is a business concern, which might involve non-trivial checks. Therefore, it's outside the scope of OpenAPI/Swagger to formalize such concepts.

However, you can add a description to explain the requirement that the field value must be unique, then implement necessary checks in the API to ensure that the value supplied by the client is actually unique.

Related