Class: Dry::Validation::MessageCompiler

Inherits:
Object
  • Object
show all
Includes:
Core::Constants
Defined in:
lib/dry/validation/message_compiler.rb,
lib/dry/validation/message_compiler/visitor_opts.rb

Defined Under Namespace

Classes: VisitorOpts

Constant Summary collapse

EMPTY_OPTS =
VisitorOpts.new
LIST_SEPARATOR =
', '.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MessageCompiler.



16
17
18
19
20
21
22
23
# File 'lib/dry/validation/message_compiler.rb', line 16

def initialize(messages, options = {})
  @messages = messages
  @options = options
  @full = @options.fetch(:full, false)
  @hints = @options.fetch(:hints, true)
  @locale = @options.fetch(:locale, messages.default_locale)
  @default_lookup_options = { locale: locale }
end

Instance Attribute Details

#default_lookup_optionsObject (readonly)

Returns the value of attribute default_lookup_options.



11
12
13
# File 'lib/dry/validation/message_compiler.rb', line 11

def default_lookup_options
  @default_lookup_options
end

#localeObject (readonly)

Returns the value of attribute locale.



11
12
13
# File 'lib/dry/validation/message_compiler.rb', line 11

def locale
  @locale
end

#messagesObject (readonly)

Returns the value of attribute messages.



11
12
13
# File 'lib/dry/validation/message_compiler.rb', line 11

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dry/validation/message_compiler.rb', line 11

def options
  @options
end

Instance Method Details

#call(ast) ⇒ Object



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

def call(ast)
  MessageSet[ast.map { |node| visit(node) }, failures: options.fetch(:failures, true)]
end

#full?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dry/validation/message_compiler.rb', line 25

def full?
  @full
end

#hints?Boolean

Returns:

  • (Boolean)


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

def hints?
  @hints
end

#lookup_options(arg_vals: [], input: nil) ⇒ Object



158
159
160
161
162
163
# File 'lib/dry/validation/message_compiler.rb', line 158

def lookup_options(arg_vals: [], input: nil)
  default_lookup_options.merge(
    arg_type: arg_vals.size == 1 && arg_vals[0].class,
    val_type: input.class
  )
end

#message_text(rule, template, tokens, opts) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/dry/validation/message_compiler.rb', line 165

def message_text(rule, template, tokens, opts)
  text = template % tokens

  if full?
    rule_name = messages.rule(rule, opts) || rule
    "#{rule_name} #{text}"
  else
    text
  end
end

#message_tokens(args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dry/validation/message_compiler.rb', line 176

def message_tokens(args)
  args.each_with_object({}) { |arg, hash|
    case arg[1]
    when Array
      hash[arg[0]] = arg[1].join(LIST_SEPARATOR)
    when Range
      hash["#{arg[0]}_left".to_sym] = arg[1].first
      hash["#{arg[0]}_right".to_sym] = arg[1].last
    else
      hash[arg[0]] = arg[1]
    end
  }
end

#visit(node, *args) ⇒ Object



42
43
44
# File 'lib/dry/validation/message_compiler.rb', line 42

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

#visit_and(node, opts = EMPTY_OPTS) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/dry/validation/message_compiler.rb', line 80

def visit_and(node, opts = EMPTY_OPTS)
  left, right = node.map { |n| visit(n, opts) }

  if right
    [left, right]
  else
    left
  end
end

#visit_check(node, opts = EMPTY_OPTS) ⇒ Object



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

def visit_check(node, opts = EMPTY_OPTS)
  keys, other = node
  visit(other, opts.(path: keys.last, check: true))
end

#visit_each(node, opts = EMPTY_OPTS) ⇒ Object



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

def visit_each(node, opts = EMPTY_OPTS)
  # TODO: we can still generate a hint for elements here!
  []
end

#visit_failure(node, opts = EMPTY_OPTS) ⇒ Object



46
47
48
49
# File 'lib/dry/validation/message_compiler.rb', line 46

