Forum Discussion

nbo's avatar
nbo
Occasional Visitor
6 years ago

Have schemas with empty array

So i'm having a situation where a queue possibly has members. It's also posible that there is no members.

This is kind of how i want to solve this problem:

QueueMembers:
  type: object
  properties:
    members:
      oneOf:
        - type: array
          items:
            allOf:
              - $ref: '#/components/schemas/QueueMember'
        - type: array

The editor doesn't give me any errors, but the example schema shows this:


So, am i thinking of this thing wrongly?
Should people just accept that, if an array of object is a possible option, than it's also possible it's not there?

  • Hi nbo,

     

    Your definition can be simplified into:

     

    QueueMembers:
      type: object
      properties:
        members:
          type: array
          items:
            $ref: '#/components/schemas/QueueMember'

    Both empty and non-empty arrays will validate against this definition.

     

    To require an array to be non-empty, add the minItems: 1 attribute.

     

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi nbo,

     

    Your definition can be simplified into:

     

    QueueMembers:
      type: object
      properties:
        members:
          type: array
          items:
            $ref: '#/components/schemas/QueueMember'

    Both empty and non-empty arrays will validate against this definition.

     

    To require an array to be non-empty, add the minItems: 1 attribute.