Class: Goliath::Rack::Validation::RequiredParam

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::Validator
Defined in:
lib/goliath/rack/validation/required_param.rb

Overview

A middleware to validate that a given parameter is provided.

Examples:

use Goliath::Rack::Validation::RequiredParam, {:key => 'mode', :type => 'Mode'}
use Goliath::Rack::Validation::RequiredParam, {:key => 'data.credentials.login', :type => 'Login'}
use Goliath::Rack::Validation::RequiredParam, {:key => %w(data credentials password), :type => 'Password'}

Constant Summary collapse

NON_WHITESPACE_REGEXP =

extracted from activesupport 3.0.9

%r![^[:space:]]!

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Goliath::Rack::Validator

safely, validation_error

Constructor Details

#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::RequiredParam

Creates the Goliath::Rack::Validation::RequiredParam validator

Parameters:

  • app

    The app object

  • opts (Hash) (defaults to: {})

    The validator options

Options Hash (opts):

  • :key (String)

    The key to look for in params (default: id) if the value is an array it defines path with nested keys (ex: [“data”, “login”] or dot syntax: ‘data.login’)

  • :type (String)

    The type string to put in the error message. (default: :key)

  • :message (String)

    The message string to display after the type string. (default: ‘identifier missing’)



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

#keyObject (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/goliath/rack/validation/required_param.rb', line 15

def key
  @key
end

#messageObject (readonly)

Returns the value of attribute message.



15
16
17
# File 'lib/goliath/rack/validation/required_param.rb', line 15

def message
  @message
end

#typeObject (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

Returns:

  • (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