Class: Dry::Mutations::Errors::ErrorCompiler

Inherits:
Validation::MessageCompiler
  • Object
show all
Defined in:
lib/dry/mutations/errors/error_compiler.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(schema = nil) ⇒ ErrorCompiler

Returns a new instance of ErrorCompiler.



5
6
7
# File 'lib/dry/mutations/errors/error_compiler.rb', line 5

def initialize schema = nil
  super (schema && schema.message_compiler || ::Dry::Validation::Schema).messages
end

Instance Method Details

#visit_error(node, opts = ::Dry::Validation::EMPTY_HASH) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dry/mutations/errors/error_compiler.rb', line 9

def visit_error(node, opts = ::Dry::Validation::EMPTY_HASH)
  rule, error = node
  node_path = Array(opts.fetch(:path, rule))
  path = (rule.is_a?(Array) && rule.size > node_path.size ? rule : node_path).compact
  text = messages[rule]

  if text
    ErrorAtom.new(
      [*node.first].join('.'),
      predicate,
      ::Dry::Validation::Message.new(node, path, text, rule: rule),
      message: text
    )
  else
    visit(error, opts.merge(path: path))
  end
end