Skip to content

Validate requests/responses against OpenAPI API descriptions. Make sure your mocks match your API description.

License

Notifications You must be signed in to change notification settings

ahx/faraday-openapi

Repository files navigation

Faraday Openapi

Validate requests/responses against OpenAPI API descriptions.

This middleware raises an error if the request or response does not match the given API description. You can use this to test your client code or to make sure your mocks do match the actual implementation, described in the API description.

Note that the middleware currently deliberately ignores unknown responses with status codes 401 or higher because those usually don't come with a useful response body.

TL;DR

conn = Faraday.new do |f|
  f.use :openapi
end

Installation

Add this line to your application's Gemfile:

gem 'faraday-openapi'

And then execute:

bundle install

Usage

In order to avoid loading YAML files at inappropriate times you should register your API description (OAD) globally and reference it via a Symbol in your client code

# initializer.rb

require 'faraday/openapi'
Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api

# Only activate in test env
Faraday::Openapi::Middleware.enabled = ENV['RACK_ENV'] == 'test'
# some_client.rb
require 'faraday/openapi'

conn = Faraday.new do |f|
  f.use :openapi, :dice_api
end

# Or validate only requests
conn = Faraday.new do |f|
  f.request :openapi, :dice_api
end

# Or validate only responses
conn = Faraday.new do |f|
  f.response :openapi, :dice_api
end

You can disable the whole middleware globally via Faraday's conventional default_options as well:

Faraday::Openapi::Middleware.default_options[:enabled] = false

Development

After checking out the repo, run bin/setup to install dependencies.

Then, run bin/test to run the tests.

To install this gem onto your local machine, run rake build.

To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run rake release. See how it works here.

Contributing

Bug reports and pull requests are welcome on Codeberg or Github.

License

The gem is available as open source under the terms of the MIT License.

About

Validate requests/responses against OpenAPI API descriptions. Make sure your mocks match your API description.

Topics

Resources

License

Stars

Watchers

Forks