class Dry::Validation::MessageSet
MessageSet
is a specialized message set for handling validation messages
@api public
Attributes
Configured locale
@return [Symbol]
@api public
Return the source set of messages used to produce final evaluated messages
@return [Array<Message, Message::Localized
, Schema::Message>]
@api private
Public Class Methods
@api private
# File lib/dry/validation/message_set.rb, line 29 def initialize(messages, options = EMPTY_HASH) @locale = options[:locale] @source_messages = options.fetch(:source) { messages.dup } super end
Public Instance Methods
Add a new message
This is used when result is being prepared
@return [MessageSet]
@api private
# File lib/dry/validation/message_set.rb, line 56 def add(message) @empty = nil source_messages << message messages << message self end
Filter message set using provided predicates
This method is open to any predicate because messages can be anything that implements Message
API, thus they can implement whatever predicates you may need.
@example get a list of base messages
message_set = contract.(input).errors message_set.filter(:base?)
@param [Array<Symbol>] predicates
@return [MessageSet]
@api public
# File lib/dry/validation/message_set.rb, line 78 def filter(*predicates) messages = select { |msg| predicates.all? { |predicate| msg.respond_to?(predicate) && msg.public_send(predicate) } } self.class.new(messages) end
@api private
# File lib/dry/validation/message_set.rb, line 86 def freeze source_messages.select { |err| err.respond_to?(:evaluate) }.each do |err| idx = messages.index(err) || source_messages.index(err) msg = err.evaluate(locale: locale, full: options[:full]) messages[idx] = msg end to_h self end
Return a new message set using updated options
@return [MessageSet]
@api private
# File lib/dry/validation/message_set.rb, line 40 def with(other, new_options = EMPTY_HASH) return self if new_options.empty? && other.eql?(messages) self.class.new( other | select { |err| err.is_a?(Message) }, options.merge(source: source_messages, **new_options) ).freeze end