Class: Dry::Validation::Rule
- Defined in:
- lib/dry/validation/rule.rb
Overview
Rules capture configuration and evaluator blocks
When a rule is applied, it creates an ‘Evaluator` using schema result and its block will be evaluated in the context of the evaluator.
Instance Attribute Summary collapse
- #keys ⇒ Array<Symbol, String, Hash> readonly private
- #macros ⇒ Array<Symbol> readonly private
Attributes inherited from Function
Instance Method Summary collapse
-
#call(contract, result) ⇒ Object
private
Evaluate the rule within the provided context.
-
#each(*macros, &block) ⇒ Rule
Define a validation function for each element of an array.
-
#inspect ⇒ String
Return a nice string representation.
-
#parse_macros(*args) ⇒ Array
private
Parse function arguments into macros structure.
-
#validate(*macros, &block) ⇒ Rule
Define which macros should be executed.
Instance Attribute Details
#keys ⇒ Array<Symbol, String, Hash> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 |
# File 'lib/dry/validation/rule.rb', line 24 option :keys |
#macros ⇒ Array<Symbol> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 |
# File 'lib/dry/validation/rule.rb', line 29 option :macros, default: proc { EMPTY_ARRAY.dup } |
Instance Method Details
#call(contract, result) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Evaluate the rule within the provided context
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dry/validation/rule.rb', line 37 def call(contract, result) Evaluator.new( contract, keys: keys, macros: macros, block_options: , result: result, values: result.values, _context: result.context, &block ) end |
#each(*macros, &block) ⇒ Rule
Define a validation function for each element of an array
The function will be applied only if schema checks passed for a given array item.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dry/validation/rule.rb', line 79 def each(*macros, &block) root = keys[0] macros = parse_macros(*macros) @keys = [] @block = proc do unless result.base_error?(root) || !values.key?(root) values[root].each_with_index do |_, idx| path = [*Schema::Path[root].to_a, idx] next if result.error?(path) evaluator = with(macros: macros, keys: [path], &block) failures.concat(evaluator.failures) end end end @block_options = map_keywords(block) if block self end |
#inspect ⇒ String
Return a nice string representation
108 109 110 |
# File 'lib/dry/validation/rule.rb', line 108 def inspect %(#<#{self.class} keys=#{keys.inspect}>) end |
#parse_macros(*args) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parse function arguments into macros structure
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dry/validation/rule.rb', line 117 def parse_macros(*args) args.each_with_object([]) do |spec, macros| case spec when Hash spec.each { |k, v| macros << [k, Array(v)] } else macros << Array(spec) end end end |
#validate(*macros, &block) ⇒ Rule
Define which macros should be executed
56 57 58 59 60 |
# File 'lib/dry/validation/rule.rb', line 56 def validate(*macros, &block) @macros = parse_macros(*macros) @block = block if block self end |