Class: Committee::SchemaValidator::HyperSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/hyper_schema.rb,
lib/committee/schema_validator/hyper_schema/router.rb,
lib/committee/schema_validator/hyper_schema/parameter_coercer.rb,
lib/committee/schema_validator/hyper_schema/request_validator.rb,
lib/committee/schema_validator/hyper_schema/response_generator.rb,
lib/committee/schema_validator/hyper_schema/response_validator.rb,
lib/committee/schema_validator/hyper_schema/string_params_coercer.rb

Defined Under Namespace

Classes: ParameterCoercer, RequestValidator, ResponseGenerator, ResponseValidator, Router, StringParamsCoercer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, request, validator_option) ⇒ HyperSchema

Returns a new instance of HyperSchema.



8
9
10
11
# File 'lib/committee/schema_validator/hyper_schema.rb', line 8

def initialize(router, request, validator_option)
  @link, @param_matches = router.find_request_link(request)
  @validator_option = validator_option
end

Instance Attribute Details

Returns the value of attribute link.



6
7
8
# File 'lib/committee/schema_validator/hyper_schema.rb', line 6

def link
  @link
end

#param_matchesObject (readonly)

Returns the value of attribute param_matches.



6
7
8
# File 'lib/committee/schema_validator/hyper_schema.rb', line 6

def param_matches
  @param_matches
end

#validator_optionObject (readonly)

Returns the value of attribute validator_option.



6
7
8
# File 'lib/committee/schema_validator/hyper_schema.rb', line 6

def validator_option
  @validator_option
end

Instance Method Details

Returns:

  • (Boolean)


47
48
49
# File 'lib/committee/schema_validator/hyper_schema.rb', line 47

def link_exist?
  !link.nil?
end

#request_validate(request) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/committee/schema_validator/hyper_schema.rb', line 13

def request_validate(request)
  request_unpack(request)

  request_schema_validation(request)
  parameter_coerce!(request, link, validator_option.params_key)
  parameter_coerce!(request, link, "rack.request.query_hash") if link_exist? && !request.GET.nil? && !link.schema.nil?
end

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



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

def response_validate(status, headers, response, _test_method = false, custom_body_parser = nil)
  return unless link_exist?

  full_body = +""
  response.each do |chunk|
    full_body << chunk
  end

  data = if custom_body_parser
           custom_body_parser.call(full_body)
  elsif !full_body.empty?
    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

    JSON.parse(full_body) if parse_to_json
  else
    {}
  end

  Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: validator_option.validate_success_only, allow_blank_structures: validator_option.allow_blank_structures).call(status, headers, data)
end