0

In Uncle Bob's clean architecture, which Hanami is inspired by, Form Objects guard the boundary between Interactors and our delivery mechanism (typically an http endpoint).

In the Hanami docs, bounary guarding is done using params blocks in Actions (see here). This seems to couple validation to the http delivery mechanism. It seems more natural to me that Form Objects (or params black which accomplish the same thing) would live in delivery-mechanism-agnostic Interactors.

Unfortunately, I cannot figure out if Hanami supports such a design. I found a similar question on the Hanami forum, but it has no answer.

To clarify, below is a snippet of code demonstrating the kind of thing I want to do, but using Virtus and ActiveModel::Validations rather than Hanami facilities. For those familiar with Trailblazer, its contract block within its operations (its term for Interactor) is another example.

Finally, am I misunderstanding something about the intended use of Hanami? Hanami supports Interactors, so it seems like this should be possible...

require 'hanami/model'
require 'active_model'
require 'virtus'

class PersonForm
  include Virtus.model
  include ActiveModel::Validations

  attribute :name, String
  attribute :age, Integer

  validates :name, :age, presence: true

  def each
    attributes.each {|a| yield a}
  end
end

class Person
  include Hanami::Entity
  attributes :name, :age
end

# Code like this would then live inside of an Interactor, which
# can accept a params hash.

jonah_form = PersonForm.new({name: 'Jonah', age: '99'})
jonah = Person.new(jonah_form) if jonah_form.valid?
p jonah #=> <Person:0x007fbdde1edcc0 @id=nil @name="Jonah" @age=99>
# do stuff with our jonah entity
Jonah
  • 15,806
  • 22
  • 87
  • 161
  • So basically, what you want to do is have form objects that contain the validation. Then on, you want to be able to pass valid form objects to models and simply dump them in the database? You may edit the question to add further clarifications regarding this. – Noman Ur Rehman Feb 05 '16 at 12:07
  • Just a more general piece of advice, have you asked the Hanami Gitter? I've found it a very useful resource, the people on there tend to be helpful and know their Hanami. – Danny Santos Jun 05 '19 at 10:44

1 Answers1

1

sorry for missing this question.

The params act as validator to save developers to create and instantiate another class. In Ruby Community this is a problem.

DSL to the rescue for this compromise.

If you prefer to have a concrete form object, instead of using Virtus and ActiveModel, you can just include Hanami::Validations and have the same behavior.

Luca Guidi
  • 1,201
  • 10
  • 10