Module: Bosh::Director::ValidationHelper

Instance Method Summary collapse

Instance Method Details

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bosh/director/validation_helper.rb', line 3

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

  if options.has_key?(:default) && !options[:default].nil?
    validate_property(property, options, options[:default], DefaultPropertyValidationMessage.new)
  end

  if !hash.nil? && !hash.kind_of?(Hash)
    raise Bosh::Director::ValidationInvalidType, "Object (#{hash.inspect}) did not match the required type 'Hash'"
  elsif !hash.nil? && hash.has_key?(property)
    return validate_property(property, options, hash[property], PropertyValidationMessage.new)
  elsif options.has_key?(:default)
    return options[:default]
  elsif !options[:optional]
    raise ValidationMissingField, "Required property '#{property}' was not specified in object (#{hash.inspect})"
  end
end