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

Use more careful format string/quoting in validator messages #944

Merged
merged 2 commits into from
Oct 21, 2024
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
7 changes: 3 additions & 4 deletions apstra/apstra_validator/forbidden_when_value_is.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apstravalidator
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand All @@ -11,9 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

var (
_ NineTypesValidator = ForbiddenWhenValueIsValidator{}
)
var _ NineTypesValidator = ForbiddenWhenValueIsValidator{}

type ForbiddenWhenValueIsValidator struct {
Expression path.Expression
Expand Down Expand Up @@ -82,7 +81,7 @@ func (o ForbiddenWhenValueIsValidator) Validate(ctx context.Context, req Forbidd
if o.Value.Equal(mpVal) {
resp.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("attribute %q must not be set when %q has value %s, got: %q", req.Path, mp, mpVal, req.ConfigValue),
fmt.Sprintf("attribute %q must not be set when %q has value %s, got: %s", req.Path, mp, mpVal, req.ConfigValue),
))
}
}
Expand Down
9 changes: 4 additions & 5 deletions apstra/apstra_validator/required_when_value_is.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package apstravalidator
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

var (
_ NineTypesValidator = RequiredWhenValueIsValidator{}
)
var _ NineTypesValidator = RequiredWhenValueIsValidator{}

type RequiredWhenValueIsValidator struct {
expression path.Expression
Expand All @@ -31,7 +30,7 @@ type RequiredWhenValueIsResponse struct {
}

func (o RequiredWhenValueIsValidator) Description(_ context.Context) string {
return fmt.Sprintf("Ensures that a value is supplied when attribute at %q has value %q", o.expression.String(), o.value)
return fmt.Sprintf("Ensures that a value is supplied when attribute at %s has value %s", o.expression.String(), o.value)
}

func (o RequiredWhenValueIsValidator) MarkdownDescription(ctx context.Context) string {
Expand Down Expand Up @@ -81,7 +80,7 @@ func (o RequiredWhenValueIsValidator) Validate(ctx context.Context, req Required
resp.Diagnostics.AddAttributeError(
req.Path,
"Missing required attribute",
fmt.Sprintf("Attribute %q required when %q has value %q.", req.Path, mp.String(), mpVal.String()),
fmt.Sprintf("Attribute %q required when %q has value %s.", req.Path, mp, mpVal),
)
}
}
Expand Down
9 changes: 4 additions & 5 deletions apstra/apstra_validator/required_when_value_null.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package apstravalidator
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

var (
_ NineTypesValidator = RequiredWhenValueNullValidator{}
)
var _ NineTypesValidator = RequiredWhenValueNullValidator{}

type RequiredWhenValueNullValidator struct {
expression path.Expression
Expand All @@ -30,7 +29,7 @@ type RequiredWhenValueNullResponse struct {
}

func (o RequiredWhenValueNullValidator) Description(_ context.Context) string {
return fmt.Sprintf("Ensures that a value is supplied when attribute at %q is null", o.expression.String())
return fmt.Sprintf("Ensures that a value is supplied when attribute at %s is null", o.expression.String())
}

func (o RequiredWhenValueNullValidator) MarkdownDescription(ctx context.Context) string {
Expand Down Expand Up @@ -84,7 +83,7 @@ func (o RequiredWhenValueNullValidator) Validate(ctx context.Context, req Requir
resp.Diagnostics.AddAttributeError(
req.Path,
"Missing required attribute",
fmt.Sprintf("Attribute %q required when attribute %q is null.", req.Path, mp.String()),
fmt.Sprintf("Attribute %q required when attribute %q is null.", req.Path, mp),
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion apstra/apstra_validator/value_at_must_be.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apstravalidator
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -32,7 +33,7 @@ type valueAtMustBeValidatorResponse struct {
}

func (o valueAtMustBeValidator) Description(_ context.Context) string {
return fmt.Sprintf("element at %q must be: %q (null is %t)", o.expression, o.value.String(), o.nullOk)
return fmt.Sprintf("element at %q must be: %s (null is %t)", o.expression, o.value, o.nullOk)
}

func (o valueAtMustBeValidator) MarkdownDescription(ctx context.Context) string {
Expand Down
23 changes: 12 additions & 11 deletions apstra/apstra_validator/when_value_is.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package apstravalidator
import (
"context"
"fmt"
"strings"

"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"strings"
)

var _ NineTypesValidator = whenValueIsValidator{}
Expand Down Expand Up @@ -57,7 +58,7 @@ func (o whenValueIsValidator) Description(ctx context.Context) string {
descriptions = append(descriptions, v.Description(ctx))
}

return fmt.Sprintf("element must satisfy all validations: %s becuase value is %q", strings.Join(descriptions, " + "), o.trigger)
return fmt.Sprintf("element must satisfy all validations: %s becuase value is %s", strings.Join(descriptions, " + "), o.trigger)
}

func (o whenValueIsValidator) MarkdownDescription(ctx context.Context) string {
Expand All @@ -78,7 +79,7 @@ func (o whenValueIsValidator) ValidateBool(ctx context.Context, req validator.Bo
for _, v := range o.boolValidators {
vResponse := new(validator.BoolResponse)
v.ValidateBool(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -97,7 +98,7 @@ func (o whenValueIsValidator) ValidateFloat64(ctx context.Context, req validator
for _, v := range o.float64Validators {
vResponse := new(validator.Float64Response)
v.ValidateFloat64(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -116,7 +117,7 @@ func (o whenValueIsValidator) ValidateInt64(ctx context.Context, req validator.I
for _, v := range o.int64Validators {
vResponse := new(validator.Int64Response)
v.ValidateInt64(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -135,7 +136,7 @@ func (o whenValueIsValidator) ValidateList(ctx context.Context, req validator.Li
for _, v := range o.listValidators {
vResponse := new(validator.ListResponse)
v.ValidateList(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -154,7 +155,7 @@ func (o whenValueIsValidator) ValidateMap(ctx context.Context, req validator.Map
for _, v := range o.mapValidators {
vResponse := new(validator.MapResponse)
v.ValidateMap(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -173,7 +174,7 @@ func (o whenValueIsValidator) ValidateNumber(ctx context.Context, req validator.
for _, v := range o.numberValidators {
vResponse := new(validator.NumberResponse)
v.ValidateNumber(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -192,7 +193,7 @@ func (o whenValueIsValidator) ValidateObject(ctx context.Context, req validator.
for _, v := range o.objectValidators {
vResponse := new(validator.ObjectResponse)
v.ValidateObject(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -211,7 +212,7 @@ func (o whenValueIsValidator) ValidateSet(ctx context.Context, req validator.Set
for _, v := range o.setValidators {
vResponse := new(validator.SetResponse)
v.ValidateSet(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand All @@ -230,7 +231,7 @@ func (o whenValueIsValidator) ValidateString(ctx context.Context, req validator.
for _, v := range o.stringValidators {
vResponse := new(validator.StringResponse)
v.ValidateString(ctx, req, vResponse)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %q", req.Path, o.trigger), vResponse.Diagnostics...)
utils.WrapEachDiagnostic(fmt.Sprintf("When attribute %s is %s", req.Path, o.trigger), vResponse.Diagnostics...)
resp.Diagnostics.Append(vResponse.Diagnostics...)
}
}
Expand Down
Loading