Skip to content
Shield Your API Endpoints from Improperly Formatted Requests!
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Build Status Gem Version


Tired of getting weird errors when users (or your own clients) submit random junk to your Rails API? SwaggerShield is here to save the day!


# You can add to any controller, or to ApplicationController if you want
# SwaggerShield to protect your whole app.
class ApplicationController < ActionController::API

    # replace with the location of your actual swagger YAML file:
    swagger_file: File.join('config', 'swagger.yml'),

    # add on any valid "if" or "unless" conditionals that can be applied to a
    # Rails before_action
    if: -> { current_user.test_user? },
    unless: -> { params[:skip_swagger_shield] }

Now, everything will work as before, as long as the requests are properly formatted. But if requests don't match your Swagger spec:

You Shall Not Pass!

OK, maybe it's not that dramatic. But your client will see an error pointing to exactly what they messed up in the request:

  "errors": [
      "status": "422",
      "detail": "The property '#/widget/price' of type string did not match the following type: integer",
      "source": {
        "pointer": "#/widget/price"

(Only errors in JSON API format are supported, hopefully that'll be updated soon...)


This project is under active development, being built up in stages as bits become necessary for projects that make money. So there's still plenty of stuff to implement; use at your own risk.

That said, the project will gratefully accept the implementation of new types, better error messaging, etc., basically anything you find useful in your own work which seems generally applicable. So please contribute!


Add this line to your application's Gemfile:

gem 'swagger_shield'

And then execute:

$ bundle

Or install it yourself as:

$ gem install swagger_shield


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


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

Code of Conduct

Everyone interacting in the SwaggerShield project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

You can’t perform that action at this time.