Class: Dry::Schema::Result

Inherits:
Object
  • Object
show all
Includes:
Monads::Result::Mixin, Extensions::Hints::ResultMethods
Defined in:
lib/dry/schema/result.rb,
lib/dry/schema/extensions/monads.rb

Overview

Monad extension for Result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Hints::ResultMethods

#hints, #messages

Class Method Details

.new {|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.

Yields:

  • (result)


34
35
36
37
38
# File 'lib/dry/schema/result.rb', line 34

def self.new(*, **)
  result = super
  yield(result) if block_given?
  result.freeze
end

Instance Method Details

#[](name) ⇒ Object

Read value from the output hash

Parameters:

  • name (Symbol)

Returns:

  • (Object)


109
110
111
# File 'lib/dry/schema/result.rb', line 109

def [](name)
  output[name]
end

#add_error(node) ⇒ 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 AST node



197
198
199
# File 'lib/dry/schema/result.rb', line 197

def add_error(node)
  result_ast << node
end

#at(at_path, &block) ⇒ 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.

Return a new result scoped to a specific path

Parameters:

  • path (Symbol, Array, Path)

Returns:



47
48
49
# File 'lib/dry/schema/result.rb', line 47

def at(at_path, &block)
  new(@output, path: Path.new([*path, *Path[at_path]]), &block)
end

#concat(other) ⇒ 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.



97
98
99
100
# File 'lib/dry/schema/result.rb', line 97

def concat(other)
  result_ast.concat(other.map(&:to_ast))
  self
end

#deconstruct_keys(_) ⇒ 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.

Pattern matching support



190
191
192
# File 'lib/dry/schema/result.rb', line 190

def deconstruct_keys(_)
  output
end

#error?(spec) ⇒ Boolean

Check if there's an error for the provided spec

Parameters:

  • spec (Symbol, Hash<Symbol=>Symbol>)

Returns:

  • (Boolean)


131
132
133
# File 'lib/dry/schema/result.rb', line 131

def error?(spec)
  message_set.any? { |msg| Path[msg.path].include?(Path[spec]) }
end

#errors(options = EMPTY_HASH) ⇒ MessageSet

Get human-readable error representation

Returns:

See Also:



160
161
162
# File 'lib/dry/schema/result.rb', line 160

def errors(options = EMPTY_HASH)
  message_set(options)
end

#failure?Boolean

Check if the result is not successful

Returns:

  • (Boolean)


149
150
151
# File 'lib/dry/schema/result.rb', line 149

def failure?
  !success?
end

#inspectString

Return a string representation of the result

Returns:

  • (String)


183
184
185
# File 'lib/dry/schema/result.rb', line 183

def inspect
  "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} path=#{path.keys.inspect}>"
end

#key?(name) ⇒ Boolean

Check if a given key is present in the output

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


120
121
122
# File 'lib/dry/schema/result.rb', line 120

def key?(name)
  output.key?(name)
end

#message_set(options = EMPTY_HASH) ⇒ MessageSet

Return the message set

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :locale (Symbol)

    Alternative locale (default is :en)

  • :hints (Boolean)

    Whether to include hint messages or not

  • :full (Boolean)

    Whether to generate messages that include key names

Returns:



174
175
176
# File 'lib/dry/schema/result.rb', line 174

def message_set(options = EMPTY_HASH)
  message_compiler.with(options).(result_ast)
end

#new(output, **opts, &block) ⇒ 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.



52
53
54
55
56
57
58
59
60
# File 'lib/dry/schema/result.rb', line 52

def new(output, **opts, &block)
  self.class.new(
    output,
    message_compiler: message_compiler,
    result_ast: result_ast,
    **opts,
    &block
  )
end

#outputHash Also known as: to_h

Dump result to a hash returning processed and validated data

Returns:

  • (Hash)


76
77
78
# File 'lib/dry/schema/result.rb', line 76

def output
  path.equal?(Path::EMPTY) ? @output : @output.dig(*path)
end

#pathObject

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.



69
70
71
# File 'lib/dry/schema/result.rb', line 69

def path
  @path || Path::EMPTY
end

#replace(value) ⇒ 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.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/schema/result.rb', line 82

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?Boolean

Check if the result is successful

Returns:

  • (Boolean)


140
141
142
# File 'lib/dry/schema/result.rb', line 140

def success?
  result_ast.empty?
end

#to_monadDry::Monads::Success, Dry::Monads::Failure

Turn result into a monad

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

Returns:

  • (Dry::Monads::Success, Dry::Monads::Failure)


20
21
22
23
24
25
26
# File 'lib/dry/schema/extensions/monads.rb', line 20

def to_monad
  if success?
    Success(self)
  else
    Failure(self)
  end
end

#update(hash) ⇒ 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.



63
64
65
66
# File 'lib/dry/schema/result.rb', line 63

def update(hash)
  output.update(hash)
  self
end