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
Instance Method Summary collapse
-
#add_macro_from_hash(macros, spec) ⇒ Object
-
#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
#add_macro_from_hash(macros, spec) ⇒ Object
131 132 133 134 135 |
# File 'lib/dry/validation/rule.rb', line 131 def add_macro_from_hash(macros, spec) spec.each do |k, v| macros << [k, v.is_a?(Array) ? v : [v]] end end |
#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.
rubocop:disable Metrics/AbcSize
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/dry/validation/rule.rb', line 81 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].nil? values[root].each_with_index do |_, idx| path = [*Schema::Path[root].to_a, idx] next if result.schema_error?(path) evaluator = with(macros: macros, keys: [path], index: idx, &block) failures.concat(evaluator.failures) end end end @block_options = map_keywords(block) if block self end |
#inspect ⇒ String
Return a nice string representation
111 112 113 |
# File 'lib/dry/validation/rule.rb', line 111 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
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dry/validation/rule.rb', line 120 def parse_macros(*args) args.each_with_object([]) do |spec, macros| case spec when Hash add_macro_from_hash(macros, spec) 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 |