These are chat archives for dry-rb/chat

28th
May 2018
Andy Holland
@AMHOL
May 28 2018 05:52
Never used them personally, I think the lexical scope issue makes them confusing and next to useless
Alexander
@cutalion
May 28 2018 08:10

ha-ha, many people will find dry-rb solutions confusing :)
For instance, thin object API's (::Kernel.binding.pry instead of binding.pry)

I used refinements few times. But nowadays nobody (except rails?) wants to patch ruby's std classes, so refinements rarely needed.

But I think it's better to use them than patch std classes if you want to make a nice dsl like 2.kilobytes.

In my opinion it's good alternative for monkey patching
Sam Starling
@samstarling
May 28 2018 14:35
Hey – am I allowed to ask questions about how to achieve something using dry-validation here? I think I'm just not quite understanding the docs correctly
Gustavo Caso
@GustavoCaso
May 28 2018 14:36
Of course you can ask
Sam Starling
@samstarling
May 28 2018 14:39
Just didn't know if this was meta chat for developers of the libraries or contributors!
I'm trying to work out how to have a rule that is mandatory if a certain field has a certain value: so if the type is set to DEPOSIT, how can I ensure that amount is a :number??
I tried something like this, but it didn't work:
required(:type).filled.when(:true?) do
  value(:amount).filled(:number?)
end

And also this:

required(:type).filled
optional(:amount).filled

rule(amount_present: [:type, :amount]) do |type, amount|
  type.eql?('DEPOSIT') > amount.filled? > amount.number?
end

But that doesn't fail when the type is DEPOSIT and amount is excluded. So I probably misunderstood something somewhere :)

Gustavo Caso
@GustavoCaso
May 28 2018 14:58
Could you paste your schema?
Sam Starling
@samstarling
May 28 2018 15:02
Sure:
TransactionSchema = Dry::Validation.Form do
  required(:type).filled
  optional(:amount).filled(:number?)
  rule(amount_present: [:type, :amount]) do |type, amount|
    type.eql?('DEPOSIT') > amount.filled? > amount.number?
  end
end
Roland Laurès
@ShamoX
May 28 2018 15:11
Hi, I'm using dry-validator on complexe structures with nested data inside Arrays. I wanted to know if it's possible to get the array index of a failing rule ? Isn't it be a good feature if it doesn't exist ? (worth an issue ? :D )
In fact getting the whole errors trace each time, would be great.
Gustavo Caso
@GustavoCaso
May 28 2018 15:16
@samstarling I have this script working
So if the type is DEPOSIT the amount must be present
I'm not a heavy user of dry-validations so I do not know if it is the best way to achieve it
require 'bundler/inline'

gemfile true do
  source 'https://rubygems.org'
  gem 'dry-validation'
  gem 'pry'
end

TransactionSchema = Dry::Validation.Form do
  configure do
    def self.messages
      super.merge(en: { errors: { amount_present: 'if type is DEPOSIT amount must be present' }})
    end
  end

  required(:type).filled
  optional(:amount).filled(:number?)

  validate(amount_present: [:type, :amount]) do |type, amount|
    if type.eql?('DEPOSIT')
      !amount.nil?
    else
      true
    end
  end
end

payload = {
  'type' => 'DEPOSIT'
}

payload2 = {
  'type' => 'OTHER'
}


result = TransactionSchema.call(payload)
puts result.success? # => false
puts result.errors # => {:amount_present=>["if type is DEPOSIT amount must be present"]}

result2 = TransactionSchema.call(payload2)
puts result2.success? # => true
puts result2.errors # => {}
Gustavo Caso
@GustavoCaso
May 28 2018 15:21
@ShamoX doesn't each macro or schema provide the index of it
Sam Starling
@samstarling
May 28 2018 15:21
@GustavoCaso Ah OK, thanks! Let me check that :)
Roland Laurès
@ShamoX
May 28 2018 15:22
@GustavoCaso This is what I expected, but it seems I'm in a case where the schema validation inside my each is not called... I'm still investigated. Then I guess my call was prematurate.
Gustavo Caso
@GustavoCaso
May 28 2018 15:23
@samstarling let me know if it helps
Same @ShamoX
Roland Laurès
@ShamoX
May 28 2018 15:24
yes, thanks.
Roland Laurès
@ShamoX
May 28 2018 15:52
@GustavoCaso is this working with the Dry::Validation.Form constructor ? Because this is what I use. I don't use Schema like in the example. Also I'm using an existing Schema (also made with Form), is that a problem ?
I'll try to make a short example where I reproduce the bug (or find my error)
Gustavo Caso
@GustavoCaso
May 28 2018 16:16
Great share when you have it