@@ -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
1517var _ validator.Int64 = betweenValidator {}
18+ var _ function.Int64ParameterValidator = betweenValidator {}
1619
17- // betweenValidator validates that an integer Attribute's value is in a range.
1820type 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+
2328func (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.
2832func (validator betweenValidator ) MarkdownDescription (ctx context.Context ) string {
2933 return validator .Description (ctx )
3034}
3135
32- // ValidateInt64 performs the validation.
3336func (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