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.
Constant Summary collapse
- NON_WHITESPACE_REGEXP =
extracted from activesupport 3.0.9
%r![^[:space:]]!
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#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) ⇒ Boolean
Methods included from Goliath::Rack::Validator
Constructor Details
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::RequiredParam
Creates the Goliath::Rack::Validation::RequiredParam validator
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/goliath/rack/validation/required_param.rb', line 30 def initialize(app, opts = {}) @app = app @key = opts[:key] || 'id' @type = opts[:type] || @key @message = opts[:message] || 'identifier missing' if @key.is_a?(String) && @key.include?('.') @key = @key.split('.') end end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/goliath/rack/validation/required_param.rb', line 15 def key @key end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
15 16 17 |
# File 'lib/goliath/rack/validation/required_param.rb', line 15 def @message end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/goliath/rack/validation/required_param.rb', line 15 def type @type end |
Instance Method Details
#call(env) ⇒ Object
41 42 43 44 |
# File 'lib/goliath/rack/validation/required_param.rb', line 41 def call(env) return validation_error(400, "#{@type} #{@message}") unless key_valid?(env['params']) @app.call(env) end |
#key_valid?(params) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/goliath/rack/validation/required_param.rb', line 46 def key_valid?(params) key_path = Array(@key) current_value = params # check that the full path is present # omit the last part of the path key_path[0...-1].each do |key_part| # if the key is missing or is nil the validation failed if !current_value.is_a?(Hash) || current_value[key_part].nil? return false end current_value = current_value[key_part] end # if we are here the full path is available, now test the real key val = current_value[key_path[-1]] case val when nil return false when String # if val is a string it must not be empty return false if val !~ NON_WHITESPACE_REGEXP when Array unless val.compact.empty? val.each do |k| return true unless k.is_a?(String) return true unless k !~ NON_WHITESPACE_REGEXP end end return false end true end |