Message path for rule with very nested params

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:

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:

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

Am I missing something?

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

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

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

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

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

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

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

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.

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 :confused: 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? :pray:

1 Like

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 :smiley:
Will report this issue)

1 Like

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.

1 Like