Skip to content

Commit 6c4f6ea

Browse files
committed
int32validator: implement parameter interface
1 parent 4af2941 commit 6c4f6ea

17 files changed

+360
-81
lines changed

float64validator/at_least.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (validator atLeastValidator) ValidateParameterFloat64(ctx context.Context,
6262
}
6363

6464
// AtLeast returns an AttributeValidator which ensures that any configured
65-
// attribute value or function parameter value:
65+
// attribute or function parameter value:
6666
//
6767
// - Is a number, which can be represented by a 64-bit floating point.
6868
// - Is greater than or equal to the given minimum.

float64validator/between.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (v betweenValidator) ValidateParameterFloat64(ctx context.Context, request
9090
}
9191

9292
// Between returns an AttributeValidator which ensures that any configured
93-
// attribute value:
93+
// attribute or function parameter value:
9494
//
9595
// - Is a number, which can be represented by a 64-bit floating point.
9696
// - Is greater than or equal to the given minimum and less than or equal to the given maximum.

int32validator/at_least.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/hashicorp/terraform-plugin-framework/function"
1011
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1112

1213
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
14+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatorfuncerr"
1315
)
1416

1517
var _ validator.Int32 = atLeastValidator{}
18+
var _ function.Int32ParameterValidator = atLeastValidator{}
1619

17-
// atLeastValidator validates that an integer Attribute's value is at least a certain value.
1820
type atLeastValidator struct {
1921
min int32
2022
}
2123

22-
// Description describes the validation in plain text formatting.
2324
func (validator atLeastValidator) Description(_ context.Context) string {
2425
return fmt.Sprintf("value must be at least %d", validator.min)
2526
}
2627

27-
// MarkdownDescription describes the validation in Markdown formatting.
2828
func (validator atLeastValidator) MarkdownDescription(ctx context.Context) string {
2929
return validator.Description(ctx)
3030
}
3131

32-
// ValidateInt32 performs the validation.
3332
func (v atLeastValidator) ValidateInt32(ctx context.Context, request validator.Int32Request, response *validator.Int32Response) {
3433
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
3534
return
@@ -44,14 +43,28 @@ func (v atLeastValidator) ValidateInt32(ctx context.Context, request validator.I
4443
}
4544
}
4645

