# Required fields based on other fields value - error message

**URL:** <https://discourse.dry-rb.org/t/required-fields-based-on-other-fields-value-error-message/304>\
**Category:** dry-validation\
**Created:** [July 7, 2017, 6:21am UTC](https://discourse.dry-rb.org/t/required-fields-based-on-other-fields-value-error-message/304 "2017-07-07T06:21:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![czerasz](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/czerasz/32/114_2.png) [@czerasz](https://discourse.dry-rb.org/u/czerasz)\
**Post date:** [July 7, 2017, 6:21am UTC](https://discourse.dry-rb.org/t/required-fields-based-on-other-fields-value-error-message/304/1 "2017-07-07T06:21:20Z")

</div>

I want to include, exclude fields based on the value of the `method` field.

Here is some pseudocode:

```
  required(:method).value(included_in?: ["a", "b"])

  if value(:method) == "a"
     required(:a).filled(:str?)
     optional(:b_1).filled(:none?)
     optional(:b_2).filled(:none?)
  else
     optional(:a).filled(:none?)
     required(:b_1).filled(:str?)
     required(:b_2).filled(:str?)
  end

```

I tired the following:

```
it {
  schema = Dry::Validation.Schema do
    required(:method).value(included_in?: ["a", "b"])
    optional(:a).filled(:str?)
    optional(:b_1).filled(:str?)
    optional(:b_2).filled(:str?)

    rule(a_rule: [:method, :a, :b_1, :b_2]) do |method, a, b_1, b_2|
      value(:method).format?(/^a$/).then(a.filled? & b_1.none? & b_2.none?)
    end

    # The same but different syntax
    rule(b_rule: [:method, :a, :b_1, :b_2]) do |method, a, b_1, b_2|
      method.format?(/^b$/) > (
        a.none? & b_1.filled? & b_2.filled?
      )
    end
  end

  errors = schema.call({
    method: "a",
  }).errors
  expect(errors).to eq({"a" => ["must be filled"]})
}

```

But I get the following error:

```
Failures:

  1) Validation invalid parameters custom should eq {"a"=>["must be filled"]}
     Failure/Error: expect(errors).to eq({"a" => ["must be filled"]})
     
       expected: {"a"=>["must be filled"]}
            got: {:a_rule=>["must be filled"]}
     
       (compared using ==)
     
       Diff:
       @@ -1,2 +1,2 @@
       -"a" => ["must be filled"],
       +:a_rule => ["must be filled"],

```

The error message is applied to the `a_rule` rule and not to the specific field.

Is there an option to achieve this differently?

---

<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:** [July 9, 2017, 8:57am UTC](https://discourse.dry-rb.org/t/required-fields-based-on-other-fields-value-error-message/304/2 "2017-07-09T08:57:43Z")

</div>

You could make them all optional and check their values based on the value of `method`.

---

<div class="post-metadata">

**Author:** ![czerasz](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/czerasz/32/114_2.png) [@czerasz](https://discourse.dry-rb.org/u/czerasz)\
**Post date:** [July 9, 2017, 10:30am UTC](https://discourse.dry-rb.org/t/required-fields-based-on-other-fields-value-error-message/304/3 "2017-07-09T10:30:00Z")

</div>

@solnic could you give me a code example?
