Skip to content

Commit f416176

Browse files
committed
int64validator: implement parameter interface
1 parent 6c4f6ea commit f416176

17 files changed

+359
-79
lines changed

int32validator/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
// Package int32validator provides validators for types.Int32 attributes.
4+
// Package int32validator provides validators for types.Int32 attributes or function parameters.
55
package int32validator

int64validator/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.Int64 = atLeastValidator{}
18+
var _ function.Int64ParameterValidator = atLeastValidator{}
1619

17-
// atLeastValidator validates that an integer Attribute's value is at least a certain value.
1820
type atLeastValidator struct {
1921
min int64
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-
// ValidateInt64 performs the validation.
3332
func (v atLeastValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
3433
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
3534
return
@@ -44,14 +43,28 @@ func (v atLeastValidator) ValidateInt64(ctx context.Context, request validator.I
4443
}
4544
}
4645

46+
func (v atLeastValidator) ValidateParameterInt64(ctx context.Context, request function.Int64ParameterValidatorRequest, response *function.Int64ParameterValidatorResponse) {
47+
if request.Value.IsNull() || request.Value.IsUnknown() {
48+
return
49+
}
50+
51+
if request.Value.ValueInt64() < v.min {
52+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
53+
request.ArgumentPosition,
54+
v.Description(ctx),
55+
fmt.Sprintf("%d", request.Value.ValueInt64()),
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 64-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 int64) validator.Int64 {
67+
func AtLeast(minVal int64) atLeastValidator {
5568
return atLeastValidator{
5669
min: minVal,
5770
}

int64validator/at_least_example_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package int64validator_test
66
import (
77
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
88
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/function"
910
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1011
)
1112

@@ -23,3 +24,17 @@ func ExampleAtLeast() {
2324
},
2425
}
2526
}
27+
28+
func ExampleAtLeast_function() {
29+
_ = function.Definition{
30+
Parameters: []function.Parameter{
31+
function.Int64Parameter{
32+
Name: "example_param",
33+
Validators: []function.Int64ParameterValidator{
34+
// Validate integer value must be at least 42
35+
int64validator.AtLeast(42),
36+
},
37+
},
38+
},
39+
}
40+
}

int64validator/at_least_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package int64validator_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("ValidateInt64 - %s", name), func(t *testing.T) {
5255
t.Parallel()
5356
request := validator.Int64Request{
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("ValidateParameterInt64 - %s", name), func(t *testing.T) {
74+
t.Parallel()
75+
request := function.Int64ParameterValidatorRequest{
76+
Value: test.val,
77+
}
78+
response := function.Int64ParameterValidatorResponse{}
79+
int64validator.AtLeast(test.min).ValidateParameterInt64(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
}

int64validator/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.Int64 = atMostValidator{}
18+
var _ function.Int64ParameterValidator = atMostValidator{}
1619

17-
// atMostValidator validates that an integer Attribute's value is at most a certain value.
1820
type atMostValidator struct {
1921
max int64
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-
// ValidateInt64 performs the validation.
3332
func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
3433
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
3534
return
@@ -44,14 +43,28 @@ func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.In
4443
}
4544
}
4645

46+
func (v atMostValidator) ValidateParameterInt64(ctx context.Context, request function.Int64ParameterValidatorRequest, response *function.Int64ParameterValidatorResponse) {
47+
if request.Value.IsNull() || request.Value.IsUnknown() {
48+
return
49+
}
50+
51+
if request.Value.ValueInt64() > v.max {
52+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
53+
request.ArgumentPosition,
54+
v.Description(ctx),
55+
fmt.Sprintf("%d", request.Value.ValueInt64()),
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 64-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 int64) validator.Int64 {
67+
func AtMost(maxVal int64) atMostValidator {
5568
return atMostValidator{
5669
max: maxVal,
5770
}

int64validator/at_most_example_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package int64validator_test
66
import (
77
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
88
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/function"
910
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1011
)
1112

@@ -23,3 +24,17 @@ func ExampleAtMost() {
2324
},
2425
}
2526
}
27+
28+
func ExampleAtMost_function() {
29+
_ = function.Definition{
30+
Parameters: []function.Parameter{
31+
function.Int64Parameter{
32+
Name: "example_param",
33+
Validators: []function.Int64ParameterValidator{
34+
// Validate integer value must be at most 42
35+
int64validator.AtMost(42),
36+
},
37+
},
38+
},
39+
}
40+
}

int64validator/at_most_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package int64validator_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("ValidateInt64 - %s", name), func(t *testing.T) {
5255
t.Parallel()
5356
request := validator.Int64Request{
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("ValidateParameterInt64 - %s", name), func(t *testing.T) {
74+
t.Parallel()
75+
request := function.Int64ParameterValidatorRequest{
76+
Value: test.val,
77+
}
78+
response := function.Int64ParameterValidatorResponse{}
79+
int64validator.AtMost(test.max).ValidateParameterInt64(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
}

int64validator/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.Int64 = betweenValidator{}
18+
var _ function.Int64ParameterValidator = betweenValidator{}
1619

17-
// betweenValidator validates that an integer Attribute's value is in a range.
1820
type betweenValidator struct {
1921
min, max int64
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-
// ValidateInt64 performs the validation.
3336
func (v betweenValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
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) ValidateInt64(ctx context.Context, request validator.I
4460
}
4561
}
4662

63+
func (v betweenValidator) ValidateParameterInt64(ctx context.Context, request function.Int64ParameterValidatorRequest, response *function.Int64ParameterValidatorResponse) {
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.ValueInt64() < v.min || request.Value.ValueInt64() > v.max {
80+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
81+
request.ArgumentPosition,
82+
v.Description(ctx),
83+
fmt.Sprintf("%d", request.Value.ValueInt64()),
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 64-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 int64) validator.Int64 {
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 int64) betweenValidator {
5999
return betweenValidator{
60100
min: minVal,
61101
max: maxVal,

0 commit comments

Comments
 (0)