How do I create a contract with required array of enum?

How do for events check that every item in the array is a part of an enum?

class Contract < Dry::Validation::Contract
schema do
required(:events).array(:string)
end
end

Pass your Enum type instead of :string

module T
  include Dry::Types(default: :strict)
  MyEnum = String.enum("foo", "bar", "baz")
end

class Contract < Dry::Validation::Contract
  schema do
    required(:events).array(T::MyEnum)
  end
end

If you want to add additional constraints, you can also write

required(:events).value(array[T::MyEnum], gt?: 2)
2 Likes