committee 1.8.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/committee/middleware/base.rb +2 -5
- data/lib/committee/middleware/request_validation.rb +7 -5
- data/lib/committee/middleware/response_validation.rb +2 -3
- data/lib/committee/validation_error.rb +23 -0
- data/lib/committee.rb +1 -0
- data/test/validation_error_test.rb +23 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 439fbc1ffad016e11c3f03d532ffa9e61289a4a7
|
4
|
+
data.tar.gz: a99fa598c8c5d9f5dc3ce1a596aa598d1a782f6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9ecf7dd62046f8e9046ed7c4cca3981848776d6c6a72becb3a97fe74ea4a080766a331d490b1f84c58b19b1da58786049c4ecd559130532ee8e59c50f0950e3
|
7
|
+
data.tar.gz: 99143e5da92042a35ca7ca95e06155e6d646186408ebf5d27d3726ff40d424628a150f1230171e1409f4117fb8f4031626ffeb0c64048cde6c70029e56eade26
|
@@ -3,12 +3,14 @@ module Committee::Middleware
|
|
3
3
|
def initialize(app, options={})
|
4
4
|
@app = app
|
5
5
|
|
6
|
+
@error_class = options.fetch(:error_class, Committee::ValidationError)
|
6
7
|
@params_key = options[:params_key] || "committee.params"
|
7
8
|
data = options[:schema] || raise("need option `schema`")
|
8
9
|
if data.is_a?(String)
|
9
10
|
warn_string_deprecated
|
10
11
|
data = MultiJson.decode(data)
|
11
12
|
end
|
13
|
+
@raise = options[:raise]
|
12
14
|
@schema = JsonSchema.parse!(data)
|
13
15
|
@schema.expand_references!
|
14
16
|
@router = Committee::Router.new(@schema, options)
|
@@ -26,11 +28,6 @@ module Committee::Middleware
|
|
26
28
|
|
27
29
|
private
|
28
30
|
|
29
|
-
def render_error(status, id, message)
|
30
|
-
[status, { "Content-Type" => "application/json" },
|
31
|
-
[MultiJson.encode({ id: id, message: message }, pretty: true)]]
|
32
|
-
end
|
33
|
-
|
34
31
|
def warn_string_deprecated
|
35
32
|
Committee.warn_deprecated("Committee: passing a string to `schema` option is deprecated; please send a deserialized hash instead.")
|
36
33
|
end
|
@@ -6,7 +6,6 @@ module Committee::Middleware
|
|
6
6
|
@allow_query_params = options.fetch(:allow_query_params, true)
|
7
7
|
@check_content_type = options.fetch(:check_content_type, true)
|
8
8
|
@optimistic_json = options.fetch(:optimistic_json, false)
|
9
|
-
@raise = options[:raise]
|
10
9
|
@strict = options[:strict]
|
11
10
|
|
12
11
|
# deprecated
|
@@ -32,14 +31,17 @@ module Committee::Middleware
|
|
32
31
|
end
|
33
32
|
rescue Committee::BadRequest, Committee::InvalidRequest
|
34
33
|
raise if @raise
|
35
|
-
|
34
|
+
@error_class.new(400, :bad_request, $!.message).render
|
36
35
|
rescue Committee::NotFound
|
37
36
|
raise if @raise
|
38
|
-
|
39
|
-
|
37
|
+
@error_class.new(
|
38
|
+
404,
|
39
|
+
:not_found,
|
40
|
+
"That request method and path combination isn't defined."
|
41
|
+
).render
|
40
42
|
rescue MultiJson::LoadError
|
41
43
|
raise Committee::InvalidRequest if @raise
|
42
|
-
|
44
|
+
@error_class.new(400, :bad_request, "Request body wasn't valid JSON.").render
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -2,7 +2,6 @@ module Committee::Middleware
|
|
2
2
|
class ResponseValidation < Base
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
|
-
@raise = options[:raise]
|
6
5
|
@validate_errors = options[:validate_errors]
|
7
6
|
end
|
8
7
|
|
@@ -21,10 +20,10 @@ module Committee::Middleware
|
|
21
20
|
[status, headers, response]
|
22
21
|
rescue Committee::InvalidResponse
|
23
22
|
raise if @raise
|
24
|
-
|
23
|
+
@error_class.new(500, :invalid_response, $!.message).render
|
25
24
|
rescue MultiJson::LoadError
|
26
25
|
raise Committee::InvalidResponse if @raise
|
27
|
-
|
26
|
+
@error_class.new(500, :invalid_response, "Response wasn't valid JSON.").render
|
28
27
|
end
|
29
28
|
|
30
29
|
def validate?(status)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Committee
|
2
|
+
class ValidationError
|
3
|
+
attr_reader :id, :message, :status
|
4
|
+
|
5
|
+
def initialize(status, id, message)
|
6
|
+
@status = status
|
7
|
+
@id = id
|
8
|
+
@message = message
|
9
|
+
end
|
10
|
+
|
11
|
+
def error_body
|
12
|
+
{ id: id, message: message }
|
13
|
+
end
|
14
|
+
|
15
|
+
def render
|
16
|
+
[
|
17
|
+
status,
|
18
|
+
{ "Content-Type" => "application/json" },
|
19
|
+
[MultiJson.encode(error_body)]
|
20
|
+
]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/committee.rb
CHANGED
@@ -8,6 +8,7 @@ require_relative "committee/request_validator"
|
|
8
8
|
require_relative "committee/response_generator"
|
9
9
|
require_relative "committee/response_validator"
|
10
10
|
require_relative "committee/router"
|
11
|
+
require_relative "committee/validation_error"
|
11
12
|
|
12
13
|
require_relative "committee/middleware/base"
|
13
14
|
require_relative "committee/middleware/request_validation"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
describe Committee::ValidationError do
|
4
|
+
before do
|
5
|
+
@error = Committee::ValidationError.new(400, :bad_request, "Error")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "creates an error body with the id and message" do
|
9
|
+
assert_equal @error.error_body, { id: :bad_request, message: "Error" }
|
10
|
+
end
|
11
|
+
|
12
|
+
it "creates a Rack response object to render" do
|
13
|
+
body = { id: :bad_request, message: "Error" }
|
14
|
+
|
15
|
+
response = [
|
16
|
+
400,
|
17
|
+
{ "Content-Type" => "application/json" },
|
18
|
+
[MultiJson.encode(body)]
|
19
|
+
]
|
20
|
+
|
21
|
+
assert_equal @error.render, response
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: committee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- lib/committee/response_validator.rb
|
132
132
|
- lib/committee/router.rb
|
133
133
|
- lib/committee/test/methods.rb
|
134
|
+
- lib/committee/validation_error.rb
|
134
135
|
- test/middleware/request_validation_test.rb
|
135
136
|
- test/middleware/response_validation_test.rb
|
136
137
|
- test/middleware/stub_test.rb
|
@@ -141,6 +142,7 @@ files:
|
|
141
142
|
- test/router_test.rb
|
142
143
|
- test/test/methods_test.rb
|
143
144
|
- test/test_helper.rb
|
145
|
+
- test/validation_error_test.rb
|
144
146
|
homepage: https://github.com/interagent/committee
|
145
147
|
licenses:
|
146
148
|
- MIT
|