Module: Bosh::Director::ValidationHelper

Instance Method Summary collapse

Instance Method Details

#invalid_type(property, klass, value) ⇒ Object



46
47
48
49
# File 'lib/bosh/director/validation_helper.rb', line 46

def invalid_type(property, klass, value)
  raise ValidationInvalidType,
    "Property `#{property}' (value #{value.inspect}) did not match the required type `#{klass}'"
end

#safe_property(hash, property, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bosh/director/validation_helper.rb', line 3

def safe_property(hash, property, options = {})
  result = nil

  if hash && hash.has_key?(property)
    result = hash[property]

    if options[:class]

      if options[:class] == :boolean
        unless result.kind_of?(TrueClass) || result.kind_of?(FalseClass)
          invalid_type(property, options[:class], result)
        end

      elsif !result.kind_of?(options[:class])
        if options[:class] == String && result.kind_of?(Numeric)
          result = result.to_s
        else
          invalid_type(property, options[:class], result)
        end
      end

    end

    if options[:min] && result < options[:min]
      raise ValidationViolatedMin,
            "`#{property}' value (#{result}) should be greater than #{options[:min]}"
    end

    if options[:max] && result > options[:max]
      raise ValidationViolatedMax,
            "`#{property}' value (#{result}) should be less than #{options[:max]}"
    end

  elsif options[:default]
    result = options[:default]

  elsif !options[:optional]
    raise ValidationMissingField,
          "Required property `#{property}' was not specified in #{self.class.name}"
  end
  result
end