Skip to content

Commit

Permalink
add attribute key to ARN validator error message
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonaj committed Mar 17, 2023
1 parent 8053f98 commit a4062c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/framework/validators/arn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (validator arnValidator) ValidateString(ctx context.Context, request valida
return
}

if errs := verify.ValidateARN(request.ConfigValue.ValueString()); errs != nil {
if errs := verify.ValidateARN(request.Path.String(), request.ConfigValue.ValueString()); errs != nil {
for _, v := range errs {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.Path,
Expand Down
14 changes: 7 additions & 7 deletions internal/verify/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Valid4ByteASN(v interface{}, k string) (ws []string, errors []error) {
func ValidARN(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if errs := ValidateARN(value); errs != nil {
if errs := ValidateARN(k, value); errs != nil {
errors = errs
}

Expand All @@ -60,7 +60,7 @@ func ValidAccountID(v interface{}, k string) (ws []string, errors []error) {
}

// ValidateARN validates that a string is an ARN.
func ValidateARN(value string) (errors []error) {
func ValidateARN(attribute, value string) (errors []error) {
if value == "" {
return
}
Expand All @@ -73,21 +73,21 @@ func ValidateARN(value string) (errors []error) {
}

if parsedARN.Partition == "" {
errors = append(errors, fmt.Errorf("(%s) is an invalid ARN: missing partition value", value))
errors = append(errors, fmt.Errorf("%q (%s) is an invalid ARN: missing partition value", attribute, value))
} else if !partitionRegexp.MatchString(parsedARN.Partition) {
errors = append(errors, fmt.Errorf("(%s) is an invalid ARN: invalid partition value (expecting to match regular expression: %s)", value, partitionRegexp))
errors = append(errors, fmt.Errorf("%q (%s) is an invalid ARN: invalid partition value (expecting to match regular expression: %s)", attribute, value, partitionRegexp))
}

if parsedARN.Region != "" && !regionRegexp.MatchString(parsedARN.Region) {
errors = append(errors, fmt.Errorf("(%s) is an invalid ARN: invalid region value (expecting to match regular expression: %s)", value, regionRegexp))
errors = append(errors, fmt.Errorf("%q (%s) is an invalid ARN: invalid region value (expecting to match regular expression: %s)", attribute, value, regionRegexp))
}

if parsedARN.AccountID != "" && !arnAccountIDRegexp.MatchString(parsedARN.AccountID) {
errors = append(errors, fmt.Errorf("(%s) is an invalid ARN: invalid account ID value (expecting to match regular expression: %s)", value, arnAccountIDRegexp))
errors = append(errors, fmt.Errorf("%q (%s) is an invalid ARN: invalid account ID value (expecting to match regular expression: %s)", attribute, value, arnAccountIDRegexp))
}

if parsedARN.Resource == "" {
errors = append(errors, fmt.Errorf("(%s) is an invalid ARN: missing resource value", value))
errors = append(errors, fmt.Errorf("%q (%s) is an invalid ARN: missing resource value", attribute, value))
}

return errors
Expand Down

0 comments on commit a4062c1

Please sign in to comment.