Class: Committee::SchemaValidator::OpenAPI3::OperationWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/open_api_3/operation_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(request_operation) ⇒ OperationWrapper

# @param request_operation [OpenAPIParser::RequestOperation]



8
9
10
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 8

def initialize(request_operation)
  @request_operation = request_operation
end

Instance Method Details

#coerce_path_parameter(validator_option) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 24

def coerce_path_parameter(validator_option)
  options = build_openapi_parser_path_option(validator_option)
  return {} unless options.coerce_value

  request_operation.validate_path_params(options)
rescue OpenAPIParser::OpenAPIError => e
  raise Committee::InvalidRequest.new(e.message, original_error: e)
end

#http_methodObject



20
21
22
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 20

def http_method
  request_operation.http_method
end

#optional_body?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 55

def optional_body?
  !request_operation.operation_object&.request_body&.required
end

#original_pathObject



16
17
18
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 16

def original_path
  request_operation.original_path
end

#path_paramsObject



12
13
14
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 12

def path_params
  request_operation.path_params
end

#request_content_typesObject



69
70
71
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 69

def request_content_types
  request_operation.operation_object&.request_body&.content&.keys || []
end

#valid_request_content_type?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 59

def valid_request_content_type?(content_type)
  if (request_body = request_operation.operation_object&.request_body)
    !request_body.select_media_type(content_type).nil?
  else
    # if not exist request body object, all content_type allow.
    # because request body object required content field so when it exists there're content type definition.
    true
  end
end

#validate_request_params(path_params, query_params, body_params, headers, validator_option) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 42

def validate_request_params(path_params, query_params, body_params, headers, validator_option)
  ret, err = case request_operation.http_method
        when 'get', 'delete', 'head'
          validate_get_request_params(path_params, query_params, headers, validator_option)
        when 'post', 'put', 'patch', 'options'
          validate_post_request_params(path_params, query_params, body_params, headers, validator_option)
        else
          raise "Committee OpenAPI3 not support #{request_operation.http_method} method"
        end
  raise err if err
  ret
end

#validate_response_params(status_code, headers, response_data, strict, check_header) ⇒ Object

Parameters:

  • strict (Boolean)

    when not content_type or status code definition, raise error



34
35
36
37
38
39
40
# File 'lib/committee/schema_validator/open_api_3/operation_wrapper.rb', line 34

def validate_response_params(status_code, headers, response_data, strict, check_header)
  response_body = OpenAPIParser::RequestOperation::ValidatableResponseBody.new(status_code, response_data, headers)

  return request_operation.validate_response_body(response_body, response_validate_options(strict, check_header))
rescue OpenAPIParser::OpenAPIError => e
  raise Committee::InvalidResponse.new(e.message, original_error: e)
end