Skip to content

Validation and Modifiers Issues #836

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

Merged
merged 6 commits into from
Jun 17, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20250605-102519.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: 'Fixed validation and modifiers issues: improved error handling in validators and plan modifiers, corrected error messages for SHA256 checksums, and enhanced diagnostic context'
time: 2025-06-05T10:25:19.227038423Z
custom:
Issue: "821"
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (d *setBoolValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ctx

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

return value != attributeChecksum.ValueString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ func (d *setStringValueToUnknownIfChecksumsChangeModifier) hasChecksumChanged(ct
var attribute types.String
diags := req.Plan.GetAttribute(ctx, path.Root(attributeName), &attribute)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return false
}

var attributeChecksum types.String
diags = req.State.GetAttribute(ctx, path.Root(checksumAttributeName), &attributeChecksum)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return false
}

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

return value != attributeChecksum.ValueString()
Expand Down
3 changes: 2 additions & 1 deletion internal/modifiers/sync_attribute_plan_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func (d *syncAttributePlanModifier) PlanModifyString(ctx context.Context, req pl
} else {
value, err := helpers.CalculateSHA256(settingsFile.ValueString())
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating MD5 checksum for %s", d.syncAttribute), err.Error())
resp.Diagnostics.AddError(fmt.Sprintf("Error calculating SHA256 checksum for %s", d.syncAttribute), err.Error())
return
}

if value == "" {
resp.Diagnostics.AddError(fmt.Sprintf("Checksum is empty for %s", d.syncAttribute), "Calculated SHA256 checksum resulted in an empty value, which is unexpected.")
resp.PlanValue = types.StringUnknown()
} else {
resp.PlanValue = types.StringValue(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func (v DynamicsColumnsValidator) Validate(ctx context.Context, config tfsdk.Con
}

var dynamicColumns types.Dynamic
_ = config.GetAttribute(ctx, matchedPaths[0], &dynamicColumns)
diags.Append(config.GetAttribute(ctx, matchedPaths[0], &dynamicColumns)...)
if diags.HasError() {
return diags
}

terraformDynamicColumns, err := dynamicColumns.UnderlyingValue().ToTerraformValue(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (av MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidator) ValidateStrin
func (av MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidator) Validate(ctx context.Context, req MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidatorRequest, res *MakeFieldRequiredWhenOtherFieldDoesNotHaveValueValidatorResponse) {
paths, _ := req.Config.PathMatches(ctx, av.OtherFieldExpression)
if paths == nil || len(paths) != 1 {
res.Diagnostics.AddError("Other field required when value of validator should have exactly one match", "")
res.Diagnostics.AddError(
"Validator Configuration Error: Other field match failed",
"The validator could not uniquely locate the other field in the configuration. Ensure that 'OtherFieldExpression' matches exactly one attribute.",
)
return
}
otherFieldValue := ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (av OtherFieldRequiredWhenValueOfValidator) Validate(ctx context.Context, r

if (av.OtherFieldValueRegex != nil && otherFieldValue != nil && !av.OtherFieldValueRegex.MatchString(*otherFieldValue)) ||
(av.OtherFieldValueRegex == nil && (otherFieldValue == nil || *otherFieldValue == "") && !isUnknown) {
res.Diagnostics.AddError(av.ErrorMessage, av.ErrorMessage)
res.Diagnostics.AddError(
av.ErrorMessage,
"Field \""+paths[0].String()+"\" does not meet required value conditions.",
)
}
}
}