|
1 | 1 |
|
| 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 | + |
2 | 27 | module OpenApiSchema
|
3 | 28 | class ResponseValidatorMiddleware
|
4 | 29 | # Initializes the middleware with the given Rack application and sets up the response validator.
|
5 | 30 | #
|
6 | 31 | # @param app [Object] The Rack application.
|
7 | 32 | def initialize(app)
|
8 | 33 | @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) |
10 | 41 | end
|
11 | 42 |
|
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. |
14 | 43 | #
|
15 | 44 | # @param env [Hash] The Rack environment hash.
|
16 | 45 | # @return [Array] The status, headers, and response.
|
17 | 46 | def call(env)
|
18 | 47 | status, headers, response = @app.call(env)
|
19 | 48 |
|
20 | 49 | if condition_for_response_validation?(env)
|
21 |
| - status, headers, response = @response_validator.call(env) |
| 50 | + status, headers, response = validate_response(env, status, headers, response) |
22 | 51 | end
|
23 | 52 |
|
24 | 53 | [ status, headers, response ]
|
25 | 54 | end
|
26 | 55 |
|
27 | 56 | private
|
28 | 57 |
|
| 58 | + |
| 59 | + |
| 60 | + |
29 | 61 | # Checks if the "Validate-Schema" header is present in the environment hash.
|
30 | 62 | #
|
31 | 63 | # @param env [Hash] The Rack environment hash.
|
32 | 64 | # @return [Boolean] True if the "Validate-Schema" header is present, false otherwise.
|
33 | 65 | 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 |
35 | 76 | end
|
36 | 77 | end
|
37 | 78 | end
|
0 commit comments