diff --git a/helper/schema/schema.go b/helper/schema/schema.go index a6e3a3f22..3e16bb3ab 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -2,6 +2,7 @@ package schema import ( "fmt" + "strings" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/mapstructure" @@ -104,20 +105,7 @@ func (m schemaMap) Diff( // Validate validates the configuration against this schema mapping. func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) { - var ws []string - var es []error - - for k, schema := range m { - ws2, es2 := m.validate(k, schema, c) - if len(ws2) > 0 { - ws = append(ws, ws2...) - } - if len(es2) > 0 { - es = append(es, es2...) - } - } - - return ws, es + return m.validateObject("", m, c) } func (m schemaMap) diff( @@ -345,9 +333,12 @@ func (m schemaMap) validateObject( var ws []string var es []error for subK, s := range schema { - key := fmt.Sprintf("%s.%s", k, subK) - ws2, es2 := m.validate(key, s, c) + key := subK + if k != "" { + key = fmt.Sprintf("%s.%s", k, subK) + } + ws2, es2 := m.validate(key, s, c) if len(ws2) > 0 { ws = append(ws, ws2...) } @@ -356,6 +347,23 @@ func (m schemaMap) validateObject( } } + // Detect any extra/unknown keys and report those as errors. + prefix := k + "." + for configK, _ := range c.Raw { + if k != "" { + if !strings.HasPrefix(configK, prefix) { + continue + } + + configK = configK[len(prefix):] + } + + if _, ok := schema[configK]; !ok { + es = append(es, fmt.Errorf( + "%s: invalid or unknown key: %s", k, configK)) + } + } + return ws, es } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 10f4f3d7e..66d70bb52 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -604,6 +604,24 @@ func TestSchemaMap_Validate(t *testing.T) { Err: false, }, + + // Invalid/unknown field + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + Config: map[string]interface{}{ + "foo": "bar", + }, + + Err: true, + }, } for i, tc := range cases {