Class: Dry::Validation::InputProcessorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::InputProcessorCompiler
show all
- Defined in:
- lib/dry/validation/input_processor_compiler.rb
Defined Under Namespace
Classes: Form, JSON, Sanitizer
Constant Summary
collapse
- DEFAULT_TYPE_NODE =
[[:type, 'string']].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of InputProcessorCompiler.
11
12
13
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 11
def initialize
@type_compiler = Dry::Types::Compiler.new(Dry::Types)
end
|
Instance Attribute Details
#type_compiler ⇒ Object
Returns the value of attribute type_compiler.
7
8
9
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 7
def type_compiler
@type_compiler
end
|
Instance Method Details
#call(ast) ⇒ Object
15
16
17
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 15
def call(ast)
type_compiler.(hash_node(schema_ast(ast)))
end
|
#schema_ast(ast) ⇒ Object
19
20
21
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 19
def schema_ast(ast)
ast.map { |node| visit(node) }
end
|
#type(predicate, args) ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 91
def type(predicate, args)
default = self.class::PREDICATE_MAP[:default]
if predicate == :type?
const = args[0]
[:type, self.class::CONST_MAP[const] || Types.identifier(const)]
else
[:type, self.class::PREDICATE_MAP[predicate] || default]
end
end
|
#visit(node, *args) ⇒ Object
23
24
25
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 23
def visit(node, *args)
send(:"visit_#{node[0]}", node[1], *args)
end
|
#visit_and(node, first = true) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 44
def visit_and(node, first = true)
if first
name, type = node.map { |n| visit(n, false) }.uniq
[:key, [name, type]]
else
result = node.map { |n| visit(n, first) }.uniq
if result.size == 1
result.first
else
(result - self.class::DEFAULT_TYPE_NODE).last
end
end
end
|
#visit_each(node, *args) ⇒ Object
77
78
79
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 77
def visit_each(node, *args)
array_node(visit(node, *args))
end
|
#visit_implication(node) ⇒ Object
59
60
61
62
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 59
def visit_implication(node)
key, types = node
[:key, [visit(key), visit(types, false)]]
end
|
#visit_key(node, *args) ⇒ Object
64
65
66
67
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 64
def visit_key(node, *args)
_, other = node
visit(other, *args)
end
|
#visit_or(node, *args) ⇒ Object
39
40
41
42
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 39
def visit_or(node, *args)
left, right = node
[:sum, [visit(left, *args), visit(right, *args)]]
end
|
#visit_predicate(node) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 81
def visit_predicate(node, *)
id, args = node
if id == :key?
args[0]
else
type(id, args)
end
end
|
#visit_schema(node, *args) ⇒ Object
35
36
37
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 35
def visit_schema(node, *args)
hash_node(node.input_processor_ast(identifier))
end
|
#visit_set(node) ⇒ Object
73
74
75
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 73
def visit_set(node, *)
hash_node(node.map { |n| visit(n) })
end
|
#visit_type(type, *args) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 27
def visit_type(type, *args)
if type.is_a?(Types::Constructor)
[:constructor, [type.primitive, type.fn]]
else
DEFAULT_TYPE_NODE
end
end
|
#visit_val(node, *args) ⇒ Object
69
70
71
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 69
def visit_val(node, *args)
visit(node, *args)
end
|