Skip to content

Commit

Permalink
Skip validation of a nested object values missing in config
Browse files Browse the repository at this point in the history
  • Loading branch information
gzigzigzeo committed Feb 9, 2022
1 parent bf0240f commit d687ac5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tfsdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ func (c Config) getAttributeValue(ctx context.Context, path *tftypes.AttributePa
// If found, convert this value to an unknown value.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/186

if attrTypeWithValidate, ok := attrType.(attr.TypeWithValidate); ok {
diags.Append(attrTypeWithValidate.Validate(ctx, tfValue, path)...)
if err == nil {
if attrTypeWithValidate, ok := attrType.(attr.TypeWithValidate); ok {
diags.Append(attrTypeWithValidate.Validate(ctx, tfValue, path)...)

if diags.HasError() {
return nil, diags
if diags.HasError() {
return nil, diags
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions tfsdk/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ func TestConfigGetAttribute(t *testing.T) {
expected: &testtypes.String{String: types.String{Value: "namevalue"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("name"))},
},
"Computed-Computed-object": {
config: Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
}, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "namevalue"),
}),
Schema: Schema{
Attributes: map[string]Attribute{
"name": {
Type: testtypes.StringTypeWithValidateWarning{},
Required: true,
},
},
},
},
target: new(testtypes.String),
expected: &testtypes.String{String: types.String{Value: "namevalue"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("name"))},
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -1585,6 +1607,35 @@ func TestConfigGetAttributeValue(t *testing.T) {
expected: testtypes.String{String: types.String{Value: "value"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("test"))},
},
"AttrTypeInt64WithValidateError-nested-missing-in-config": {
config: Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"parent": tftypes.Object{},
},
}, map[string]tftypes.Value{
"parent": tftypes.NewValue(tftypes.Object{}, nil),
}),
Schema: Schema{
Attributes: map[string]Attribute{
"parent": {
Attributes: SingleNestedAttributes(map[string]Attribute{
"test": {
Type: types.Int64Type,
Optional: true,
Computed: true,
},
}),
Computed: true,
Optional: true,
},
},
},
},
path: tftypes.NewAttributePath().WithAttributeName("parent").WithAttributeName("test"),
expected: types.Int64{Null: true},
expectedDiags: nil,
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit d687ac5

Please sign in to comment.