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 10 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
54 changes: 54 additions & 0 deletions float64validator/at_least.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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, diags := validateFloat(ctx, validator, request)

if diags.HasError() {
response.Diagnostics.Append(diags...)

return
}

if f < validator.min {
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%f", f),
))

return
}
}

// AtLeast returns a new float value at least validator.
func AtLeast(min float64) tfsdk.AttributeValidator {
return atLeastValidator{
min: min,
}
}
88 changes: 88 additions & 0 deletions float64validator/at_least_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package float64validator

import (
"context"
"math/big"
"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 number": {
val: types.Bool{Value: true},
min: 0.90,
expectError: true,
},
"unknown number": {
val: types.Float64{Unknown: true},
min: 0.90,
expectError: true,
},
"null number": {
val: types.Number{Null: true},
min: 0.90,
expectError: true,
},
"valid integer as Number": {
val: types.Number{Value: big.NewFloat(2)},
min: 0.90,
},
"valid integer as Float64": {
val: types.Float64{Value: 2},
min: 0.90,
},
"valid float as Number": {
val: types.Number{Value: big.NewFloat(2.2)},
min: 0.90,
},
"valid float as Float64": {
val: types.Float64{Value: 2.2},
min: 0.90,
},
"valid float as Number min": {
val: types.Float64{Value: 0.9},
min: 0.90,
},
"valid float as Float64 min": {
val: types.Float64{Value: 0.9},
min: 0.90,
},
"too small float as Number": {
val: types.Number{Value: big.NewFloat(-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)
}
})
}
}
119 changes: 119 additions & 0 deletions float64validator/at_most.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package float64validator

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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, diags := validateFloat(ctx, validator, request)

if diags.HasError() {
response.Diagnostics.Append(diags...)

return
}

if f > validator.max {
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%f", f),
))

return
}
}

// AtMost returns a new float value at most validator.
func AtMost(max float64) tfsdk.AttributeValidator {
return atMostValidator{
max: max,
}
}

func validateFloat(ctx context.Context, validator tfsdk.AttributeValidator, request tfsdk.ValidateAttributeRequest) (float64, diag.Diagnostics) {
var n types.Float64

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
var n types.Number

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
return 0, diags
}

if n.Unknown {
return 0, []diag.Diagnostic{
validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
"Unknown",
),
}
}

if n.Null {
return 0, []diag.Diagnostic{
validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
"Null",
),
}
}

f, _ := n.Value.Float64()

return f, nil

}

if n.Unknown {
return 0, []diag.Diagnostic{
validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
"Unknown",
),
}
}

if n.Null {
return 0, []diag.Diagnostic{
validatordiag.AttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
"Null",
),
}
}

return n.Value, nil
}
88 changes: 88 additions & 0 deletions float64validator/at_most_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package float64validator

import (
"context"
"math/big"
"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 number": {
val: types.Bool{Value: true},
max: 2.00,
expectError: true,
},
"unknown number": {
val: types.Number{Unknown: true},
max: 2.00,
expectError: true,
},
"null number": {
val: types.Float64{Null: true},
max: 2.00,
expectError: true,
},
"valid integer as Number": {
val: types.Number{Value: big.NewFloat(1)},
max: 2.00,
},
"valid integer as Float64": {
val: types.Float64{Value: 1},
max: 2.00,
},
"valid float as Number": {
val: types.Number{Value: big.NewFloat(1.1)},
max: 2.00,
},
"valid float as Float64": {
val: types.Float64{Value: 1.1},
max: 2.00,
},
"valid float as Number max": {
val: types.Number{Value: big.NewFloat(2.0)},
max: 2.00,
},
"valid float as Float64 max": {
val: types.Float64{Value: 2.0},
max: 2.00,
},
"too large float as Number": {
val: types.Number{Value: big.NewFloat(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)
}
})
}
}
Loading