These are chat archives for hanami/chat

14th
Mar 2017
Sebastjan Hribar
@sebastjan-hribar
Mar 14 2017 04:30 UTC
@jodosha thank you for the confirmation.
Tobias Sandelius
@sandelius
Mar 14 2017 07:36 UTC
What's the "Hanami way" to handle unique constraints/validations of records?
Gabriel Gizotti
@gizotti
Mar 14 2017 07:41 UTC
@sandelius I would say define them in the constraints in the db
Tobias Sandelius
@sandelius
Mar 14 2017 07:48 UTC
@gizotti yeah but I ment providing the message to the client together with other validation messages.
Nicolò G.
@nickgnd
Mar 14 2017 11:32 UTC
@sandelius I've the same question. I want to validate email uniqueness on new user registration action and I wondering if there is an "Hanami way" for doing this.
My main doubt is where to put this validation: in the action or in the interactor which is in charge to persist the new user. Any suggestions about it?
I think I'm going to put it in the action with the other params validation for the moment. Probably I will create an inline custom predicate to check email uniqueness for returning useful error messages when validation fails.
@sandelius For custom predicates take a look also to dry-rb docs
Luca Guidi
@jodosha
Mar 14 2017 13:18 UTC
@sandelius @nickgnd I use interactors for ensure uniqueness constraint is respected.
Cẩm Huỳnh
@qcam
Mar 14 2017 14:59 UTC
Hi could I ask why ruby 2.2 is no longer supported in future version of Hanami?
Anton Davydov
@davydovanton
Mar 14 2017 15:01 UTC
@qcam yep :)
Cẩm Huỳnh
@qcam
Mar 14 2017 15:06 UTC
but it's still in ruby lang's support plan I suppose?
Anton Davydov
@davydovanton
Mar 14 2017 15:12 UTC
yes, of course
@qcam h, sorry. Why: if I remember right hanami, after 1.0, wants to create releases without breaking changes. And that's why hanami set only latest version of ruby and rack
of course, you can try to use 2.2 but I'm not sure that all will work correct
Cẩm Huỳnh
@qcam
Mar 14 2017 15:18 UTC
thanks! I see, I suppose it won't, since I couldn't download any version further than 0.6.0
I meant hanami-model particularly
thanks anyway for your answer
Nicolò G.
@nickgnd
Mar 14 2017 20:57 UTC

Thanks @jodosha for your reply :)

I use interactors for ensure uniqueness constraint is respected.

So you mean, that you have an interactor for ensuring uniqueness constraint and you use it your action's param validation, something like this? Thanks

class Signup
  include Hanami::Action

  predicate :unique?, message: 'Not unique' do |current|
    # call the interactor and return the result
  end

  params do
    required(:first_name).filled(:str?)
    required(:last_name).filled(:str?)
    required(:email).filled?(:str?, format?: /\A.+@.+\z/, :unique?)
    # ...
  end

  def call(params)
    halt 400 unless params.valid?
    # ...
  end
end