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_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.
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 |
#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_json_content_type? raise InvalidResponseContentType when !valid? raise InvalidResponseType, validator.errors end end end |
#data ⇒ Array, Hash
Returns 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_item ⇒ Hash
Returns 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.
50 51 52 |
# File 'lib/rack/json_schema/response_validation.rb', line 50 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.
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
98 99 100 |
# File 'lib/rack/json_schema/response_validation.rb', line 98 def has_redirection_or_error_status? response_status >= 300 end |
#headers ⇒ Hash
Returns Response headers.
80 81 82 |
# File 'lib/rack/json_schema/response_validation.rb', line 80 def headers @response[1] end |
#mime_type ⇒ String?
Returns 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
114 115 116 |
# File 'lib/rack/json_schema/response_validation.rb', line 114 def mime_type_json?(value) %r<\Aapplication/(?:.*\+)?json> === value end |
#response_status ⇒ Fixnum
Returns 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.
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 |
#validator ⇒ JsonSchema::Validator
The result is memoized for returning errors in invalid case
75 76 77 |
# File 'lib/rack/json_schema/response_validation.rb', line 75 def validator @validator ||= ::JsonSchema::Validator.new(schema_for_current_link) end |