Class: Dry::Validation::ErrorCompiler

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

Direct Known Subclasses

HintCompiler

Constant Summary collapse

DEFAULT_RESULT =
{}.freeze
KEY_SEPARATOR =
'.'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages, options = {}) ⇒ ErrorCompiler

Returns a new instance of ErrorCompiler.



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

def initialize(messages, options = {})
  @messages = messages
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_meth, *args) ⇒ Object (private)



152
153
154
# File 'lib/dry/validation/error_compiler.rb', line 152

def method_missing(_meth, *args)
  { value: args[1] }
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



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

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#call(ast) ⇒ Object



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

def call(ast)
  merge(ast.map { |node| visit(node) }) || DEFAULT_RESULT
end

#visit(node, *args) ⇒ Object



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

def visit(node, *args)
  __send__(:"visit_#{node[0]}", node[1], *args)
end

#visit_attr(rule, name, value) ⇒ Object



57
58
59
60
# File 'lib/dry/validation/error_compiler.rb', line 57

def visit_attr(rule, name, value)
  _, predicate = rule
  visit(predicate, value, name)
end

#visit_attr?(*args, _value) ⇒ Boolean

Returns:

  • (Boolean)


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

def visit_attr?(*args, _value)
  { name: args[0][0] }
end

#visit_check(node) ⇒ Object



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

def visit_check(node, *)
  name = normalize_name(node[0])
  messages[name, rule: name]
end

#visit_eql?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/dry/validation/error_compiler.rb', line 124

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

#visit_error(error) ⇒ Object



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

def visit_error(error)
  visit(error)
end

#visit_exclusion?(*args, _value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_group(_, name, _) ⇒ Object



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

def visit_group(_, name, _)
  messages[name, rule: name]
end

#visit_gt?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_gteq?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_inclusion?(*args, _value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_input(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dry/validation/error_compiler.rb', line 30

def visit_input(input, *)
  name = normalize_name(input[0])
  _, value, rules = input
  errors = [rules.map { |rule| visit(rule, name, value) }, value]

  if input[0].is_a?(Hash)
    root, sub = input[0].to_a.flatten
    { root => { sub => errors } }
  else
    { input[0] => errors }
  end
end

#visit_int?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_key(rule, name, value) ⇒ Object



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

def visit_key(rule, name, value)
  _, predicate = rule
  visit(predicate, value, name)
end

#visit_key?(*args, _value) ⇒ Boolean

Returns:

  • (Boolean)


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

def visit_key?(*args, _value)
  { name: args[0][0] }
end

#visit_lt?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_lteq?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_max_size?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#visit_min_size?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/dry/validation/error_compiler.rb', line 120

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

#visit_predicate(predicate, value, name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dry/validation/error_compiler.rb', line 67

def visit_predicate(predicate, value, name)
  predicate_name, args = predicate

  lookup_options = options.merge(
    rule: name, val_type: value.class, arg_type: args[0].class
  )

  template = messages[predicate_name, lookup_options]
  tokens = visit(predicate, value).merge(name: name)

  template % tokens
end

#visit_size?(*args, value) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
# File 'lib/dry/validation/error_compiler.rb', line 128

def visit_size?(*args, value)
  num = args[0][0]

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

#visit_val(rule, name, value) ⇒ Object



62
63
64
65
# File 'lib/dry/validation/error_compiler.rb', line 62

def visit_val(rule, name, value)
  name, predicate = rule
  visit(predicate, value, name)
end

#with(new_options) ⇒ Object



18
19
20
# File 'lib/dry/validation/error_compiler.rb', line 18

def with(new_options)
  self.class.new(messages, options.merge(new_options))
end