Class: OpenapiRuby::Testing::RequestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/testing/request_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(document_hash = nil) ⇒ RequestValidator

Returns a new instance of RequestValidator.



6
7
8
# File 'lib/openapi_ruby/testing/request_validator.rb', line 6

def initialize(document_hash = nil)
  @document_hash = document_hash
end

Instance Method Details

#validate(operation:, path_context: nil, params: {}, headers: {}, body: nil, path_params: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openapi_ruby/testing/request_validator.rb', line 10

def validate(operation:, path_context: nil, params: {}, headers: {}, body: nil, path_params: {})
  errors = []

  # Collect all parameters (path-level + operation-level)
  all_parameters = (path_context&.path_parameters || []) + (operation.parameters || [])

  # Validate each declared parameter
  all_parameters.each do |param|
    name = param["name"]
    next unless name

    value = extract_param_value(param, params, headers, path_params)

    if value.nil?
      errors << "Missing required #{param["in"]} parameter: #{name}" if param["required"]
      next
    end

    if param["schema"]
      param_errors = validate_value(value, param["schema"], "#{param["in"]} parameter '#{name}'")
      errors.concat(param_errors)
    end
  end

  # Validate request body
  errors.concat(validate_request_body(operation, body))

  errors
end