Class: Committee::SchemaValidator::OpenAPI3

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

Defined Under Namespace

Classes: OperationWrapper, RequestValidator, ResponseValidator, Router

Instance Method Summary collapse

Constructor Details

#initialize(router, request, validator_option) ⇒ OpenAPI3

Returns a new instance of OpenAPI3.

Parameters:



7
8
9
10
11
12
# File 'lib/committee/schema_validator/open_api_3.rb', line 7

def initialize(router, request, validator_option)
  @router = router
  @request = request
  @operation_object = router.operation_object(request)
  @validator_option = validator_option
end

Instance Method Details

Returns:

  • (Boolean)


51
52
53
# File 'lib/committee/schema_validator/open_api_3.rb', line 51

def link_exist?
  !@operation_object.nil?
end

#request_validate(request) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/committee/schema_validator/open_api_3.rb', line 14

def request_validate(request)
  return unless link_exist?

  request_unpack(request)
  request_schema_validation(request)

  copy_coerced_data_to_params(request)
end

#response_validate(status, headers, response, test_method = false, custom_body_parser = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/committee/schema_validator/open_api_3.rb', line 23

def response_validate(status, headers, response, test_method = false, custom_body_parser = nil)
  full_body = +""
  response.each do |chunk|
    full_body << chunk
  end

  parse_to_json = if validator_option.parse_response_by_content_type
                    content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
                    headers.fetch(content_type_key, nil)&.start_with?('application/json')
                  else
                    true
                  end

  data = if custom_body_parser
           custom_body_parser.call(full_body)
         elsif parse_to_json
           full_body.empty? ? {} : JSON.parse(full_body)
         else
           full_body
         end

  # TODO: refactoring name
  strict = test_method
  Committee::SchemaValidator::OpenAPI3::ResponseValidator.
      new(@operation_object, validator_option).
      call(status, headers, data, strict)
end