Skip to content

Commit ba4e7ea

Browse files
authored
Merge pull request #4 from SiloGecho97/add-error-log-for-validator
feat: add validation log instead of returning error
2 parents 56abad4 + f4d3a21 commit ba4e7ea

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

config/application.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class Application < Rails::Application
3131
config.api_only = true
3232

3333
# Add the request and response validation middleware to the application.
34-
# TODO: This can be changed later to add middleware in exact position e.g config.middleware.after Rack::Runtime, OpenApiSchema::RequestValidatorMiddleware
35-
config.middleware.use ::OpenApiSchema::RequestValidatorMiddleware
36-
config.middleware.use ::OpenApiSchema::ResponseValidatorMiddleware
34+
config.middleware.insert_after Rack::Runtime, ::OpenApiSchema::RequestValidatorMiddleware
35+
config.middleware.insert_after Rack::Runtime, ::OpenApiSchema::ResponseValidatorMiddleware
3736
end
3837
end

lib/open_api_schema/response_validator_middleware.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
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+
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-
# 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.
1344
#
1445
# @param env [Hash] The Rack environment hash.
1546
# @return [Array] The status, headers, and response.
1647
def call(env)
1748
status, headers, response = @app.call(env)
1849

1950
if condition_for_response_validation?(env)
20-
status, headers, response = @response_validator.call(env)
51+
@response_validator.call(env)
2152
end
2253

2354
[ status, headers, response ]

0 commit comments

Comments
 (0)