class Dry::Validation::Result
Monad extension for contract results
@example
Dry::Validation.load_extensions(:monads) contract = Dry::Validation::Contract.build do schema do required(:name).filled(:string) end end contract.call(name: nil).to_monad
@api public
Result
objects are returned by contracts
@api public
Attributes
Context that's shared between rules
@return [Concurrent::Map]
@api public
Public Class Methods
Build a new result
@param [Dry::Schema::Result] schema_result
@api private
# File lib/dry/validation/result.rb, line 23 def self.new(schema_result, context = ::Concurrent::Map.new, options = EMPTY_HASH) result = super yield(result) if block_given? result.freeze end
Initialize a new result
@api private
# File lib/dry/validation/result.rb, line 53 def initialize(schema_result, context, options) @schema_result = schema_result @context = context @options = options @errors = initialize_errors end
Public Instance Methods
Read a value under provided key
@param [Symbol] key
@return [Object]
@api public
# File lib/dry/validation/result.rb, line 161 def [](key) values[key] end
Add a new error for the provided key
@api private
# File lib/dry/validation/result.rb, line 149 def add_error(error) @errors.add(error) self end
Check if there's any error for the provided key
This does not consider errors from the nested values
@api private
# File lib/dry/validation/result.rb, line 135 def base_error?(key) schema_result.errors.any? { |error| key_path = Schema::Path[key] err_path = Schema::Path[error.path] next unless key_path.same_root?(err_path) key_path == err_path } end
Check if the result contains any base rule errors
@api private
# File lib/dry/validation/result.rb, line 126 def base_rule_error? !errors.filter(:base?).empty? end
Pattern matching
@api private
# File lib/dry/validation/result.rb, line 214 def deconstruct [values, context.each.to_h] end
Pattern matching
@api private
# File lib/dry/validation/result.rb, line 207 def deconstruct_keys(keys) values.deconstruct_keys(keys) end
Check if values include an error for the provided key
@api public
# File lib/dry/validation/result.rb, line 105 def error?(key) errors.any? { |msg| Schema::Path[msg.path].include?(Schema::Path[key]) } end
Get error set
@!macro errors-options
@param [Hash] new_options @option new_options [Symbol] :locale Set locale for messages @option new_options [Boolean] :hints Enable/disable hints @option new_options [Boolean] :full Get messages that include key names
@return [MessageSet]
@api public
# File lib/dry/validation/result.rb, line 80 def errors(new_options = EMPTY_HASH) new_options.empty? ? @errors : @errors.with(schema_errors(new_options), new_options) end
Check if result is not successful
@return [Bool]
@api public
# File lib/dry/validation/result.rb, line 98 def failure? !success? end
Freeze result and its error set
@api private
# File lib/dry/validation/result.rb, line 197 def freeze values.freeze errors.freeze super end
Return a string representation
@api public
# File lib/dry/validation/result.rb, line 186 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
Check if a key was set
@param [Symbol] key
@return [Bool]
@api public
# File lib/dry/validation/result.rb, line 172 def key?(key) values.key?(key) end
Check if the rules includes an error for the provided key
@api private
# File lib/dry/validation/result.rb, line 119 def rule_error?(key) !schema_error?(key) && error?(key) end
Check if the base schema (without rules) includes an error for the provided key
@api private
# File lib/dry/validation/result.rb, line 112 def schema_error?(key) schema_result.error?(key) end
Check if result is successful
@return [Bool]
@api public
# File lib/dry/validation/result.rb, line 89 def success? @errors.empty? end
Coerce to a hash
@api public
# File lib/dry/validation/result.rb, line 179 def to_h values.to_h end
Returns a result monad
@return [Dry::Monads::Result]
@api public
# File lib/dry/validation/extensions/monads.rb, line 29 def to_monad success? ? Success(self) : Failure(self) end
Return values wrapper with the input processed by schema
@return [Values]
@api public
# File lib/dry/validation/result.rb, line 65 def values @values ||= Values.new(schema_result.to_h) end
Private Instance Methods
@api private
# File lib/dry/validation/result.rb, line 222 def initialize_errors(options = self.options) MessageSet.new(schema_errors(options), options) end
@api private
# File lib/dry/validation/result.rb, line 227 def schema_errors(options) schema_result.message_set(options).to_a end