Module: EasyType::Validators::PortNumber

Defined in:
lib/easy_type/validators.rb

Overview

This validator validates if it is valid port number.

Examples:


newparam(:name) do
  include ::EasyType::Validators::PortNumber

Raises:

  • (Puppet::Error)

    when the name is invalid

Class Method Summary collapse

Class Method Details

.unsafe_validate(value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/easy_type/validators.rb', line 95

def unsafe_validate(value)
  return if value == 'absent'
  klass = value.class.to_s
  actual_value = case klass
                 when 'Integer', 'Fixnum', 'Bignum'
                   value.to_i
                 when 'String'
                   begin
                     Integer(value)
                   rescue ArgumentError
                     fail Puppet::Error, "#{value} is not valid as a port number"
                   end
                 else
                   fail Puppet::Error, "#{value} is not valid as a port number"
                 end
  fail Puppet::Error, "#{value} is not valid as a port number" if actual_value < 0 || actual_value > 65_535
end