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
- #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
Returns a new instance of ErrorCompiler.
10 11 12 13 14 15 |
# File 'lib/dry/validation/error_compiler.rb', line 10 def initialize(, = {}) @messages = @options = Hash[] @hints = @options.fetch(:hints, {}) @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 @messages end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/dry/validation/error_compiler.rb', line 4 def @options 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 |
#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
78 79 80 81 |
# File 'lib/dry/validation/error_compiler.rb', line 78 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 |
# 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) else merge_hints(result) end end end |
#visit_implication(node) ⇒ Object
68 69 70 71 |
# File 'lib/dry/validation/error_compiler.rb', line 68 def visit_implication(node) _, right = node visit(right) end |
#visit_input(node, path = nil) ⇒ Object
58 59 60 61 |
# File 'lib/dry/validation/error_compiler.rb', line 58 def visit_input(node, path = nil) name, result = node visit(result, path || name) end |
#visit_key(rule) ⇒ Object
73 74 75 76 |
# File 'lib/dry/validation/error_compiler.rb', line 73 def visit_key(rule) _, predicate = rule visit(predicate) end |
#visit_result(node, name = nil) ⇒ Object
63 64 65 66 |
# File 'lib/dry/validation/error_compiler.rb', line 63 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
83 84 85 |
# File 'lib/dry/validation/error_compiler.rb', line 83 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 |