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

Inherits:
Dry::Validation::ErrorCompiler show all
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 inherited from Dry::Validation::ErrorCompiler

#call, #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

Returns a new instance of Input.



6
7
8
9
10
11
12
# File 'lib/dry/validation/error_compiler/input.rb', line 6

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.



4
5
6
# File 'lib/dry/validation/error_compiler/input.rb', line 4

def input
  @input
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/dry/validation/error_compiler/input.rb', line 4

def name
  @name
end

#ruleObject (readonly)

Returns the value of attribute rule.



4
5
6
# File 'lib/dry/validation/error_compiler/input.rb', line 4

def rule
  @rule
end

#val_typeObject (readonly)

Returns the value of attribute val_type.



4
5
6
# File 'lib/dry/validation/error_compiler/input.rb', line 4

def val_type
  @val_type
end

Instance Method Details

#input_visitor(new_name, value) ⇒ Object



143
144
145
# File 'lib/dry/validation/error_compiler/input.rb', line 143

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dry/validation/error_compiler/input.rb', line 131

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

  defaults = { name: rule, rule: rule, value: input }

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

  defaults
end

#options_for_attr?(*args) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/dry/validation/error_compiler/input.rb', line 77

def options_for_attr?(*args)
  { name: args[0][0] }
end

#options_for_eql?(*args) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/dry/validation/error_compiler/input.rb', line 117

def options_for_eql?(*args)
  { eql_value: args[0][0], value: input }
end

#options_for_exclusion?(*args) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/dry/validation/error_compiler/input.rb', line 81

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

#options_for_gt?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def options_for_gt?(*args)
  { num: args[0][0], value: input }
end

#options_for_gteq?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def options_for_gteq?(*args)
  { num: args[0][0], value: input }
end

#options_for_inclusion?(*args) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/dry/validation/error_compiler/input.rb', line 85

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

#options_for_int?(*args) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/dry/validation/error_compiler/input.rb', line 105

def options_for_int?(*args)
  { num: args[0][0], value: input }
end

#options_for_key?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def options_for_key?(*args)
  { name: args[0][0] }
end

#options_for_lt?(*args) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/dry/validation/error_compiler/input.rb', line 97

def options_for_lt?(*args)
  { num: args[0][0], value: input }
end

#options_for_lteq?(*args) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/dry/validation/error_compiler/input.rb', line 101

def options_for_lteq?(*args)
  { num: args[0][0], value: input }
end

#options_for_max_size?(*args) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/dry/validation/error_compiler/input.rb', line 109

def options_for_max_size?(*args)
  { num: args[0][0], value: input }
end

#options_for_min_size?(*args) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/dry/validation/error_compiler/input.rb', line 113

def options_for_min_size?(*args)
  { num: args[0][0], value: input }
end

#options_for_size?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def options_for_size?(*args)
  num = args[0][0]

  if num.is_a?(Range)
    { left: num.first, right: num.last, value: input }
  else
    { num: args[0][0], value: input }
  end
end

#options_for_type?(*args) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/dry/validation/error_compiler/input.rb', line 69

def options_for_type?(*args)
  { type: args[0][0] }
end

#visit_check(node) ⇒ Object



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

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

#visit_each(node) ⇒ Object



14
15
16
# File 'lib/dry/validation/error_compiler/input.rb', line 14

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

#visit_el(node) ⇒ Object



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

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

#visit_predicate(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/validation/error_compiler/input.rb', line 36

def visit_predicate(node)
  predicate, args = node

  lookup_options = options.merge(
    rule: rule, val_type: val_type, arg_type: args[0].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

  message =
    if full?
      "#{rule_name} #{template % tokens}"
    else
      template % tokens
    end

  path = [[message], *[tokens[:name], *Array(name).reverse].uniq]

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

#visit_set(node) ⇒ Object



18
19
20
21
22
23
# File 'lib/dry/validation/error_compiler/input.rb', line 18

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