Module: Icinga2::Validator

Included in:
Client
Defined in:
lib/icinga2/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:

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)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/icinga2/validator.rb', line 48

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? && 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