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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 173ee1ad63a2f3bbd80eb012363b1825186ccf0c
4
- data.tar.gz: aada311d9d39946ba34e456cf63d3505ff22558d
3
+ metadata.gz: 439fbc1ffad016e11c3f03d532ffa9e61289a4a7
4
+ data.tar.gz: a99fa598c8c5d9f5dc3ce1a596aa598d1a782f6d
5
5
  SHA512:
6
- metadata.gz: b7d52505a1d958dfd6181834f790a5567a1d6b1f13a8e81f56b4bd7ae60aa83030bb1af920a2ed6cc090fc235f3bd5d683619af072bd94e89be6094486852edf
7
- data.tar.gz: 67952f61e059649f6a55d06d92d39e49ec4476f4ffa40ec0e9f0b27c0cb74115c3666268f4927b73f75fac200cbd753cb8d653ba4d238ec712d370ce79320bff
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
- render_error(400, :bad_request, $!.message)
34
+ @error_class.new(400, :bad_request, $!.message).render
36
35
  rescue Committee::NotFound
37
36
  raise if @raise
38
- render_error(404, :not_found,
39
- "That request method and path combination isn't defined.")
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
- render_error(400, :bad_request, "Request body wasn't valid JSON.")
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
- render_error(500, :invalid_response, $!.message)
23
+ @error_class.new(500, :invalid_response, $!.message).render
25
24
  rescue MultiJson::LoadError
26
25
  raise Committee::InvalidResponse if @raise
27
- render_error(500, :invalid_response, "Response wasn't valid JSON.")
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.8.0
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