class Dry::Validation::Rule

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.

@see Contract#rule

@api public

Public Instance Methods

add_macro_from_hash(macros, spec) click to toggle source
# File lib/dry/validation/rule.rb, line 128
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) click to toggle source

Evaluate the rule within the provided context

@param [Contract] contract @param [Result] result

@api private

# File lib/dry/validation/rule.rb, line 37
def call(contract, result)
  Evaluator.new(
    contract,
    keys: keys,
    macros: macros,
    block_options: block_options,
    result: result,
    values: result.values,
    _context: result.context,
    &block
  )
end
each(*macros, &block) click to toggle source

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.

@example

rule(:nums).each do |index:|
  key([:number, index]).failure("must be greater than 0") if value < 0
end
rule(:nums).each(min: 3)
rule(address: :city) do
   key.failure("oops") if value != 'Munich'
end

@return [Rule]

@api public

# 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.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() click to toggle source

Return a nice string representation

@return [String]

@api public

# File lib/dry/validation/rule.rb, line 108
def inspect
  %(#<#{self.class} keys=#{keys.inspect}>)
end
parse_macros(*args) click to toggle source

Parse function arguments into macros structure

@return [Array]

@api private

# File lib/dry/validation/rule.rb, line 117
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) click to toggle source

Define which macros should be executed

@see Contract#rule @return [Rule]

@api public

# File lib/dry/validation/rule.rb, line 56
def validate(*macros, &block)
  @macros = parse_macros(*macros)
  @block = block if block
  self
end