# Possible to add an optional validation based on if another optional value is true?

**URL:** <https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635>\
**Category:** dry-validation\
**Created:** [October 3, 2018, 11:27pm UTC](https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635 "2018-10-03T23:27:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkolath](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dkolath/32/307_2.png) [@dkolath](https://discourse.dry-rb.org/u/dkolath)\
**Post date:** [October 3, 2018, 11:27pm UTC](https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635/1 "2018-10-03T23:27:38Z")

</div>

If an optional value is true, I want to add another optional value to the schema. The following doesn’t work as intended. If I pass

`{ setting_is_allowed: true, setting: 200 }`

setting is not returned in the output. I was trying to build a rule, but I couldn’t find enough examples to piece together the exact scenario I am dealing with.

```
optional(:setting_is_allowed).maybe.when(:true?) do
  optional(:setting).maybe {none? | int?}
end
```

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [October 5, 2018, 7:46am UTC](https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635/2 "2018-10-05T07:46:14Z")

</div>

So, you need to check if `setting` **key** is present or not?

---

<div class="post-metadata">

**Author:** ![AlexTua](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@AlexTua](https://discourse.dry-rb.org/u/AlexTua)\
**Post date:** [October 2, 2019, 2:29pm UTC](https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635/3 "2019-10-02T14:29:05Z")

</div>

@solnic, hi, I didn’t find answer anywhere. My question is very similar to this one.

```ruby
define do
  optional(:lat).filled(:float) 
  optional(:lng).filled(:float)
end

```

I have schema above(I use only dry-schema). This 2 params optional, but if at least 1 of them present, second is also required. How can I implement this? Thanks.

---

<div class="post-metadata">

**Author:** ![Minty-Fresh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/minty-fresh/32/411_2.png) [@Minty-Fresh](https://discourse.dry-rb.org/u/Minty-Fresh)\
**Post date:** [October 2, 2019, 5:32pm UTC](https://discourse.dry-rb.org/t/possible-to-add-an-optional-validation-based-on-if-another-optional-value-is-true/635/4 "2019-10-02T17:32:18Z")

</div>

If I had to guess, this probably falls more into the realms of dry-validation than it doesn’t into dry-schema.

Hence, defining some dry-validation contract that looks like:

```ruby
class SomeContract < Dry::Validation::Contract
  params do
    optional(:lat).filled(:float)
    optional(:lng).filled(:float)
  end

  rule(:lat, :lng) do
    key(:lat).failure(:filled?) if values[:lng] && !values[:lat]
    key(:lng).failure(:filled?) if values[:lat] && !values[:lng]
  end
end

```

Aside:  
I do think that having dependent fields might make sense to define at schema level.
