A simple parameter checker in controller for Rails Api.
Gemfile
gem "lucky_param"
app/controllers/application_controller.rb
class ApplicationController
include LuckyParam
# If necessary, you can overide or define your own parameter checkers.
LuckyParam::CUSTOM_CHECKER = {
Password: [
->(params, column) { params[column] =~ /([0-9a-zA-Z]){6,30}/ },
"must be valid password"
],
}.freeze
end
app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def create
required :email, :Email
required :password, :Password
optional :nick_name, :String
render json: 'ok'
end
end
- Fork it ( https://github.com/shootingfly/lucky_param/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Make your changes
- Run
ruby test/lucky_param_test.rb
to run the tests - Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
The gem is available as open source under the terms of the MIT License.