{"id":15640010,"url":"https://github.com/lygaret/rack-params","last_synced_at":"2025-09-05T11:36:45.851Z","repository":{"id":90191381,"uuid":"118575214","full_name":"lygaret/rack-params","owner":"lygaret","description":"`Rack::Request.params` validation and type coercion, on Rack.","archived":false,"fork":false,"pushed_at":"2022-06-09T04:42:38.000Z","size":133,"stargazers_count":31,"open_issues_count":2,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-01-11T15:58:02.761Z","etag":null,"topics":["coercion","parameters","rack","ruby","validation"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lygaret.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-23T07:37:24.000Z","updated_at":"2022-06-19T00:16:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"84de8d00-18fe-46aa-aaea-cf7ccc06ce3c","html_url":"https://github.com/lygaret/rack-params","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lygaret%2Frack-params","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lygaret%2Frack-params/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lygaret%2Frack-params/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lygaret%2Frack-params/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lygaret","download_url":"https://codeload.github.com/lygaret/rack-params/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234675473,"owners_count":18869958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coercion","parameters","rack","ruby","validation"],"created_at":"2024-10-03T11:29:48.284Z","updated_at":"2025-01-19T17:42:40.567Z","avatar_url":"https://github.com/lygaret.png","language":"Ruby","readme":"# Rack::Params\n[![Gem Version](https://badge.fury.io/rb/rack-params.svg)](https://badge.fury.io/rb/rack-params) [![Build Status](https://travis-ci.org/lygaret/rack-params.svg?branch=master)](https://travis-ci.org/lygaret/rack-params) [![Coverage Status](https://coveralls.io/repos/github/lygaret/rack-params/badge.svg?branch=master)](https://coveralls.io/github/lygaret/rack-params?branch=master)\n\n`Rack::Request.params` validation and type coercion, on Rack.\n\n## Usage\n\n**[Documentation](https://lygaret.github.io/rack-params/)**\n\n1. Include `Rack::Params` to get the `.validator`, `#validate` and `#validate!` methods.\n2. Call `.validator(name, options = {}, \u0026code)` to register a named validator for use later.\n3. Call `#validate(name = nil, params = request.params, options = {}, \u0026code)` to build a new result, with the results of validation and coercion.\n4. The blocks passed to the validation methods run in the context of `HashContext` and `ArrayContext`, which is where the coercion methods are defined.\n\n## Example\n\n```ruby\n# NOTE (to self) - if this changes, update `readme_spec.rb`\n\nclass SomeExampleApp\n  include Rack::Params\n\n  # create named validators that can be run at any time\n  \n  validator :document do\n    param :id,      Integer,  required: true\n    param :title,   String,   required: true\n    param :created, DateTime\n    \n    param :tags, Array do\n      every :symbol\n    end\n    \n    param :content, Hash, required: true do\n      param :header, String\n      param :body,   String, required: true\n    end\n  end\n  \n  # run pre-defined or ad-hoc transforms on some hash\n  # only keys in the validator blocks are copied, see #splat\n\n  def call(env)\n    request = Rack::Request.new(env)\n    \n    params = request.params\n    params = validate(request.params, :document)\n    if params.valid?\n      assert params[\"id\"].is_a? Integer\n      assert (not params[\"content\"][\"body\"].nil?)\n    else\n      assert params.errors.length \u003e 0\n      assert params.invalid?\n    end\n\n    # or\n    params = { \"flag\" =\u003e \"f\", \"some\" =\u003e \"other key\", \"and\" =\u003e \"one more\" }\n    params = validate(params) do\n      param :flag,  :boolean, required: true\n      splat :rest\n    end\n    \n    if params.valid?\n      assert [true, false].include?(params[\"flag\"])\n      assert ([\"some\", \"and\"] - params[\"rest\"]).empty?\n    end\n\n  end\nend\n\n# if you're using a framework which provides `request` as a getter method\n# include the connector, which provides a `#params` override, and allows\n# defaulting to request.params in validate calls\n\nclass FancyApp \u003c Nancy::Base\n  include Rack::Params::Connect\n\n  validator :check do\n    :id, Integer\n  end\n  \n  get \"/\" do\n    validate :check\n\n    if params.valid?\n      assert params[\"id\"].is_a? Integer\n    else\n      assert params.errors.length \u003e 0\n      assert params.invalid?\n    end\n    \n    \"magic 8-ball, how's my params? \u003c\u003c uncertan. \u003e\u003e\"\n  end\n\n  get \"/blow-up-on-failure\" do\n    validate! :check\n    assert params.valid?\n\n    \"if I'm going down, I'm taking you all with me.\"\n  end\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rack-params'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rack-params\n\n## Development\n\nAfter 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.\n\nTo 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).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/lygaret/rack-params.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Authors\n\n* Jon Raphaelson - https://accidental.cc\n* Based heavily on [`mattt/sinatra-param`](https://github.com/mattt/sinatra-param), though diverging significantly in ways.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flygaret%2Frack-params","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flygaret%2Frack-params","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flygaret%2Frack-params/lists"}