Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down