Arrays of enums as input #2894
-
I'm encountering a problem when trying to implement an array of an enum as an input on an endpoint. I have seen this discussion post and it almost answers this but not completely. This input generates the correct openapi spec status: z.array(zScanResultCondition).optional() GetV2WorkspacesParameterStatus:
type: array
items:
type: string
enum:
- COMPLIANT
- ALL_FIXED
- SOME_FIXED
- NONE_FIXED The issue is that this results in an error as the input comes in as a string from the query parameters instead as a array. The earlier discussion had a solution to this: z.string()
.transform( (str) => str.split(",") )
.pipe(
z.array(zScanResultCondition)
)
.optional() However this results in the input type being a string instead of the enum values that I would like. GetV2WorkspacesParameterStatus:
type: string I have found a solution to this, but it's not very elegant, it is to just make an enum of the string transform, and my array type: status: z.union([
z.string().transform(str => str.split(',') as z.infer<typeof zPullRequestStatus>[]),
z.array(zPullRequestStatus)
]).optional() And this works, except that it then has a union type as the input, when I would ideally like it to only be an array, as I'm using this spec to generate types elsewhere: GetV2WorkspacesParameterStatus:
anyOf:
- type: string
- type: array
items:
type: string
enum:
- EXPECTED
- MERGED
- CLOSED
- OPEN Is there any solution to this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
That's because you're using comma separator, which is not a conventional way to pass arrays via query string, @JacksonDahl2 .
|
Beta Was this translation helpful? Give feedback.
-
I'm going to make it configurable in #2922 |
Beta Was this translation helpful? Give feedback.
That's because you're using comma separator, which is not a conventional way to pass arrays via query string, @JacksonDahl2 .
Try this: