class Dry::Schema::Result

Monad extension for Result

@api public

Processing result

@see Processor#call

@api public

Public Class Methods

new(*, **) { |result| ... } click to toggle source

@api private

Calls superclass method
# File lib/dry/schema/result.rb, line 35
def self.new(*, **)
  result = super
  yield(result) if block_given?
  result.freeze
end

Public Instance Methods

[](name) click to toggle source

Read value from the output hash

@param [Symbol] name

@return [Object]

@api public

# File lib/dry/schema/result.rb, line 110
def [](name)
  output[name]
end
add_error(node) click to toggle source

Add a new error AST node

@api private

# File lib/dry/schema/result.rb, line 198
def add_error(node)
  result_ast << node
end
at(at_path, &block) click to toggle source

Return a new result scoped to a specific path

@param path [Symbol, Array, Path]

@return [Result]

@api private

# File lib/dry/schema/result.rb, line 48
def at(at_path, &block)
  new(@output, path: Path.new([*path, *Path[at_path]]), &block)
end
concat(other) click to toggle source

@api private

# File lib/dry/schema/result.rb, line 98
def concat(other)
  result_ast.concat(other.map(&:to_ast))
  self
end
deconstruct_keys(_) click to toggle source

Pattern matching support

@api private

# File lib/dry/schema/result.rb, line 191
def deconstruct_keys(_)
  output
end
error?(spec) click to toggle source

Check if there's an error for the provided spec

@param [Symbol, Hash<Symbol=>Symbol>] spec

@return [Boolean]

@api public

# File lib/dry/schema/result.rb, line 132
def error?(spec)
  message_set.any? { |msg| Path[msg.path].include?(Path[spec]) }
end
errors(options = EMPTY_HASH) click to toggle source

Get human-readable error representation

@see message_set

@return [MessageSet]

@api public

# File lib/dry/schema/result.rb, line 161
def errors(options = EMPTY_HASH)
  message_set(options)
end
failure?() click to toggle source

Check if the result is not successful

@return [Boolean]

@api public

# File lib/dry/schema/result.rb, line 150
def failure?
  !success?
end
inspect() click to toggle source

Return a string representation of the result

@return [String]

@api public

# File lib/dry/schema/result.rb, line 184
def inspect
  "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} path=#{path.keys.inspect}>"
end
key?(name) click to toggle source

Check if a given key is present in the output

@param [Symbol] name

@return [Boolean]

@api public

# File lib/dry/schema/result.rb, line 121
def key?(name)
  output.key?(name)
end
message_set(options = EMPTY_HASH) click to toggle source

Return the message set

@param [Hash] options @option options [Symbol] :locale Alternative locale (default is :en) @option options [Boolean] :hints Whether to include hint messages or not @option options [Boolean] :full Whether to generate messages that include key names

@return [MessageSet]

@api public

# File lib/dry/schema/result.rb, line 175
def message_set(options = EMPTY_HASH)
  message_compiler.with(options).(result_ast)
end
new(output, **opts, &block) click to toggle source

@api private

# File lib/dry/schema/result.rb, line 53
def new(output, **opts, &block)
  self.class.new(
    output,
    message_compiler: message_compiler,
    result_ast: result_ast,
    **opts,
    &block
  )
end
output() click to toggle source

Dump result to a hash returning processed and validated data

@return [Hash]

# File lib/dry/schema/result.rb, line 77
def output
  path.equal?(Path::EMPTY) ? @output : @output.dig(*path)
end
Also aliased as: to_h
path() click to toggle source

@api private

# File lib/dry/schema/result.rb, line 70
def path
  @path || Path::EMPTY
end
replace(value) click to toggle source

@api private

# File lib/dry/schema/result.rb, line 83
def replace(value)
  if value.is_a?(output.class)
    output.replace(value)
  elsif path.equal?(Path::EMPTY)
    @output = value
  else
    value_holder = path.keys.length > 1 ? @output.dig(*path.to_a[0..-2]) : @output

    value_holder[path.last] = value
  end

  self
end
success?() click to toggle source

Check if the result is successful

@return [Boolean]

@api public

# File lib/dry/schema/result.rb, line 141
def success?
  result_ast.empty?
end
to_h()
Alias for: output
to_monad() click to toggle source

Turn result into a monad

This makes result objects work with dry-monads (or anything with a compatible interface)

@return [Dry::Monads::Success,Dry::Monads::Failure]

@api public

# File lib/dry/schema/extensions/monads.rb, line 20
def to_monad
  if success?
    Success(self)
  else
    Failure(self)
  end
end
update(hash) click to toggle source

@api private

# File lib/dry/schema/result.rb, line 64
def update(hash)
  output.update(hash)
  self
end