Class: Committee::SchemaValidator::HyperSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/hyper_schema.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.



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

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.



3
4
5
# File 'lib/committee/schema_validator/hyper_schema.rb', line 3

def link
  @link
end

#param_matchesObject (readonly)

Returns the value of attribute param_matches.



3
4
5
# File 'lib/committee/schema_validator/hyper_schema.rb', line 3

def param_matches
  @param_matches
end

#validator_optionObject (readonly)

Returns the value of attribute validator_option.



3
4
5
# File 'lib/committee/schema_validator/hyper_schema.rb', line 3

def validator_option
  @validator_option
end

Instance Method Details

#coerce_form_params(parameter) ⇒ Object



43
44
45
46
47
# File 'lib/committee/schema_validator/hyper_schema.rb', line 43

def coerce_form_params(parameter)
  return unless link_exist?
  return unless link.schema
  Committee::SchemaValidator::HyperSchema::StringParamsCoercer.new(parameter, link.schema).call!
end

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/committee/schema_validator/hyper_schema.rb', line 10

def request_validate(request)
  # Attempts to coerce parameters that appear in a link's URL to Ruby
  # types that can be validated with a schema.
  param_matches_hash = validator_option.coerce_path_params ? coerce_path_params : {}

  # Attempts to coerce parameters that appear in a query string to Ruby
  # types that can be validated with a schema.
  coerce_query_params(request) if validator_option.coerce_query_params

  request_unpack(request)

  request.env[validator_option.params_key].merge!(param_matches_hash) if param_matches_hash

  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



28
29
30
31
32
33
34
35
36
37
# File 'lib/committee/schema_validator/hyper_schema.rb', line 28

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

  full_body = ""
  response.each do |chunk|
    full_body << chunk
  end
  data = full_body.empty? ? {} : JSON.parse(full_body)
  Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: validator_option.validate_success_only).call(status, headers, data)
end