rack-json_schema 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 276eb5729514a350ea04991833eb262d34ed663b
4
- data.tar.gz: c9d398ae44ccc2ca33813fb210830552e81a876b
3
+ metadata.gz: 71834752a3561e3bf63a60884a2335f172cc58ff
4
+ data.tar.gz: d631e1916036d2c632611d7f49571283cf01d069
5
5
  SHA512:
6
- metadata.gz: a1edd0f145b7b97ee5364e6c3e358df7ae21a47f93c6037c3411a69bf178880867347f446d510186888d8b0b1afc6da178ab5904bdd176336fc509655bbd3782
7
- data.tar.gz: 2bd34182a08c20b032c55634413bb67746f53f35b61befa6d9e41a5569c01d6ad652f8a948551729ef47a3950a26140c11aa2de70192b3447946596948fda048
6
+ metadata.gz: 3496d7e180b999cfb6350ea7db3cfc12ca0f060390ba2715e72eb378cdcf0c52d850a42cc8eec6601dd79381f87f7578f3787520d9ab023066db284651146fc7
7
+ data.tar.gz: da8d31beb10245571b67fb7e955ee4fb9ffd1ce6ee35d46f47dd6e4ad2c65fa3bdf4c6b069bf62a3f509973d834a7786e867a6b63e971a0aa38ecee0729130c3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 1.5.0
2
+ - Separate :strict option into 2 other options
3
+
1
4
  ## 1.4.0
2
5
  - Allow non JSON Content-Type response on empty body
3
6
 
@@ -3,19 +3,26 @@ module Rack
3
3
  class RequestValidation
4
4
  # Behaves as a rack-middleware
5
5
  # @param app [Object] Rack application
6
+ # @param ignore_invalid_content_type [false, true] Ignore requests with non json content-type
7
+ # @param ignore_missing_path [false, true] Ignore requests not defined in schema
6
8
  # @param schema [Hash] Schema object written in JSON schema format
7
- # @param strict [Boolean] Strict mode or not (default: true)
8
9
  # @raise [JsonSchema::SchemaError]
9
- def initialize(app, schema: nil, strict: nil)
10
+ def initialize(app, ignore_invalid_content_type: false, ignore_missing_path: false, schema: nil)
10
11
  @app = app
12
+ @ignore_invalid_content_type = ignore_invalid_content_type
13
+ @ignore_missing_path = ignore_missing_path
11
14
  @schema = Schema.new(schema)
12
- @strict = strict
13
15
  end
14
16
 
15
17
  # @raise [Rack::JsonSchema::RequestValidation::Error] Raises if given request is invalid to JSON Schema
16
18
  # @param env [Hash] Rack env
17
19
  def call(env)
18
- Validator.call(env: env, schema: @schema, strict: @strict)
20
+ Validator.call(
21
+ env: env,
22
+ ignore_invalid_content_type: @ignore_invalid_content_type,
23
+ ignore_missing_path: @ignore_missing_path,
24
+ schema: @schema
25
+ )
19
26
  @app.call(env)
20
27
  end
21
28
 
@@ -26,12 +33,14 @@ module Rack
26
33
  end
27
34
 
28
35
  # @param env [Hash] Rack env
36
+ # @param ignore_invalid_content_type [false, true]
37
+ # @param ignore_missing_path [false, true]
29
38
  # @param schema [JsonSchema::Schema] Schema object
30
- # @param strict [Boolean] Strict mode or not (default: true)
31
- def initialize(env: nil, schema: nil, strict: nil)
39
+ def initialize(env: nil, ignore_invalid_content_type: false, ignore_missing_path: false, schema: nil)
32
40
  @env = env
41
+ @ignore_invalid_content_type = ignore_invalid_content_type
42
+ @ignore_missing_path = ignore_missing_path
33
43
  @schema = schema
34
- @strict = strict
35
44
  end
36
45
 
37
46
  # Raises an error if any error detected
@@ -39,7 +48,7 @@ module Rack
39
48
  def call
40
49
  if has_link_for_current_action?
41
50
  if has_body? && !has_valid_content_type?
42
- if strict?
51
+ unless ignore_invalid_content_type?
43
52
  raise InvalidContentType
44
53
  end
45
54
  else
@@ -50,7 +59,7 @@ module Rack
50
59
  raise InvalidParameter, "Invalid request.\n#{schema_validation_error_message}"
51
60
  end
52
61
  end
