Skip to content

Commit 668b4ce

Browse files
author
Ivan De Marino
authored
Introducing OneOf and NoneOf (#42)
* Relocating `validatordiag` package to `helpers/validatordiag` * typo in `.gitignore` * Adding `OneOf()` and `NoneOf()` validators, a pair for each type. The underlying implementation sits in `primitivevalidator` package. * Use explicit `.Type()` interrogation instead of reflection to validate type * Locating `primitivevalidator` into `internal/` * Remove "public" mention of `primitivevalidator` package * PR review * Adding more tests for `OneOf()` and `NoneOf()` for all packages * Fix conflicts from rebase
1 parent d3ecda1 commit 668b4ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1795
-144
lines changed

.changelog/42.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:enhancement
2+
floatvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`
3+
```
4+
5+
```release-note:enhancement
6+
int64validator: 2 new validation functions, `OneOf()` and `NoneOf()`
7+
```
8+
9+
```release-note:feature
10+
numbervalidator: New package that starts with 2 validation functions, `OneOf()` and `NoneOf()`
11+
```
12+
13+
```release-note:enhancement
14+
stringvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`, that offer case-sensitivity control
15+
```

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Jetbrains IDEs
1+
# JetBrains IDEs project files
22
.idea/
33
*.iws

float64validator/at_least.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
7+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
99
)
1010

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

3636
if f < validator.min {
37-
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/at_least_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -58,7 +59,7 @@ func TestAtLeastValidator(t *testing.T) {
5859
AttributeConfig: test.val,
5960
}
6061
response := tfsdk.ValidateAttributeResponse{}
61-
AtLeast(test.min).Validate(context.TODO(), request, &response)
62+
float64validator.AtLeast(test.min).Validate(context.TODO(), request, &response)
6263

6364
if !response.Diagnostics.HasError() && test.expectError {
6465
t.Fatal("expected error, got no error")

float64validator/at_most.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
7+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
99
)
1010

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

3636
if f > validator.max {
37-
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/at_most_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -58,7 +59,7 @@ func TestAtMostValidator(t *testing.T) {
5859
AttributeConfig: test.val,
5960
}
6061
response := tfsdk.ValidateAttributeResponse{}
61-
AtMost(test.max).Validate(context.TODO(), request, &response)
62+
float64validator.AtMost(test.max).Validate(context.TODO(), request, &response)
6263

6364
if !response.Diagnostics.HasError() && test.expectError {
6465
t.Fatal("expected error, got no error")

float64validator/between.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
7+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
99
)
1010

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

3636
if f < validator.min || f > validator.max {
37-
response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/between_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package float64validator
1+
package float64validator_test
22

33
import (
44
"context"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -76,7 +77,7 @@ func TestBetweenValidator(t *testing.T) {
7677
AttributeConfig: test.val,
7778
}
7879
response := tfsdk.ValidateAttributeResponse{}
79-
Between(test.min, test.max).Validate(context.TODO(), request, &response)
80+
float64validator.Between(test.min, test.max).Validate(context.TODO(), request, &response)
8081

8182
if !response.Diagnostics.HasError() && test.expectError {
8283
t.Fatal("expected error, got no error")

float64validator/none_of.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package float64validator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/internal/primitivevalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/attr"
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
// NoneOf checks that the float64 held in the attribute
11+
// is none of the given `unacceptableFloats`.
12+
func NoneOf(unacceptableFloats ...float64) tfsdk.AttributeValidator {
13+
unacceptableFloatValues := make([]attr.Value, 0, len(unacceptableFloats))
14+
for _, f := range unacceptableFloats {
15+
unacceptableFloatValues = append(unacceptableFloatValues, types.Float64{Value: f})
16+
}
17+
18+
return primitivevalidator.NoneOf(unacceptableFloatValues...)
19+
}

float64validator/none_of_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package float64validator_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
func TestNoneOfValidator(t *testing.T) {
15+
t.Parallel()
16+
17+
type testCase struct {
18+
in attr.Value
19+
validator tfsdk.AttributeValidator
20+
expErrors int
21+
}
22+
23+
testCases := map[string]testCase{
24+
"simple-match": {
25+
in: types.Float64{Value: 123.456},
26+
validator: float64validator.NoneOf(
27+
123.456,
28+
234.567,
29+
8910.11,
30+
1213.1415,
31+
),
32+
expErrors: 1,
33+
},
34+
"simple-mismatch": {
35+
in: types.Float64{Value: 123.456},
36+
validator: float64validator.NoneOf(
37+
234.567,
38+
8910.11,
39+
1213.1415,
40+
),
41+
expErrors: 0,
42+
},
43+
"skip-validation-on-null": {
44+
in: types.Float64{Null: true},
45+
validator: float64validator.NoneOf(
46+
234.567,
47+
8910.11,
48+
1213.1415,
49+
),
50+
expErrors: 0,
51+
},
52+
"skip-validation-on-unknown": {
53+
in: types.Float64{Unknown: true},
54+
validator: float64validator.NoneOf(
55+
234.567,
56+
8910.11,
57+
1213.1415,
58+
),
59+
expErrors: 0,
60+
},
61+
}
62+
63+
for name, test := range testCases {
64+
name, test := name, test
65+
t.Run(name, func(t *testing.T) {
66+
req := tfsdk.ValidateAttributeRequest{
67+
AttributeConfig: test.in,
68+
}
69+
res := tfsdk.ValidateAttributeResponse{}
70+
test.validator.Validate(context.TODO(), req, &res)
71+
72+
if test.expErrors > 0 && !res.Diagnostics.HasError() {
73+
t.Fatalf("expected %d error(s), got none", test.expErrors)
74+
}
75+
76+
if test.expErrors > 0 && test.expErrors != validatordiag.ErrorsCount(res.Diagnostics) {
77+
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
78+
}
79+
80+
if test.expErrors == 0 && res.Diagnostics.HasError() {
81+
t.Fatalf("expected no error(s), got %d: %v", validatordiag.ErrorsCount(res.Diagnostics), res.Diagnostics)
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)