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
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 98
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 46
def visit_and(node, first = true)
if first
name, type = node.map { |n| visit(n, false) }.uniq
if name.is_a?(Array)
type
else
[:key, [name, type]]
end
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
84
85
86
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 84
def visit_each(node, *args)
array_node(visit(node, *args))
end
|
#visit_implication(node) ⇒ Object
66
67
68
69
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 66
def visit_implication(node)
key, types = node
[:key, [visit(key), visit(types, false)]]
end
|
#visit_key(node, *args) ⇒ Object
71
72
73
74
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 71
def visit_key(node, *args)
_, other = node
visit(other, *args)
end
|
#visit_or(node, *args) ⇒ Object
41
42
43
44
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 41
def visit_or(node, *args)
left, right = node
[:sum, [visit(left, *args), visit(right, *args)]]
end
|
#visit_predicate(node) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 88
def visit_predicate(node, *)
id, args = node
if id == :key?
args[0][1]
else
type(id, args.map(&:last))
end
end
|
#visit_schema(schema, *args) ⇒ Object
37
38
39
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 37
def visit_schema(schema, *args)
hash_node(schema.input_processor_ast(identifier))
end
|
#visit_set(node) ⇒ Object
80
81
82
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 80
def visit_set(node, *)
hash_node(node.map { |n| visit(n) })
end
|
#visit_type(type, *args) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# 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]]
elsif type.respond_to?(:rule)
visit(type.rule.to_ast, *args)
else
DEFAULT_TYPE_NODE.first
end
end
|
#visit_val(node, *args) ⇒ Object
76
77
78
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 76
def visit_val(node, *args)
visit(node, *args)
end
|