Swagger: describe string array where elements are subset of a specific set of values

时间:2015-09-14 15:36:37

标签: swagger

I have the following JSON:

{ "animals": ["tiger", "bear"] }

The animals array can be any subset of the set

["tiger", "lion", "elephant", "wolf", "fox", "bear", "zebra"]

but cannot include any other elements. The following syntax is limited because I lose all the info about the possible values the elements can have:

{
    "description":"wild animals",
    "type": "array",
    "items": {
        "type": "string"
    }
}

Is there any way to describe this? Or should I just document it in the description?

Thanks.

1 个答案:

答案 0 :(得分:0)

You could define an enum for it. So your definition will look like the following

"WildAnimals": {
    "description":"wild animals",
    "type": "array",
    "items": {
        "$ref": "#/definitions/Animal"
    }
},
"Animal": {
    "type": "string",
    "enum": ["tiger", "lion", "elephant", "wolf", "fox", "bear", "zebra"]
}