Class: Dry::Validation::InputTypeCompiler

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

Constant Summary collapse

TYPES =
{
  default: 'string',
  none?: 'form.nil',
  bool?: 'form.bool',
  str?: 'string',
  int?: 'form.int',
  float?: 'form.float',
  decimal?: 'form.decimal',
  date?: 'form.date',
  date_time?: 'form.date_time',
  time?: 'form.time'
}.freeze
DEFAULT_TYPE_NODE =
[[:type, 'string']].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputTypeCompiler

Returns a new instance of InputTypeCompiler.



24
25
26
# File 'lib/dry/validation/input_type_compiler.rb', line 24

def initialize
  @type_compiler = Dry::Data::Compiler.new(Dry::Data)
end

Instance Attribute Details

#type_compilerObject (readonly)

Returns the value of attribute type_compiler.



7
8
9
# File 'lib/dry/validation/input_type_compiler.rb', line 7

def type_compiler
  @type_compiler
end

Instance Method Details

#call(ast) ⇒ Object



28
29
30
31
# File 'lib/dry/validation/input_type_compiler.rb', line 28

def call(ast)
  schema = ast.map { |node| visit(node) }
  type_compiler.([:type, ['hash', [:symbolized, schema]]])
end

#visit(node, *args) ⇒ Object



33
34
35
# File 'lib/dry/validation/input_type_compiler.rb', line 33

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

#visit_and(node, first = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dry/validation/input_type_compiler.rb', line 42

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 - DEFAULT_TYPE_NODE).first
    end
  end
end

#visit_each(node, *args) ⇒ Object



74
75
76
# File 'lib/dry/validation/input_type_compiler.rb', line 74

def visit_each(node, *args)
  [:type, ['form.array', visit(node[1], *args)]]
end

#visit_implication(node) ⇒ Object



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

def visit_implication(node)
  key, types = node
  [:key, [visit(key), visit(types, false)]]
end

#visit_key(node, *args) ⇒ Object



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

def visit_key(node, *args)
  node[0].to_s
end

#visit_or(node, *args) ⇒ Object



37
38
39
40
# File 'lib/dry/validation/input_type_compiler.rb', line 37

def visit_or(node, *args)
  left, right = node
  [:sum, [visit(left, *args), visit(right, *args)]]
end

#visit_predicate(node, *args) ⇒ Object



78
79
80
# File 'lib/dry/validation/input_type_compiler.rb', line 78

def visit_predicate(node, *args)
  [:type, TYPES[node[0]] || TYPES[:default]]
end

#visit_set(node, *args) ⇒ Object



70
71
72
# File 'lib/dry/validation/input_type_compiler.rb', line 70

def visit_set(node, *args)
  [:type, ['form.hash', [:symbolized, node[1].map { |n| visit(n) }]]]
end

#visit_val(node, *args) ⇒ Object



66
67
68
# File 'lib/dry/validation/input_type_compiler.rb', line 66

def visit_val(node, *args)
  visit(node[1], false)
end