Coerced params values not available in controller


#1

I found Hanami when I first discovered dry-rb and ROM a couple months ago. dry-validations in particular was specifically the answer to problems I was attempting to deal with. I’ve recently started a fairly major project using Hanami and as I began building some of my first controllers, I wanted access to the coerced results of the validated params, specifically (from the context of a controller instance):

params is a Hanami::Validations::Form and has an instance variable of @result which is an instance of Dry::Validation::Result.

The public interface, unless I missed something in my code diving, does not expose @result directly nor is a there a good way (so far as I can tell) of getting to @result.output which returns the coerced values I’m looking for. For the time being, I’ve resorted to adding this to my controllers:

# NOTE: Haxx!
def validated_params
  params.instance_variable_get(:@result)&.output
end

Calling params.validate does return an instance of Dry::Validation::Result, but… that would appear to run the validations a second time, which happens at initialization already. (I could be wrong about this assumption, but I am trying to digest a lot of new code right now.)

  1. Is there a reason @result isn’t exposed? (Or, if it is, where?)
  2. Rather than accessing @result what I actually want is @result.output. Is there a better way than what is clearly a hack on my part?

Thanks in advance if anyone can either point me in the right direction or perhaps point out that I’m thinking about something all wrong in the first place!