These are chat archives for dry-rb/chat

8th
Jan 2018
Chris Richards
@cmrichards
Jan 08 2018 11:03
Is Factory Girl a good technique to create dB objects use during testing with a new app that will use dry-validation heavily when creating records from form data? Or would it be better to create servicey-type objects and only use them to setup initial DB data in tests?
Piotr Solnica
@solnic
Jan 08 2018 12:10
@cmrichards whatever works, we use rom-factory but it's not as mature as FactoryBot (reminder: they renamed the gem)
Chris Richards
@cmrichards
Jan 08 2018 12:15
ok. I thought you'd use the same service objects you create in your app to make the objects required in your tests. e.g. CreateCompany.({...}).
Chris Richards
@cmrichards
Jan 08 2018 12:29
My concern with using something like factory-bot is that it's a third place you need to maintain the idea of what a valid object is: tests, normal-code, and factory-bot.
Chris Richards
@cmrichards
Jan 08 2018 14:07
The functional style of dry-validation::Form is great. The way simple_form and rails renders forms using the form-object/AM-interface is also great. I want some way of living in both worlds, but im not sure if it is possible
@solnic how do you render forms? Do you just pass in a hash to the form renderer rather than any kind of form object?
Chris Richards
@cmrichards
Jan 08 2018 14:49
So it seems like this can't be placed in a Service superclass: include Dry::Matcher.for(:call, with: Dry::Matcher::EitherMatcher)
Piotr Solnica
@solnic
Jan 08 2018 15:13
@cmrichards we use an in-house gem called formalist
Chris Richards
@cmrichards
Jan 08 2018 15:13
Strange. I have to turn rails' params into a hash before the string will be accepted as an integer:
(byebug) params[:company_branch]
<ActionController::Parameters {"company_id"=>"9", "name"=>"", "address"=>"", "post_code"=>"", "phone"=>""} permitted: true>
(byebug)  CreateCompanyBranch.new.call(params[:company_branch])
Failure({:company_id=>["must be an integer"], :name=>["must be filled"]})
(byebug)  CreateCompanyBranch.new.call(params[:company_branch].to_h)
Failure({:name=>["must be filled"]})
Scott Eckenrode
@selectport
Jan 08 2018 17:38
@cmrichards - assuming you're on rails 5 now - params now returns a ActionController::Parameters object which is not hash-like itself
roma
@milushov
Jan 08 2018 19:29

Hey guys, mb somebody already asked, but I did not find; so can u help me find information about:

How to add custom variable to the error message?

en:
  errors:
    minimum_amount?: "amount must be greteat or equal to %{minimum}, yours is %{value}"

I have the value, but what is the best way to pass minimum variable to the err message?

Pablo Herrero
@pabloh
Jan 08 2018 22:47

Sorry, an idea why this works:

class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

class MainForm < Form
  define! do
    required(:qux).filled(:int?)

    optional(:bar).maybe do
      schema(SubForm.new) # Using new
    end
  end
end

but this doesn't:

class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

class MainForm < Form
  define! do
    required(:qux).filled(:int?)

    optional(:bar).maybe do
      schema(SubForm) # Using class directly
    end
  end
end

?

Aaron Barthel
@abrthel
Jan 08 2018 23:06
@pabloh a couple of questions, what's Form and what are you trying to do with these schemas, are you just trying to be able to reuse them or are you trying to create some form of base schema that other schemas can build off of?
Aaron Barthel
@abrthel
Jan 08 2018 23:23
I'm just going to take a leap of faith and assume you're most likely just looking for reusable validation schemas. Hopefully that's the case :smile:
SubForm = Dry::Validation.Form do
  required(:foo).filled(:int?)
  optional(:bar).maybe(:int?)
end

MainForm = Dry::Validation.Form do
  required(:qux).filled(:int?)
  optional(:bar).maybe do
    schema(SubForm)
  end
end