Swagger yaml file items must be an object error

3.7k Views Asked by At

I converted my jsonschema that works in the script to YAML format for swagger documentation. The issue is that in line 8 (-type: string) swagger shows error: should be object, 'items' must be an object.

      type: object
      properties:
        answers:
          type: array
          minItems: 4
          maxItems: 4
          items:
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Yes', 'No']

Json schema

        "type": "object",
        "properties": {
            "answers": {
                "type": "array",
                "items": [
                    {
                        "type": "string",
                        "enum": ["Not really", "Slightly", "Significantly"]
                    },
                    {
                        "type": "string",
                        "enum": ["Not really", "Slightly", "Significantly"]
                    },
                    {
                        "type": "string",
                        "enum": ["Not really", "Slightly", "Significantly"]
                    },
                    {
                        "type": "string",
                        "enum": ["Yes", "No"]
                    },

                ],
                "minItems": 4,
                "maxItems": 4,
            }
        },
    }
1

There are 1 best solutions below

0
On

It because items should be an object (i.e. YAML mapping) but you give an array (i.e. YAML sequence).

You probably want to use prefixItems because you seem to want to validate the array as tuple:

        answers:
          type: array
          minItems: 4
          maxItems: 4
          prefixItems:
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Not really', 'Slightly', 'Significantly']
            - type: string
              enum: ['Yes', 'No']

See docs:

In Draft 4 - 2019-09, tuple validation was handled by an alternate form of the items keyword. When items was an array of schemas instead of a single schema, it behaved the way prefixItems behaves.