Skip to content

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 16 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/18.txt
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
```
58 changes: 58 additions & 0 deletions float64validator/at_least.go
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,
}
}
72 changes: 72 additions & 0 deletions float64validator/at_least_test.go
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)
}
})
}
}
78 changes: 78 additions & 0 deletions float64validator/at_most.go
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
}
72 changes: 72 additions & 0 deletions float64validator/at_most_test.go
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)
}
})
}
}
63 changes: 63 additions & 0 deletions float64validator/between.go
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,
}
}
Loading