Class: Servactory::TestKit::Rspec::Helpers::InputValidator

Inherits:
Object
  • Object
show all
Includes:
Concerns::ErrorMessages
Defined in:
lib/servactory/test_kit/rspec/helpers/input_validator.rb

Overview

Validates mock input values against service definitions.

## Purpose

Ensures that mocked input arguments match the service’s input definitions in terms of names. Called automatically when using ‘.with()` on the fluent builder.

## Usage

Called automatically from ServiceMockBuilder:

“‘ruby allow_service(MyService)

.with(user_id: 123)
.succeeds(result: "ok")

“‘

Can also be called directly:

“‘ruby InputValidator.validate!(

service_class: MyService,
inputs_matcher: { user_id: 123 }

) “‘

## Supported Matcher Types

  • Hash - validates all keys against service inputs

  • ‘including(hash)` - validates specified keys only

  • ‘any_inputs` - skips validation (accepts anything)

  • ‘no_inputs` - validates service has no required inputs

  • ‘excluding(hash)` - skips validation (cannot validate exclusion)

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_class:, inputs_matcher:) ⇒ InputValidator

Creates a new validator instance.

Parameters:

  • service_class (Class)

    The service class

  • inputs_matcher (Hash, Object)

    Input values or matcher



64
65
66
67
# File 'lib/servactory/test_kit/rspec/helpers/input_validator.rb', line 64

def initialize(service_class:, inputs_matcher:)
  @service_class = service_class
  @inputs_matcher = inputs_matcher
end

Class Method Details

.validate!(service_class:, inputs_matcher:) ⇒ void

This method returns an undefined value.

Validates inputs and raises on failure.

Parameters:

  • service_class (Class)

    The service class

  • inputs_matcher (Hash, Object)

    Input values or matcher

Raises:



54
55
56
# File 'lib/servactory/test_kit/rspec/helpers/input_validator.rb', line 54

def validate!(service_class:, inputs_matcher:)
  new(service_class:, inputs_matcher:).validate!
end

Instance Method Details

#validate!void

This method returns an undefined value.

Runs validation based on matcher type.

Raises:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/servactory/test_kit/rspec/helpers/input_validator.rb', line 73

def validate!
  keys_to_validate = extract_keys_for_validation

  return if keys_to_validate.nil?

  if keys_to_validate == :no_args
    validate_service_has_no_required_inputs!
  else
    validate_input_names!(keys_to_validate)
  end
end