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?
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With