Module: ParamsVerification

Defined in:
lib/params_verification.rb

Overview

ParamsVerification module. Written to verify a service params without creating new objects. This module is used on all requests requiring validation and therefore performance security and maintainability are critical.

Defined Under Namespace

Classes: InvalidParamType, InvalidParamValue, MissingParam, NoParamsDefined, ParamError, UnexpectedParam

Class Method Summary collapse

Class Method Details

.type_validationsHash

An array of validation regular expressions. The array gets cached but can be accessed via the symbol key.

Returns:

  • (Hash)

    An array with all the validation types as keys and regexps as values.



21
22
23
24
25
26
27
28
29
# File 'lib/params_verification.rb', line 21

def self.type_validations
  @type_validations ||= { :integer  => /^-?\d+$/,
                          :float    => /^-?(\d*\.\d+|\d+)$/,
                          :decimal  => /^-?(\d*\.\d+|\d+)$/,
                          :datetime => /^[-\d:T\s]+$/,  # "T" is for ISO date format
                          :boolean  => /^(1|true|TRUE|T|Y|0|false|FALSE|F|N)$/,
                          #:array    => /,/
                        }
end

.validate!(params, service_params, ignore_unexpected = false) ⇒ Hash

Validation against each required WSDSL::Params::Rule and returns the potentially modified params (with default values)

Examples:

Validate request params against a service’s defined param rules

ParamsVerification.validate!(request.params, @service.defined_params)

Parameters:

  • params (Hash)

    The params to verify (incoming request params)

  • service_params (WSDSL::Params)

    A Playco service param compatible object listing required and optional params

  • ignore_unexpected (Boolean) (defaults to: false)

    Flag letting the validation know if unexpected params should be ignored

Returns:

  • (Hash)

    The passed params potentially modified by the default rules defined in the service.



45
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
85
86
87
88
89
90
91
# File 'lib/params_verification.rb', line 45

def self.validate!(params, service_params, ignore_unexpected=false)

  # Verify that no garbage params are passed, if they are, an exception is raised.
  # only the first level is checked at this point
  unless ignore_unexpected
    unexpected_params?(params, service_params.param_names)
  end

  # Create a duplicate of the params hash that uses symbols as keys,
  # while preserving the original hash
  updated_params = symbolify_keys(params)

  # Required param verification
  service_params.list_required.each do |rule|
    updated_params = validate_required_rule(rule, updated_params)
  end

  # Set optional defaults if any optional
  service_params.list_optional.each do |rule|
    updated_params = run_optional_rule(rule, updated_params)
  end

  # check the namespaced params
  service_params.namespaced_params.each do |param|
    param.list_required.each do |rule|
      updated_params = validate_required_rule(rule, updated_params, param.space_name.to_s)
    end
    param.list_optional.each do |rule|
      updated_params = run_optional_rule(rule, updated_params, param.space_name.to_s)
    end

  end

  # verify nested params, only 1 level deep tho
  params.each_pair do |key, value|
    # We are now assuming a file param is a hash due to Rack::Mulitpart.parse_multipart
    # turns this data into a hash, but param verification/DSL dont expect this or define this behavior and it shouldn't.
    # so special case it if its a file type and the value is a hash.
    if value.is_a?(Hash) && type_for_param(service_params, key) != :file
      namespaced = service_params.namespaced_params.find{|np| np.space_name.to_s == key.to_s}
      raise UnexpectedParam, "Request included unexpected parameter: #{key}" if namespaced.nil?
      unexpected_params?(params[key], namespaced.param_names)
    end
  end

  updated_params
end