### Run the example with bundle install and rackup Source: https://github.com/ahx/openapi_first/blob/main/examples/README.md Commands to set up and start the example application. Run from the examples directory, then open http://localhost:9292/. ```bash cd examples bundle install bundle exec rackup ``` -------------------------------- ### Start example app with puma (openapi_first) Source: https://github.com/ahx/openapi_first/blob/main/benchmarks/README.md Starts the example app using puma. Example: openapi_first. ```bash bundle exec puma apps/openapi_first_with_response_validation.ru ``` -------------------------------- ### Setup: bundle install in benchmarks directory Source: https://github.com/ahx/openapi_first/blob/main/benchmarks/README.md Run from the benchmarks directory to install required gems. ```bash cd benchmarks bundle install ``` -------------------------------- ### Register example app for assertions Source: https://github.com/ahx/openapi_first/blob/main/README.md Registers an OpenAPI file for use with `assert_api_conform`. Place in spec_helper.rb. ```ruby # spec_helper.rb OpenapiFirst::Test.setup do |test| test.register(File.join(__dir__, '../examples/openapi.yaml'), as: :example_app) end ``` -------------------------------- ### YAML $ref for parameters array Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Example of a YAML $ref for a parameters array, fixing $ref-resolving for referenced arrays. ```yaml parameters: $ref: 'my-paramters.yaml' ``` -------------------------------- ### Setup test mode in spec_helper.rb Source: https://github.com/ahx/openapi_first/blob/main/README.md Initializes OpenapiFirst::Test for contract testing. Place in spec_helper.rb before running tests. ```ruby # spec_helper.rb require 'openapi_first' OpenapiFirst::Test.setup ``` -------------------------------- ### Run benchmarks Source: https://github.com/ahx/openapi_first/blob/main/README.md Shell commands to run the benchmarks. Requires navigating to the 'benchmarks' directory, installing dependencies with Bundler, and executing the benchmark script. ```sh cd benchmarks bundle bundle exec ruby benchmarks.rb ``` -------------------------------- ### JSON:API error response example Source: https://github.com/ahx/openapi_first/blob/main/README.md Example of the JSON:API error response format. Uses application/vnd.api+json and includes errors with status, source.pointer, title, and code. ```json // http-status: 400 // content-type: "application/vnd.api+json" { "errors": [ { "status": "400", "source": { "pointer": "/data/name" }, "title": "value at `/data/name` is not a string", "code": "string" }, { "status": "400", "source": { "pointer": "/data/numberOfLegs" }, "title": "number at `/data/numberOfLegs` is less than: 2", "code": "minimum" }, { "status": "400", "source": { "pointer": "/data" }, "title": "object at `/data` is missing required properties: mandatory", "code": "required" } ] } ``` -------------------------------- ### Skip response coverage in OpenapiFirst::Test setup Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Use inside OpenapiFirst::Test.setup to exclude responses (e.g., status 401) from coverage calculation. Requires requiring 'openapi_first' and registering an OAD first. ```ruby require 'openapi_first' OpenapiFirst::Test.setup do |s| test.register('openapi/openapi.yaml') test.skip_response_coverage { it.status == '401' } end ``` -------------------------------- ### OpenAPI YAML with server URL and path Source: https://github.com/ahx/openapi_first/blob/main/README.md Example OpenAPI schema showing a server URL and a path definition. Used in the FAQ to illustrate a scenario where the actual endpoint URL includes an '/api' prefix not present in the schema. ```yaml servers: - url: https://yourhost/api paths: # The actual endpoint URL is https://yourhost/api/resource /resource: ``` -------------------------------- ### Configure OpenapiFirst::Test to ignore unknown requests Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md In OpenapiFirst 2.9.0, OpenapiFirst::Test raises an error for unknown requests. Set test.ignore_unknown_request = true inside the setup block to deactivate this behavior. ```ruby OpenapiFirst::Test.setup do |test| # ... test.ignore_unknown_request = true end ``` -------------------------------- ### Configure hooks globally with OpenapiFirst.configure Source: https://github.com/ahx/openapi_first/blob/main/README.md Use this to register hooks that apply to all instances. The after_request_parameter_property_validation hook receives the data hash, property name, and property schema. In the example, it converts date-formatted properties to Date objects. ```ruby OpenapiFirst.configure do |config| config.after_request_parameter_property_validation do |data, property, property_schema| data[property] = Date.iso8601(data[property]) if property_schema['format'] == 'date' end end ``` -------------------------------- ### Configure default error response plugin globally Source: https://github.com/ahx/openapi_first/blob/main/README.md Sets the plugin used to render error responses from the request validation middleware. The default is :default; this example changes it to :jsonapi. ```ruby OpenapiFirst.configure do |config| # Specify which plugin is used to render error responses returned by the request validation middleware (defaults to :default) config.request_validation_error_response = :jsonapi end ``` -------------------------------- ### Ignore request and response errors in tests Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Configure which request/response errors should not raise during testing. Use `ignore_request_error` to skip unknown requests on certain paths, and `ignore_response_error` to skip invalid response bodies on legacy paths. Note: the example uses `validated_request` in the response block, which may be a typo. ```ruby OpenapiFirst::Test.setup do |test| test.ignore_request_error do |validated_request| # Ignore unknown requests on certain paths validated_request.path.start_with?('/api/v1') && validated_request.unknown? end test.ignore_response_error do |validated_response, rack_request| # Ignore invalid response bodies on certain paths validated_request.path.start_with?('/api/legacy/stuff') && validated_request.error.type == :invalid_body end end ``` -------------------------------- ### Configure hooks per instance with OpenapiFirst.load Source: https://github.com/ahx/openapi_first/blob/main/README.md Use this to register request/response validation hooks for a single OpenAPI instance. The after_request_validation hook receives a validated_request object; after_response_validation receives the validated_response and the original request. In the example, after_response_validation warns with the request method, path, and error message when the response is invalid. ```ruby OpenapiFirst.load('openapi.yaml') do |config| config.after_request_validation do |validated_request| validated_request.valid? # => true / false end config.after_response_validation do |validated_response, request| if validated_response.invalid? warn "#{request.request_method} #{request.path}: #{validated_response.error.message}" end end end ``` -------------------------------- ### Run Ruby benchmarks: bundle exec ruby benchmarks.rb Source: https://github.com/ahx/openapi_first/blob/main/benchmarks/README.md Compares ips and memory usage for all apps defined in /apps. ```bash bundle exec ruby benchmarks.rb ``` -------------------------------- ### OpenapiFirst::Test.setup with minimum_coverage Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Sets up OpenapiFirst tests with a registered OpenAPI file and a minimum coverage threshold. Setting minimum_coverage to 100 causes an exit code 2 if coverage is below the minimum. ```ruby OpenapiFirst::Test.setup do |test| test.register('openapi/openapi.yaml') test.minimum_coverage = 100 # Setting this will lead to an `exit 2` if coverage is below minimum end ``` -------------------------------- ### Inject request/response validation in Rack app with Test.setup Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Use Test.setup with observe to inject validation into a Rack app as an alternative to overwriting the app method in a test. ```ruby Test.setup { it.observe(MyApp) } ``` -------------------------------- ### Run wrk benchmark script Source: https://github.com/ahx/openapi_first/blob/main/benchmarks/README.md Runs the wrk benchmark script. ```bash ./benchmark-wrk.sh ``` -------------------------------- ### Register OAD globally via OpenapiFirst.configure Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Use this to register an OpenAPI document globally, making the `spec` argument in middlewares optional and adding a cache for parsed OADs. ```ruby OpenapiFirst.configure { |config| config.register('openapi.yaml') } ``` -------------------------------- ### Configure static path_prefix for Definition Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Set a static path_prefix when registering an OpenAPI definition. This is useful when your API is mounted under a sub-path like '/weather'. ```ruby OpenapiFirst.configure do |config| config.register('openapi/openapi.yaml' path_prefix: '/weather') end ``` -------------------------------- ### Sinatra integration: define routes by referencing OpenAPI operations Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Register the OpenapiFirst::Sinatra extension and use the `operation` DSL to define routes. The HTTP method and path come from the operationId, and request validation is called automatically. ```ruby require 'openapi_first/sinatra' class PetsApi < Sinatra::Base register OpenapiFirst::Sinatra openapi 'openapi.yaml' operation :index_pets do |params| json index_pets(params[:filter]) end operation :create_pet do pet = create_pet(parsed_body) headers['Location'] = operation_url(:show_pet, petId: pet.id) status :created json pet end operation :show_pet do |params| json show_pet(params[:petId]) end end ``` -------------------------------- ### Configure error response plugin per instance Source: https://github.com/ahx/openapi_first/blob/main/README.md Loads an OpenAPI description and configures the error response plugin for that instance only, overriding the global default. ```ruby OpenapiFirst.load('openapi.yaml') do |config| config.request_validation_error_response = :jsonapi end ``` -------------------------------- ### Sinatra classic app with OpenapiFirst::Sinatra Source: https://github.com/ahx/openapi_first/blob/main/README.md Use this in a classic (top-level) Sinatra app. The extension registers itself, so only requiring the file is needed. Define operation blocks similarly to the modular version. ```ruby require 'sinatra' require 'openapi_first/sinatra' openapi 'openapi.yaml' operation :create_pet do json create_pet(parsed_body) end ``` -------------------------------- ### Set up contract testing with RSpec Source: https://github.com/ahx/openapi_first/blob/main/README.md Configures RSpec to include OpenapiFirst test methods for request specs, turning them into contract tests against the OpenAPI description. Place this in spec_helper.rb. ```ruby # spec_helper.rb require 'openapi_first' OpenapiFirst::Test.setup require 'my_app' RSpec.configure do |config| config.include OpenapiFirst::Test::Methods[MyApp], type: :request end ``` -------------------------------- ### Register multiple OADs with configuration Source: https://github.com/ahx/openapi_first/blob/main/README.md Registers multiple OpenAPI descriptions globally with optional names. The first registration is the default; the second is named :v1. ```ruby OpenapiFirst.configure do |config| config.register('openapi/openapi.yaml') # :default config.register('openapi/v1.openapi.yaml', as: :v1) end ``` -------------------------------- ### Sinatra modular app with OpenapiFirst::Sinatra Source: https://github.com/ahx/openapi_first/blob/main/README.md Use this in a modular Sinatra application to define routes from OpenAPI operations. Register the extension, call openapi with the spec file, and define operation blocks that receive params. Each operation validates the request before the block runs; violations return 400/415. ```ruby require 'sinatra/base' require 'openapi_first/sinatra' class PetsApi < Sinatra::Base register OpenapiFirst::Sinatra openapi 'openapi.yaml' operation :index_pets do |params| json index_pets(params[:filter]) end operation :create_pet do json create_pet(parsed_body[:data]) end end ``` -------------------------------- ### Register an OpenAPI description Source: https://github.com/ahx/openapi_first/blob/main/README.md Registers an OpenAPI description (OAD) globally so it can be used for validation and contract testing. This is the first step before using the middlewares or test helpers. ```ruby OpenapiFirst.register('openapi/openapi.yaml') ``` -------------------------------- ### Observe application with RSpec configuration Source: https://github.com/ahx/openapi_first/blob/main/README.md Adds an `app` method that wraps your application with silent request/response validation for plain rack-test (sinatra, roda, hanami, etc.). Include in RSpec configuration for request specs. ```ruby RSpec.configure do |config| config.include OpenapiFirst::Test::Methods[MyApp], type: :request end ``` -------------------------------- ### Configure path transformation with OpenapiFirst.load Source: https://github.com/ahx/openapi_first/blob/main/README.md Ruby configuration for OpenapiFirst to transform request paths by removing the '/api' prefix, bridging the gap between the schema's paths and the actual mounted application path. Use this when your app is mounted under a prefix not present in the schema. ```ruby oad = OpenapiFirst.load('openapi.yaml') do |config| config.path = ->(req) { req.path.delete_prefix('/api') } end use OpenapiFirst::Middlewares::RequestValidation, oad ``` -------------------------------- ### Use assert_api_conform in RSpec test Source: https://github.com/ahx/openapi_first/blob/main/README.md Runs request and response validation in a test using rack-test. Includes Rack::Test::Methods and OpenapiFirst::Test::Methods[App]. ```ruby RSpec.describe 'Example App' do include Rack::Test::Methods include OpenapiFirst::Test::Methods[App] it 'is API conform' do get '/' assert_api_conform(status: 200) end end ``` -------------------------------- ### Load API definition with OpenapiFirst.load Source: https://github.com/ahx/openapi_first/blob/main/README.md Loads an OpenAPI description from a YAML file into an OpenapiFirst definition object for subsequent request and response validation. ```ruby require 'openapi_first' definition = OpenapiFirst.load('openapi.yaml') ``` -------------------------------- ### Restore old request validation behavior in tests Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Use this include to restore the pre-3.2.0 behavior where the request is validated before the app handles it. This is needed if you rely on validation errors being raised before your app logic runs. ```ruby include OpenapiFirst::Test::Methods[MyApp, validate_request_before_handling: true] ``` -------------------------------- ### Configure response coverage reporting in OpenapiFirst::Test Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Set report_coverage to false or :warn to adapt coverage behavior. Tests fail if API description is not covered by tests by default. ```ruby OpenapiFirst::Test::Configuration#report_coverage = false ``` -------------------------------- ### Configure RequestValidation middleware with hooks Source: https://github.com/ahx/openapi_first/blob/main/README.md Passes a registered name or Definition with a block to configure hooks and options for this middleware only. Uses a copy of the OAD, leaving the registered one unchanged. ```ruby use OpenapiFirst::Middlewares::RequestValidation, :v1 do |config| config.request_validation_error_response = :jsonapi config.after_request_validation do |validated_request| warn validated_request.error.message if validated_request.invalid? end end ``` -------------------------------- ### Observe a specific API with Test.observe Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Use Test.observe to inject validation for a specific API into a Rack app. ```ruby Test.observe(App, api: :my_api) ``` -------------------------------- ### Access uploaded file from validated request Source: https://github.com/ahx/openapi_first/blob/main/README.md Shows how to access a multipart file from the validated request's parsed body. The tempfile is only usable during request handling because Rack removes it afterwards. ```ruby file = validated_request.parsed_body['file'] file[:filename] # => "cat.jpg" file[:type] # => "image/jpeg" file[:tempfile] # => # Read or stream this in your application. ``` -------------------------------- ### Default error response format Source: https://github.com/ahx/openapi_first/blob/main/README.md Default machine-readable error response for request validation failures. Uses application/problem+json and includes title, status, and errors with message, pointer, and code. ```json http-status: 400 content-type: "application/problem+json" { "title": "Bad Request Body", "status": 400, "errors": [ { "message": "value at `/data/name` is not a string", "pointer": "/data/name", "code": "string" }, { "message": "number at `/data/numberOfLegs` is less than: 2", "pointer": "/data/numberOfLegs", "code": "minimum" }, { "message": "object at `/data` is missing required properties: mandatory", "pointer": "/data", "code": "required" } ] } ``` -------------------------------- ### Configure ignored unknown statuses in Test::Configuration Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Set ignored_unknown_status to configure response statuses that do not need to be described in the API description. 404 statuses are ignored by default. ```ruby Test::Configuration#ignored_unknown_status ``` -------------------------------- ### Ignore all unknown response statuses Source: https://github.com/ahx/openapi_first/blob/main/README.md Sets `ignore_unknown_response_status` to true to ignore all unknown response statuses instead of raising an error. ```ruby OpenapiFirst::Test.setup do |test| test.ignore_unknown_response_status = true end ``` -------------------------------- ### Use request validation middleware Source: https://github.com/ahx/openapi_first/blob/main/README.md Adds the request validation middleware to a Rack application. It validates incoming requests against the registered OpenAPI description. ```ruby use OpenapiFirst::Middlewares::RequestValidation ``` -------------------------------- ### Ignore request and response errors Source: https://github.com/ahx/openapi_first/blob/main/README.md Configures OpenapiFirst::Test to ignore certain request/response errors when the block returns true. Useful for ignoring unknown requests on specific paths or invalid response bodies on legacy paths. ```ruby OpenapiFirst::Test.setup do |test| test.ignore_request_error do |validated_request| # Ignore unknown requests on certain paths validated_request.path.start_with?('/api/v1') && validated_request.unknown? end test.ignore_response_error do |validated_response, rack_request| # Ignore invalid response bodies on certain paths rack_request.path.start_with?('/api/legacy/stuff') && validated_response.error.type == :invalid_body end end ``` -------------------------------- ### Use JSON:API error response Source: https://github.com/ahx/openapi_first/blob/main/README.md Enables the JSON:API error response format by passing error_response: :jsonapi to the middleware. ```ruby use OpenapiFirst::Middlewares::RequestValidation, error_response: :jsonapi ``` -------------------------------- ### Validate request with definition.validate_request Source: https://github.com/ahx/openapi_first/blob/main/README.md Validates a Rack request against the loaded API definition. Returns a validated request object with methods to inspect validity, errors, and parsed parameters. Use `raise_error: true` to raise OpenapiFirst::RequestInvalidError or OpenapiFirst::NotFoundError on invalid requests. ```ruby validated_request = definition.validate_request(rack_request) # Inspect the request and access parsed parameters validated_request.valid? validated_request.invalid? validated_request.error # => Failure object or nil validated_request.parsed_body # => The parsed request body (Hash) validated_request.parsed_query # A Hash of query parameters that are defined in the API description, parsed exactly as described. validated_request.parsed_path_parameters validated_request.parsed_headers validated_request.parsed_cookies validated_request.parsed_params # Merged parsed path, query parameters and request body # Access the Openapi 3 Operation Object Hash validated_request.operation['x-foo'] validated_request.operation['operationId'] => "getStuff" # or the whole request definition validated_request.request_definition.path # => "/pets/{petId}" validated_request.request_definition.operation_id # => "showPetById" # Inspect the parameters that are defined for this request, in the order path, query, header, cookie. # Parameters that openapi_first ignores (Content-Type, Accept, Authorization) are not included. parameter = validated_request.request_definition.parameters.first parameter.name # => "petId" parameter.location # => "path" ("path", "query", "header" or "cookie") parameter.schema # => { "type" => "integer" } (the JSON Schema, with $refs resolved) parameter.required? # => true parameter.deprecated? # => false parameter.style # => "simple" parameter.explode? # => false parameter.media_type # => "application/json" if the parameter uses `content`, otherwise nil # Or you can raise an exception if validation fails: definition.validate_request(rack_request, raise_error: true) # Raises OpenapiFirst::RequestInvalidError or OpenapiFirst::NotFoundError if request is invalid ``` -------------------------------- ### Use ResponseValidation middleware Source: https://github.com/ahx/openapi_first/blob/main/README.md Raises an error by default if the response is invalid. Useful in test or staging environments. Pass raise_error: false to not raise. ```ruby use OpenapiFirst::Middlewares::ResponseValidation if ENV['RACK_ENV'] == 'test' # Pass `raise_error: false` to not raise an error: use OpenapiFirst::Middlewares::ResponseValidation, raise_error: false ``` -------------------------------- ### Skip coverage for a route Source: https://github.com/ahx/openapi_first/blob/main/README.md Skips coverage for a request and all its responses for a specific route using `skip_coverage`. The block receives path and request method. ```ruby OpenapiFirst::Test.setup do |test| test.skip_coverage do |path, request_method| path == '/bookings/{bookingId}' && request_method == 'DELETE' end end ``` -------------------------------- ### Ignore unknown requests Source: https://github.com/ahx/openapi_first/blob/main/README.md Sets `ignore_unknown_requests` to true to deactivate the error raised when a request is not defined. ```ruby OpenapiFirst::Test.setup do |test| test.ignore_unknown_requests = true end ``` -------------------------------- ### definition.validate_response Source: https://github.com/ahx/openapi_first/blob/main/README.md Validates a Rack response against the OpenAPI definition. Returns a validated response object that provides access to parsed body and headers. ```APIDOC ## definition.validate_response ### Description Validates a Rack response against the OpenAPI definition. Returns a validated response object that provides access to parsed body and headers. ### Method validate_response ### Parameters - **rack_request** (Rack::Request) - Required - The Rack request associated with the response. - **rack_response** (Rack::Response) - Required - The Rack response to validate. - **raise_error** (Boolean) - Optional - If true, raises an exception on validation failure. Default is false. ### Returns - **validated_response** (OpenapiFirst::ValidatedResponse) - The validated response object. ### Example ```ruby validated_response = definition.validate_response(rack_request, rack_response) # Inspect the response and access parsed parameters validated_response.valid? validated_response.invalid? validated_response.error # => Failure object or nil validated_response.status # => 200 validated_response.parsed_body validated_response.parsed_headers # Or you can raise an exception if validation fails: definition.validate_response(rack_request, rack_response, raise_error: true) # Raises OpenapiFirst::ResponseInvalidError or OpenapiFirst::ResponseNotFoundError ``` ``` -------------------------------- ### Add ignored unknown status code Source: https://github.com/ahx/openapi_first/blob/main/README.md Adds 403 to the list of ignored unknown response statuses. By default, OpenapiFirst::Test raises an error for undefined response statuses except 404 and 500. ```ruby OpenapiFirst::Test.setup do |test| test.ignored_unknown_status << 403 end ``` -------------------------------- ### definition.validate_request Source: https://github.com/ahx/openapi_first/blob/main/README.md Validates a Rack request against the OpenAPI definition. Returns a validated request object that provides access to parsed parameters and validation status. ```APIDOC ## definition.validate_request ### Description Validates a Rack request against the OpenAPI definition. Returns a validated request object that provides access to parsed parameters and validation status. ### Method validate_request ### Parameters - **rack_request** (Rack::Request) - Required - The Rack request to validate. - **raise_error** (Boolean) - Optional - If true, raises an exception on validation failure. Default is false. ### Returns - **validated_request** (OpenapiFirst::ValidatedRequest) - The validated request object. ### Example ```ruby validated_request = definition.validate_request(rack_request) # Inspect the request and access parsed parameters validated_request.valid? validated_request.invalid? validated_request.error # => Failure object or nil validated_request.parsed_body # => The parsed request body (Hash) validated_request.parsed_query # A Hash of query parameters that are defined in the API description, parsed exactly as described. validated_request.parsed_path_parameters validated_request.parsed_headers validated_request.parsed_cookies validated_request.parsed_params # Merged parsed path, query parameters and request body # Access the Openapi 3 Operation Object Hash validated_request.operation['x-foo'] validated_request.operation['operationId'] => "getStuff" # or the whole request definition validated_request.request_definition.path # => "/pets/{petId}" validated_request.request_definition.operation_id # => "showPetById" # Inspect the parameters that are defined for this request, in the order path, query, header, cookie. # Parameters that openapi_first ignores (Content-Type, Accept, Authorization) are not included. parameter = validated_request.request_definition.parameters.first parameter.name # => "petId" parameter.location # => "path" ("path", "query", "header" or "cookie") parameter.schema # => { "type" => "integer" } (the JSON Schema, with $refs resolved) parameter.required? # => true parameter.deprecated? # => false parameter.style # => "simple" parameter.explode? # => false parameter.media_type # => "application/json" if the parameter uses `content`, otherwise nil # Or you can raise an exception if validation fails: definition.validate_request(rack_request, raise_error: true) # Raises OpenapiFirst::RequestInvalidError or OpenapiFirst::NotFoundError if request is invalid ``` ``` -------------------------------- ### Configure OpenapiFirst::Test to not raise on invalid response Source: https://github.com/ahx/openapi_first/blob/main/CHANGELOG.md Set response_raise_error to false to revert to old behavior where invalid responses do not raise an error. This is not recommended. ```ruby OpenapiFirst::Test::Configuration#response_raise_error = false ``` -------------------------------- ### Skip coverage for certain responses Source: https://github.com/ahx/openapi_first/blob/main/README.md Excludes certain responses from coverage using `skip_response_coverage`. The block receives a response definition and returns true to skip. ```ruby OpenapiFirst::Test.setup do |test| test.skip_response_coverage do |response_definition| response_definition.status == '5XX' end end ``` -------------------------------- ### Validate response with definition.validate_response Source: https://github.com/ahx/openapi_first/blob/main/README.md Validates a Rack response against the loaded API definition. Returns a validated response object with methods to inspect validity, errors, status, and parsed body/headers. Use `raise_error: true` to raise OpenapiFirst::ResponseInvalidError or OpenapiFirst::ResponseNotFoundError on invalid responses. ```ruby validated_response = definition.validate_response(rack_request, rack_response) # Inspect the response and access parsed parameters validated_response.valid? validated_response.invalid? validated_response.error # => Failure object or nil validated_response.status # => 200 validated_response.parsed_body validated_response.parsed_headers # Or you can raise an exception if validation fails: definition.validate_response(rack_request, rack_response, raise_error: true) # Raises OpenapiFirst::ResponseInvalidError or OpenapiFirst::ResponseNotFoundError ```