lib/dry/schema/message/or.rb in dry-schema-1.4.3 vs lib/dry/schema/message/or.rb in dry-schema-1.5.0
- old
+ new
@@ -1,53 +1,32 @@
# frozen_string_literal: true
-require 'dry/equalizer'
+require "dry/schema/message/or/single_path"
+require "dry/schema/message/or/multi_path"
module Dry
module Schema
# Message objects used by message sets
#
# @api public
class Message
- # A message sub-type used by OR operations
- #
- # @api public
- class Or
+ module Or
# @api private
- attr_reader :left
+ def self.[](left, right, messages)
+ msgs = [left, right].flatten
+ paths = msgs.map(&:path)
- # @api private
- attr_reader :right
-
- # @api private
- attr_reader :path
-
- # @api private
- attr_reader :messages
-
- # @api private
- def initialize(left, right, messages)
- @left = left
- @right = right
- @messages = messages
- @path = left.path
- end
-
- # Dump a message into a string
- #
- # @see Message#dump
- #
- # @return [String]
- #
- # @api public
- def dump
- to_a.map(&:dump).join(" #{messages[:or][:text]} ")
- end
- alias to_s dump
-
- # @api private
- def to_a
- [left, right]
+ if paths.uniq.size == 1
+ SinglePath.new(left, right, messages)
+ elsif right.is_a?(Array)
+ if left.is_a?(Array) && paths.uniq.size > 1
+ MultiPath.new(left, right)
+ else
+ right
+ end
+ else
+ msgs.max
+ end
end
end
end
end
end