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
45
46
47
|
# File 'lib/bosh/director/validation_helper.rb', line 3
def safe_property(hash, property, options = {})
result = nil
if hash && !hash.kind_of?(Hash)
raise Bosh::Director::ValidationInvalidType,
"Object (#{hash.inspect}) did not match the required type `Hash'"
elsif 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.inspect}) should be greater than #{options[:min].inspect}"
end
if options[:max] && result > options[:max]
raise ValidationViolatedMax,
"`#{property}' value (#{result.inspect}) should be less than #{options[:max].inspect}"
end
elsif options[:default]
result = options[:default]
elsif !options[:optional]
raise ValidationMissingField,
"Required property `#{property}' was not specified in object (#{hash.inspect})"
end
result
end
|