@@ -7,31 +7,29 @@ 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.List = sizeBetweenValidator {}
18+ var _ function.ListParameterValidator = sizeBetweenValidator {}
1619
17- // sizeBetweenValidator validates that list contains at least min elements
18- // and at most max elements.
1920type sizeBetweenValidator struct {
2021 min int
2122 max int
2223}
2324
24- // Description describes the validation in plain text formatting.
2525func (v sizeBetweenValidator ) Description (_ context.Context ) string {
2626 return fmt .Sprintf ("list must contain at least %d elements and at most %d elements" , v .min , v .max )
2727}
2828
29- // MarkdownDescription describes the validation in Markdown formatting.
3029func (v sizeBetweenValidator ) MarkdownDescription (ctx context.Context ) string {
3130 return v .Description (ctx )
3231}
3332
34- // Validate performs the validation.
3533func (v sizeBetweenValidator ) ValidateList (ctx context.Context , req validator.ListRequest , resp * validator.ListResponse ) {
3634 if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
3735 return
@@ -48,14 +46,30 @@ func (v sizeBetweenValidator) ValidateList(ctx context.Context, req validator.Li
4846 }
4947}
5048
49+ func (v sizeBetweenValidator ) ValidateParameterList (ctx context.Context , req function.ListParameterValidatorRequest , resp * function.ListParameterValidatorResponse ) {
50+ if req .Value .IsNull () || req .Value .IsUnknown () {
51+ return
52+ }
53+
54+ elems := req .Value .Elements ()
55+
56+ if len (elems ) < v .min || len (elems ) > v .max {
57+ resp .Error = validatorfuncerr .InvalidParameterValueFuncError (
58+ req .ArgumentPosition ,
59+ v .Description (ctx ),
60+ fmt .Sprintf ("%d" , len (elems )),
61+ )
62+ }
63+ }
64+
5165// SizeBetween returns an AttributeValidator which ensures that any configured
52- // attribute value:
66+ // attribute or function parameter value:
5367//
5468// - Is a List.
5569// - Contains at least min elements and at most max elements.
5670//
5771// Null (unconfigured) and unknown (known after apply) values are skipped.
58- func SizeBetween (minVal , maxVal int ) validator. List {
72+ func SizeBetween (minVal , maxVal int ) sizeBetweenValidator {
5973 return sizeBetweenValidator {
6074 min : minVal ,
6175 max : maxVal ,
0 commit comments