hashicorp / terraform-plugin-framework

A next-generation framework for building Terraform providers.
https://developer.hashicorp.com/terraform/plugin/framework
Mozilla Public License 2.0
298 stars 92 forks source link

Add type-based provider-defined function parameter validation #968

Closed bendbennett closed 5 months ago

bendbennett commented 6 months ago

Closes: #589 Closes: #893

Background

Provider-defined functions can accept parameters, or arguments as input. There is an opportunity to provide validation of such parameters in an analogous manner to the validation of values supplied in configuration for attributes, by implementing parameter-based and type-based validation for provider-defined function parameters.

This PR is concerned with the addition of type-based validation, which enables provider developers using custom value types to implement parameter validation.

Validation Package

This PR adds type-based provider-defined function parameter validation through the introduction of a validation package with a new ValidateableParameter interface.

type ValidateableParameter interface {
    ValidateParameter(context.Context, ValidateParameterRequest, *ValidateParameterResponse)
}

type ValidateParameterRequest struct {
    Position int64
}

type ValidateParameterResponse struct {
    Error *function.FuncError
}

Deprecation of xattr.TypeWithValidate

This PR also introduces a new ValidateableAttribute interface, and deprecates the xattr.TypeWithValidate interface. Provider developers who are using custom value types that will be used in the context of both attribute validation and parameter validation would need to implement both interfaces on the custom value type, but could share the validation logic to reduce duplication.

type ValidateableAttribute interface {
    ValidateAttribute(context.Context, ValidateAttributeRequest, *ValidateAttributeResponse)
}

type ValidateAttributeRequest struct {
    Path path.Path
}

type ValidateAttributeResponse struct {
    Diagnostics diag.Diagnostics
}
github-actions[bot] commented 4 months ago

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.