46+
func (v atLeastValidator) ValidateParameterInt32(ctx context.Context, request function.Int32ParameterValidatorRequest, response *function.Int32ParameterValidatorResponse) {
47+
if request.Value.IsNull() || request.Value.IsUnknown() {
48+
return
49+
}
50+
51+
if request.Value.ValueInt32() < v.min {
52+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
53+
request.ArgumentPosition,
54+
v.Description(ctx),
55+
fmt.Sprintf("%d", request.Value.ValueInt32()),
56+
)
57+
}
58+
}
59+
4760
// AtLeast returns an AttributeValidator which ensures that any configured
48-
// attribute value:
61+
// attribute or function parameter value:
4962
//
5063
// - Is a number, which can be represented by a 32-bit integer.
5164
// - Is greater than or equal to the given minimum.
5265
//
5366
// Null (unconfigured) and unknown (known after apply) values are skipped.
54-
func AtLeast(minVal int32) validator.Int32 {
67+
func AtLeast(minVal int32) atLeastValidator {
5568
return atLeastValidator{
5669
min: minVal,
5770
}

int32validator/at_least_example_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package int32validator_test
55

66
import (
77
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/function"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
910

1011
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
@@ -24,3 +25,17 @@ func ExampleAtLeast() {
2425
},
2526
}
2627
}
28+
29+
func ExampleAtLeast_function() {
30+
_ = function.Definition{
31+
Parameters: []function.Parameter{
32+
function.Int32Parameter{
33+
Name: "example_param",
34+
Validators: []function.Int32ParameterValidator{
35+
// Validate integer value must be at least 42
36+
int32validator.AtLeast(42),
37+
},
38+
},
39+
},
40+
}
41+
}

int32validator/at_least_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package int32validator_test
55

66
import (
77
"context"
8+
"fmt"
89
"testing"
910

11+
"github.com/hashicorp/terraform-plugin-framework/function"
1012
"github.com/hashicorp/terraform-plugin-framework/path"
1113
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1214
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -48,7 +50,8 @@ func TestAtLeastValidator(t *testing.T) {
4850

4951
for name, test := range tests {
5052
name, test := name, test
51-
t.Run(name, func(t *testing.T) {
53+
54+
t.Run(fmt.Sprintf("ValidateInt32 - %s", name), func(t *testing.T) {
5255
t.Parallel()
5356
request := validator.Int32Request{
5457
Path: path.Root("test"),
@@ -66,5 +69,22 @@ func TestAtLeastValidator(t *testing.T) {
6669
t.Fatalf("got unexpected error: %s", response.Diagnostics)
6770
}
6871
})
72+
73+
t.Run(fmt.Sprintf("ValidateParameterInt32 - %s", name), func(t *testing.T) {
74+
t.Parallel()
75+
request := function.Int32ParameterValidatorRequest{
76+
Value: test.val,
77+
}
78+
response := function.Int32ParameterValidatorResponse{}
79+
int32validator.AtLeast(test.min).ValidateParameterInt32(context.TODO(), request, &response)
80+
81+
if response.Error == nil && test.expectError {
82+
t.Fatal("expected error, got no error")
83+
}
84+
85+
if response.Error != nil && !test.expectError {
86+
t.Fatalf("got unexpected error: %s", response.Error)
87+
}
88+
})
6989
}
7090
}

int32validator/at_most.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/hashicorp/terraform-plugin-framework/function"
1011
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1112

1213
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
14+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatorfuncerr"
1315
)
1416

1517
var _ validator.Int32 = atMostValidator{}
18+
var _ function.Int32ParameterValidator = atMostValidator{}
1619

17-
// atMostValidator validates that an integer Attribute's value is at most a certain value.
1820
type atMostValidator struct {
1921
max int32
2022
}
2123

22-
// Description describes the validation in plain text formatting.
2324
func (validator atMostValidator) Description(_ context.Context) string {
2425
return fmt.Sprintf("value must be at most %d", validator.max)
2526
}
2627

27-
// MarkdownDescription describes the validation in Markdown formatting.
2828
func (validator atMostValidator) MarkdownDescription(ctx context.Context) string {
2929
return validator.Description(ctx)
3030
}
3131

32-
// ValidateInt32 performs the validation.
3332
func (v atMostValidator) ValidateInt32(ctx context.Context, request validator.Int32Request, response *validator.Int32Response) {
3433
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
3534
return
@@ -44,14 +43,28 @@ func (v atMostValidator) ValidateInt32(ctx context.Context, request validator.In
4443
}
4544
}
4645

46+
func (v atMostValidator) ValidateParameterInt32(ctx context.Context, request function.Int32ParameterValidatorRequest, response *function.Int32ParameterValidatorResponse) {
47+
if request.Value.IsNull() || request.Value.IsUnknown() {
48+
return
49+
}
50+
51+
if request.Value.ValueInt32() > v.max {
52+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
53+
request.ArgumentPosition,
54+
v.Description(ctx),
55+
fmt.Sprintf("%d", request.Value.ValueInt32()),
56+
)
57+
}
58+
}
59+
4760
// AtMost returns an AttributeValidator which ensures that any configured
48-
// attribute value:
61+
// attribute or function parameter value:
4962
//
5063
// - Is a number, which can be represented by a 32-bit integer.
5164
// - Is less than or equal to the given maximum.
5265
//
5366
// Null (unconfigured) and unknown (known after apply) values are skipped.
54-
func AtMost(maxVal int32) validator.Int32 {
67+
func AtMost(maxVal int32) atMostValidator {
5568
return atMostValidator{
5669
max: maxVal,
5770
}

int32validator/at_most_example_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package int32validator_test
55

66
import (
77
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/function"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
910

1011
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
@@ -24,3 +25,17 @@ func ExampleAtMost() {
2425
},
2526
}
2627
}
28+
29+
func ExampleAtMost_function() {
30+
_ = function.Definition{
31+
Parameters: []function.Parameter{
32+
function.Int32Parameter{
33+
Name: "example_param",
34+
Validators: []function.Int32ParameterValidator{
35+
// Validate integer value must be at most 42
36+
int32validator.AtMost(42),
37+
},
38+
},
39+
},
40+
}
41+
}

int32validator/at_most_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package int32validator_test
55

66
import (
77
"context"
8+
"fmt"
89
"testing"
910

11+
"github.com/hashicorp/terraform-plugin-framework/function"
1012
"github.com/hashicorp/terraform-plugin-framework/path"
1113
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1214
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -48,7 +50,8 @@ func TestAtMostValidator(t *testing.T) {
4850

4951
for name, test := range tests {
5052
name, test := name, test
51-
t.Run(name, func(t *testing.T) {
53+
54+
t.Run(fmt.Sprintf("ValidateInt32 - %s", name), func(t *testing.T) {
5255
t.Parallel()
5356
request := validator.Int32Request{
5457
Path: path.Root("test"),
@@ -66,5 +69,22 @@ func TestAtMostValidator(t *testing.T) {
6669
t.Fatalf("got unexpected error: %s", response.Diagnostics)
6770
}
6871
})
72+
73+
t.Run(fmt.Sprintf("ValidateParameterInt32 - %s", name), func(t *testing.T) {
74+
t.Parallel()
75+
request := function.Int32ParameterValidatorRequest{
76+
Value: test.val,
77+
}
78+
response := function.Int32ParameterValidatorResponse{}
79+
int32validator.AtMost(test.max).ValidateParameterInt32(context.TODO(), request, &response)
80+
81+
if response.Error == nil && test.expectError {
82+
t.Fatal("expected error, got no error")
83+
}
84+
85+
if response.Error != nil && !test.expectError {
86+
t.Fatalf("got unexpected error: %s", response.Error)
87+
}
88+
})
6989
}
7090
}

int32validator/between.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,46 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/hashicorp/terraform-plugin-framework/function"
1011
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1112

1213
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
14+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatorfuncerr"
1315
)
1416

1517
var _ validator.Int32 = betweenValidator{}
18+
var _ function.Int32ParameterValidator = betweenValidator{}
1619

17-
// betweenValidator validates that an integer Attribute's value is in a range.
1820
type betweenValidator struct {
1921
min, max int32
2022
}
2123

22-
// Description describes the validation in plain text formatting.
24+
func (validator betweenValidator) invalidUsageMessage() string {
25+
return fmt.Sprintf("minVal cannot be greater than maxVal - minVal: %d, maxVal: %d", validator.min, validator.max)
26+
}
27+
2328
func (validator betweenValidator) Description(_ context.Context) string {
2429
return fmt.Sprintf("value must be between %d and %d", validator.min, validator.max)
2530
}
2631

27-
// MarkdownDescription describes the validation in Markdown formatting.
2832
func (validator betweenValidator) MarkdownDescription(ctx context.Context) string {
2933
return validator.Description(ctx)
3034
}
3135

32-
// ValidateInt32 performs the validation.
3336
func (v betweenValidator) ValidateInt32(ctx context.Context, request validator.Int32Request, response *validator.Int32Response) {
37+
// Return an error if the validator has been created in an invalid state
38+
if v.min > v.max {
39+
response.Diagnostics.Append(
40+
validatordiag.InvalidValidatorUsageDiagnostic(
41+
request.Path,
42+
"Between",
43+
v.invalidUsageMessage(),
44+
),
45+
)
46+
47+
return
48+
}
49+
3450
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
3551
return
3652
}
@@ -44,18 +60,42 @@ func (v betweenValidator) ValidateInt32(ctx context.Context, request validator.I
4460
}
4561
}
4662

63+
func (v betweenValidator) ValidateParameterInt32(ctx context.Context, request function.Int32ParameterValidatorRequest, response *function.Int32ParameterValidatorResponse) {
64+
// Return an error if the validator has been created in an invalid state
65+
if v.min > v.max {
66+
response.Error = validatorfuncerr.InvalidValidatorUsageFuncError(
67+
request.ArgumentPosition,
68+
"Between",
69+
v.invalidUsageMessage(),
70+
)
71+
72+
return
73+
}
74+
75+
if request.Value.IsNull() || request.Value.IsUnknown() {
76+
return
77+
}
78+
79+
if request.Value.ValueInt32() < v.min || request.Value.ValueInt32() > v.max {
80+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
81+
request.ArgumentPosition,
82+
v.Description(ctx),
83+
fmt.Sprintf("%d", request.Value.ValueInt32()),
84+
)
85+
}
86+
}
87+
4788
// Between returns an AttributeValidator which ensures that any configured
48-
// attribute value:
89+
// attribute or function parameter value:
4990
//
5091
// - Is a number, which can be represented by a 32-bit integer.
5192
// - Is greater than or equal to the given minimum and less than or equal to the given maximum.
5293
//
5394
// Null (unconfigured) and unknown (known after apply) values are skipped.
54-
func Between(minVal, maxVal int32) validator.Int32 {
55-
if minVal > maxVal {
56-
return nil
57-
}
58-
95+
//
96+
// minVal cannot be greater than maxVal. Invalid combinations of
97+
// minVal and maxVal will result in an implementation error message during validation.
98+
func Between(minVal, maxVal int32) betweenValidator {
5999
return betweenValidator{
60100
min: minVal,
61101
max: maxVal,

0 commit comments

Comments
 (0)