Skip to content

Commit 22d35b1

Browse files
authored
Move response validation logic from middleware to validator classes (#462)
1 parent fe87f00 commit 22d35b1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/committee/schema_validator/hyper_schema/response_validator.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def call(status, headers, data)
5353
end
5454

5555
begin
56-
if Committee::Middleware::ResponseValidation.validate?(status, validate_success_only)
56+
if validate?(status)
5757
if @link.is_a?(Drivers::OpenAPI2::Link)
5858
raise InvalidResponse, "Invalid response.#{@link.href} status code #{status} definition does not exist" if @validators[status].nil?
5959
if !@validators[status].validate(data)
@@ -73,6 +73,19 @@ def call(status, headers, data)
7373
end
7474
end
7575

76+
def validate?(status)
77+
case status
78+
when 204
79+
false
80+
when 200..299
81+
true
82+
when 304
83+
false
84+
else
85+
!validate_success_only
86+
end
87+
end
88+
7689
private
7790

7891
def response_media_type(response)

lib/committee/schema_validator/open_api_3/response_validator.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ def initialize(operation_wrapper, validator_option)
1717
end
1818

1919
def call(status, headers, response_data, strict)
20-
return unless Committee::Middleware::ResponseValidation.validate?(status, validate_success_only)
20+
return unless validate?(status)
2121

2222
validator_options = { allow_empty_date_and_datetime: @allow_empty_date_and_datetime, coerce_value: @coerce_response_values }
2323

2424
operation_wrapper.validate_response_params(status, headers, response_data, strict, check_header, validator_options: validator_options)
2525
end
2626

27+
def validate?(status)
28+
case status
29+
when 204
30+
false
31+
when 200..299
32+
true
33+
when 304
34+
false
35+
else
36+
!validate_success_only
37+
end
38+
end
39+
2740
private
2841

2942
attr_reader :operation_wrapper, :check_header

0 commit comments

Comments
 (0)