Search code examples
yamlopenapiswagger-editor

OpenApi 3.0: How to define uniqueness of array elements based upon a key (property) in object that is used as an element schema for that array?


I am having following schema defined in 'components' section of my openApi 3.0:

schema1:
   required:
      - prop1
      - prop2
   properties:
      prop1
      prop2:
        enum:
         - option1
         - option2
      prop3

This Schema is being used to define array elements in another schema as:

schema2:
   required:
      - prop4
      - items
      - prop6
   properties:
      prop4
      prop5:
       type: array
       items:
         $ref: '#/components/schemas/schema1'
       maxItems: 2
      prop6

Now in prop5 of Schema 2, I want the elements of array having unique value of prop2. Any ideas how to achieve this.


Solution

  • This is not possible as of OpenAPI 3.1 and JSON Schema 2020-12. Currently such validations need to be implemented on the backend.


    Here's the corresponding feature request in one of the JSON Schema issue trackers:
    https://github.com/json-schema-org/json-schema-vocabularies/issues/22

    OpenAPI Schema is based on JSON Schema so any new schema keywords will come from JSON Schema.