You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform-provider-dynamix/internal/validate/divisibleBy.go

48 lines
1.3 KiB

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,
}
}