hashicorp / terraform-plugin-framework-validators

Common Use Case Validators for terraform-plugin-framework
Mozilla Public License 2.0
25 stars 11 forks source link

Add `AtMostDecimalDigits` validator for float64 attribute precision validation #196

Closed TheNilesh closed 6 months ago

TheNilesh commented 7 months ago

Description

This pull request introduces a new validator, AtMostDecimalDigits, to validate float attributes in Terraform providers. The AtMostDecimalDigits validator ensures that a float attribute's value has at most a specified number of decimal digits. This is particularly useful for enforcing precision requirements in floating-point values.

Usage

The AtMostDecimalDigits validator can be used within the Schema method of a DataSource, Provider, or Resource as follows:

_ = schema.Schema{
    Attributes: map[string]schema.Attribute{
        "example_attr": schema.Float64Attribute{
            Required: true,
            Validators: []validator.Float64{
                // Validate floating point value must have precision up to 5 decimal digits
                float64validator.AtMostDecimalDigits(5),
            },
        },
    },
}

This example demonstrates using the AtMostDecimalDigits validator to enforce a precision requirement of up to 5 decimal digits for the "example_attr" attribute. For example 3.0, 23.00025, 3.99999, 0.000010 are valid but 0.25666698 is invalid.

Implementation Details

The implementation of the AtMostDecimalDigits validator includes logic to check the number of decimal digits in a float attribute's value and ensure that it does not exceed the specified limit.

hashicorp-cla commented 7 months ago

CLA assistant check
All committers have signed the CLA.

TheNilesh commented 7 months ago

I'm not sure if I should call it something else, maybe MaxDecimalDigits or WithPrecision. Suggestions are welcome.

bflad commented 7 months ago

Hi @TheNilesh 👋 Thank you for submitting this! Out of curiousity, could you explain your use case some more? Are you trying to work around the framework not directly having float32 type handling, some API-based restriction, or something else? Much appreciated!

TheNilesh commented 7 months ago

We offer flexibility in setting infrastructure component prices via Terraform configuration. However, our API doesn't support price amounts like 0.000001 or 0.019999, but supports 0.001, 0.003. Therefore, we proposed adding client-side validation along with server side check.

Initially, I considered this as a generic requirement regarding float64 precision. If you believe, this validation is deemed applicable only to specific use cases, I agree to implement it as a custom validator in our provider code and not here. Feel free to close this PR in that case.

TheNilesh commented 6 months ago

I believe this should be implemented as a custom validator and should not be provided by the framework.

github-actions[bot] commented 5 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.