Class: Dry::Validation::ErrorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::ErrorCompiler
show all
- Defined in:
- lib/dry/validation/error_compiler.rb
Constant Summary
collapse
- DEFAULT_RESULT =
{}.freeze
- KEY_SEPARATOR =
'.'.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#call(ast) ⇒ Object
-
#initialize(messages, options = {}) ⇒ ErrorCompiler
constructor
A new instance of ErrorCompiler.
-
#visit(node, *args) ⇒ Object
-
#visit_attr(rule, name, value) ⇒ Object
-
#visit_attr?(*args, _value) ⇒ Boolean
-
#visit_check(node) ⇒ Object
-
#visit_eql?(*args, value) ⇒ Boolean
-
#visit_error(error) ⇒ Object
-
#visit_exclusion?(*args, _value) ⇒ Boolean
-
#visit_group(_, name, _) ⇒ Object
-
#visit_gt?(*args, value) ⇒ Boolean
-
#visit_gteq?(*args, value) ⇒ Boolean
-
#visit_inclusion?(*args, _value) ⇒ Boolean
-
#visit_input(input) ⇒ Object
-
#visit_int?(*args, value) ⇒ Boolean
-
#visit_key(rule, name, value) ⇒ Object
-
#visit_key?(*args, _value) ⇒ Boolean
-
#visit_lt?(*args, value) ⇒ Boolean
-
#visit_lteq?(*args, value) ⇒ Boolean
-
#visit_max_size?(*args, value) ⇒ Boolean
-
#visit_min_size?(*args, value) ⇒ Boolean
-
#visit_predicate(predicate, value, name) ⇒ Object
-
#visit_size?(*args, value) ⇒ Boolean
-
#visit_val(rule, name, value) ⇒ Object
-
#with(new_options) ⇒ Object
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
152
153
154
|
# File 'lib/dry/validation/error_compiler.rb', line 152
def method_missing(_meth, *args)
{ value: args[1] }
end
|
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
4
5
6
|
# File 'lib/dry/validation/error_compiler.rb', line 4
def messages
@messages
end
|
#options ⇒ Object
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
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
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
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
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
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
92
93
94
|
# File 'lib/dry/validation/error_compiler.rb', line 92
def visit_inclusion?(*args, _value)
{ list: args[0][0].join(', ') }
end
|
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
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
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
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
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
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
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
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
|