Module: MCollective::Validator

Defined in:
lib/mcollective/validator.rb,
lib/mcollective/validator/array_validator.rb,
lib/mcollective/validator/regex_validator.rb,
lib/mcollective/validator/length_validator.rb,
lib/mcollective/validator/shellsafe_validator.rb,
lib/mcollective/validator/typecheck_validator.rb,
lib/mcollective/validator/ipv4address_validator.rb,
lib/mcollective/validator/ipv6address_validator.rb,
lib/mcollective/validator/bolt_task_name_validator.rb

Defined Under Namespace

Classes: ArrayValidator, Bolt_task_nameValidator, Ipv4addressValidator, Ipv6addressValidator, LengthValidator, RegexValidator, ShellsafeValidator, TypecheckValidator

Constant Summary collapse

@@validator_mutex =

rubocop:disable Style/ClassVars

Mutex.new

Class Method Summary collapse

Class Method Details

.[](klass) ⇒ Object

Returns and instance of the Plugin class from which objects can be created. Valid plugin names are

:valplugin
"valplugin"
"ValpluginValidator"


22
23
24
25
26
27
28
29
30
# File 'lib/mcollective/validator.rb', line 22

def self.[](klass)
  if klass.is_a?(Symbol)
    klass = validator_class(klass)
  elsif !klass.match(/.*Validator$/)
    klass = validator_class(klass)
  end

  const_get(klass)
end

.has_validator?(validator) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mcollective/validator.rb', line 45

def self.has_validator?(validator)
  const_defined?(validator_class(validator))
end

.load_validatorsObject

Loads the validator plugins. Validators will only be loaded every 5 minutes



7
8
9
10
11
12
13
14
15
# File 'lib/mcollective/validator.rb', line 7

def self.load_validators
  @@validator_mutex.lock
  if load_validators?
    @last_load = Time.now.to_i
    PluginManager.find_and_load("validator")
  end
ensure
  @@validator_mutex.unlock
end

.load_validators?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/mcollective/validator.rb', line 53

def self.load_validators?
  return true if @last_load.nil?

  (@last_load - Time.now.to_i) > 300
end

.method_missing(method, *args, &block) ⇒ Object

Allows validation plugins to be called like module methods : Validator.validate()



33
34
35
36
37
38
39
# File 'lib/mcollective/validator.rb', line 33

def self.method_missing(method, *args, &block)
  if has_validator?(method)
    Validator[method].validate(*args)
  else
    raise ValidatorError, "Unknown validator: '#{method}'."
  end
end

.respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mcollective/validator.rb', line 41

def self.respond_to_missing?(method, *)
  has_validator?(method)
end

.validate(validator, validation) ⇒ Object

Generic validate method that will call the correct validator plugin based on the type of the validation parameter



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
# File 'lib/mcollective/validator.rb', line 61

def self.validate(validator, validation)
  Validator.load_validators

  begin
    if [:integer, :boolean, :float, :number, :string, :array, :hash].include?(validation)
      Validator.typecheck(validator, validation)

    else
      case validation
      when Regexp, String
        Validator.regex(validator, validation)

      when Symbol
        Validator.send(validation, validator)

      when Array
        Validator.array(validator, validation)

      when Class
        Validator.typecheck(validator, validation)
      end
    end
  rescue => e
    raise ValidatorError, e.to_s, e.backtrace
  end
end

.validator_class(validator) ⇒ Object



49
50
51
# File 'lib/mcollective/validator.rb', line 49

def self.validator_class(validator)
  "#{validator.to_s.capitalize}Validator"
end