https://github.com/lygaret/rack-params
`Rack::Request.params` validation and type coercion, on Rack.
https://github.com/lygaret/rack-params
coercion parameters rack ruby validation
Last synced: about 1 month ago
JSON representation
`Rack::Request.params` validation and type coercion, on Rack.
- Host: GitHub
- URL: https://github.com/lygaret/rack-params
- Owner: lygaret
- License: mit
- Created: 2018-01-23T07:37:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-06-09T04:42:38.000Z (over 3 years ago)
- Last Synced: 2025-01-11T15:58:02.761Z (9 months ago)
- Topics: coercion, parameters, rack, ruby, validation
- Language: Ruby
- Size: 130 KB
- Stars: 31
- Watchers: 7
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Rack::Params
[](https://badge.fury.io/rb/rack-params) [](https://travis-ci.org/lygaret/rack-params) [](https://coveralls.io/github/lygaret/rack-params?branch=master)`Rack::Request.params` validation and type coercion, on Rack.
## Usage
**[Documentation](https://lygaret.github.io/rack-params/)**
1. Include `Rack::Params` to get the `.validator`, `#validate` and `#validate!` methods.
2. Call `.validator(name, options = {}, &code)` to register a named validator for use later.
3. Call `#validate(name = nil, params = request.params, options = {}, &code)` to build a new result, with the results of validation and coercion.
4. The blocks passed to the validation methods run in the context of `HashContext` and `ArrayContext`, which is where the coercion methods are defined.## Example
```ruby
# NOTE (to self) - if this changes, update `readme_spec.rb`class SomeExampleApp
include Rack::Params# create named validators that can be run at any time
validator :document do
param :id, Integer, required: true
param :title, String, required: true
param :created, DateTime
param :tags, Array do
every :symbol
end
param :content, Hash, required: true do
param :header, String
param :body, String, required: true
end
end
# run pre-defined or ad-hoc transforms on some hash
# only keys in the validator blocks are copied, see #splatdef call(env)
request = Rack::Request.new(env)
params = request.params
params = validate(request.params, :document)
if params.valid?
assert params["id"].is_a? Integer
assert (not params["content"]["body"].nil?)
else
assert params.errors.length > 0
assert params.invalid?
end# or
params = { "flag" => "f", "some" => "other key", "and" => "one more" }
params = validate(params) do
param :flag, :boolean, required: true
splat :rest
end
if params.valid?
assert [true, false].include?(params["flag"])
assert (["some", "and"] - params["rest"]).empty?
endend
end# if you're using a framework which provides `request` as a getter method
# include the connector, which provides a `#params` override, and allows
# defaulting to request.params in validate callsclass FancyApp < Nancy::Base
include Rack::Params::Connectvalidator :check do
:id, Integer
end
get "/" do
validate :checkif params.valid?
assert params["id"].is_a? Integer
else
assert params.errors.length > 0
assert params.invalid?
end
"magic 8-ball, how's my params? << uncertan. >>"
endget "/blow-up-on-failure" do
validate! :check
assert params.valid?"if I'm going down, I'm taking you all with me."
end
end
```## Installation
Add this line to your application's Gemfile:
```ruby
gem 'rack-params'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install rack-params
## 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](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/lygaret/rack-params.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Authors
* Jon Raphaelson - https://accidental.cc
* Based heavily on [`mattt/sinatra-param`](https://github.com/mattt/sinatra-param), though diverging significantly in ways.