class Rack::JsonSchema::ResponseValidation::Validator
Public Class Methods
Public Instance Methods
@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
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
@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
@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
@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
@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
@return [true, false] True if Link mediaType is for JSON
# File lib/rack/json_schema/response_validation.rb, line 50 def has_link_of_media_type_json? mime_type_json?(link.media_type) end
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
@return [Hash] Response headers
# File lib/rack/json_schema/response_validation.rb, line 85 def headers @response[1] end
@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
@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
@return [Fixnum] Response status code
# File lib/rack/json_schema/response_validation.rb, line 97 def response_status @response[0] end
@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
@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