helper/schema: validate objects are objects [GH-2166]

This commit is contained in:
Mitchell Hashimoto 2015-06-23 16:39:02 -07:00
parent 642bdd585f
commit 4f391902a0
2 changed files with 30 additions and 1 deletions

View File

@ -1096,6 +1096,13 @@ func (m schemaMap) validateObject(
k string,
schema map[string]*Schema,
c *terraform.ResourceConfig) ([]string, []error) {
raw, _ := c.GetRaw(k)
if _, ok := raw.(map[string]interface{}); !ok {
return nil, []error{fmt.Errorf(
"%s: expected object, got %s",
k, reflect.ValueOf(raw).Kind())}
}
var ws []string
var es []error
for subK, s := range schema {
@ -1114,7 +1121,6 @@ func (m schemaMap) validateObject(
}
// Detect any extra/unknown keys and report those as errors.
raw, _ := c.GetRaw(k)
if m, ok := raw.(map[string]interface{}); ok {
for subk, _ := range m {
if _, ok := schema[subk]; !ok {

View File

@ -2981,6 +2981,29 @@ func TestSchemaMap_Validate(t *testing.T) {
Err: false,
},
"Sub-resource is the wrong type": {
Schema: map[string]*Schema{
"ingress": &Schema{
Type: TypeList,
Required: true,
Elem: &Resource{
Schema: map[string]*Schema{
"from": &Schema{
Type: TypeInt,
Required: true,
},
},
},
},
},
Config: map[string]interface{}{
"ingress": []interface{}{"foo"},
},
Err: true,
},
"Not a list": {
Schema: map[string]*Schema{
"ingress": &Schema{