No release in over a year
A simple params sanitizer (originally created for sinatra)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

sinatra-my-params

Check for your inputs params, both by name and type, either hard or soft check

Method signature

permitted_params(params, permitted = {}, strong_validation = false)

If strong_validation is set to true and the input parameter is not valid, method will rise an error. If strong_validation is set to false and the input parameter is not valid, the parameter will be just ignored(removed). Parameters outside of permitted ones will be removed.

Example 1:

input = { parameter: 'a string' }

permitted_params(
  input, { parameter: String }
)

output

{ parameter: 'a string' }

Example 2:

input = { parameter: 'a string' }

permitted_params(
  input, { parameter: Integer }
)

output

{ }

Example 3:

To ignore type

input = { parameter: 'a string' }

permitted_params(
  input, { parameter: Any }
)

output

{ parameter: 'a string' }

Usage in class

class Controller 
  include PermitParams

  get "/endpoint" do
    permitted_params = permitted_params(
      params, { parameter: String }
    )

    ...
  end
end

Permitted params types

  • Any(no type check, only name check)
  • Boolean
  • Integer
  • Float
  • String
  • Date
  • Time
  • DateTime
  • Array
  • Hash
  • TrueClass
  • FalseClass
  • Shape(experimental hash shape). More info in tests

All feedback is welcome.