These are chat archives for dry-rb/chat

16th
Oct 2018
Alexander
@cutalion
Oct 16 2018 12:49
Hi, is there less "verbose" interface for custom rules in dry-validations?
Here is how I check "all or none" fields for location.
rule(location_provided: [:address, :latitude, :longitude]) do |address, latitude, longitude|
  (address.filled? & latitude.filled? & longitude.filled?) |
       (address.none? & latitude.none? & longitude.none?)
end

each attribute name repeated 4 times. Seems not readable.

I could extract internal code to some helper function like all_or_none(address, latitude, longitude)
but this will only reduce 1 mention of each name

Is it possible to use something like this?

rule(:location_provided) do |s| #s for schema
  all_or_none(s.address, s.latitude, s.longitude)
end
Alexander
@cutalion
Oct 16 2018 12:58
Or maybe something like macros for high-level rules
rule(location_provided: [:address, :latitude, :longitude]).all_or_none