Module: IcingaCertService::Validator

Included in:
Client
Defined in:
lib/validator.rb

Overview

namespace for validate options

Instance Method Summary collapse

Instance Method Details

#validate(params, options) ⇒ Mixed

function to validate function parameters

Examples:

name = validate( params, required: true, var: 'name', type: String )
vars = validate( params, required: false, var: 'vars', type: Hash )

Parameters:

  • params (Hash)
  • options (Hash)

Options Hash (options):

  • requiered (Bool)
  • vat (String)
  • type (Object)

    Ruby Object to check the valid type. e.g String, Ineger Hash, …

Returns:

  • (Mixed)

    the variable from the parmas Hash or nil

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/validator.rb', line 21

def validate( params, options )
  required = options.dig(:required) || false
  var      = options.dig(:var)
  type     = options.dig(:type)

  params   = params.deep_symbolize_keys
  variable = params.dig(var.to_sym)

  clazz = Object.const_get(type.to_s)

  raise ArgumentError.new(format('\'%s\' is requiered and missing!', var)) if(variable.nil?) if(required == true )
  raise ArgumentError.new(format('wrong type. \'%s\' must be an %s, given \'%s\'', var, type, variable.class.to_s)) unless( variable.nil? || variable.is_a?(clazz) )

  variable
end