Class: Dry::Validation::Result

Inherits:
Object
  • Object
show all
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

Examples:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options)
  @schema_result = schema_result
  @context = context
  @options = options
  @errors = initialize_errors
end

Instance Attribute Details

#contextConcurrent::Map (readonly)

Context that’s shared between rules

Returns:

  • (Concurrent::Map)


34
35
36
# File 'lib/dry/validation/result.rb', line 34

def context
  @context
end

#optionsHash (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

Returns:

  • (Hash)


48
49
50
# File 'lib/dry/validation/result.rb', line 48

def options
  @options
end

#schema_resultDry::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

Returns:

  • (Dry::Schema::Result)


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

Parameters:

  • schema_result (Dry::Schema::Result)

Yields:

  • (result)


23
24
25
26
27
# 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

Instance Method Details

#[](key) ⇒ Object

Read a value under provided key

Parameters:

  • key (Symbol)

Returns:

  • (Object)


161
162
163
# File 'lib/dry/validation/result.rb', line 161

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



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

def add_error(error)
  @errors.add(error)
  self
end

#base_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 there’s any error for the provided key

This does not consider errors from the nested values

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
# 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?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 the result contains any base rule errors

Returns:

  • (Boolean)


126
127
128
# File 'lib/dry/validation/result.rb', line 126

def base_rule_error?
  !errors.filter(:base?).empty?
end

#deconstructObject

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



214
215
216
# File 'lib/dry/validation/result.rb', line 214

def deconstruct
  [values, context.each.to_h]
end

#deconstruct_keys(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



207
208
209
# File 'lib/dry/validation/result.rb', line 207

def deconstruct_keys(keys)
  values.deconstruct_keys(keys)
end

#error?(key) ⇒ Boolean

Check if values include an error for the provided key

Returns:

  • (Boolean)


105
106
107
# 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) ⇒ MessageSet

Get error set

Parameters:

  • new_options (Hash) (defaults to: EMPTY_HASH)

Options Hash (new_options):

  • :locale (Symbol)

    Set locale for messages

  • :hints (Boolean)

    Enable/disable hints

  • :full (Boolean)

    Get messages that include key names

Returns:



80
81
82
# 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?Bool

Check if result is not successful

Returns:

  • (Bool)


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

def failure?
  !success?
end

#freezeObject

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



197
198
199
200
201
# File 'lib/dry/validation/result.rb', line 197

def freeze
  values.freeze
  errors.freeze
  super
end

#hints(new_options = EMPTY_HASH) ⇒ MessageSet Originally defined in module Hints::ResultExtensions

Return hint messages

Parameters:

  • new_options (Hash) (defaults to: EMPTY_HASH)

Options Hash (new_options):

  • :locale (Symbol)

    Set locale for messages

  • :hints (Boolean)

    Enable/disable hints

  • :full (Boolean)

    Get messages that include key names

Returns:

#inspectObject

Return a string representation



186
187
188
189
190
191
192
# 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) ⇒ Bool

Check if a key was set

Parameters:

  • key (Symbol)

Returns:

  • (Bool)


172
173
174
# File 'lib/dry/validation/result.rb', line 172

def key?(key)
  values.key?(key)
end

#messages(new_options = EMPTY_HASH) ⇒ MessageSet Originally defined in module Hints::ResultExtensions

Return errors and hints

Parameters:

  • new_options (Hash) (defaults to: EMPTY_HASH)

Options Hash (new_options):

  • :locale (Symbol)

    Set locale for messages

  • :hints (Boolean)

    Enable/disable hints

  • :full (Boolean)

    Get messages that include key names

Returns:

#rule_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 the rules includes an error for the provided key

Returns:

  • (Boolean)


119
120
121
# File 'lib/dry/validation/result.rb', line 119

def rule_error?(key)
  !schema_error?(key) && error?(key)
end

#schema_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 the base schema (without rules) includes an error for the provided key

Returns:

  • (Boolean)


112
113
114
# File 'lib/dry/validation/result.rb', line 112

def schema_error?(key)
  schema_result.error?(key)
end

#success?Bool

Check if result is successful

Returns:

  • (Bool)


89
90
91
# File 'lib/dry/validation/result.rb', line 89

def success?
  @errors.empty?
end

#to_hObject

Coerce to a hash



179
180
181
# File 'lib/dry/validation/result.rb', line 179

def to_h
  values.to_h
end

#to_monadDry::Monads::Result

Returns a result monad

Returns:

  • (Dry::Monads::Result)


29
30
31
# File 'lib/dry/validation/extensions/monads.rb', line 29

def to_monad
  success? ? Success(self) : Failure(self)
end

#valuesValues

Return values wrapper with the input processed by schema

Returns:



65
66
67
# File 'lib/dry/validation/result.rb', line 65

def values
  @values ||= Values.new(schema_result.to_h)
end