Class: Dry::Validation::Rust::Contract::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/rust/contract/result.rb

Overview

The outcome of calling a Dry::Validation::Rust::Contract.

A result contains coerced output, schema and rule failures, and the context supplied to the call. Use #success? or #failure? to check the outcome, #to_h to read the output, and #errors to inspect failures.

Examples:

Validating input and matching the output

result = UserContract.new.call(name: "Ada")

case result
in { name: String => name }
  name # => "Ada"
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_result, context = {}) ⇒ Result

Creates a result from a schema result and call context.

Parameters:

  • schema_result (Schema::Result)

    structural validation outcome

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

    context supplied to the contract call



34
35
36
37
38
# File 'lib/dry/validation/rust/contract/result.rb', line 34

def initialize(schema_result, context = {})
  @schema_result = schema_result
  @context = context
  @rule_messages = []
end

Instance Attribute Details

#contextHash (readonly)

Returns context supplied to the contract call.

Returns:

  • (Hash)

    context supplied to the contract call.



27
28
29
# File 'lib/dry/validation/rust/contract/result.rb', line 27

def context
  @context
end

#schema_resultSchema::Result (readonly)

Returns structural validation outcome.

Returns:



25
26
27
# File 'lib/dry/validation/rust/contract/result.rb', line 25

def schema_result
  @schema_result
end

Instance Method Details

#[](key) ⇒ Object?

Reads a validated value by key or path.

Parameters:

  • key (Symbol, String, Array)

    key or supported path specification

Returns:

  • (Object, nil)

    coerced value, if present



120
121
122
# File 'lib/dry/validation/rust/contract/result.rb', line 120

def [](key)
  values[key]
end

#add_error(message) ⇒ Result

Adds a rule message before finalization and returns this result.

Parameters:

  • message (Message)

    rule failure to add

Returns:



64
65
66
67
# File 'lib/dry/validation/rust/contract/result.rb', line 64

def add_error(message)
  @rule_messages << message
  self
end

#base_rule_error?Boolean

Returns whether a base-level rule message exists.

Returns:

  • (Boolean)


112
113
114
# File 'lib/dry/validation/rust/contract/result.rb', line 112

def base_rule_error?
  @rule_messages.any?(&:base?)
end

#deconstructArray<(Values, Hash)>

Supports tuple pattern matching as values and context.

Returns:

  • (Array<(Values, Hash)>)

    coerced output and call context



165
166
167
# File 'lib/dry/validation/rust/contract/result.rb', line 165

def deconstruct
  [values, context]
end

#deconstruct_keys(keys) ⇒ Hash

Deconstructs coerced output for Hash pattern matching.

This lets a result match as though it were its output hash, such as in { name: String => name }. The call context is not included in this matching form; use #deconstruct for positional matching.

Parameters:

  • keys (Array<Symbol>, nil)

    requested keys, or nil for all keys

Returns:

  • (Hash)

    output entries available to the pattern



158
159
160
# File 'lib/dry/validation/rust/contract/result.rb', line 158

def deconstruct_keys(keys)
  values.deconstruct_keys(keys)
end

#error?(key) ⇒ Boolean

Returns whether a message exists at or below a path.

Parameters:

  • key (Symbol, String, Array, Hash)

    key or supported path specification

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/dry/validation/rust/contract/result.rb', line 87

def error?(key)
  path = Path.parse(key)
  errors.any? { |message| Path.prefix?(message.path, path) }
end

#errors(options = {}) ⇒ MessageSet

Returns schema and rule errors, optionally with display options.

Call MessageSet#messages on the returned set for its immutable message-object view, or MessageSet#to_h for nested error hashes.

Parameters:

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

    message rendering options; pass full: true to include full message text

Returns:

  • (MessageSet)

    combined schema and rule errors



55
56
57
58
# File 'lib/dry/validation/rust/contract/result.rb', line 55

def errors(options = {})
  set = MessageSet.new([*schema_result.messages, *@rule_messages], options)
  options.empty? ? set : set.with(options)
end

#failure?Boolean

Returns true when at least one schema or rule message exists.

Returns:

  • (Boolean)


79
80
81
# File 'lib/dry/validation/rust/contract/result.rb', line 79

def failure?
  !success?
end

#finalize!Result

Prevents further rule-message mutation and returns this result.

Returns:

  • (Result)

    this finalized result



172
173
174
175
# File 'lib/dry/validation/rust/contract/result.rb', line 172

def finalize!
  @rule_messages.freeze
  self
end

#inspectString

Returns a diagnostic representation of output, errors, and context.

Returns:

  • (String)


142
143
144
145
146
147
148
# File 'lib/dry/validation/rust/contract/result.rb', line 142

def inspect
  if context.empty?
    "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
  else
    "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} context=#{context.inspect}>"
  end
end

#key?(key) ⇒ Boolean

Returns whether validated output contains a key or path.

Parameters:

  • key (Symbol, String, Array)

    key or supported path specification

Returns:

  • (Boolean)


128
129
130
# File 'lib/dry/validation/rust/contract/result.rb', line 128

def key?(key)
  values.key?(key)
end

#rule_error?(key) ⇒ Boolean

Returns whether a rule message exists at or below a path.

Parameters:

  • key (Symbol, String, Array, Hash)

    key or supported path specification

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/dry/validation/rust/contract/result.rb', line 104

def rule_error?(key)
  path = Path.parse(key)
  @rule_messages.any? { |message| Path.prefix?(message.path, path) }
end

#schema_error?(key) ⇒ Boolean

Returns whether a schema message exists at or below a path.

Parameters:

  • key (Symbol, String, Array, Hash)

    key or supported path specification

Returns:

  • (Boolean)


96
97
98
# File 'lib/dry/validation/rust/contract/result.rb', line 96

def schema_error?(key)
  schema_result.error?(key)
end

#success?Boolean

Returns true when no schema or rule messages exist.

Returns:

  • (Boolean)


72
73
74
# File 'lib/dry/validation/rust/contract/result.rb', line 72

def success?
  errors.empty?
end

#to_hHash

Returns validated output as a Hash.

Returns:

  • (Hash)

    coerced output



135
136
137
# File 'lib/dry/validation/rust/contract/result.rb', line 135

def to_h
  values.to_h
end

#valuesValues

Returns validated output wrapped in a Values object.

Returns:

  • (Values)

    coerced output with key and path access



43
44
45
# File 'lib/dry/validation/rust/contract/result.rb', line 43

def values
  @values ||= Values.new(schema_result.to_h)
end