mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-06-30 02:31:10 +00:00
* chore(code): code cleanup / renaming * chore(code): flatten and rename fw provider package * chore(code): refactor & update network tests
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
package validators
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
|
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
|
)
|
|
|
|
// NewParseValidator creates a validator which uses a parsing function to validate a string. The function is expected
|
|
// to return a value of type `T` and an error. If the error is non-nil, the validator will fail. The `description`
|
|
// argument should contain a description of the validator's effect.
|
|
func NewParseValidator[T any](parseFunction func(string) (T, error), description string) validator.String {
|
|
return &parseValidator[T]{
|
|
parseFunction: parseFunction,
|
|
description: description,
|
|
}
|
|
}
|
|
|
|
// parseValidator is a validator which uses a parsing function to validate a string.
|
|
type parseValidator[T any] struct {
|
|
parseFunction func(string) (T, error)
|
|
description string
|
|
}
|
|
|
|
func (val *parseValidator[T]) Description(_ context.Context) string {
|
|
return val.description
|
|
}
|
|
|
|
func (val *parseValidator[T]) MarkdownDescription(_ context.Context) string {
|
|
return val.description
|
|
}
|
|
|
|
func (val *parseValidator[T]) ValidateString(
|
|
ctx context.Context,
|
|
request validator.StringRequest,
|
|
response *validator.StringResponse,
|
|
) {
|
|
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
|
|
return
|
|
}
|
|
|
|
value := request.ConfigValue
|
|
|
|
_, err := val.parseFunction(value.ValueString())
|
|
if err != nil {
|
|
response.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
|
|
request.Path,
|
|
val.Description(ctx),
|
|
value.String(),
|
|
))
|
|
}
|
|
}
|