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)


39
40
41
# File 'lib/committee/schema_validator/hyper_schema.rb', line 39

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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/committee/schema_validator/hyper_schema.rb', line 21

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

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

  data = {}
  unless full_body.empty?
    parse_to_json = !validator_option.parse_response_by_content_type ||
                    headers.fetch('Content-Type', nil)&.start_with?('application/json')
    data = JSON.parse(full_body) if parse_to_json
  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