Class: Goliath::Rack::Validation::RequiredParam
- Inherits:
-
Object
- Object
- Goliath::Rack::Validation::RequiredParam
- Includes:
- Goliath::Rack::Validator
- Defined in:
- lib/goliath/rack/validation/required_param.rb
Overview
A middleware to validate that a given parameter is provided.
Instance Attribute Summary (collapse)
-
- (Object) key
readonly
Returns the value of attribute key.
-
- (Object) type
readonly
Returns the value of attribute type.
Instance Method Summary (collapse)
- - (Object) call(env)
-
- (Goliath::Rack::Validation::RequiredParam) initialize(app, opts = {})
constructor
Creates the Goliath::Rack::Validation::RequiredParam validator.
- - (Boolean) key_valid?(params)
Methods included from Goliath::Rack::Validator
Constructor Details
- (Goliath::Rack::Validation::RequiredParam) initialize(app, opts = {})
Creates the Goliath::Rack::Validation::RequiredParam validator
22 23 24 25 26 |
# File 'lib/goliath/rack/validation/required_param.rb', line 22 def initialize(app, opts = {}) @app = app @key = opts[:key] || 'id' @type = opts[:type] || @key.capitalize end |
Instance Attribute Details
- (Object) key (readonly)
Returns the value of attribute key
13 14 15 |
# File 'lib/goliath/rack/validation/required_param.rb', line 13 def key @key end |
- (Object) type (readonly)
Returns the value of attribute type
13 14 15 |
# File 'lib/goliath/rack/validation/required_param.rb', line 13 def type @type end |
Instance Method Details
- (Object) call(env)
28 29 30 31 |
# File 'lib/goliath/rack/validation/required_param.rb', line 28 def call(env) return validation_error(400, "#{@type} identifier missing") unless key_valid?(env['params']) @app.call(env) end |
- (Boolean) key_valid?(params)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/goliath/rack/validation/required_param.rb', line 33 def key_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) unless params[key].compact.empty? params[key].each do |k| return true unless k.is_a?(String) return true unless k =~ /^\s*$/ end end return false end true end |