Class: Dry::Validation::ErrorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::ErrorCompiler
- Defined in:
- lib/dry/validation/error_compiler.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Input
Constant Summary collapse
- DEFAULT_RESULT =
{}.freeze
- EMPTY_HINTS =
[].freeze
- KEY_SEPARATOR =
'.'.freeze
Instance Attribute Summary collapse
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(ast, *args) ⇒ Object
- #dump_messages(hash) ⇒ Object
- #full? ⇒ Boolean
-
#initialize(messages, options = {}) ⇒ ErrorCompiler
constructor
A new instance of ErrorCompiler.
- #visit(node, *args) ⇒ Object
- #visit_attr(rule) ⇒ Object
- #visit_error(error, schema = false) ⇒ Object
- #visit_implication(node) ⇒ Object
- #visit_input(node, path = nil) ⇒ Object
- #visit_key(rule) ⇒ Object
- #visit_result(node, name = nil) ⇒ Object
- #visit_schema(node, *args) ⇒ Object
- #visit_set(node, *args) ⇒ Object
- #visit_val(node) ⇒ Object
- #with(new_options) ⇒ Object
Constructor Details
#initialize(messages, options = {}) ⇒ ErrorCompiler
10 11 12 13 14 15 |
# File 'lib/dry/validation/error_compiler.rb', line 10 def initialize(, = {}) = = Hash[] @hints = .fetch(:hints, DEFAULT_RESULT) @full = .fetch(:full, false) end |
Instance Attribute Details
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
4 5 6 |
# File 'lib/dry/validation/error_compiler.rb', line 4 def hints @hints end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
4 5 6 |
# File 'lib/dry/validation/error_compiler.rb', line 4 def end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/dry/validation/error_compiler.rb', line 4 def end |
Instance Method Details
#call(ast, *args) ⇒ Object
21 22 23 |
# File 'lib/dry/validation/error_compiler.rb', line 21 def call(ast, *args) merge(ast.map { |node| visit(node, *args) }) || DEFAULT_RESULT end |
#dump_messages(hash) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/dry/validation/error_compiler.rb', line 91 def (hash) hash.each_with_object({}) do |(key, val), res| res[key] = case val when Hash then (val) when Array then val.map(&:to_s) end end end |
#full? ⇒ Boolean
17 18 19 |
# File 'lib/dry/validation/error_compiler.rb', line 17 def full? @full end |
#visit(node, *args) ⇒ Object
29 30 31 |
# File 'lib/dry/validation/error_compiler.rb', line 29 def visit(node, *args) __send__(:"visit_#{node[0]}", node[1], *args) end |
#visit_attr(rule) ⇒ Object
82 83 84 85 |
# File 'lib/dry/validation/error_compiler.rb', line 82 def visit_attr(rule) _, predicate = rule visit(predicate) end |
#visit_error(error, schema = false) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dry/validation/error_compiler.rb', line 41 def visit_error(error, schema = false) name, other = error = [name] if { name => [] } else result = schema ? visit(other, name) : visit(other) if result.is_a?(Array) merge(result) elsif !schema merge_hints(result) elsif schema merge_hints(result, hints[schema] || DEFAULT_RESULT) else result end end end |
#visit_implication(node) ⇒ Object
72 73 74 75 |
# File 'lib/dry/validation/error_compiler.rb', line 72 def visit_implication(node) _, right = node visit(right) end |
#visit_input(node, path = nil) ⇒ Object
62 63 64 65 |
# File 'lib/dry/validation/error_compiler.rb', line 62 def visit_input(node, path = nil) name, result = node visit(result, path || name) end |
#visit_key(rule) ⇒ Object
77 78 79 80 |
# File 'lib/dry/validation/error_compiler.rb', line 77 def visit_key(rule) _, predicate = rule visit(predicate) end |
#visit_result(node, name = nil) ⇒ Object
67 68 69 70 |
# File 'lib/dry/validation/error_compiler.rb', line 67 def visit_result(node, name = nil) value, other = node input_visitor(name, value).visit(other) end |
#visit_schema(node, *args) ⇒ Object
33 34 35 |
# File 'lib/dry/validation/error_compiler.rb', line 33 def visit_schema(node, *args) visit_error(node[1], true) end |
#visit_set(node, *args) ⇒ Object
37 38 39 |
# File 'lib/dry/validation/error_compiler.rb', line 37 def visit_set(node, *args) call(node, *args) end |
#visit_val(node) ⇒ Object
87 88 89 |
# File 'lib/dry/validation/error_compiler.rb', line 87 def visit_val(node) visit(node) end |
#with(new_options) ⇒ Object
25 26 27 |
# File 'lib/dry/validation/error_compiler.rb', line 25 def with() self.class.new(, .merge()) end |