# Conditional validation using dry-system settings

**URL:** <https://discourse.dry-rb.org/t/conditional-validation-using-dry-system-settings/586>\
**Category:** Uncategorized\
**Created:** [July 16, 2018, 6:27pm UTC](https://discourse.dry-rb.org/t/conditional-validation-using-dry-system-settings/586 "2018-07-16T18:27:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmaze](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dmaze](https://discourse.dry-rb.org/u/dmaze)\
**Post date:** [July 16, 2018, 6:27pm UTC](https://discourse.dry-rb.org/t/conditional-validation-using-dry-system-settings/586/1 "2018-07-16T18:27:40Z")

</div>

I’m trying to configure a pair of related settings in my application with somewhat complicated interaction.

```
Application.boot(:settings, from: :system) do
  settings do
    key :option_a, Types::Strict::String.optional
    key :option_b, Types::Params.Bool.default(false)
  end
end

```

Either or both of `OPTION_A` and `OPTION_B` can be specified. If neither is specified, though, the application won’t work well. In a development environment I can provide a reasonable default, but it’s not a reasonable default in production. I’d sort of like to say something like

```
after(:start) do
  settings = target[:settings]
  if settings.option_a.nil? && !settings.option_b
    if env == :production
      # Is there a more idiomatic way to complain?
      raise 'either OPTION_A or OPTION_B must be specified'
    else
      # Can't overwrite the value in the dry-container
      target[:settings] = settings.new(option_b: true)
    end
  end
end

```

Any advice on the best way to do this sort of conditional, cross-option, environment-dependent option validation?

---

<div class="post-metadata">

**Author:** ![alexandru-calinoiu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alexandru-calinoiu/32/98_2.png) [@alexandru-calinoiu](https://discourse.dry-rb.org/u/alexandru-calinoiu)\
**Post date:** [July 23, 2018, 8:41am UTC](https://discourse.dry-rb.org/t/conditional-validation-using-dry-system-settings/586/2 "2018-07-23T08:41:05Z")

</div>

It almost sound like you want to implement a high level validation rule [http://dry-rb.org/gems/dry-validation/high-level-rules/](http://dry-rb.org/gems/dry-validation/high-level-rules/)

Maybe consider validating your setting with a schema at boot time?

---

<div class="post-metadata">

**Author:** ![dmaze](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dmaze](https://discourse.dry-rb.org/u/dmaze)\
**Post date:** [July 31, 2018, 4:09pm UTC](https://discourse.dry-rb.org/t/conditional-validation-using-dry-system-settings/586/3 "2018-07-31T16:09:53Z")

</div>

I like that idea and I could write a `dry-validation` filter that ran after the `dry-system` settings handling…but I still hit the problem that I can’t overwrite the `target[:settings]`. I don’t think there’s anywhere I get to hook in in between `dry-system` reading environment variables and environment files, and it producing the fully populated settings object and registering it in the `dry-container`. Is there?
