Class: Rack::JsonSchema::ResponseValidation::Validator

Inherits:
BaseRequestHandler show all
Defined in:
lib/rack/json_schema/response_validation.rb

Instance Method Summary collapse

Methods inherited from BaseRequestHandler

call

Constructor Details

#initialize(env: nil, response: nil, schema: nil) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • env (Hash) (defaults to: nil)

    Rack env

  • response (Array) (defaults to: nil)

    Rack response

  • schema (JsonSchema::Schema) (defaults to: nil)

    Schema object



25
26
27
28
29
# File 'lib/rack/json_schema/response_validation.rb', line 25

def initialize(env: nil, response: nil, schema: nil)
  @env = env
  @response = response
  @schema = schema
end

Instance Method Details

#bodyString

Returns Response body.

Returns:

  • (String)

    Response body



85
86
87
88
89
# File 'lib/rack/json_schema/response_validation.rb', line 85

def body
  result = ""
  @response[2].each {|str| result << str }
  result
end

#callObject

Raises an error if any error detected, skipping validation for non-defined link

Raises:

  • (Rack::JsonSchema::ResponseValidation::InvalidResponse)


33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/json_schema/response_validation.rb', line 33

def call
  if !has_redirection_or_error_status? && has_link_for_current_action? && has_link_of_media_type_json?
    case
    when !has_json_content_type?
      raise InvalidResponseContentType
    when !valid?
      raise InvalidResponseType, validator.errors
    end
  end
end

#dataArray, Hash

Returns Response body data, decoded from JSON.

Returns:

  • (Array, Hash)

    Response body data, decoded from JSON



69
70
71
# File 'lib/rack/json_schema/response_validation.rb', line 69

def data
  JSON.parse(body)
end

#example_itemHash

Returns Choose an item from response data, to be validated.

Returns:

  • (Hash)

    Choose an item from response data, to be validated



60
61
62
63
64
65
66
# File 'lib/rack/json_schema/response_validation.rb', line 60

def example_item
  if has_list_data?
    data.first
  else
    data
  end
end

#has_json_content_type?true, false

Returns True if response Content-Type is for JSON.

Returns:

  • (true, false)

    True if response Content-Type is for JSON



50
51
52
# File 'lib/rack/json_schema/response_validation.rb', line 50

def has_json_content_type?
  mime_type_json?(mime_type)
end

Returns True if Link mediaType is for JSON.

Returns:

  • (true, false)

    True if Link mediaType is for JSON



45
46
47
# File 'lib/rack/json_schema/response_validation.rb', line 45

def has_link_of_media_type_json?
  mime_type_json?(link.media_type)
end

#has_redirection_or_error_status?true, false

Skips validation if status code is equal to or larger than 300

Returns:

  • (true, false)


98
99
100
# File 'lib/rack/json_schema/response_validation.rb', line 98

def has_redirection_or_error_status?
  response_status >= 300
end

#headersHash

Returns Response headers.

Returns:

  • (Hash)

    Response headers



80
81
82
# File 'lib/rack/json_schema/response_validation.rb', line 80

def headers
  @response[1]
end

#mime_typeString?

Returns Response MIME Type specified in Content-Type header field.

Examples:

mime_type #=> "application/json"

Returns:

  • (String, nil)

    Response MIME Type specified in Content-Type header field



105
106
107
# File 'lib/rack/json_schema/response_validation.rb', line 105

def mime_type
  headers["Content-Type"].split(";").first if headers["Content-Type"]
end

#mime_type_json?(value) ⇒ true, false

Return true if mime type is for JSON

Examples:

"application/json" #=> true
"application/calendar+json" #=> true
"application/vnd.myapp.custom-json" #=> false

Returns:

  • (true, false)

    return true if mime type is for JSON



114
115
116
# File 'lib/rack/json_schema/response_validation.rb', line 114

def mime_type_json?(value)
  %r<\Aapplication/(?:.*\+)?json> === value
end

#response_statusFixnum

Returns Response status code.

Returns:

  • (Fixnum)

    Response status code



92
93
94
# File 'lib/rack/json_schema/response_validation.rb', line 92

def response_status
  @response[0]
end

#valid?true, false

Returns True if given data is valid to the JSON schema.

Returns:

  • (true, false)

    True if given data is valid to the JSON schema



55
56
57
# File 'lib/rack/json_schema/response_validation.rb', line 55

def valid?
  (has_list_data? && data.empty?) || validator.validate(example_item)
end

#validatorJsonSchema::Validator

Note:

The result is memoized for returning errors in invalid case

Returns:

  • (JsonSchema::Validator)


75
76
77
# File 'lib/rack/json_schema/response_validation.rb', line 75

def validator
  @validator ||= ::JsonSchema::Validator.new(schema_for_current_link)
end