Class: Goliath::Rack::Validation::RequiredParam
- Inherits:
-
Object
- Object
- Goliath::Rack::Validation::RequiredParam
- Defined in:
- lib/goliath/rack/validation/required_param.rb
Overview
A middleware to validate that a given parameter is provided.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::RequiredParam
constructor
Creates the Goliath::Rack::Validation::RequiredParam validator.
- #key_valid!(params) ⇒ Object
Constructor Details
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::RequiredParam
Creates the Goliath::Rack::Validation::RequiredParam validator
21 22 23 24 25 |
# File 'lib/goliath/rack/validation/required_param.rb', line 21 def initialize(app, opts = {}) @app = app @key = opts[:key] || 'id' @type = opts[:type] || @key.capitalize end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
12 13 14 |
# File 'lib/goliath/rack/validation/required_param.rb', line 12 def key @key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/goliath/rack/validation/required_param.rb', line 12 def type @type end |
Instance Method Details
#call(env) ⇒ Object
27 28 29 30 |
# File 'lib/goliath/rack/validation/required_param.rb', line 27 def call(env) key_valid!(env['params']) @app.call(env) end |
#key_valid!(params) ⇒ Object
32 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 32 def key_valid!(params) error = false if !params.has_key?(key) || params[key].nil? || (params[key].is_a?(String) && params[key] =~ /^\s*$/) error = true end if params[key].is_a?(Array) unless params[key].compact.empty? params[key].each do |k| return unless k.is_a?(String) return unless k =~ /^\s*$/ end end error = true end raise Goliath::Validation::Error.new(400, "#{@type} identifier missing") if error end |