diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 3e16bb3ab..7eb50203d 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -279,12 +279,18 @@ func (m schemaMap) validate( if !ok { if schema.Required { return nil, []error{fmt.Errorf( - "%s: required field is not set")} + "%s: required field is not set", k)} } return nil, nil } + if !schema.Required && !schema.Optional { + // This is a computed-only field + return nil, []error{fmt.Errorf( + "%s: this field cannot be set", k)} + } + return m.validatePrimitive(k, raw, schema, c) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 66d70bb52..2a8c4b482 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -582,7 +582,8 @@ func TestSchemaMap_Validate(t *testing.T) { { Schema: map[string]*Schema{ "ingress": &Schema{ - Type: TypeList, + Type: TypeList, + Optional: true, Elem: &Resource{ Schema: map[string]*Schema{ "from": &Schema{ @@ -622,6 +623,22 @@ func TestSchemaMap_Validate(t *testing.T) { Err: true, }, + + // Computed field set + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Computed: true, + }, + }, + + Config: map[string]interface{}{ + "availability_zone": "bar", + }, + + Err: true, + }, } for i, tc := range cases {