-
Notifications
You must be signed in to change notification settings - Fork 13
Float64 AtLeast
, AtMost
and Between
validators
#18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
28e3544
Add Float validators from 'terraform-provider-awscc'.
ewbankkit 332a134
Run 'go mod tidy -compat=1.17'.
ewbankkit 04d5da6
Rename package 'validate' -> 'float64validator'.
ewbankkit 03944bd
Split each validator into its own source file.
ewbankkit 0c6ff1f
Remove 'float'/'Float' from type and function names.
ewbankkit dcb9247
Use 'var _ tfsdk.AttributeValidator =' syntax to ensure interface imp…
ewbankkit f3ccdf9
Add 'validatordiag' package.
ewbankkit d63b3d2
Use 'validatordiag.AttributeValueDiagnostic'.
ewbankkit 2329511
Use 'attr.Value' for test values, not 'tftypes.Value'.
ewbankkit 1f1f3c0
Unknown and Null float values are not valid.
ewbankkit 578816e
Update float64validator/between.go
ewbankkit 1adb12c
Update float64validator/at_most.go
ewbankkit 9e4e780
Update float64validator/at_least.go
ewbankkit b391503
Revert "Unknown and Null float values are not valid."
ewbankkit 43e028f
Do not allow 'types.Number'.
ewbankkit fca3710
Add CHANGELOG entry.
ewbankkit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
Introduced `float64validator` package with `AtLeast()`, `AtMost()`, and `Between()` validation functions | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package float64validator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = atLeastValidator{} | ||
|
||
// atLeastValidator validates that an float Attribute's value is at least a certain value. | ||
type atLeastValidator struct { | ||
min float64 | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (validator atLeastValidator) Description(_ context.Context) string { | ||
return fmt.Sprintf("value must be at least %f", 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) { | ||
f, ok := validateFloat(ctx, request, response) | ||
|
||
if !ok { | ||
return | ||
} | ||
|
||
if f < validator.min { | ||
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
request.AttributePath, | ||
validator.Description(ctx), | ||
fmt.Sprintf("%f", f), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// AtLeast returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a number, which can be represented by a 64-bit floating point. | ||
// - Is exclusively greater than the given minimum. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func AtLeast(min float64) tfsdk.AttributeValidator { | ||
return atLeastValidator{ | ||
min: min, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package float64validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestAtLeastValidator(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val attr.Value | ||
min float64 | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"not a Float64": { | ||
val: types.Bool{Value: true}, | ||
expectError: true, | ||
}, | ||
"unknown Float64": { | ||
val: types.Float64{Unknown: true}, | ||
min: 0.90, | ||
}, | ||
"null Float64": { | ||
val: types.Float64{Null: true}, | ||
min: 0.90, | ||
}, | ||
"valid integer as Float64": { | ||
val: types.Float64{Value: 2}, | ||
min: 0.90, | ||
}, | ||
"valid float as Float64": { | ||
val: types.Float64{Value: 2.2}, | ||
min: 0.90, | ||
}, | ||
"valid float as Float64 min": { | ||
val: types.Float64{Value: 0.9}, | ||
min: 0.90, | ||
}, | ||
"too small float as Float64": { | ||
val: types.Float64{Value: -1.1111}, | ||
min: 0.90, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
request := tfsdk.ValidateAttributeRequest{ | ||
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), | ||
AttributeConfig: test.val, | ||
} | ||
response := tfsdk.ValidateAttributeResponse{} | ||
AtLeast(test.min).Validate(context.TODO(), request, &response) | ||
|
||
if !response.Diagnostics.HasError() && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Diagnostics.HasError() && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Diagnostics) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package float64validator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = atMostValidator{} | ||
|
||
// atMostValidator validates that an float Attribute's value is at most a certain value. | ||
type atMostValidator struct { | ||
max float64 | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (validator atMostValidator) Description(_ context.Context) string { | ||
return fmt.Sprintf("value must be at most %f", validator.max) | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (validator atMostValidator) MarkdownDescription(ctx context.Context) string { | ||
return validator.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (validator atMostValidator) Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) { | ||
f, ok := validateFloat(ctx, request, response) | ||
|
||
if !ok { | ||
return | ||
} | ||
|
||
if f > validator.max { | ||
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
request.AttributePath, | ||
validator.Description(ctx), | ||
fmt.Sprintf("%f", f), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// AtMost returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a number, which can be represented by a 64-bit floating point. | ||
// - Is exclusively less than the given maximum. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func AtMost(max float64) tfsdk.AttributeValidator { | ||
return atMostValidator{ | ||
max: max, | ||
} | ||
} | ||
|
||
// validateFloat ensures that the request contains a Float64 value. | ||
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) { | ||
var n types.Float64 | ||
|
||
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n) | ||
|
||
if diags.HasError() { | ||
response.Diagnostics = append(response.Diagnostics, diags...) | ||
|
||
return 0, false | ||
} | ||
|
||
if n.Unknown || n.Null { | ||
return 0, false | ||
} | ||
|
||
return n.Value, true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package float64validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestAtMostValidator(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val attr.Value | ||
max float64 | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"not a Float64": { | ||
val: types.Bool{Value: true}, | ||
expectError: true, | ||
}, | ||
"unknown Float64": { | ||
val: types.Float64{Unknown: true}, | ||
max: 2.00, | ||
}, | ||
"null Float64": { | ||
val: types.Float64{Null: true}, | ||
max: 2.00, | ||
}, | ||
"valid integer as Float64": { | ||
val: types.Float64{Value: 1}, | ||
max: 2.00, | ||
}, | ||
"valid float as Float64": { | ||
val: types.Float64{Value: 1.1}, | ||
max: 2.00, | ||
}, | ||
"valid float as Float64 max": { | ||
val: types.Float64{Value: 2.0}, | ||
max: 2.00, | ||
}, | ||
"too large float as Float64": { | ||
val: types.Float64{Value: 3.0}, | ||
max: 2.00, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
request := tfsdk.ValidateAttributeRequest{ | ||
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), | ||
AttributeConfig: test.val, | ||
} | ||
response := tfsdk.ValidateAttributeResponse{} | ||
AtMost(test.max).Validate(context.TODO(), request, &response) | ||
|
||
if !response.Diagnostics.HasError() && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Diagnostics.HasError() && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Diagnostics) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package float64validator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = betweenValidator{} | ||
|
||
// betweenValidator validates that an float Attribute's value is in a range. | ||
type betweenValidator struct { | ||
min, max float64 | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (validator betweenValidator) Description(_ context.Context) string { | ||
return fmt.Sprintf("value must be between %f and %f", validator.min, validator.max) | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (validator betweenValidator) MarkdownDescription(ctx context.Context) string { | ||
return validator.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (validator betweenValidator) Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) { | ||
f, ok := validateFloat(ctx, request, response) | ||
|
||
if !ok { | ||
return | ||
} | ||
|
||
if f < validator.min || f > validator.max { | ||
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
request.AttributePath, | ||
validator.Description(ctx), | ||
fmt.Sprintf("%f", f), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// Between returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a number, which can be represented by a 64-bit floating point. | ||
// - Is exclusively greater than the given minimum and less than the given maximum. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func Between(min, max float64) tfsdk.AttributeValidator { | ||
if min > max { | ||
return nil | ||
} | ||
|
||
return betweenValidator{ | ||
min: min, | ||
max: max, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.