Module: StubRequests::Concerns::ArgumentValidation

Extended by:
ArgumentValidation
Included in:
ArgumentValidation, Property, Property::Validator
Defined in:
lib/stub_requests/concerns/argument_validation.rb

Overview

Module ArgumentValidation provides validation of method arguments

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#validate!(name:, value:, type:) ⇒ void

This method returns an undefined value.

Require the value to be any of the types past in

Parameters:

  • name (Symbol)

    the name of the argument

  • value (Object)

    the actual value of the argument

  • type (Array, Class, Module)

    nil the expected argument value class

Raises:

Since:

  • 0.1.0



31
32
33
34
# File 'lib/stub_requests/concerns/argument_validation.rb', line 31

def validate!(name:, value:, type:)
  validate_type!(:name, name, [Symbol, String]) unless name
  validate_type!(name, value, type) if type
end

#validate_type!(name, value, type) ⇒ Object

Raises:

Since:

  • 0.1.0



36
37
38
39
40
41
42
43
44
# File 'lib/stub_requests/concerns/argument_validation.rb', line 36

def validate_type!(name, value, type)
  expected_types = Array(type).flatten
  return if expected_types.any? { |is_a| value.is_a?(is_a) }

  raise StubRequests::InvalidArgumentType,
        name: name,
        actual: value.class,
        expected: expected_types
end