Skip to content

Commit 08385ce

Browse files
CopilotmawasileCopilotmattdot
authored
Validation and Modifiers Issues (#836)
* Initial plan for issue * Fix validation and modifiers issues Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com> * Fix linting issue, generate docs, and add changie entry Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com> * Update internal/modifiers/set_bool_value_unknown_if_checksum_change_modifier.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Matt Dotson <mattdot@users.noreply.github.com>
1 parent 2951829 commit 08385ce

7 files changed

+27
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: fixed
2+
body: 'Fixed validation and modifiers issues: improved error handling in validators and plan modifiers, corrected error messages for SHA256 checksums, and enhanced diagnostic context'
3+
time: 2025-06-05T10:25:19.227038423Z
4+
custom:
5+
Issue: "821"

internal/modifiers/set_bool_value_unknown_if_checksum_change_modifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (d *setBoolValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ctx
6161

6262
value, err := helpers.CalculateSHA256(attribute.ValueString())
6363
if err != nil {
64-
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", attribute), err.Error())
64+
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating SHA256 checksum for %q", attribute), err.Error())
6565
}
6666

6767
return value != attributeChecksum.ValueString()

internal/modifiers/set_string_value_unknown_if_checksum_change_modifier.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ func (d *setStringValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ct
5454
var attribute types.String
5555
diags := req.Plan.GetAttribute(ctx, path.Root(attributeName), &attribute)
5656
resp.Diagnostics.Append(diags...)
57+
if diags.HasError() {
58+
return false
59+
}
5760

5861
var attributeChecksum types.String
5962
diags = req.State.GetAttribute(ctx, path.Root(checksumAttributeName), &attributeChecksum)
6063
resp.Diagnostics.Append(diags...)
64+
if diags.HasError() {
65+
return false
66+
}
6167

6268
value, err := helpers.CalculateSHA256(attribute.ValueString())
6369
if err != nil {
64-
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", attribute), err.Error())
70+
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating SHA256 checksum for attribute %q", attributeName), err.Error())
6571
}
6672

6773
return value != attributeChecksum.ValueString()

internal/modifiers/sync_attribute_plan_modifier.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ func (d *syncAttributePlanModifier) PlanModifyString(ctx context.Context, req pl
4747
} else {
4848
value, err := helpers.CalculateSHA256(settingsFile.ValueString())
4949
if err != nil {
50-
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", d.syncAttribute), err.Error())
50+
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating SHA256 checksum for %s", d.syncAttribute), err.Error())
5151
return
5252
}
5353

5454
if value == "" {
55+
resp.Diagnostics.AddError(fmt.Sprintf("Checksum is empty for %s", d.syncAttribute), "Calculated SHA256 checksum resulted in an empty value, which is unexpected.")
5556
resp.PlanValue = types.StringUnknown()
5657
} else {
5758
resp.PlanValue = types.StringValue(value)

internal/services/data_record/dynamic_columns_validator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ func (v DynamicsColumnsValidator) Validate(ctx context.Context, config tfsdk.Con
6060
}
6161

6262
var dynamicColumns types.Dynamic
63-
_ = config.GetAttribute(ctx, matchedPaths[0], &dynamicColumns)
63+
diags.Append(config.GetAttribute(ctx, matchedPaths[0], &dynamicColumns)...)
64+
if diags.HasError() {
65+
return diags
66+
}
6467

6568
terraformDynamicColumns, err := dynamicColumns.UnderlyingValue().ToTerraformValue(ctx)
6669
if err != nil {

internal/validators/make_field_required_when_other_field_does_not_have_value_validator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ func (av MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidator) ValidateStrin
6262
func (av MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidator) Validate(ctx context.Context, req MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidatorRequest, res *MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidatorResponse) {
6363
paths, _ := req.Config.PathMatches(ctx, av.OtherFieldExpression)
6464
if paths == nil || len(paths) != 1 {
65-
res.Diagnostics.AddError("Other field required when value of validator should have exactly one match", "")
65+
res.Diagnostics.AddError(
66+
"Validator Configuration Error: Other field match failed",
67+
"The validator could not uniquely locate the other field in the configuration. Ensure that 'OtherFieldExpression' matches exactly one attribute.",
68+
)
6669
return
6770
}
6871
otherFieldValue := ""

internal/validators/other_field_required_when_value_of_validator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func (av OtherFieldRequiredWhenValueOfValidator) Validate(ctx context.Context, r
8484

8585
if (av.OtherFieldValueRegex != nil && otherFieldValue != nil && !av.OtherFieldValueRegex.MatchString(*otherFieldValue)) ||
8686
(av.OtherFieldValueRegex == nil && (otherFieldValue == nil || *otherFieldValue == "") && !isUnknown) {
87-
res.Diagnostics.AddError(av.ErrorMessage, av.ErrorMessage)
87+
res.Diagnostics.AddError(
88+
av.ErrorMessage,
89+
"Field \""+paths[0].String()+"\" does not meet required value conditions.",
90+
)
8891
}
8992
}
9093
}

0 commit comments

Comments
 (0)