Class: Dry::Validation::Schema::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/schema/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, result, errors, error_compiler, hint_compiler) ⇒ Result

Returns a new instance of Result.



17
18
19
20
21
22
23
# File 'lib/dry/validation/schema/result.rb', line 17

def initialize(params, result, errors, error_compiler, hint_compiler)
  @params = params
  @result = result
  @errors = errors
  @error_compiler = error_compiler
  @hint_compiler = hint_compiler
end

Instance Attribute Details

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



13
14
15
# File 'lib/dry/validation/schema/result.rb', line 13

def error_compiler
  @error_compiler
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/dry/validation/schema/result.rb', line 11

def errors
  @errors
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



15
16
17
# File 'lib/dry/validation/schema/result.rb', line 15

def hint_compiler
  @hint_compiler
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/dry/validation/schema/result.rb', line 7

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/dry/validation/schema/result.rb', line 9

def result
  @result
end

Instance Method Details

#each(&block) ⇒ Object



25
26
27
# File 'lib/dry/validation/schema/result.rb', line 25

def each(&block)
  failures.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dry/validation/schema/result.rb', line 29

def empty?
  errors.empty?
end

#failuresObject



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

def failures
  result.failures
end

#messages(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dry/validation/schema/result.rb', line 37

def messages(options = {})
  @messages ||=
    begin
      all_msgs = error_compiler.with(options).(errors.map(&:to_ary))
      hints = hint_compiler.with(options).call

      msgs_data = all_msgs.map do |(name, data)|
        msgs, input =
          if data.is_a?(Hash)
            values = data.values
            [values.map(&:first).flatten.concat(hints[name]).uniq, values[0][1]]
          else
            [data[0].concat(hints[name]).uniq.flatten, data[1]]
          end

        [name, [msgs, input]]
      end

      Hash[msgs_data]
    end
end

#successesObject



59
60
61
# File 'lib/dry/validation/schema/result.rb', line 59

def successes
  result.successes
end

#to_aryObject



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

def to_ary
  errors.map(&:to_ary)
end