rack-json_schema 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/rack/json_schema/request_validation.rb +16 -4
- data/lib/rack/json_schema/version.rb +1 -1
- data/spec/rack/spec_spec.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1bc0b3aeb691e9f03410743681b3aeb47636727
|
4
|
+
data.tar.gz: 78f4809fbaa6d7732b5ba700f464be4187ad6263
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82933f6a328ccb1f7809880262f4f2119c19962c6830082ca188b768c995a55701f58f2e6f6d003c2522a3667d43f7eee91237bd844e4ccdc30da52733feb643
|
7
|
+
data.tar.gz: 694562757728e378acf77e615247bc480f51048b9f649eb4ccc9b503889a1a545d39ad5af3ce860cb485e390186ddcce14853dd55c35a97ccceac606b21da49d
|
data/CHANGELOG.md
CHANGED
@@ -56,7 +56,11 @@ module Rack
|
|
56
56
|
when content_type_json? && has_body? && !has_valid_json?
|
57
57
|
raise InvalidJson
|
58
58
|
when content_type_json? && has_schema? && !has_valid_parameter?
|
59
|
-
|
59
|
+
if has_hash_request_body?
|
60
|
+
raise InvalidParameter, "Invalid request.\n#{schema_validation_error_message}"
|
61
|
+
else
|
62
|
+
raise InvalidParameter, "Invalid request. Request body must be an Object in JSON."
|
63
|
+
end
|
60
64
|
end
|
61
65
|
end
|
62
66
|
elsif !ignore_missing_path?
|
@@ -66,8 +70,12 @@ module Rack
|
|
66
70
|
|
67
71
|
private
|
68
72
|
|
73
|
+
def has_hash_request_body?
|
74
|
+
parsed_body.is_a?(Hash)
|
75
|
+
end
|
76
|
+
|
69
77
|
def has_valid_json?
|
70
|
-
|
78
|
+
parsed_body
|
71
79
|
true
|
72
80
|
rescue JSON::JSONError
|
73
81
|
false
|
@@ -75,7 +83,7 @@ module Rack
|
|
75
83
|
|
76
84
|
# @return [true, false] True if request parameters are all valid
|
77
85
|
def has_valid_parameter?
|
78
|
-
schema_validation_result[0]
|
86
|
+
parsed_body.is_a?(Hash) && schema_validation_result[0]
|
79
87
|
end
|
80
88
|
|
81
89
|
# @return [true, false] True if any schema is defined for the current action
|
@@ -151,7 +159,7 @@ module Rack
|
|
151
159
|
# @raise [JSON::JSONError]
|
152
160
|
def parameters_from_body
|
153
161
|
if has_body?
|
154
|
-
|
162
|
+
parsed_body
|
155
163
|
else
|
156
164
|
{}
|
157
165
|
end
|
@@ -161,6 +169,10 @@ module Rack
|
|
161
169
|
def parameters_from_query
|
162
170
|
request.GET
|
163
171
|
end
|
172
|
+
|
173
|
+
def parsed_body
|
174
|
+
@parsed_body ||= JSON.parse(body)
|
175
|
+
end
|
164
176
|
end
|
165
177
|
|
166
178
|
# Base error class for Rack::JsonSchema::RequestValidation
|
data/spec/rack/spec_spec.rb
CHANGED
@@ -155,6 +155,14 @@ describe Rack::JsonSchema do
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
158
|
+
context "with Array request body", :with_valid_post_request do
|
159
|
+
let(:params) do
|
160
|
+
[].to_json
|
161
|
+
end
|
162
|
+
|
163
|
+
it { should == 400 }
|
164
|
+
end
|
165
|
+
|
158
166
|
context "with valid request property", :with_valid_post_request do
|
159
167
|
it { should == 200 }
|
160
168
|
end
|