Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an array-property as comma separated?

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?

like image 826
slartidan Avatar asked Feb 22 '21 07:02

slartidan


People also ask

How do you make a comma-separated from an array?

The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.

How do you get a comma-separated string from an array in C?

How to get a comma separated string from an array in C#? We can get a comma-separated string from an array using String. Join() method. In the same way, we can get a comma-separated string from the integer array.

How do you add a comma to a string array?

Join Together an Array as a String with Commas. When you need to join together every item in a JavaScript array to form a comma-separated list of values, use . join(",") or equivalently just . join() without any arguments at all.


Video Answer


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.

like image 104
stringy05 Avatar answered Nov 21 '22 13:11

stringy05