Class: Goliath::Rack::Validation::Param

Inherits:
Object
  • Object
show all
Includes:
Coerce, Required, Goliath::Rack::Validator
Defined in:
lib/goliath/rack/validation/param.rb

Overview

A middleware to validate that a given parameter is provided and/or coerce a given parameter to a given type.

By default, Goliath supports Integer, Boolean, Float and Symbol. You can also create a custom coerce type by creating a class that has an instance method, coerce. Example:

class CustomJSON

def coerce(value, opts={})
  MultiJson.load(value)
end

end

Where value is the value that should be coerced and opts is a hash that contains two potential values:

  • default is the default value optionally specified in the middleware declaration. This means default will be nil if it was not set.

  • message is the failure message optionally specified in the middleware declaration. This means message will be nil if it was not set.

If default is not set, Integer, Boolean, Float and Symbol will return validation_error, otherwise params will be set to default.

If message is not set, it will have the error message of the exception caught by the coercion, otherwise the error message will be set to message.

For your custom CoerceTypes, you can raise Goliath::Rack::Validation::FailedCoerce.new(value) where value is what will be returned from the call method.

Examples:

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

use Goliath::Rack::Validation::CoerceValue, :key => 'flag', :as => Goliath::Rack::Types::Boolean

(or include Goliath::Rack::Types to reference the types without the namespaces.)

include Goliath::Rack::Types
use Goliath::Rack::Validation::Param, :key => 'amount', :as => Float

Constant Summary collapse

UNKNOWN_OPTIONS =
"Unknown options: %s"

Constants included from Required

Required::NON_WHITESPACE_REGEXP

Constants included from Coerce

Coerce::INVALID_COERCE_TYPE, Coerce::NOT_CLASS_ERROR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Required

#key_valid?, #required_setup!

Methods included from Coerce

#coerce_setup!, #coerce_value

Methods included from Goliath::Rack::Validator

safely, validation_error

Constructor Details

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

Creates the Goliath::Rack::Validation::Param 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’)

  • :as (Class)

    The type to coerce params to. (default: String)

  • :default (String) — default: default: validation_error

Raises:

  • (Exception)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/goliath/rack/validation/param.rb', line 69

def initialize(app, opts = {})
  @app = app
  @optional = opts.delete(:optional) || false
  @key = opts.delete(:key)
  raise Exception.new("key option required") unless @key

  @type = opts.delete(:type) || @key
  @message = opts.delete(:message) || 'identifier missing'
  @default = opts.delete(:default)

  coerce_setup!(opts)
  required_setup!(opts)

  raise Exception.new(UNKNOWN_OPTIONS % opts.inspect) unless opts.empty?
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



54
55
56
# File 'lib/goliath/rack/validation/param.rb', line 54

def default
  @default
end

#keyObject (readonly)

Returns the value of attribute key.



54
55
56
# File 'lib/goliath/rack/validation/param.rb', line 54

def key
  @key
end

#messageObject (readonly)

Returns the value of attribute message.



54
55
56
# File 'lib/goliath/rack/validation/param.rb', line 54

def message
  @message
end

#optionalObject (readonly)

Returns the value of attribute optional.



54
55
56
# File 'lib/goliath/rack/validation/param.rb', line 54

def optional
  @optional
end

#typeObject (readonly)

Returns the value of attribute type.



54
55
56
# File 'lib/goliath/rack/validation/param.rb', line 54

def type
  @type
end

Instance Method Details

#call(env) ⇒ Object



85
86
87
88
89
90
# File 'lib/goliath/rack/validation/param.rb', line 85

def call(env)
  previous_call = super
  return previous_call if previous_call

  @app.call(env)
end

#fetch_key(params, set_value = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/goliath/rack/validation/param.rb', line 92

def fetch_key(params, set_value=nil)
  key_path = Array(@key)
  current_value = params

  # check that the full path is present
  # omit the last part of the path
  val = key_path[0...-1].each do |key_part|
    # if the key is missing or is nil
    if !current_value.is_a?(Hash) || current_value[key_part].nil?
      break
    end

    current_value = current_value[key_part]
  end

  current_value[key_path[-1]] = set_value unless set_value.nil?
  val.nil? ? val : current_value[key_path[-1]]
end