|
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 | + |
| 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 |
| - # Sets up the middleware to validate the response schema if the "VALIDATE_SCHEMA" header is present. |
| 43 | + # Sets up the middleware to validate the response schema if the "VALIDATE_SCHEMA" header is present log the error. |
13 | 44 | #
|
14 | 45 | # @param env [Hash] The Rack environment hash.
|
15 | 46 | # @return [Array] The status, headers, and response.
|
16 | 47 | def call(env)
|
17 | 48 | status, headers, response = @app.call(env)
|
18 | 49 |
|
19 | 50 | if condition_for_response_validation?(env)
|
20 |
| - status, headers, response = @response_validator.call(env) |
| 51 | + @response_validator.call(env) |
21 | 52 | end
|
22 | 53 |
|
23 | 54 | [ status, headers, response ]
|
|
0 commit comments