How to define an array-property as comma separated?

Viewed 623

How can I define an array object property, that contains several items in a comma separated string in openapi-v3?

I want to validate a request body (not query parameter!) like this:

{
    "friends": "Ann,Bob"
}

I am dreaming of an openApi v3 schema definition like this one:

"friends": {
    "type": "array",
    "items": {
        "type": "string",
        "enum": [
          "Ann",
          "Bob",
          "Charlie"
        ]
    },
    "commaSeparation": ",", // does not exist
}

Is there a officially supported way to describe such string contents? If not: What could be a workaround, that still precisely defines and validates those texts?

1 Answers

No, there is not. Unfortunately to the parser friends is and will always be a string.

You could add a pattern regex to enforce the contents, something like:

"friends": {
    "type": "string",
    "pattern": "[Ann|Bob|Charlie],+" // some regex that enforces the allowed tokens and a trailing comma
}

If you want a real array, then you need to use an array type.

Related