Class: Dry::Validation::ErrorCompiler

Inherits:
MessageCompiler show all
Defined in:
lib/dry/validation/error_compiler.rb

Instance Attribute Summary

Attributes inherited from MessageCompiler

#default_lookup_options, #locale, #messages, #options

Instance Method Summary collapse

Methods inherited from MessageCompiler

#call, #full?, #initialize, #message_path, #message_text, #message_tokens, #visit, #visit_el, #visit_implication, #visit_key, #visit_predicate, #visit_set, #visit_val, #visit_xor, #with

Constructor Details

This class inherits a constructor from Dry::Validation::MessageCompiler

Instance Method Details

#lookup_options(opts, arg_vals = []) ⇒ Object



72
73
74
# File 'lib/dry/validation/error_compiler.rb', line 72

def lookup_options(opts, arg_vals = [])
  super.update(val_type: opts[:input].class)
end

#message_classObject



10
11
12
# File 'lib/dry/validation/error_compiler.rb', line 10

def message_class
  Message
end

#message_typeObject



6
7
8
# File 'lib/dry/validation/error_compiler.rb', line 6

def message_type
  :failure
end

#visit_check(node, opts = EMPTY_HASH) ⇒ Object



67
68
69
70
# File 'lib/dry/validation/error_compiler.rb', line 67

def visit_check(node, opts = EMPTY_HASH)
  path, other = node
  visit(other, opts.merge(path: Array(path)))
end

#visit_each(node, opts = EMPTY_HASH) ⇒ Object



52
53
54
# File 'lib/dry/validation/error_compiler.rb', line 52

def visit_each(node, opts = EMPTY_HASH)
  node.map { |el| visit(el, opts.merge(each: true)) }
end

#visit_error(node, opts = EMPTY_HASH) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dry/validation/error_compiler.rb', line 14

def visit_error(node, opts = EMPTY_HASH)
  rule, error = node
  node_path = Array(opts.fetch(:path, rule))

  path = if rule.is_a?(Array) && rule.size > node_path.size
           rule
         else
           node_path
         end

  path.compact!

  template = messages[rule, default_lookup_options]

  if template
    predicate, args, tokens = visit(error, opts.merge(path: path, message: false))
    message_class[predicate, path, template % tokens, rule: rule, args: args]
  else
    visit(error, opts.merge(rule: rule, path: path))
  end
end

#visit_input(node, opts = EMPTY_HASH) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/dry/validation/error_compiler.rb', line 36

def visit_input(node, opts = EMPTY_HASH)
  rule, result = node
  opt_rule = opts[:rule]

  if opts[:each] && opt_rule.is_a?(Array)
    visit(result, opts.merge(rule: rule, path: opts[:path] + [opt_rule.last]))
  else
    visit(result, opts.merge(rule: rule))
  end
end

#visit_result(node, opts = EMPTY_HASH) ⇒ Object



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

def visit_result(node, opts = EMPTY_HASH)
  input, other = node
  visit(other, opts.merge(input: input))
end

#visit_schema(node, opts = EMPTY_HASH) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/dry/validation/error_compiler.rb', line 56

def visit_schema(node, opts = EMPTY_HASH)
  path, other = node

  if opts[:path]
    opts[:path] << path.last
    visit(other, opts)
  else
    visit(other, opts.merge(path: [path]))
  end
end