Digget
This gem aims to make for an easier search API by validating incoming parameters and filtering your model accordingly.
Note: this gem is under very early development and is not finished in any way
Usage
Validating of parameters
The only thing you have to do is extends the Validator class, so you can use the verify
method.
class YourValidator < Digget::Validator
def validate_id_params
verify :id, Integer, max: 100, min: 50
end
def validate_search_params
verify :name, String, required: true
verify :size, Float, required: true, min: 15
end
end
To validate the params
you need to run the validator;
validator = YourValidator.new(params)
validator.validate_id_params
You can then see if there are any errors during validation
validator.errors
You can also get a Hash with the parameters casted to the right datatype;
validator.casted_params
Supported datatypes
- Float
- Integer
- String
- Date
- Time
Supported validation options
All of the following options can be combined
- required (checks whether the parameter is present)
- min (checks whether the parameter is larger than the provided value)
- max (checks whether the parameter is lower than the provided value)
- min_length (checks whether the parameter is longer than this)
- max_length (checks whether the parameter is shorter than this)
- length (checks whether the parameter has this exact length)
- equal (check whether the parameter is equal to the provided value)
- not_equal (checks whether the parameter is not equal to the provided value)
Installation
Add this line to your application's Gemfile:
gem 'digget'
And then execute:
$ bundle
Or install it yourself as:
$ gem install digget
License
The gem is available as open source under the terms of the MIT License.