These are chat archives for dry-rb/chat

4th
Oct 2016
Maciej Mensfeld
@mensfeld
Oct 04 2016 10:21
@solnic don't want to rush but any noews on that? dry-rb/dry-validation#266 ;) I want to be up 2 date
Egor Lynko
@flexoid
Oct 04 2016 14:50
is it possible to have multiple high-level rules on the same field? (to have errors output by the same field)
I want to implement something like this:
schema = Dry::Validation.JSON do
  optional(:alarm).filled { bool? }
  optional(:alarm_in).maybe { int? }

  rule(alarm_in: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(true).then(alarm_in.filled?)
    alarm.eql?(false).then(alarm_in.none?)
  end
end

schema.call(alarm: true).messages
# => { alarm_in: ["must be filled"] }
schema.call(alarm: false).messages
# => {}
Egor Lynko
@flexoid
Oct 04 2016 15:07
Found a solution by reading commits: looks like when rule is named with "?" in the end, the last dependency field will be used as message key, instead of rule name itself
So I can write:
rule(alarm_in_present?: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(true).then(alarm_in.filled?)
end
rule(alarm_in_absent?: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(false).then(alarm_in.none?)
end
Piotr Solnica
@solnic
Oct 04 2016 15:34
@mensfeld i'm on holidays until Monday