Module: Rack::Params

Defined in:
lib/rack/params.rb,
lib/rack/params/errors.rb,
lib/rack/params/result.rb,
lib/rack/params/context.rb,
lib/rack/params/version.rb

Overview

Rack::Params provides a lightweight DSL for type coercion and validation of request parameters.

Defined Under Namespace

Modules: ClassMethods, Connector, Result Classes: ArrayContext, Context, HashContext, ParameterValidationError

Constant Summary

VERSION =
"0.0.1.pre5"

Instance Method Summary collapse

Instance Method Details

#validate(name, params, options = {}) ⇒ Result #validate(params, options = {}) { ... } ⇒ Result

validate the given parameters

Overloads:

  • #validate(name, params, options = {}) ⇒ Result

    validates the given parameters against the named validator

    Parameters:

    • name (Symbol)

      the name of a registered validator.

    • params (Hash)

      the parameter hash to validate

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

    Returns:

    • (Result)

      a result hash containing the extracted keys, and errors.

  • #validate(params, options = {}) { ... } ⇒ Result

    validates the given parameters against the given block

    Parameters:

    • params (Hash)

      the parameter hash to validate

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

    Yields:

    • a code block that will run in the context of a HashContext to validate the params

    Returns:

    • (Result)

      a result hash containing the extracted keys, and errors.

    See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rack/params.rb', line 53

def validate(name = nil, params = nil, options = {}, &block)
  if params.nil? && (name.class <= Hash)
    params = name
    name   = nil
  end

  fail ArgumentError, "no parameters provided!" if params.nil?
  if name.nil?
    fail ArgumentError, "no validation block was provided!" unless block_given?
    HashContext.new(params, options).exec(&block)
  else
    fail ArgumentError, "no validation is registered under #{name}" unless self.class.validators.key? name
    self.class.validators[name].exec(params)
  end
end

#validate!(name, params, options = {}) ⇒ Result #validate!(params, options = {}) { ... } ⇒ Result

validate the given parameters

Overloads:

  • #validate!(name, params, options = {}) ⇒ Result

    Returns a valid result hash containing the extracted keys, and no errors.

    Parameters:

    • name (Symbol)

      the name of a registered validator.

    • params (Hash)

      the parameter hash to validate

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

    Returns:

    • (Result)

      a valid result hash containing the extracted keys, and no errors.

    Raises:

  • #validate!(params, options = {}) { ... } ⇒ Result

    validates the given parameters against the given block

    Parameters:

    • params (Hash)

      the parameter hash to validate

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

    Yields:

    • a code block that will run in the context of a HashContext to validate the params

    Returns:

    • (Result)

      a valid result hash containing the extracted keys, and no errors.

    Raises:



83
84
85
86
87
# File 'lib/rack/params.rb', line 83

def validate!(name = nil, params = nil, options = {}, &block)
  validate(name, params, options, &block).tap do |res|
    fail ParameterValidationError, res.errors if res.invalid?
  end
end