Class: Dry::Validation::Rust::Contract::Result
- Inherits:
-
Object
- Object
- Dry::Validation::Rust::Contract::Result
- 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.
Instance Attribute Summary collapse
-
#context ⇒ Hash
readonly
Context supplied to the contract call.
-
#schema_result ⇒ Schema::Result
readonly
Structural validation outcome.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Reads a validated value by key or path.
-
#add_error(message) ⇒ Result
Adds a rule message before finalization and returns this result.
-
#base_rule_error? ⇒ Boolean
Returns whether a base-level rule message exists.
-
#deconstruct ⇒ Array<(Values, Hash)>
Supports tuple pattern matching as values and context.
-
#deconstruct_keys(keys) ⇒ Hash
Deconstructs coerced output for Hash pattern matching.
-
#error?(key) ⇒ Boolean
Returns whether a message exists at or below a path.
-
#errors(options = {}) ⇒ MessageSet
Returns schema and rule errors, optionally with display options.
-
#failure? ⇒ Boolean
Returns true when at least one schema or rule message exists.
-
#finalize! ⇒ Result
Prevents further rule-message mutation and returns this result.
-
#initialize(schema_result, context = {}) ⇒ Result
constructor
Creates a result from a schema result and call context.
-
#inspect ⇒ String
Returns a diagnostic representation of output, errors, and context.
-
#key?(key) ⇒ Boolean
Returns whether validated output contains a key or path.
-
#rule_error?(key) ⇒ Boolean
Returns whether a rule message exists at or below a path.
-
#schema_error?(key) ⇒ Boolean
Returns whether a schema message exists at or below a path.
-
#success? ⇒ Boolean
Returns true when no schema or rule messages exist.
-
#to_h ⇒ Hash
Returns validated output as a Hash.
-
#values ⇒ Values
Returns validated output wrapped in a Values object.
Constructor Details
#initialize(schema_result, context = {}) ⇒ Result
Creates a result from a schema result and call context.
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
#context ⇒ Hash (readonly)
Returns context supplied to the contract call.
27 28 29 |
# File 'lib/dry/validation/rust/contract/result.rb', line 27 def context @context end |
#schema_result ⇒ Schema::Result (readonly)
Returns structural validation outcome.
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.
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.
64 65 66 67 |
# File 'lib/dry/validation/rust/contract/result.rb', line 64 def add_error() @rule_messages << self end |
#base_rule_error? ⇒ Boolean
Returns whether a base-level rule message exists.
112 113 114 |
# File 'lib/dry/validation/rust/contract/result.rb', line 112 def base_rule_error? @rule_messages.any?(&:base?) end |
#deconstruct ⇒ Array<(Values, Hash)>
Supports tuple pattern matching as values and 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.
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.
87 88 89 90 |
# File 'lib/dry/validation/rust/contract/result.rb', line 87 def error?(key) path = Path.parse(key) errors.any? { || Path.prefix?(.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.
55 56 57 58 |
# File 'lib/dry/validation/rust/contract/result.rb', line 55 def errors( = {}) set = MessageSet.new([*schema_result., *@rule_messages], ) .empty? ? set : set.with() end |
#failure? ⇒ Boolean
Returns true when at least one schema or rule message exists.
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.
172 173 174 175 |
# File 'lib/dry/validation/rust/contract/result.rb', line 172 def finalize! @rule_messages.freeze self end |
#inspect ⇒ String
Returns a diagnostic representation of output, errors, and context.
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.
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.
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? { || Path.prefix?(.path, path) } end |
#schema_error?(key) ⇒ Boolean
Returns whether a schema message exists at or below a path.
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.
72 73 74 |
# File 'lib/dry/validation/rust/contract/result.rb', line 72 def success? errors.empty? end |
#to_h ⇒ Hash
Returns validated output as a Hash.
135 136 137 |
# File 'lib/dry/validation/rust/contract/result.rb', line 135 def to_h values.to_h end |