0.0
No commit activity in last 3 years
No release in over 3 years
Validate parameters presence and type for Rails API methods
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Dependencies

Development

~> 1.13
~> 10.0
~> 3.0

Runtime

~> 5.0.0.1
 Project Readme

ParamCheck

CircleCI codecov

Easily validate params for API methods. Are you sick of writing respond_with(error) unless params[:foo].present?? With ParamCheck, you can abstract all of the validation.

Installation

Add this line to your application's Gemfile:

gem 'param_check'

And then execute:

$ bundle

Usage

Validating presence

By default, a parameter is not required unless you specify the required: true option

def index
  param! :foo, required: true
end
def index
  param! :foo, required: true do |foo| # params[foo]
    foo.param! :bar, required: true # params[foo][bar]
  end
end

Validating type

def index
  param! :foo, type: String
end
def index
  param! :foo, type: ActionController::Parameters do |foo|
    foo.param! :bar, type: Integer
    foo.param! :baz, type: String
  end
end

# Passes
params[:foo] = {
  bar: 1,
  baz: 'Two',
}

# Fails
params[:foo] = {
  bar: 'One',
  baz: 2,
}
params[:foo] = 'bar'

Validating numbers

For number validation, you can use either the long hand keys less_than or the short hand lt

def index
  param! :foo, lt: 2
  param! :bar, lte: 3
  param! :baz, mt: 4
  param! :bazzy, mte: 5
end

Validating inclusion

def index
  param! :foo, in: ['foo', 'bar']
  param! :bar, in: 1..4
  param! :baz, in: 'a'..'z'
end

By default, ParamCheck throws a ParameterError exception. You can rescue from this exception in order to respond with JSON for example:

# in API::ApplicationController
rescue_from ParameterError do |e|
  render json: { error: e.message }
end

Options

Option Type Default Values Description
required Boolean false true/false Is the parameter required?
type Class nil Fixnum/Integer/Float/String/ActionController::Params Specifies the type the parameter should be
lt Integer nil Integer/Float The parameter value should be less than
lte Integer nil Integer/Float The parameter value should be less than or equal to
mt Integer nil Integer/Float The parameter value should be more than
mte Integer nil Integer/Float The parameter value should be more than or equal to
in Array nil Array An array of values the param should match

Production sites using this gem

Submit a PR to add your site to the list

Givey

Development

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.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cjmarkham/param_check. 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.

License

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