-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation on virtual_cluster to forbid empty string values
- Loading branch information
Showing
3 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
internal/schema/resource_kafka_cluster_v2/kafka_cluster_v2_resource_gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
) | ||
|
||
var _ validator.String = nonEmpty{} | ||
|
||
// stringNonEmpty validates that a string Attribute's length is at least a certain value. | ||
type nonEmpty struct { | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (validator nonEmpty) Description(_ context.Context) string { | ||
return "string should not be empty" | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (validator nonEmpty) MarkdownDescription(ctx context.Context) string { | ||
return validator.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (v nonEmpty) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { | ||
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
value := request.ConfigValue.ValueString() | ||
|
||
if len(value) == 0 { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
request.Path, | ||
v.Description(ctx), | ||
"\"\"", | ||
)) | ||
return | ||
} | ||
} | ||
|
||
// NonEmptyString returns a validator which ensures that any configured | ||
// attribute value a not empty string. Null (unconfigured) and unknown (known after apply) | ||
// values are skipped. | ||
func NonEmptyString() validator.String { | ||
return nonEmpty{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters