You can write custom validations that give users feedback about required syntax, types, and acceptable values in your
provider. The Framework has a collection of
predefined validators. Refer to
Predefined Validators to learn how to use them.
This page explains how to migrate attribute validation functions from SDKv2 to attribute validators in the Framework.
In the Framework, you implement either type of validation by setting the Validators field on the tfsdk.Attribute
struct with types that satisfy the tfsdk.AttributeValidator interface.
The following example shows how to implement a validation that ensures that an integer attribute has a value
greater than one.
Remember the following details when migrating from SDKv2 to the Framework.
In SDKv2, ValidateDiagFunc is a field on schema.Schema that you can use to define custom validation functions. In
the Framework, Validators is a field on each tfsdk.Attribute struct that can be used for custom validations.
The following examples show how to migrate portions of the
random provider.
For a complete example, clone the
terraform-provider-random repository and compare the resource_password.go file in
v3.3.2
with the file after the migration.
The following example from the resource_password.go file shows the implementation of the ValidateDiagFunc field for
the random_password's length attribute to validate that it's value is at least 1 (greater than zero).
var_ tfsdk.AttributeValidator = atLeastValidator{}// atLeastValidator validates that an integer Attribute's value is at least a certain value.type atLeastValidator struct{
min int64}// Description describes the validation in plain text formatting.func(validator atLeastValidator)Description(_ context.Context)string{return fmt.Sprintf("value must be at least %d", validator.min)}// MarkdownDescription describes the validation in Markdown formatting.func(validator atLeastValidator)MarkdownDescription(ctx context.Context)string{return validator.Description(ctx)}// Validate performs the validation.func(validator atLeastValidator)Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse){
i, ok :=validateInt(ctx, request, response)if!ok {return}if i < validator.min {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%d", i),))return}}// AtLeast returns an AttributeValidator which ensures that any configured// attribute value://// - Is a number, which can be represented by a 64-bit integer.// - Is exclusively greater than the given minimum.//// Null (unconfigured) and unknown (known after apply) values are skipped.funcAtLeast(min int64) tfsdk.AttributeValidator {return atLeastValidator{
min: min,}}
var_ tfsdk.AttributeValidator = atLeastValidator{}// atLeastValidator validates that an integer Attribute's value is at least a certain value.type atLeastValidator struct{ min int64}// Description describes the validation in plain text formatting.func(validator atLeastValidator)Description(_ context.Context)string{return fmt.Sprintf("value must be at least %d", validator.min)}// MarkdownDescription describes the validation in Markdown formatting.func(validator atLeastValidator)MarkdownDescription(ctx context.Context)string{return validator.Description(ctx)}// Validate performs the validation.func(validator atLeastValidator)Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse){ i, ok :=validateInt(ctx, request, response)if!ok {return}if i < validator.min { response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( request.AttributePath, validator.Description(ctx), fmt.Sprintf("%d", i),))return}}// AtLeast returns an AttributeValidator which ensures that any configured// attribute value://// - Is a number, which can be represented by a 64-bit integer.// - Is exclusively greater than the given minimum.//// Null (unconfigured) and unknown (known after apply) values are skipped.funcAtLeast(min int64) tfsdk.AttributeValidator {return atLeastValidator{ min: min,}}