#Is there a way to obtain an array of the elements in an enum?

1 messages · Page 1 of 1 (latest)

prisma sedge
#

For example, if I have an enum like,

enum Color {
    RED = "Red",
    BLUE = "Blue"
}

I want to retrieve an array [RED, BLUE].

pseudo burrow
#

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?

prisma sedge
#

I am attempting to parse user input provided to a REST API in the form of an array. I want to,

  1. Make sure the input is restricted to the enums in Color
  2. Have a way to identify instances where the user input contains all the possible values (in this case [RED, BLUE])
pseudo burrow
#

Can you share the signature of the relevant parts of the resource method?

prisma sedge
#

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. 🤔