Class: Dry::Validation::Macros::Container

Inherits:
Object
  • Object
show all
Includes:
Container::Mixin
Defined in:
lib/dry/validation/macros.rb

Overview

Registry for macros

Instance Method Summary collapse

Instance Method Details

#register(name, *args, &block) ⇒ self

Register a new macro

Examples:

in a contract class

class MyContract < Dry::Validation::Contract
  register_macro(:even_numbers) do
    key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
  end
end

Parameters:

  • name (Symbol)

    The name of the macro

Returns:

  • (self)

61
62
63
64
65
# File 'lib/dry/validation/macros.rb', line 61

def register(name, *args, &block)
  macro = Macro.new(name, args: args, block: block)
  super(name, macro, call: false, &nil)
  self
end