#Is there a way to obtain an array of the elements in an enum?
1 messages · Page 1 of 1 (latest)
This is not possible at the moment. Is it possible to share more details about your use-case to check if there's a way to achieve it?
I am attempting to parse user input provided to a REST API in the form of an array. I want to,
- Make sure the input is restricted to the enums in
Color - Have a way to identify instances where the user input contains all the possible values (in this case
[RED, BLUE])
Can you share the signature of the relevant parts of the resource method?
Sure. The payload looks something like this.
type AccessConfig record {|
AccessGroup[] accessGroups;
string noAccessReason?;
|};
AccessGroup is an enum defined as follows.
enum AccessGroup {
GROUP_A = "group-a",
GROUP_B = "group-b"
}
In the event the payload does not contain all of the access groups, I want to validate that the noAccessReason field is present in the payload.
Ideally, I would like to be able to add more "valid" groups simply by adding more elements to the AccessGroup enum without any modifications to other sections of the code.
I'm also noticing another issue with this approach now that I look at it. accessGroups will accept inputs like ["group-a", "group-a", "group-b"] as well, which shouldn't be the case. So I feel like maintaining an array ["group-a", "group-b"] and manually validating the payload against it might be a better alternative. 🤔