# Message path for rule with very nested params

**URL:** <https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257>\
**Category:** dry-validation\
**Created:** [May 5, 2021, 5:02pm UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257 "2021-05-05T17:02:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleksandra-tomilina](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aleksandra-tomilina/32/644_2.png) [@aleksandra-tomilina](https://discourse.dry-rb.org/u/aleksandra-tomilina)\
**Post date:** [May 5, 2021, 5:02pm UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/1 "2021-05-05T17:02:23Z")

</div>

Hey!  
Need help with adding messages to rules for nested params.  
I’m getting this error `Dry::Validation::MissingMessageError: Message template for :invalid under "data.attributes.phone_number.phone_number_prefix" was not found` for such schema:

```auto
params do
  required(:data).hash do
    required(:type).filled(:string)
    required(:attributes).hash do
      ....
      required(:phone_number).maybe(:string)
      required(:phone_number_prefix).maybe(:string)
    end
  end
  .....
end

rule(data: {attributes: [:phone_number, :phone_number_prefix]}) do
  phone_number = ....

  key.failure(:invalid) unless phone_number.valid?
end

```

I tried different nesting of keys in `en.yml`, but nothing works. This is the last edit:

```auto
en:
  dry_validation:
    errors:
      rules:
        data:
          attributes:
            phone_number:
              phone_number_prefix:
                invalid: 'is of invalid format'

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![drqCode](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/drqcode/32/223_2.png) [@drqCode](https://discourse.dry-rb.org/u/drqCode)\
**Post date:** [May 6, 2021, 5:00am UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/2 "2021-05-06T05:00:57Z")

</div>

Firstly, I would have taken out format specific validation (the envelope), then validate only the attributes with another contract

---

<div class="post-metadata">

**Author:** ![aleksandra-tomilina](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aleksandra-tomilina/32/644_2.png) [@aleksandra-tomilina](https://discourse.dry-rb.org/u/aleksandra-tomilina)\
**Post date:** [May 6, 2021, 11:24am UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/3 "2021-05-06T11:24:56Z")

</div>

managed to do it like this:  
(schema remains the same)  
1)

```auto
  rule('data.attributes.phone_number') do
    phone_number = ...

    key.failure(:invalid_format) unless phone_number.valid?
  end

```

1. 

added separate \*.yml file with dry-validation error messages  
3)  
added config to ApplicationContract:

```auto
config.messages.backend = :i18n
config.messages.load_paths << "config/locales/*.yml"

```

1. 

`config/locales/*.yml` structure looks like this:

```auto
en:
  dry_validation:
    errors:
      rules:
        data:
          attributes:
            phone_number:
              invalid_format: "is of invalid format"

      ...here go other standard dry-schema errors...

```

The main problem was that I thought that I NEED to nest parameters in rule “name”, but actually I can put anything I need there in any structure so that my error messages have specific paths afterwards. And we can get `values` anyway in the rule, so all needed parameters are available in the rule block.

---

<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:** [May 7, 2021, 8:37am UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/4 "2021-05-07T08:37:54Z")

</div>

> The main problem was that I thought that I NEED to nest parameters in rule “name”

@aleksandra-tomilina so, given the error you got it’s no surprise this is what you thought 😕 I think this is a bug, in a way. The error message shouldn’t say `data.attributes.phone_number.phone_number_prefix`, so it’s worth addressing because I know people may waste a lot of time trying to get stuff working.

Could you please report this and link to this thread? 🙏

---

<div class="post-metadata">

**Author:** ![aleksandra-tomilina](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aleksandra-tomilina/32/644_2.png) [@aleksandra-tomilina](https://discourse.dry-rb.org/u/aleksandra-tomilina)\
**Post date:** [May 11, 2021, 3:35pm UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/5 "2021-05-11T15:35:37Z")

</div>

Yes, sure. The example of yml, file I provided was one of the theories I tried, but yes, the error message definitely added things I should try to debug list 😃  
Will report this issue)

---

<div class="post-metadata">

**Author:** ![aleksandra-tomilina](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aleksandra-tomilina/32/644_2.png) [@aleksandra-tomilina](https://discourse.dry-rb.org/u/aleksandra-tomilina)\
**Post date:** [May 13, 2021, 11:39am UTC](https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/6 "2021-05-13T11:39:54Z")

</div>

I created a bug report and just added link to this discussion. If I can link it in some other way - ping me, I’ll do that.

> <https://github.com/dry-rb/dry-validation/issues/685>
>
> \## Describe the bug
> The error message from dry-validation for incorrect key nes…ting in en.yml file for rule with nested params is giving wrong path.
> 
> \## To Reproduce
> Consider having such schema and some rule depending on several nested parameters.
> \`\`\`
> params do
> required(:data).hash do
> required(:type).filled(:string)
> required(:attributes).hash do
> ....
> required(:phone\_number).maybe(:string)
> required(:phone\_number\_prefix).maybe(:string)
> end
> end
> .....
> end
> 
> rule(data: {attributes: \[:phone\_number, :phone\_number\_prefix\]}) do
> phone\_number = ....
> 
> key.failure(:invalid) unless phone\_number.valid?
> end
> \`\`\`
> I tried different nesting of keys in en.yml, but nothing worked. This was the last edit:
> \`\`\`
> en:
> dry\_validation:
> errors:
> rules:
> data:
> attributes:
> phone\_number:
> phone\_number\_prefix:
> invalid: 'is of invalid format'
> \`\`\`
> 
> Which as many other tries was giving this error
> \`Dry::Validation::MissingMessageError: Message template for :invalid under "data.attributes.phone\_number.phone\_number\_prefix" was not found for such schema\`
> 
> \## Expected behavior
> 
> Error message with valid path in yml file for such rule or which says that rule name should be written in another way. 
> 
> Discussion started here:
> \[https://discourse.dry-rb.org/t/message-path-for-rule-with-very-nested-params/1257/3\](url)
