Dredd Validation of OpenAPI v3 Arrays
Dredd is a useful tool for validating arrays, but its behavior in regards to collections takes a bit of getting used to. When you first start validating OpenAPI docs using Dredd, you expect arrays to be automatically validated using a normal definition such as the one below:
type: array
items:
type: object
required:
- id
- name
- text
properties:
id:
type: integer
example: 1
name:
type: string
example: foobar
text:
type: string
example: "foo bar baz ipsum"
However, it may not be apparent, but it won't verify any of the array items. It will just validate that there is an array. To validate the individual items you need to set the minItems
property for the array. If you set the minItems
property to greater than 1,
then it will validate the structure of the items in the array as well if you set the required
properties on the object as shown below.
type: array
minItems: 1
items:
type: object
required:
- id
- name
- text
properties:
id:
type: integer
example: 1
name:
type: string
example: foobar
text:
type: string
example: "foo bar baz ipsum"
There are other properties, as well, and ways to use arrays as shown in the docs. Of particular interest are: uniqueItems
to specify all unique items, oneOf
to specify multiple types, and {}
to specify any type for the array.