Class: Dry::Validation::ErrorCompiler::Input

Inherits:
Dry::Validation::ErrorCompiler show all
Includes:
Deprecations
Defined in:
lib/dry/validation/error_compiler/input.rb

Direct Known Subclasses

HintCompiler

Constant Summary

Constants inherited from Dry::Validation::ErrorCompiler

DEFAULT_RESULT, EMPTY_HINTS, KEY_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from Dry::Validation::ErrorCompiler

#hints, #messages, #options

Instance Method Summary collapse

Methods included from Deprecations

format, #logger, #warn

Methods inherited from Dry::Validation::ErrorCompiler

#call, #dump_messages, #full?, #visit, #visit_attr, #visit_error, #visit_implication, #visit_input, #visit_key, #visit_result, #visit_schema, #visit_val, #with

Constructor Details

#initialize(messages, options) ⇒ Input



24
25
26
27
28
29
30
# File 'lib/dry/validation/error_compiler/input.rb', line 24

def initialize(messages, options)
  super
  @name = options.fetch(:name)
  @input = options.fetch(:input)
  @rule = Array(name).last
  @val_type = input.class
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



22
23
24
# File 'lib/dry/validation/error_compiler/input.rb', line 22

def input
  @input
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/dry/validation/error_compiler/input.rb', line 22

def name
  @name
end

#ruleObject (readonly)

Returns the value of attribute rule.



22
23
24
# File 'lib/dry/validation/error_compiler/input.rb', line 22

def rule
  @rule
end

#val_typeObject (readonly)

Returns the value of attribute val_type.



22
23
24
# File 'lib/dry/validation/error_compiler/input.rb', line 22

def val_type
  @val_type
end

Instance Method Details

#input_visitor(new_name, value) ⇒ Object



130
131
132
# File 'lib/dry/validation/error_compiler/input.rb', line 130

def input_visitor(new_name, value)
  self.class.new(messages, options.merge(name: [*name, *new_name].uniq, input: value))
end

#options_for(predicate, args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dry/validation/error_compiler/input.rb', line 117

def options_for(predicate, args)
  meth = :"options_for_#{predicate}"

  args_map = Hash[args]
  defaults = { name: rule, rule: rule, value: input }.update(args_map)

  if respond_to?(meth)
    defaults.merge!(__send__(meth, args_map))
  end

  defaults
end

#options_for_excluded_from?(args) ⇒ Boolean



99
100
101
# File 'lib/dry/validation/error_compiler/input.rb', line 99

def options_for_excluded_from?(args)
  { list: args[:list].join(', ') }
end

#options_for_exclusion?(args) ⇒ Boolean



94
95
96
97
# File 'lib/dry/validation/error_compiler/input.rb', line 94

def options_for_exclusion?(args)
  warn 'exclusion is deprecated - use excluded_from instead.'
  options_for_excluded_from?(args)
end

#options_for_included_in?(args) ⇒ Boolean



103
104
105
# File 'lib/dry/validation/error_compiler/input.rb', line 103

def options_for_included_in?(args)
  { list: args[:list].join(', ') }
end

#options_for_inclusion?(args) ⇒ Boolean



89
90
91
92
# File 'lib/dry/validation/error_compiler/input.rb', line 89

def options_for_inclusion?(args)
  warn 'inclusion is deprecated - use included_in instead.'
  options_for_included_in?(args)
end

#options_for_size?(args) ⇒ Boolean



107
108
109
110
111
112
113
114
115
# File 'lib/dry/validation/error_compiler/input.rb', line 107

def options_for_size?(args)
  size = args[:size]

  if size.is_a?(Range)
    { left: size.first, right: size.last }
  else
    args
  end
end

#visit_check(node) ⇒ Object



49
50
51
52
# File 'lib/dry/validation/error_compiler/input.rb', line 49

def visit_check(node)
  _, other = node
  visit(other)
end

#visit_each(node) ⇒ Object



32
33
34
# File 'lib/dry/validation/error_compiler/input.rb', line 32

def visit_each(node)
  node.map { |el| visit(el) }
end

#visit_el(node) ⇒ Object



43
44
45
46
47
# File 'lib/dry/validation/error_compiler/input.rb', line 43

def visit_el(node)
  idx, el = node
  path = [*Array(name), idx]
  input_visitor(path, input[idx]).visit(el)
end

#visit_predicate(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dry/validation/error_compiler/input.rb', line 54

def visit_predicate(node)
  predicate, args = node

  lookup_options = options.merge(
    rule: rule, val_type: val_type, arg_type: args.size > 0 && args[0][1].class
  )

  tokens = options_for(predicate, args)
  template = messages[predicate, lookup_options.merge(tokens)]

  unless template
    raise MissingMessageError.new("message for #{predicate} was not found")
  end

  rule_name =
    if rule.is_a?(Symbol)
      messages.rule(rule, lookup_options) || rule
    else
      rule
    end

  text =
    if full?
      "#{rule_name || tokens[:name]} #{template % tokens}"
    else
      template % tokens
    end

  *arg_vals, _ = args.map(&:last)
  message = Message.new(rule, [predicate, arg_vals], text)
  path = [[message], *[tokens[:name], *Array(name).reverse].uniq]

  path.reduce { |a, e| { e => a } }
end

#visit_set(node) ⇒ Object



36
37
38
39
40
41
# File 'lib/dry/validation/error_compiler/input.rb', line 36

def visit_set(node, *)
  result = node.map do |input|
    visit(input)
  end
  merge(result)
end