Forum Discussion

cscott952's avatar
cscott952
New Contributor
6 years ago
Solved

Required parameters from multiple schemas

Hello, I'm trying to enforce required parameters in both the 'check_single' and 'check_set' below. The required parameters work when passing when they match the 'check_single', but I haven't been s...
  • RonRatovsky's avatar
    6 years ago

    The use of `anyOf` means that the input would need to match one (or more) of the sub-schemas described in it.

     

    The problem is that you have `anyOf` with two different types - `object` and `array`. The `required` and `properties` are at the same level of the `anyOf` - that means that they would only be applied `check_single` as its type is `object`. When you put type `array` alongside `required` or `properties`, the latter two would have no impact as they have no meaning with tpe `array`.

     

    So what can you do to solve this?

     

    Take the 'single' object definition and extract it to a model. For our purpose, let's call it `MyObject`.

     

    So:

     

    MyObject:
      type: object
      required: [...]
      properties: ...

     

     

    And then you can define `check` as this:

     

    check:
      anyOf:
        - $ref: '#/components/schemas/MyObject'
        - type: array
          items:
    $ref: '#/components/schemas/MyObject

     

    In this case, you may want to use `oneOf` instead, because there will never be case where a compared input will have both.