Skip to content

Commit 45318ac

Browse files
committed
feat: add validation log instead of returning error
1 parent 979dc33 commit 45318ac

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed
Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,78 @@
11

2+
# Common options for both request and response validation
3+
# common_options = {
4+
# schema_path: Rails.root.join("docs", "schema.json").to_s,
5+
# error_handler: error_handler,
6+
# strict: true, # Enforce strict validation
7+
# coerce_date_times: true, # Automatically coerce date-time strings to DateTime objects
8+
# coerce_path_params: true, # Coerce path parameters to expected types
9+
# coerce_query_params: true, # Coerce query parameters to expected types
10+
# coerce_form_params: true, # Coerce form parameters to expected types
11+
# allow_blank: true, # Allow blank values in parameters
12+
# allow_form_params: true, # Allow form parameters in requests
13+
# allow_query_params: true, # Allow query parameters in requests
14+
# check_content_type: true, # Validate the Content-Type header
15+
# check_header: true, # Validate headers according to schema
16+
# optimistic_json: true, # Parse JSON in an optimistic manner
17+
# parse_response_by_content_type: true, # Parse response based on Content-Type
18+
# validate_success_only: true, # Validate only successful responses (2xx status codes)
19+
# query_hash_key: "action_dispatch.request.query_parameters", # Key for query parameters in the environment
20+
# form_hash_key: "action_dispatch.request.request_parameters", # Key for form parameters in the environment
21+
# headers_key: "action_dispatch.request.headers", # Key for headers in the environment
22+
# raise: false, # Do not raise exceptions on validation errors
23+
# ignore_error: true # Continue processing even if validation fails
24+
# }
25+
26+
227
module OpenApiSchema
328
class ResponseValidatorMiddleware
429
# Initializes the middleware with the given Rack application and sets up the response validator.
530
#
631
# @param app [Object] The Rack application.
732
def initialize(app)
833
@app = app
9-
@response_validator = Committee::Middleware::ResponseValidation.new(app, schema_path: "docs/openapi.json", strict_reference_validation: true)
34+
# Handles the middleware call to validate the schema if the "VALIDATE_SCHEMA" header is present.
35+
error_handler = Proc.new do |error, env|
36+
logger = Logger.new(Rails.root.join("log", "committee_validation.log"))
37+
logger.error("Committee Validation Error: #{error.message}")
38+
end
39+
40+
@response_validator = Committee::Middleware::ResponseValidation.new(app, schema_path: "docs/openapi.json", ignore_error: true, error_handler: error_handler)
1041
end
1142

12-
# Handles the middleware call to validate the schema if the "VALIDATE_SCHEMA" header is present.
13-
# If the header is set to "1", it validates the response schema.
1443
#
1544
# @param env [Hash] The Rack environment hash.
1645
# @return [Array] The status, headers, and response.
1746
def call(env)
1847
status, headers, response = @app.call(env)
1948

2049
if condition_for_response_validation?(env)
21-
status, headers, response = @response_validator.call(env)
50+
status, headers, response = validate_response(env, status, headers, response)
2251
end
2352

2453
[ status, headers, response ]
2554
end
2655

2756
private
2857

58+
59+
60+
2961
# Checks if the "Validate-Schema" header is present in the environment hash.
3062
#
3163
# @param env [Hash] The Rack environment hash.
3264
# @return [Boolean] True if the "Validate-Schema" header is present, false otherwise.
3365
def condition_for_response_validation?(env)
34-
env.key?("HTTP_VALIDATE_SCHEMA")
66+
true # env.key?("HTTP_VALIDATE_SCHEMA")
67+
end
68+
69+
def validate_response(env, status, headers, response)
70+
begin
71+
@response_validator.call(env)
72+
rescue StandardError => e
73+
@logger.error("Response Validation Failed: #{e.message}") # Log the error, TODO: add more information
74+
[ status, headers, response ] # Return original response even if validation fails
75+
end
3576
end
3677
end
3778
end

0 commit comments

Comments
 (0)