[dry-validation 1.6] change errors method name to output

Hi!

Newbie question: Can I change #errors method to #output without destroying dry-validation?

#output is a built-in methods, why would you replace it with another built-in method? Without thinking it through I’d say no, you cannot do it.

Thanks. I am migrating forms from dry-validation 0.11 to 1.6.
In app, we call #output on form after validation: form.call(data)form.output → receive hash object. However in 1.6 version I get NoMethodError on #output. Am I correct that in this case #output is equivalent to #to_h?

I got thing mixed up, actually #output isn’t defined for contracts so you can have an alias without risks. You can create a custom subclass for your contracts

class MyContract < Dry::Validation::Contract
  alias_method :output, :to_h
end
1 Like

Big thanks!

Hmm.@flash-gordon I think we don’t understand each other(probably because of my initial question). [dry-validation 1.6] When I do: form.call(data) form is validated, but the result is no longer an object of a class I defined(Dry::Validation::Contract::MyContract). It is now an object of Dry::Validation::Result class. I think I have to extend this class so I can call #output instead of #to_h in order to get a hash of data. I don’t want to manually change #output to #to_h in app I am working on.

Quite simple:

module Dry
  module Validation
    class Result
      def output
        self.to_h
      end
    end
  end
end

and it works as expected now. No need to change #output to #to_h