A really naive parameter filtering implementation for Sinatra.
Add this line to your application's Gemfile:
gem 'sinatra-strong-params', require: 'sinatra/strong-params'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sinatra-strong-params
If you are using a Modular Sinatra application such as class MyApp < Sinatra::Base
you must include any desired extensions explicitly within your Sinatra application:
register Sinatra::StrongParams
This gem adds two filters to Sinatra routes: allows
and needs
.
A way to whitelist parameters in the request scope.
get '/', allows: [:id, :action] do
erb :index
end
allows
modifies the parameters available in the request scope keeping just the allowed params.
A way to require parameters in the request scope.
get '/', needs: [:id, :action] do
erb :index
end
needs
does not modify the parameters available to the request scope
but raises a RequiredParamMissing
error if a needed param is missing.
Catching a missing parameter error:
error RequiredParamMissing do
[400, env['sinatra.error'].message]
end
Wanna get super restrictive? Can do.
post '/login', needs: [:email, :password], allows: [:name] do
# handle yo business
end
- Fork it ( https://github.com/[my-github-username]/sinatra-strong-params/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes with tests (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request