Check if type is an enum and list possible values


Long story short - let’s say i have an array of various dry types, one of them being enum type types_array = [Types::Params::String.default('red').enum('red', 'green', 'blue')].

  1. How can i check if given type has an enum defined for it? I’m talking about something like types_array[0].enum?. I’m currently using types_array[0].respond_to?(:values) but it feels a bit hacky
  2. How can i list possible values for given enum type? Something like types_array[0].enum_values this one is solved - i just have to do types_arra[0].values

If that’s something easy and documented - please forgive me. I spent a decent amount of time googling and going through your docs but without any luck sadly.


Dry Types mostly work by behavior not by identity. So there don’t tend to be a lot of predicates that tell you what it is prior to using it.

That said, when dry-types need to represent themselves abstractly, they use an s-expression syntax available from #to_ast.

In this specific case, you can do

Types = Dry.Types(default: :params)

module Types
  Color = String.default('red').enum('red', 'green', 'blue')

if T::Color.to_ast in [:enum, *]
  # do something