53
- elsif strict?
62
+ elsif !ignore_missing_path?
54
63
  raise LinkNotFound, "Could not find the link definition for request path #{path}."
55
64
  end
56
65
  end
@@ -84,6 +93,16 @@ module Rack
84
93
  mime_type.nil? || Rack::Mime.match?(link.enc_type, mime_type)
85
94
  end
86
95
 
96
+ # @return [false, true]
97
+ def ignore_invalid_content_type?
98
+ !!@ignore_invalid_content_type
99
+ end
100
+
101
+ # @return [false, true]
102
+ def ignore_missing_path?
103
+ !!@ignore_missing_path
104
+ end
105
+
87
106
  # @return [true, false] True if the current link supports json format
88
107
  def content_type_json?
89
108
  Rack::Mime.match?(link.enc_type, "application/json")
@@ -142,11 +161,6 @@ module Rack
142
161
  def parameters_from_query
143
162
  request.GET
144
163
  end
145
-
146
- # @return [Boolean] Strict mode or not.
147
- def strict?
148
- @strict != false
149
- end
150
164
  end
151
165
 
152
166
  # Base error class for Rack::JsonSchema::RequestValidation
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module JsonSchema
3
- VERSION = "1.4.0"
3
+ VERSION = "1.5.0"
4
4
  end
5
5
  end
@@ -5,12 +5,13 @@ describe Rack::JsonSchema do
5
5
 
6
6
  let(:app) do
7
7
  local_schema = schema
8
- local_strict = strict
8
+ local_ignore_invalid_content_type = ignore_invalid_content_type
9
+ local_ignore_missing_path = ignore_missing_path
9
10
  local_response_body = response_body
10
11
  local_response_headers = response_headers
11
12
  Rack::Builder.app do
12
13
  use Rack::JsonSchema::ErrorHandler
13
- use Rack::JsonSchema::RequestValidation, schema: local_schema, strict: local_strict
14
+ use Rack::JsonSchema::RequestValidation, ignore_invalid_content_type: local_ignore_invalid_content_type, ignore_missing_path: local_ignore_missing_path, schema: local_schema
14
15
  use Rack::JsonSchema::ResponseValidation, schema: local_schema
15
16
  run ->(env) do
16
17
  [200, local_response_headers, [local_response_body]]
@@ -46,8 +47,12 @@ describe Rack::JsonSchema do
46
47
  File.expand_path("../../fixtures/schema.json", __FILE__)
47
48
  end
48
49
 
49
- let(:strict) do
50
- true
50
+ let(:ignore_invalid_content_type) do
51
+ false
52
+ end
53
+
54
+ let(:ignore_missing_path) do
55
+ false
51
56
  end
52
57
 
53
58
  let(:response) do
@@ -94,14 +99,18 @@ describe Rack::JsonSchema do
94
99
  "/apps"
95
100
  end
96
101
 
102
+ shared_context "with undefined route" do
103
+ let(:path) do
104
+ "/undefined"
105
+ end
106
+ end
107
+
97
108
  context "with defined route" do
98
109
  it { should == 200 }
99
110
  end
100
111
 
101
112
  context "with undefined route" do
102
- let(:path) do
103
- "/undefined"
104
- end
113
+ include_context "with undefined route"
105
114
 
106
115
  it "returns link_not_found error" do
107
116
  should == 404
@@ -112,6 +121,16 @@ describe Rack::JsonSchema do
112
121
  end
113
122
  end
114
123
 
124
+ context "with undefined route and ignore_missing_path option" do
125
+ include_context "with undefined route"
126
+
127
+ let(:ignore_missing_path) do
128
+ true
129
+ end
130
+
131
+ it { should == 200 }
132
+ end
133
+
115
134
  context "with request body & invalid content type", :with_valid_post_request do
116
135
  before do
117
136
  env["CONTENT_TYPE"] = "text/plain"
@@ -124,15 +143,15 @@ describe Rack::JsonSchema do
124
143
  message: "Invalid content type",
125
144
  )
126
145
  end
146
+ end
127
147
 
128
- context "when strict is false" do
129
- let(:strict) do
130
- false
131
- end
148
+ context "with request body & invalid content type and ignore_invalid_content_type option" do
149
+ let(:ignore_invalid_content_type) do
150
+ true
151
+ end
132
152
 
133
- it "skips content_type check" do
134
- should == 200
135
- end
153
+ it "skips content_type check" do
154
+ should == 200
136
155
  end
137
156
  end
138
157
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis