Class: Dry::Validation::Result
- Inherits:
-
Object
- Object
- Dry::Validation::Result
- Includes:
- Monads::Result::Mixin, Hints::ResultExtensions
- Defined in:
- lib/dry/validation/result.rb,
lib/dry/validation/extensions/monads.rb
Overview
Monad extension for contract results
Instance Attribute Summary collapse
-
#context ⇒ Concurrent::Map
readonly
Context that’s shared between rules.
-
#options ⇒ Hash
readonly
private
Result options.
-
#schema_result ⇒ Dry::Schema::Result
readonly
private
Result from contract’s schema.
Class Method Summary collapse
-
.new(schema_result, context = ::Concurrent::Map.new, options = EMPTY_HASH) {|result| ... } ⇒ Object
private
Build a new result.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Read a value under provided key.
-
#add_error(error) ⇒ Object
private
Add a new error for the provided key.
-
#error?(key) ⇒ Boolean
private
Check if values include an error for the provided key.
-
#errors(new_options = EMPTY_HASH) ⇒ MessageSet
Get error set.
-
#failure? ⇒ Bool
Check if result is not successful.
-
#freeze ⇒ Object
private
Freeze result and its error set.
-
#initialize(schema_result, context, options) ⇒ Result
constructor
private
Initialize a new result.
-
#inspect ⇒ Object
Return a string representation.
-
#key?(key) ⇒ Bool
Check if a key was set.
-
#success? ⇒ Bool
Check if result is successful.
-
#to_h ⇒ Object
Coerce to a hash.
-
#to_monad ⇒ Dry::Monads::Result
Returns a result monad.
-
#values ⇒ Values
Return values wrapper with the input processed by schema.
Methods included from Hints::ResultExtensions
Constructor Details
#initialize(schema_result, context, options) ⇒ Result
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.
Initialize a new result
53 54 55 56 57 58 |
# File 'lib/dry/validation/result.rb', line 53 def initialize(schema_result, context, ) @schema_result = schema_result @context = context = @errors = initialize_errors end |
Instance Attribute Details
#context ⇒ Concurrent::Map (readonly)
Context that’s shared between rules
34 35 36 |
# File 'lib/dry/validation/result.rb', line 34 def context @context end |
#options ⇒ Hash (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.
Result options
48 49 50 |
# File 'lib/dry/validation/result.rb', line 48 def end |
#schema_result ⇒ Dry::Schema::Result (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.
Result from contract’s schema
41 42 43 |
# File 'lib/dry/validation/result.rb', line 41 def schema_result @schema_result end |
Class Method Details
.new(schema_result, context = ::Concurrent::Map.new, options = EMPTY_HASH) {|result| ... } ⇒ Object
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.
Build a new result
23 24 25 26 27 |
# File 'lib/dry/validation/result.rb', line 23 def self.new(schema_result, context = ::Concurrent::Map.new, = EMPTY_HASH) result = super yield(result) if block_given? result.freeze end |
Instance Method Details
#[](key) ⇒ Object
Read a value under provided key
124 125 126 |
# File 'lib/dry/validation/result.rb', line 124 def [](key) values[key] end |
#add_error(error) ⇒ Object
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 error for the provided key
112 113 114 115 |
# File 'lib/dry/validation/result.rb', line 112 def add_error(error) @errors.add(error) self end |
#error?(key) ⇒ Boolean
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.
Check if values include an error for the provided key
105 106 107 |
# File 'lib/dry/validation/result.rb', line 105 def error?(key) schema_result.error?(key) end |
#errors(new_options = EMPTY_HASH) ⇒ MessageSet
Get error set
80 81 82 |
# File 'lib/dry/validation/result.rb', line 80 def errors( = EMPTY_HASH) .empty? ? @errors : @errors.with(schema_errors(), ) end |
#failure? ⇒ Bool
Check if result is not successful
98 99 100 |
# File 'lib/dry/validation/result.rb', line 98 def failure? !success? end |
#freeze ⇒ Object
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.
Freeze result and its error set
160 161 162 163 164 |
# File 'lib/dry/validation/result.rb', line 160 def freeze values.freeze errors.freeze super end |
#inspect ⇒ Object
Return a string representation
149 150 151 152 153 154 155 |
# File 'lib/dry/validation/result.rb', line 149 def inspect if context.empty? "#<#{self.class}#{to_h} errors=#{errors.to_h}>" else "#<#{self.class}#{to_h} errors=#{errors.to_h} context=#{context.each.to_h}>" end end |
#key?(key) ⇒ Bool
Check if a key was set
135 136 137 |
# File 'lib/dry/validation/result.rb', line 135 def key?(key) values.key?(key) end |
#success? ⇒ Bool
Check if result is successful
89 90 91 |
# File 'lib/dry/validation/result.rb', line 89 def success? @errors.empty? end |
#to_h ⇒ Object
Coerce to a hash
142 143 144 |
# File 'lib/dry/validation/result.rb', line 142 def to_h values.to_h end |
#to_monad ⇒ Dry::Monads::Result
Returns a result monad
29 30 31 |
# File 'lib/dry/validation/extensions/monads.rb', line 29 def to_monad success? ? Success(self) : Failure(self) end |
#values ⇒ Values
Return values wrapper with the input processed by schema
65 66 67 |
# File 'lib/dry/validation/result.rb', line 65 def values @values ||= Values.new(schema_result.to_h) end |