# \[Dry-Struct\] Array with conditional multiple members

**URL:** <https://discourse.dry-rb.org/t/dry-struct-array-with-conditional-multiple-members/718>\
**Category:** Support\
**Created:** [February 13, 2019, 7:26pm UTC](https://discourse.dry-rb.org/t/dry-struct-array-with-conditional-multiple-members/718 "2019-02-13T19:26:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![camilacampos](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/camilacampos/32/342_2.png) [@camilacampos](https://discourse.dry-rb.org/u/camilacampos)\
**Post date:** [February 13, 2019, 7:26pm UTC](https://discourse.dry-rb.org/t/dry-struct-array-with-conditional-multiple-members/718/1 "2019-02-13T19:26:42Z")

</div>

Is there a way to impose a Type based on some attribute?

For instance, I have some structs like these:

```ruby

class Base < Dry::Struct
  module Types
    include Dry::Types.module
  end
end

class Vehicle < Base
  attribute :value, Types::Coercible::Float
  attribute :manufacturing_year, Types::Coercible::Integer.meta(omittable: true)
  attribute :type, Types::String.default('vehicle')
end

class RealEstate < Base
  attribute :value, Types::Coercible::Float
  attribute :address, Types::String
  attribute :type, Types::String.default('real_estate')
end

class LoanApplication < Base
  attribute :collaterals, Types::Array.of(Vehicle | RealEstate)
end

```

When I cannot have a vehicle and a real estate:

```ruby
LoanApplication.new({
  collaterals: [
    {value: 4, manufacturing_year: 2019},
    {value: 4, address: '135 Courtney St.'}
  ]
}) # => Both collaterals are Vehicles

```

I can change the order `dry-struct` matches Vehicles or Real Estates here (and it works), but this is a simplified view of a more complex struct where I cannot simply change the order.

Is there a way to match a Struct based on a attribute value? That is, is there a way to know it is a vehicle when I use `type: 'vehicle'` as an attribute?

In a way that I would have something like the following:

```ruby
LoanApplication.new({
  collaterals: [
    {value: 4, manufacturing_year: 2019, type: 'vehicle'}, # this would be a Vehicle because of the `type: 'vehicle'`
    {value: 4, address: '135 Courtney St.', type: 'real_estate'} # this would be a Vehicle because of the `type: 'real_estate'`
  ]
})

```

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [February 13, 2019, 10:34pm UTC](https://discourse.dry-rb.org/t/dry-struct-array-with-conditional-multiple-members/718/2 "2019-02-13T22:34:11Z")

</div>

Yes, use `Type::Value`

```ruby
class Vehicle < Base
  attribute :value, Types::Coercible::Float
  attribute :manufacturing_year, Types::Coercible::Integer.meta(omittable: true)
  attribute :type, Types::Value('vehicle')
end

class RealEstate < Base
  attribute :value, Types::Coercible::Float
  attribute :address, Types::String
  attribute :type, Types::Value('real_estate')
end

```

---

<div class="post-metadata">

**Author:** ![camilacampos](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/camilacampos/32/342_2.png) [@camilacampos](https://discourse.dry-rb.org/u/camilacampos)\
**Post date:** [February 14, 2019, 1:45am UTC](https://discourse.dry-rb.org/t/dry-struct-array-with-conditional-multiple-members/718/3 "2019-02-14T01:45:41Z")

</div>

Yaaaay! It worked 😃 Thank you very much (:
