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

Skip validation of a values missing in config #259

Closed
wants to merge 4 commits into from
Closed
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
1 change: 0 additions & 1 deletion tfsdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (c Config) getAttributeValue(ctx context.Context, path *tftypes.AttributePa
// TODO: If ErrInvalidStep, check parent paths for unknown value.
// 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)...)

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"))},
},
Comment on lines +338 to +359
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks identical to the one above, so will remove on merge.

}

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": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will ensure there is a covering test in plan as well.

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
10 changes: 6 additions & 4 deletions tfsdk/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ func (s State) getAttributeValue(ctx context.Context, path *tftypes.AttributePat
// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to Config, this logic should not be changed, will fix on merge.

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
29 changes: 29 additions & 0 deletions tfsdk/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,35 @@ func TestStateGetAttributeValue(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": {
state: State{
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
8 changes: 4 additions & 4 deletions types/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
func float64Validate(_ context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics {
var diags diag.Diagnostics

if !in.IsKnown() || in.IsNull() {
return diags
}

if !in.Type().Equal(tftypes.Number) {
diags.AddAttributeError(
path,
Expand All @@ -23,10 +27,6 @@ func float64Validate(_ context.Context, in tftypes.Value, path *tftypes.Attribut
return diags
}

if !in.IsKnown() || in.IsNull() {
return diags
}

var value *big.Float
err := in.As(&value)

Expand Down
8 changes: 4 additions & 4 deletions types/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
func int64Validate(_ context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics {
var diags diag.Diagnostics

if !in.IsKnown() || in.IsNull() {
return diags
}

if !in.Type().Equal(tftypes.Number) {
diags.AddAttributeError(
path,
Expand All @@ -23,10 +27,6 @@ func int64Validate(_ context.Context, in tftypes.Value, path *tftypes.AttributeP
return diags
}

if !in.IsKnown() || in.IsNull() {
return diags
}

var value *big.Float
err := in.As(&value)

Expand Down