Skip to content

Commit

Permalink
feat: Improve filter validation to show error on misconfiguration (#45)
Browse files Browse the repository at this point in the history
* feat: Improve filter validation to show error on misconfiguration

* fix: Lint issue with comments
  • Loading branch information
alexluong authored May 6, 2024
1 parent 78faa23 commit d155963
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/provider/connection/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (r *connectionResource) Schema(_ context.Context, _ resource.SchemaRequest,
Optional: true,
},
},
Validators: []validator.Object{
validators.ExactlyOneChild(),
},
}

resp.Schema = schema.Schema{
Expand Down Expand Up @@ -111,6 +114,9 @@ func (r *connectionResource) Schema(_ context.Context, _ resource.SchemaRequest,
"path": filterRulePropertySchema,
"query": filterRulePropertySchema,
},
Validators: []validator.Object{
validators.AtLeastOneChild(),
},
},
"retry_rule": schema.SingleNestedAttribute{
Optional: true,
Expand Down
52 changes: 52 additions & 0 deletions internal/validators/AtLeastOneChild.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package validators

import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var _ validator.Object = atLeastOneChild{}

// atLeastOneChild validates if the provided object has at least one child attribute.
type atLeastOneChild struct {
}

func (validator atLeastOneChild) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
// Only validate the attribute configuration value if it is known.
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
defined := make(map[string]bool)
count := 0
for key, attr := range req.ConfigValue.Attributes() {
if attr.IsUnknown() || attr.IsNull() {
continue
}
defined[key] = true
count++
}
if count < 1 {
resp.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
req.Path,
validator.MarkdownDescription(ctx),
req.ConfigValue.String(),
))
}
}

func (validator atLeastOneChild) Description(ctx context.Context) string {
return "value must have at least one child attribute defined"
}

func (validator atLeastOneChild) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}

// AtLeastOneChild returns an AttributeValidator which ensures that any configured
// attribute object has at least one child attribute.
// Null (unconfigured) and unknown values are skipped.
func AtLeastOneChild() validator.Object {
return atLeastOneChild{}
}
2 changes: 1 addition & 1 deletion internal/validators/ExactlyOneChild.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var _ validator.Object = exactlyOneChild{}

// exactlyOneChild validates if the provided value is of type string and can be parsed as JSON.
// exactlyOneChild validates if the provided object has exactly one child attribute.
type exactlyOneChild struct {
}

Expand Down

0 comments on commit d155963

Please sign in to comment.