All Projects → lygaret → rack-params

lygaret / rack-params

Licence: MIT license
`Rack::Request.params` validation and type coercion, on Rack.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to rack-params

rack-cargo
🚚 Batch requests for Rack apps (works with Rails, Sinatra, etc)
Stars: ✭ 17 (-45.16%)
Mutual labels:  rack
ethereumjs-common
Project is in active development and has been moved to the EthereumJS VM monorepo.
Stars: ✭ 25 (-19.35%)
Mutual labels:  parameters
whitelister
Simple, basic filtering and validation tool for Node.js.
Stars: ✭ 46 (+48.39%)
Mutual labels:  parameters
opzioni
The wanna-be-simplest command line arguments library for C++
Stars: ✭ 29 (-6.45%)
Mutual labels:  parameters
talos
Elixir parameter validation library. Simple and extensible
Stars: ✭ 23 (-25.81%)
Mutual labels:  parameters
encrypted cookie
AES-128 encrypted session cookies for Rack (and Sinatra and other frameworks).
Stars: ✭ 54 (+74.19%)
Mutual labels:  rack
grape-jwt-authentication
A reusable Grape JWT authentication concern
Stars: ✭ 31 (+0%)
Mutual labels:  rack
web pipe
One-way pipe, composable, rack application builder
Stars: ✭ 56 (+80.65%)
Mutual labels:  rack
lkm-sandbox
Collection of Linux Kernel Modules and PoC to discover, learn and practice Linux Kernel Development
Stars: ✭ 36 (+16.13%)
Mutual labels:  parameters
rack-secure-upload
Upload files securely
Stars: ✭ 75 (+141.94%)
Mutual labels:  rack
rack-queries
A page in your rack-based application that allows quick execution of pre-built queries
Stars: ✭ 25 (-19.35%)
Mutual labels:  rack
anycable-rack-server
AnyCable-compatible Ruby Rack middleware
Stars: ✭ 21 (-32.26%)
Mutual labels:  rack
definition
Simple and composable validation and coercion of data structures
Stars: ✭ 15 (-51.61%)
Mutual labels:  coercion
itop-datacenter-view
Extension for iTop: Easily manage & visualize your racks, enclosures and datacenter devices.
Stars: ✭ 24 (-22.58%)
Mutual labels:  rack
sinatra-api-server-toolbox
Sinatra API Server Toolbox (Ruby, Sinatra, ActiveRecord, postgreSQL, JSON, jQuery, AJAX)
Stars: ✭ 21 (-32.26%)
Mutual labels:  rack
cleye
👁‍🗨 cleye — The intuitive & typed CLI development tool for Node.js
Stars: ✭ 235 (+658.06%)
Mutual labels:  parameters
dredd-rack
The Dredd API blueprint testing tool for your Rack applications.
Stars: ✭ 50 (+61.29%)
Mutual labels:  rack
ruby wolf
Tiny ruby web server for research and studying purpose
Stars: ✭ 19 (-38.71%)
Mutual labels:  rack
koa-smart
A framework base on Koajs2 with Decorator, Params checker and a base of modules (cors, bodyparser, compress, I18n, etc…) to let you develop smart api easily
Stars: ✭ 31 (+0%)
Mutual labels:  parameters
TestNG-Foundation
TestNG Foundation is a lightweight collection of TestNG listeners, interfaces, and static utility classes that supplement and augment the functionality provided by the TestNG API.
Stars: ✭ 12 (-61.29%)
Mutual labels:  parameters

Rack::Params

Gem Version Build Status Coverage Status

Rack::Request.params validation and type coercion, on Rack.

Usage

Documentation

  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

# 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 #splat

  def 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?
    end

  end
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 calls

class FancyApp < Nancy::Base
  include Rack::Params::Connect

  validator :check do
    :id, Integer
  end
  
  get "/" do
    validate :check

    if 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. >>"
  end

  get "/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:

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.

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.

Authors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].