Module: CMDx::Validators::Validate

Extended by:
Validate
Included in:
Validate
Defined in:
lib/cmdx/validators/validate.rb

Overview

Invokes an inline ‘:validate` handler. Used by #validate for each handler passed under the `:validate` option.

Instance Method Summary collapse

Instance Method Details

#call(task, value, handler) ⇒ Validators::Failure, ...

Note:

Symbol handlers are dispatched via ‘send` so private helpers on the task are reachable. Handlers are baked into class definitions; never derive them from untrusted input.

Returns handler’s return value.

Parameters:

  • task (Task)

    receiver for Symbol/Proc handlers, also passed to callable handlers

  • value (Object)
  • handler (Symbol, Proc, #call)

Returns:

Raises:

  • (ArgumentError)

    when ‘handler` isn’t a supported type



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cmdx/validators/validate.rb', line 19

def call(task, value, handler)
  case handler
  when Symbol
    task.send(handler, value)
  when Proc
    task.instance_exec(value, &handler)
  else
    return handler.call(value, task) if handler.respond_to?(:call)

    raise ArgumentError, <<~MSG.chomp
      validate handler must be a Symbol, Proc, or respond to #call (got #{handler.class}).
      See https://drexed.github.io/cmdx/inputs/validations/#inline-validate-callable
    MSG
  end
end