Class: Dry::Validation::MessageSet

Inherits:
Schema::MessageSet
  • Object
show all
Defined in:
lib/dry/validation/message_set.rb

Overview

MessageSet is a specialized message set for handling validation messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MessageSet.


29
30
31
32
33
# 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

Instance Attribute Details

#localeSymbol (readonly)

Configured locale

Returns:

  • (Symbol)

26
27
28
# File 'lib/dry/validation/message_set.rb', line 26

def locale
  @locale
end

#source_messagesArray<Message, Message::Localized, Schema::Message> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the source set of messages used to produce final evaluated messages

Returns:


19
20
21
# File 'lib/dry/validation/message_set.rb', line 19

def source_messages
  @source_messages
end

Instance Method Details

#add(message) ⇒ MessageSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add a new message

This is used when result is being prepared

Returns:


56
57
58
59
60
61
62
# File 'lib/dry/validation/message_set.rb', line 56

def add(message)
  @empty = nil
  source_messages << message
  messages << message
  initialize_placeholders!
  self
end

#filter(*predicates) ⇒ MessageSet

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.

Examples:

get a list of base messages

message_set = contract.(input).errors
message_set.filter(:base?)

Parameters:

  • predicates (Array<Symbol>)

Returns:


79
80
81
82
83
84
# File 'lib/dry/validation/message_set.rb', line 79

def filter(*predicates)
  messages = select { |msg|
    predicates.all? { |predicate| msg.respond_to?(predicate) && msg.public_send(predicate) }
  }
  self.class.new(messages)
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


87
88
89
90
91
92
93
94
95
# File 'lib/dry/validation/message_set.rb', line 87

def freeze
  source_messages.select { |err| err.respond_to?(:evaluate) }.each do |err|
    idx = source_messages.index(err)
    msg = err.evaluate(locale: locale, full: options[:full])
    messages[idx] = msg
  end
  to_h
  self
end

#with(other, new_options = EMPTY_HASH) ⇒ MessageSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a new message set using updated options

Returns:


40
41
42
43
44
45
46
47
# 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