How do i run custom predicate on passed value itself?

#1

Input is an array of zone objects - hashes with name and other fields. Zone itself must match certain schema but i want to check if all required zones were passed.

REQUIRED_ZONES = ["name", "date", "number"]

ZonesArraySchema = Dry::Validation.Schema do
  configure do
    def includes_required_zones?(zones)
      filled_zones = zones.pluck(:name) & REQUIRED_ZONES
      filled_zones.sort == REQUIRED_ZONES.sort
    end
  end

  includes_required_zones? # Perform it on array itself 

  each do
    schema(Schemas::Zone)
  end
end

I could perform this predicate one level up but i want it to be self-contained.
Thank you!

0 Likes