class Rack::JsonSchema::ResponseValidation::Validator

Public Class Methods

new(env: nil, response: nil, schema: nil) click to toggle source

@param env [Hash] Rack env @param response [Array] Rack response @param schema [JsonSchema::Schema] Schema object

# 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

Public Instance Methods

body() click to toggle source

@return [String] Response body

# File lib/rack/json_schema/response_validation.rb, line 90
def body
  result = ""
  @response[2].each {|str| result << str }
  result
end
call() click to toggle source

Raises an error if any error detected, skipping validation for non-defined link @raise [Rack::JsonSchema::ResponseValidation::InvalidResponse]

# 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_body? && !has_json_content_type?
      raise InvalidResponseContentType
    when !valid?
      raise InvalidResponseType, validator.errors
    end
  end
end
data() click to toggle source

@return [Array, Hash] Response body data, decoded from JSON

# File lib/rack/json_schema/response_validation.rb, line 74
def data
  JSON.parse(body)
end
example_item() click to toggle source

@return [Hash] Choose an item from response data, to be validated

# File lib/rack/json_schema/response_validation.rb, line 65
def example_item
  if has_list_data?
    data.first
  else
    data
  end
end
has_body?() click to toggle source

@return [true, false] True if any bytes are included in response body

# File lib/rack/json_schema/response_validation.rb, line 45
def has_body?
  !body.size.zero?
end
has_json_content_type?() click to toggle source

@return [true, false] True if response Content-Type is for JSON

# File lib/rack/json_schema/response_validation.rb, line 55
def has_json_content_type?
  mime_type_json?(mime_type)
end
has_redirection_or_error_status?() click to toggle source

Skips validation if status code is equal to or larger than 300 @return [true, false]

# File lib/rack/json_schema/response_validation.rb, line 103
def has_redirection_or_error_status?
  response_status >= 300
end
headers() click to toggle source

@return [Hash] Response headers

# File lib/rack/json_schema/response_validation.rb, line 85
def headers
  @response[1]
end
mime_type() click to toggle source

@return [String, nil] Response MIME Type specified in Content-Type header field @example

mime_type #=> "application/json"
# File lib/rack/json_schema/response_validation.rb, line 110
def mime_type
  headers["Content-Type"].split(";").first if headers["Content-Type"]
end
mime_type_json?(value) click to toggle source

@return [true, false] return true if mime type is for JSON @example

"application/json" #=> true
"application/calendar+json" #=> true
"application/vnd.myapp.custom-json" #=> false
# File lib/rack/json_schema/response_validation.rb, line 119
def mime_type_json?(value)
  %r<\Aapplication/(?:.*\+)?json> === value
end
response_status() click to toggle source

@return [Fixnum] Response status code

# File lib/rack/json_schema/response_validation.rb, line 97
def response_status
  @response[0]
end
valid?() click to toggle source

@return [true, false] True if given data is valid to the JSON schema

# File lib/rack/json_schema/response_validation.rb, line 60
def valid?
  !has_body? || (has_list_data? && data.empty?) || validator.validate(example_item)
end
validator() click to toggle source

@return [JsonSchema::Validator] @note The result is memoized for returning errors in invalid case

# File lib/rack/json_schema/response_validation.rb, line 80
def validator
  @validator ||= ::JsonSchema::Validator.new(schema_for_current_link)
end