Closed TheNilesh closed 6 months ago
I'm not sure if I should call it something else, maybe MaxDecimalDigits
or WithPrecision
. Suggestions are welcome.
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!
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.
I believe this should be implemented as a custom validator and should not be provided by the framework.
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.
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:
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 but0.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.