Skip to content

Commit

Permalink
tfsdk: Validate Attribute sets at least one of Required, Optional, or…
Browse files Browse the repository at this point in the history
… Computed (#111)

Reference: #94

This can help schema consumers outside Terraform CLI ensure a valid schema is being defined.
  • Loading branch information
bflad authored Sep 1, 2021
1 parent 0c94d3e commit 1778b40
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/111.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
tfsdk: Validate `Attribute` defines at least one of `Required`, `Optional`, or `Computed`
```
15 changes: 15 additions & 0 deletions tfsdk/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (a Attribute) tfprotov6SchemaAttribute(ctx context.Context, name string, pa
Sensitive: a.Sensitive,
}

if !a.Required && !a.Optional && !a.Computed {
return nil, path.NewErrorf("must have Required, Optional, or Computed set")
}

if a.DeprecationMessage != "" {
schemaAttribute.Deprecated = true
}
Expand Down Expand Up @@ -245,6 +249,17 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r
return
}

if !a.Required && !a.Optional && !a.Computed {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Invalid Attribute Definition",
Detail: "Attribute missing Required, Optional, or Computed definition. This is always a problem with the provider and should be reported to the provider developer.",
Attribute: req.AttributePath,
})

return
}

attributeConfig, diags := req.Config.GetAttribute(ctx, req.AttributePath)

resp.Diagnostics = append(resp.Diagnostics, diags...)
Expand Down
39 changes: 39 additions & 0 deletions tfsdk/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ func TestAttributeTfprotov6SchemaAttribute(t *testing.T) {
path: tftypes.NewAttributePath(),
expectedErr: "must have Attributes or Type set",
},
"missing-required-optional-and-computed": {
name: "whoops",
attr: Attribute{
Type: types.StringType,
},
path: tftypes.NewAttributePath(),
expectedErr: "must have Required, Optional, or Computed set",
},
}

for name, tc := range tests {
Expand Down Expand Up @@ -749,6 +757,37 @@ func TestAttributeValidate(t *testing.T) {
},
},
},
"missing-required-optional-and-computed": {
req: ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
Config: Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
}, map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "testvalue"),
}),
Schema: Schema{
Attributes: map[string]Attribute{
"test": {
Type: types.StringType,
},
},
},
},
},
resp: ValidateAttributeResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Invalid Attribute Definition",
Detail: "Attribute missing Required, Optional, or Computed definition. This is always a problem with the provider and should be reported to the provider developer.",
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
},
},
},
},
"config-error": {
req: ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
Expand Down

0 comments on commit 1778b40

Please sign in to comment.