def visit_failure(node, opts = EMPTY_OPTS)
  rule, other = node
  visit(other, opts.(rule: rule))
end

#visit_hint(node, opts = EMPTY_OPTS) ⇒ Object



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

def visit_hint(node, opts = EMPTY_OPTS)
  if hints?
    visit(node, opts.(message_type: :hint))
  end
end

#visit_implication(node, *args) ⇒ Object



144
145
146
147
# File 'lib/dry/validation/message_compiler.rb', line 144

def visit_implication(node, *args)
  _, right = node
  visit(right, *args)
end

#visit_key(node, opts = EMPTY_OPTS) ⇒ Object



135
136
137
138
# File 'lib/dry/validation/message_compiler.rb', line 135

def visit_key(node, opts = EMPTY_OPTS)
  name, other = node
  visit(other, opts.(path: name))
end

#visit_not(node, opts = EMPTY_OPTS) ⇒ Object



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

def visit_not(node, opts = EMPTY_OPTS)
  visit(node, opts.(not: true))
end

#visit_or(node, opts = EMPTY_OPTS) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/validation/message_compiler.rb', line 90

def visit_or(node, opts = EMPTY_OPTS)
  left, right = node.map { |n| visit(n, opts) }

  if [left, right].flatten.map(&:path).uniq.size == 1
    Message::Or.new(left, right, -> k { messages[k, default_lookup_options] })
  elsif right.is_a?(Array)
    right
  else
    [left, right]
  end
end

#visit_predicate(node, base_opts = EMPTY_OPTS) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dry/validation/message_compiler.rb', line 102

def visit_predicate(node, base_opts = EMPTY_OPTS)
  predicate, args = node

  *arg_vals, val = args.map(&:last)
  tokens = message_tokens(args)

  input = val != Undefined ? val : nil

  options = base_opts.update(lookup_options(arg_vals: arg_vals, input: input))
  msg_opts = options.update(tokens)

  rule = msg_opts[:rule]
  path = msg_opts[:path]

  template = messages[rule] || messages[predicate, msg_opts]

  unless template
    raise MissingMessageError, "message for #{predicate} was not found"
  end

  text = message_text(rule, template, tokens, options)

  message_class = options[:message_type] == :hint ? Hint : Message

  message_class[
    predicate, path, text,
    args: arg_vals,
    input: input,
    rule: rule,
    check: base_opts[:check]
  ]
end

#visit_rule(node, opts = EMPTY_OPTS) ⇒ Object



71
72
73
74
# File 'lib/dry/validation/message_compiler.rb', line 71

def visit_rule(node, opts = EMPTY_OPTS)
  name, other = node
  visit(other, opts.(rule: name))
end

#visit_schema(node, opts = EMPTY_OPTS) ⇒ Object



76
77
78
# File 'lib/dry/validation/message_compiler.rb', line 76

def visit_schema(node, opts = EMPTY_OPTS)
  node.rule_ast.map { |rule| visit(rule, opts) }
end

#visit_set(node, opts = EMPTY_OPTS) ⇒ Object



140
141
142
# File 'lib/dry/validation/message_compiler.rb', line 140

def visit_set(node, opts = EMPTY_OPTS)
  node.map { |el| visit(el, opts) }
end

#visit_type(node, opts = EMPTY_OPTS) ⇒ Object



154
155
156
# File 'lib/dry/validation/message_compiler.rb', line 154

def visit_type(node, opts = EMPTY_OPTS)
  visit(node.rule.to_ast, opts)
end

#visit_xor(node, opts = EMPTY_OPTS) ⇒ Object



149
150
151
152
# File 'lib/dry/validation/message_compiler.rb', line 149

def visit_xor(node, opts = EMPTY_OPTS)
  left, right = node
  [visit(left, opts), visit(right, opts)].uniq
end

#with(new_options) ⇒ Object



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

def with(new_options)
  return self if new_options.empty?
  self.class.new(messages, options.merge(new_options))
end