1.0.0
This commit is contained in:
47
internal/validate/divisibleBy.go
Normal file
47
internal/validate/divisibleBy.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
)
|
||||
|
||||
var _ validator.Int64 = divisibleByValidator{}
|
||||
|
||||
// divisibleByValidator validates that an integer Attribute's value is divisible by certain value
|
||||
type divisibleByValidator struct {
|
||||
divisor int64
|
||||
}
|
||||
|
||||
// Description describes the validation in plain text formatting.
|
||||
func (v divisibleByValidator) Description(_ context.Context) string {
|
||||
return fmt.Sprintf("value must be divisible by %d", v.divisor)
|
||||
}
|
||||
|
||||
// MarkdownDescription describes the validation in Markdown formatting.
|
||||
func (v divisibleByValidator) MarkdownDescription(ctx context.Context) string {
|
||||
return v.Description(ctx)
|
||||
}
|
||||
|
||||
// Validate performs the validation.
|
||||
func (v divisibleByValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, response *validator.Int64Response) {
|
||||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
|
||||
return
|
||||
}
|
||||
|
||||
if req.ConfigValue.ValueInt64()%v.divisor != 0 {
|
||||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
v.Description(ctx),
|
||||
fmt.Sprintf("%d", req.ConfigValue.ValueInt64()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func DivisibleBy(divisor int64) validator.Int64 {
|
||||
return divisibleByValidator{
|
||||
divisor: divisor,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user