Class: Rack::JsonSchema::ResponseValidation::Validator
- Inherits:
-
BaseRequestHandler
- Object
- BaseRequestHandler
- Rack::JsonSchema::ResponseValidation::Validator
- Defined in:
- lib/rack/json_schema/response_validation.rb
Instance Method Summary collapse
-
#body ⇒ String
Response body.
-
#call ⇒ Object
Raises an error if any error detected, skipping validation for non-defined link.
-
#data ⇒ Array, Hash
Response body data, decoded from JSON.
-
#example_item ⇒ Hash
Choose an item from response data, to be validated.
-
#has_body? ⇒ true, false
True if any bytes are included in response body.
-
#has_json_content_type? ⇒ true, false
True if response Content-Type is for JSON.
-
#has_link_of_media_type_json? ⇒ true, false
True if Link mediaType is for JSON.
-
#has_redirection_or_error_status? ⇒ true, false
Skips validation if status code is equal to or larger than 300.
-
#headers ⇒ Hash
Response headers.
-
#initialize(env: nil, response: nil, schema: nil) ⇒ Validator
constructor
A new instance of Validator.
-
#mime_type ⇒ String?
Response MIME Type specified in Content-Type header field.
-
#mime_type_json?(value) ⇒ true, false
Return true if mime type is for JSON.
-
#response_status ⇒ Fixnum
Response status code.
-
#valid? ⇒ true, false
True if given data is valid to the JSON schema.
- #validator ⇒ JsonSchema::Validator
Methods inherited from BaseRequestHandler
Constructor Details
#initialize(env: nil, response: nil, schema: nil) ⇒ Validator
Returns a new instance of Validator.
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
#body ⇒ String
Returns Response body.
90 91 92 93 94 |
# File 'lib/rack/json_schema/response_validation.rb', line 90 def body result = "" @response[2].each {|str| result << str } result end |
#call ⇒ Object
Raises an error if any error detected, skipping validation for non-defined link
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_body? && !has_json_content_type? raise InvalidResponseContentType when !valid? raise InvalidResponseType, validator.errors end end end |
#data ⇒ Array, Hash
Returns Response body data, decoded from JSON.
74 75 76 |
# File 'lib/rack/json_schema/response_validation.rb', line 74 def data JSON.parse(body) end |
#example_item ⇒ Hash
Returns Choose an item from response data, to be validated.
65 66 67 68 69 70 71 |
# 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? ⇒ true, false
Returns True if any bytes are included in response body.
45 46 47 |
# File 'lib/rack/json_schema/response_validation.rb', line 45 def has_body? !body.size.zero? end |
#has_json_content_type? ⇒ true, false
Returns True if response Content-Type is for JSON.
55 56 57 |
# File 'lib/rack/json_schema/response_validation.rb', line 55 def has_json_content_type? mime_type_json?(mime_type) end |
#has_link_of_media_type_json? ⇒ true, false
Returns True if Link mediaType is for JSON.
50 51 52 |
# File 'lib/rack/json_schema/response_validation.rb', line 50 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
103 104 105 |
# File 'lib/rack/json_schema/response_validation.rb', line 103 def has_redirection_or_error_status? response_status >= 300 end |
#headers ⇒ Hash
Returns Response headers.
85 86 87 |
# File 'lib/rack/json_schema/response_validation.rb', line 85 def headers @response[1] end |
#mime_type ⇒ String?
Returns Response MIME Type specified in Content-Type header field.
110 111 112 |
# 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) ⇒ true, false
Return true if mime type is for JSON
119 120 121 |
# File 'lib/rack/json_schema/response_validation.rb', line 119 def mime_type_json?(value) %r<\Aapplication/(?:.*\+)?json> === value end |
#response_status ⇒ Fixnum
Returns Response status code.
97 98 99 |
# File 'lib/rack/json_schema/response_validation.rb', line 97 def response_status @response[0] end |
#valid? ⇒ true, false
Returns True if given data is valid to the JSON schema.
60 61 62 |
# 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 ⇒ JsonSchema::Validator
The result is memoized for returning errors in invalid case
80 81 82 |
# File 'lib/rack/json_schema/response_validation.rb', line 80 def validator @validator ||= ::JsonSchema::Validator.new(schema_for_current_link) end |