Class: Goliath::Rack::Validation::Param
- Inherits:
-
Object
- Object
- Goliath::Rack::Validation::Param
- 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.
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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #fetch_key(params, set_value = nil) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::Param
constructor
Creates the Goliath::Rack::Validation::Param validator.
Methods included from Required
Methods included from Coerce
Methods included from Goliath::Rack::Validator
Constructor Details
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::Param
Creates the Goliath::Rack::Validation::Param validator
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
#default ⇒ Object (readonly)
Returns the value of attribute default.
54 55 56 |
# File 'lib/goliath/rack/validation/param.rb', line 54 def default @default end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
54 55 56 |
# File 'lib/goliath/rack/validation/param.rb', line 54 def key @key end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
54 55 56 |
# File 'lib/goliath/rack/validation/param.rb', line 54 def @message end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
54 55 56 |
# File 'lib/goliath/rack/validation/param.rb', line 54 def optional @optional end |
#type ⇒ Object (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 |