Class: Dry::Validation::Rust::MessageSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/rust/message_set.rb

Overview

A collection of validation messages with filtering and rendering views.

Message sets preserve individual Message objects until a caller asks for a nested error hash with #to_h. Use #with with full: true to render field names into message text.

Examples:

Read nested errors from a contract result

result.errors.to_h # => { name: ["is missing"] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages = [], options = {}) ⇒ MessageSet

Creates a message set from messages and optional rendering options.

Parameters:

  • messages (Array<Message>) (defaults to: [])

    messages to include

  • options (Hash) (defaults to: {})

    rendering options, such as full: true



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

def initialize(messages = [], options = {})
  @messages = messages.dup
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns rendering options applied to this message set.

Returns:

  • (Hash)

    rendering options applied to this message set.



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

def options
  @options
end

Instance Method Details

#[](key) ⇒ MessageSet

Returns messages at or below a key or path.

Examples:

Select nested address messages

result.errors[:address].to_h # => { city: ["is missing"] }

Parameters:

  • key (Symbol, String, Array, Hash)

    key or supported path specification

Returns:

  • (MessageSet)

    messages whose paths start with key



54
55
56
57
# File 'lib/dry/validation/rust/message_set.rb', line 54

def [](key)
  wanted = Path.parse(key)
  self.class.new(@messages.select { |message| Path.prefix?(message.path, wanted) }, options)
end

#add(message) ⇒ MessageSet

Adds a message and invalidates derived views.

Parameters:

  • message (Message)

    message to append

Returns:



63
64
65
66
67
68
# File 'lib/dry/validation/rust/message_set.rb', line 63

def add(message)
  @messages << message
  @readonly_messages = nil
  @to_h = nil
  self
end

#each {|message| ... } ⇒ Enumerator, MessageSet

Iterates over messages.

Examples:

Print each message text

result.errors.each { |message| puts message.text }

Yields:

  • (message)

    each message in the set

Yield Parameters:

Returns:

  • (Enumerator, MessageSet)

    an enumerator without a block, or this set with one



37
38
39
# File 'lib/dry/validation/rust/message_set.rb', line 37

def each(&)
  @messages.each(&)
end

#empty?Boolean

Returns whether this set contains no messages.

Returns:

  • (Boolean)


73
74
75
# File 'lib/dry/validation/rust/message_set.rb', line 73

def empty?
  @messages.empty?
end

#filter(*predicates) ⇒ MessageSet

Returns messages satisfying every supplied message predicate.

Each predicate must be a public predicate method on a message and return a truthy value. A message without a requested predicate is excluded.

Examples:

Select base-level messages

result.errors.filter(:base?)

Parameters:

  • predicates (Array<Symbol, String>)

    message predicate names

Returns:



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

def filter(*predicates)
  self.class.new(
    @messages.select do |message|
      predicates.all? { |predicate| message.respond_to?(predicate) && message.public_send(predicate) }
    end,
    options
  )
end

#freezeMessageSet

Freezes this set and its cached derived Hash.

Returns:



125
126
127
128
129
130
# File 'lib/dry/validation/rust/message_set.rb', line 125

def freeze
  @messages.freeze
  # Keep the derived representation so callers of a frozen set reuse it.
  to_h.freeze
  super
end

#messagesArray<Message>

Returns an immutable snapshot of the messages.

Returns:

  • (Array<Message>)

    frozen message-object view



44
45
46
# File 'lib/dry/validation/rust/message_set.rb', line 44

def messages
  readonly_messages
end

#to_hHash

Returns messages grouped into a nested Hash by path.

Examples:

Read errors as a hash

result.errors.to_h # => { address: { city: ["is missing"] } }

Returns:

  • (Hash)

    nested error messages keyed by their paths



118
119
120
# File 'lib/dry/validation/rust/message_set.rb', line 118

def to_h
  @to_h ||= build_nested_hash
end

#with(new_options = {}) ⇒ MessageSet

Returns a copy with rendering options, including full-message text.

With full: true, non-base message text is prefixed with its humanized path (for example, :first_name becomes "first name").

Examples:

Render full messages

result.errors.with(full: true).messages.map(&:text)
# => ["name is missing"]

Parameters:

  • new_options (Hash) (defaults to: {})

    rendering options to merge into #options

Returns:

  • (MessageSet)

    a new set using the merged options



106
107
108
109
110
111
# File 'lib/dry/validation/rust/message_set.rb', line 106

def with(new_options = {})
  merged = options.merge(new_options)
  return self.class.new(@messages, merged) unless merged[:full]

  self.class.new(@messages.map { |message| full_message(message) }, merged)
end