class Goliath::Rack::Validation::RequiredValue
use Goliath::Rack::Validation::RequiredValue, {:key => ‘baz’, :values => ‘awesome’}
use Goliath::Rack::Validation::RequiredValue, {:key => ‘mode’, :values => %w(foo bar)}
@example
Middleware to validate that a given parameter has a specified value.
def call(env)
def call(env) return validation_error(400, "Provided #{@key} is invalid") unless value_valid?(env['params']) @app.call(env) end
def initialize(app, opts = {})
-
(Goliath::Rack::Validation::RequiredValue)
- The validator
Options Hash:
(**opts)
-
:values
(String | Array
) -- The values to verify are in the params -
:key
(String
) -- The key to look for in params (default: id)
Parameters:
-
opts
(Hash
) -- The options to create the validator with -
app
() -- The app object
def initialize(app, opts = {}) @app = app @key = opts[:key] || 'id' @values = [opts[:values]].flatten end
def value_valid?(params)
def value_valid?(params) if !params.has_key?(key) || params[key].nil? || (params[key].is_a?(String) && params[key] =~ /^\s*$/) return false end if params[key].is_a?(Array) return false if params[key].empty? params[key].each { |k| return false unless values.include?(k) } elsif !values.include?(params[key]) return false end true end