# How can I use an external dependency in a dynamic predicate argument?

**URL:** <https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818>\
**Category:** dry-validation\
**Created:** [July 1, 2019, 5:12pm UTC](https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818 "2019-07-01T17:12:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shepmaster](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/shepmaster/32/396_2.png) [@shepmaster](https://discourse.dry-rb.org/u/shepmaster)\
**Post date:** [July 1, 2019, 5:12pm UTC](https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818/1 "2019-07-01T17:12:19Z")

</div>

I’m using dry-validations 0.13.3 with Ruby 2.6.2. I’ve attempted to combine external dependencies with dynamic predicates:

```ruby
schema = Dry::Validation.Schema do
  configure do
    option :my_collection

    def part_of_collection
      my_collection.values
    end
  end

  required(:the_thing).value(included_in?: part_of_collection)
end

schema.with(my_collection: { alpha: 1, beta: 2 }).call({ the_thing: 42 })

```

Unfortunately, it looks like `my_collection` always returns `nil`:

```nohighlight
NoMethodError: undefined method `value' for nil:NilClass
from repro.rb:6:in `part_of_collection'

```

It does work to access the external dependency in a custom predicate:

```ruby
schema = Dry::Validation.Schema do
  configure do
    option :my_collection

    def part_of_collection?(v)
      my_collection.values.include? v
    end
  end

  required(:the_thing).value(:part_of_collection?)
end

schema.with(my_collection: { alpha: 1, beta: 2 }).call({ the_thing: 42 })

```

Unfortunately, this requires me to define my own messages for my custom predicate:

```nohighlight
Dry::Validation::MissingMessageError: message for part_of_collection? was not found

```

Is there a trick I’m missing?

---

<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:** [July 2, 2019, 11:13am UTC](https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818/2 "2019-07-02T11:13:20Z")

</div>

It is not possible to pass a dynamic list of allowed values to `included_in?` but it’s not specific to dry-validation, it’s how ruby works. For that, you’ll need one more level of indirection as you have in the second example where invocation of `:part_of_collection?` is postponed until validation is run. In any case, this type of rules were removed in dry-validation 1.0 and replaced with a more powerful and flexible approach, I suggest you upgrading to 1.x.

---

<div class="post-metadata">

**Author:** ![shepmaster](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/shepmaster/32/396_2.png) [@shepmaster](https://discourse.dry-rb.org/u/shepmaster)\
**Post date:** [July 2, 2019, 1:50pm UTC](https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818/3 "2019-07-02T13:50:48Z")

</div>

> [@flash-gordon](#):
>
> It is not possible to pass a dynamic list of allowed values to `included_in?`

Could you expand on this? This is literally an example from the documentation section titled [Dynamic Predicate Arguments](https://dry-rb.org/gems/dry-validation/0.13/dynamic-predicate-arguments/):

```ruby
DataSchema = Dry::Validation.Schema do
  configure do
    def data
      %w(a b c)
    end
  end

  required(:letter).filled(included_in?: data)
end

```

> [@flash-gordon](#):
>
> I suggest you upgrading to 1.x.

I’d love to, but we are currently attempting to move from Reform to Dry, but our existing usage of Reform requires Dry 0.13. Once we have replaced Reform with Dry, then we can upgrade.

> [@flash-gordon](#):
>
> replaced with a more powerful and flexible approach

Could you give an example (or point to some docs) for how that would be done in 1.0? It will be good to know for when we get there.

---

<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:** [July 3, 2019, 8:49am UTC](https://discourse.dry-rb.org/t/how-can-i-use-an-external-dependency-in-a-dynamic-predicate-argument/818/4 "2019-07-03T08:49:14Z")

</div>

> Could you expand on this? This is literally an example from the documentation section titled [Dynamic Predicate Arguments](https://dry-rb.org/gems/dry-validation/0.13/dynamic-predicate-arguments/):

Well, good point and mb dry-v 0.x adds indirection there automatically, never tried it so honestly don’t know what’s the cause of the error.

> Could you give an example (or point to some docs) for how that would be done in 1.0? It will be good to know for when we get there.

Normally, via a contract option + a rule:

```ruby
Class.new(Dry::Validation::Contract) do
  option :my_collection

  schema do
    required(:the_thing).value(:integer)
  end

  rule(:the_thing) do
    key.failure('not in collection') unless my_collection.include?(value)
  end
end

```
