Skip to content

Introducing OneOf and NoneOf #42

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 9 commits into from
Jun 23, 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
15 changes: 15 additions & 0 deletions .changelog/42.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:enhancement
floatvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`
```

```release-note:enhancement
int64validator: 2 new validation functions, `OneOf()` and `NoneOf()`
```

```release-note:feature
numbervalidator: New package that starts with 2 validation functions, `OneOf()` and `NoneOf()`
```

```release-note:enhancement
stringvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`, that offer case-sensitivity control
```
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Jetbrains IDEs
# JetBrains IDEs project files
.idea/
*.iws
4 changes: 2 additions & 2 deletions float64validator/at_least.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand Down Expand Up @@ -34,7 +34,7 @@ func (validator atLeastValidator) Validate(ctx context.Context, request tfsdk.Va
}

if f < validator.min {
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%f", f),
Expand Down
5 changes: 3 additions & 2 deletions float64validator/at_least_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package float64validator
package float64validator_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestAtLeastValidator(t *testing.T) {
AttributeConfig: test.val,
}
response := tfsdk.ValidateAttributeResponse{}
AtLeast(test.min).Validate(context.TODO(), request, &response)
float64validator.AtLeast(test.min).Validate(context.TODO(), request, &response)

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
4 changes: 2 additions & 2 deletions float64validator/at_most.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand Down Expand Up @@ -34,7 +34,7 @@ func (validator atMostValidator) Validate(ctx context.Context, request tfsdk.Val
}

if f > validator.max {
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%f", f),
Expand Down
5 changes: 3 additions & 2 deletions float64validator/at_most_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package float64validator
package float64validator_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestAtMostValidator(t *testing.T) {
AttributeConfig: test.val,
}
response := tfsdk.ValidateAttributeResponse{}
AtMost(test.max).Validate(context.TODO(), request, &response)
float64validator.AtMost(test.max).Validate(context.TODO(), request, &response)

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
4 changes: 2 additions & 2 deletions float64validator/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand Down Expand Up @@ -34,7 +34,7 @@ func (validator betweenValidator) Validate(ctx context.Context, request tfsdk.Va
}

if f < validator.min || f > validator.max {
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%f", f),
Expand Down
5 changes: 3 additions & 2 deletions float64validator/between_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package float64validator
package float64validator_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestBetweenValidator(t *testing.T) {
AttributeConfig: test.val,
}
response := tfsdk.ValidateAttributeResponse{}
Between(test.min, test.max).Validate(context.TODO(), request, &response)
float64validator.Between(test.min, test.max).Validate(context.TODO(), request, &response)

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
19 changes: 19 additions & 0 deletions float64validator/none_of.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package float64validator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/primitivevalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// NoneOf checks that the float64 held in the attribute
// is none of the given `unacceptableFloats`.
func NoneOf(unacceptableFloats ...float64) tfsdk.AttributeValidator {
unacceptableFloatValues := make([]attr.Value, 0, len(unacceptableFloats))
for _, f := range unacceptableFloats {
unacceptableFloatValues = append(unacceptableFloatValues, types.Float64{Value: f})
}

return primitivevalidator.NoneOf(unacceptableFloatValues...)
}
85 changes: 85 additions & 0 deletions float64validator/none_of_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package float64validator_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func TestNoneOfValidator(t *testing.T) {
t.Parallel()

type testCase struct {
in attr.Value
validator tfsdk.AttributeValidator
expErrors int
}

testCases := map[string]testCase{
"simple-match": {
in: types.Float64{Value: 123.456},
validator: float64validator.NoneOf(
123.456,
234.567,
8910.11,
1213.1415,
),
expErrors: 1,
},
"simple-mismatch": {
in: types.Float64{Value: 123.456},
validator: float64validator.NoneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
"skip-validation-on-null": {
in: types.Float64{Null: true},
validator: float64validator.NoneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
"skip-validation-on-unknown": {
in: types.Float64{Unknown: true},
validator: float64validator.NoneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
req := tfsdk.ValidateAttributeRequest{
AttributeConfig: test.in,
}
res := tfsdk.ValidateAttributeResponse{}
test.validator.Validate(context.TODO(), req, &res)

if test.expErrors > 0 && !res.Diagnostics.HasError() {
t.Fatalf("expected %d error(s), got none", test.expErrors)
}

if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
}

if test.expErrors == 0 && res.Diagnostics.HasError() {
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
}
})
}
}
19 changes: 19 additions & 0 deletions float64validator/one_of.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package float64validator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/primitivevalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// OneOf checks that the float64 held in the attribute
// is one of the given `acceptableFloats`.
func OneOf(acceptableFloats ...float64) tfsdk.AttributeValidator {
acceptableFloatValues := make([]attr.Value, 0, len(acceptableFloats))
for _, f := range acceptableFloats {
acceptableFloatValues = append(acceptableFloatValues, types.Float64{Value: f})
}

return primitivevalidator.OneOf(acceptableFloatValues...)
}
85 changes: 85 additions & 0 deletions float64validator/one_of_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package float64validator_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func TestOneOfValidator(t *testing.T) {
t.Parallel()

type testCase struct {
in attr.Value
validator tfsdk.AttributeValidator
expErrors int
}

testCases := map[string]testCase{
"simple-match": {
in: types.Float64{Value: 123.456},
validator: float64validator.OneOf(
123.456,
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
"simple-mismatch": {
in: types.Float64{Value: 123.456},
validator: float64validator.OneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 1,
},
"skip-validation-on-null": {
in: types.Float64{Null: true},
validator: float64validator.OneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
"skip-validation-on-unknown": {
in: types.Float64{Unknown: true},
validator: float64validator.OneOf(
234.567,
8910.11,
1213.1415,
),
expErrors: 0,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
req := tfsdk.ValidateAttributeRequest{
AttributeConfig: test.in,
}
res := tfsdk.ValidateAttributeResponse{}
test.validator.Validate(context.TODO(), req, &res)

if test.expErrors > 0 && !res.Diagnostics.HasError() {
t.Fatalf("expected %d error(s), got none", test.expErrors)
}

if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
}

if test.expErrors == 0 && res.Diagnostics.HasError() {
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
}
})
}
}
25 changes: 14 additions & 11 deletions float64validator/type_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package float64validator
import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// 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
t := request.AttributeConfig.Type(ctx)
if t != types.Float64Type {
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
request.AttributePath,
"Expected value of type float64",
t.String(),
))
return 0.0, false
}

if n.Unknown || n.Null {
return 0, false
f := request.AttributeConfig.(types.Float64)

if f.Unknown || f.Null {
return 0.0, false
}

return n.Value, true
return f.Value, true
}
Loading