Using error message variables in custom predicates

Hi All,

I’m trying to achieve the same result as https://github.com/dry-rb/dry-validation/issues/179, which is report on unsupported keys in the provided (JSON API) parameters.

I’m using the latest (unreleased version) of Reform gem which supports input parameter validation and which makes the provided input parameters available as input and the supported parameters via the ‘form’ variable.

This means I can easily build this check using

Dry::Validation.Schema do 
  configure do |config|

    def self.messages
      super.merge(en: { errors: {only_defined_keys_present?: "non-supported key is provided" }  })
    end

    def only_defined_keys_present?(input)
     ( input.keys - form.class.definitions.keys ).empty?
    end
  end

  input(:hash?,:only_defined_keys_present?)
end   

However this will return an Array :

    ['non-supported key is provided']

which is not so user-friendly. I’m searching for a way to have the non-supported keys listed in the error message, like

  [ "non-supported keys key_1,key_2,....  are provided"] 

I didn’t find anything in the documentation, but perhaps I missed it?

So far I found

  • dry-validation-0.9.5/lib/dry/validation/message_compiler.rb#101 def message_text : this routines tried to replace variables in the error message template
  • dry-validation-0.9.5/lib/dry/validation/message_compiler.rb#35 def visit_predicate : gets these variables from the ‘node’ which seems to be ast version of the custom predicate
  • from the rspec tests : the custom predicate input parameters are added to the node/ast so are available as error message templates

Support for this will be added before 1.0.0.