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[R]

Context that's shared between rules

@return [Concurrent::Map]

@api public

options[R]

Result options

@return [Hash]

@api private

schema_result[R]

Result from contract's schema

@return [Dry::Schema::Result]

@api private

Public Class Methods

new(schema_result, context = ::Concurrent::Map.new, options = EMPTY_HASH) { |result| ... } click to toggle source

Build a new result

@param [Dry::Schema::Result] schema_result

@api private

Calls superclass method
# 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
new(schema_result, context, options) click to toggle source

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

[](key) click to toggle source

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_error(error) click to toggle source

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
base_error?(key) click to toggle source

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
base_rule_error?() click to toggle source

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
deconstruct() click to toggle source

Pattern matching

@api private

# File lib/dry/validation/result.rb, line 214
def deconstruct
  [values, context.each.to_h]
end
deconstruct_keys(keys) click to toggle source

Pattern matching

@api private

# File lib/dry/validation/result.rb, line 207
def deconstruct_keys(keys)
  values.deconstruct_keys(keys)
end
error?(key) click to toggle source

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
errors(new_options = EMPTY_HASH) click to toggle source

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
failure?() click to toggle source

Check if result is not successful

@return [Bool]

@api public

# File lib/dry/validation/result.rb, line 98
def failure?
  !success?
end
freeze() click to toggle source

Freeze result and its error set

@api private

Calls superclass method
# File lib/dry/validation/result.rb, line 197
def freeze
  values.freeze
  errors.freeze
  super
end
inspect() click to toggle source

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
key?(key) click to toggle source

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
rule_error?(key) click to toggle source

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
schema_error?(key) click to toggle source

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
success?() click to toggle source

Check if result is successful

@return [Bool]

@api public

# File lib/dry/validation/result.rb, line 89
def success?
  @errors.empty?
end
to_h() click to toggle source

Coerce to a hash

@api public

# File lib/dry/validation/result.rb, line 179
def to_h
  values.to_h
end
to_monad() click to toggle source

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
values() click to toggle source

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

initialize_errors(options = self.options) click to toggle source

@api private

# File lib/dry/validation/result.rb, line 222
def initialize_errors(options = self.options)
  MessageSet.new(schema_errors(options), options)
end
schema_errors(options) click to toggle source

@api private

# File lib/dry/validation/result.rb, line 227
def schema_errors(options)
  schema_result.message_set(options).to_a
end