Skip to content

Commit 0ddd14e

Browse files
committed
set validators
1 parent ca4f8dc commit 0ddd14e

File tree

6 files changed

+150
-6
lines changed

6 files changed

+150
-6
lines changed

setvalidator/size_at_least.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ func (v sizeAtLeastValidator) MarkdownDescription(ctx context.Context) string {
3030
}
3131

3232
func (v sizeAtLeastValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
33-
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
33+
if req.ConfigValue.IsNull() {
34+
return
35+
}
36+
37+
if req.ConfigValue.IsUnknown() {
38+
if refn, ok := req.ConfigValue.LengthUpperBoundRefinement(); ok && refn.UpperBound() < int64(v.min) {
39+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
40+
req.Path,
41+
v.Description(ctx),
42+
fmt.Sprintf("unknown value that will have at most %d elements", refn.UpperBound()),
43+
))
44+
}
3445
return
3546
}
3647

@@ -46,7 +57,18 @@ func (v sizeAtLeastValidator) ValidateSet(ctx context.Context, req validator.Set
4657
}
4758

4859
func (v sizeAtLeastValidator) ValidateParameterSet(ctx context.Context, req function.SetParameterValidatorRequest, resp *function.SetParameterValidatorResponse) {
49-
if req.Value.IsNull() || req.Value.IsUnknown() {
60+
if req.Value.IsNull() {
61+
return
62+
}
63+
64+
if req.Value.IsUnknown() {
65+
if refn, ok := req.Value.LengthUpperBoundRefinement(); ok && refn.UpperBound() < int64(v.min) {
66+
resp.Error = validatorfuncerr.InvalidParameterValueFuncError(
67+
req.ArgumentPosition,
68+
v.Description(ctx),
69+
fmt.Sprintf("unknown value that will have at most %d elements", refn.UpperBound()),
70+
)
71+
}
5072
return
5173
}
5274

setvalidator/size_at_least_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ func TestSizeAtLeastValidator(t *testing.T) {
6565
min: 1,
6666
expectError: true,
6767
},
68+
// Unknown value will have <= 2 elements
69+
"unknown length upper bound - valid less than bound": {
70+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
71+
min: 1,
72+
},
73+
"unknown length upper bound - valid matches bound": {
74+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
75+
min: 2,
76+
},
77+
"unknown length upper bound - invalid greater than bound": {
78+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
79+
min: 3,
80+
expectError: true,
81+
},
6882
}
6983

7084
for name, test := range tests {

setvalidator/size_at_most.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ func (v sizeAtMostValidator) MarkdownDescription(ctx context.Context) string {
3030
}
3131

3232
func (v sizeAtMostValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
33-
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
33+
if req.ConfigValue.IsNull() {
34+
return
35+
}
36+
37+
if req.ConfigValue.IsUnknown() {
38+
if refn, ok := req.ConfigValue.LengthLowerBoundRefinement(); ok && refn.LowerBound() > int64(v.max) {
39+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
40+
req.Path,
41+
v.Description(ctx),
42+
fmt.Sprintf("unknown value that will have at least %d elements", refn.LowerBound()),
43+
))
44+
}
3445
return
3546
}
3647

@@ -46,7 +57,18 @@ func (v sizeAtMostValidator) ValidateSet(ctx context.Context, req validator.SetR
4657
}
4758

4859
func (v sizeAtMostValidator) ValidateParameterSet(ctx context.Context, req function.SetParameterValidatorRequest, resp *function.SetParameterValidatorResponse) {
49-
if req.Value.IsNull() || req.Value.IsUnknown() {
60+
if req.Value.IsNull() {
61+
return
62+
}
63+
64+
if req.Value.IsUnknown() {
65+
if refn, ok := req.Value.LengthLowerBoundRefinement(); ok && refn.LowerBound() > int64(v.max) {
66+
resp.Error = validatorfuncerr.InvalidParameterValueFuncError(
67+
req.ArgumentPosition,
68+
v.Description(ctx),
69+
fmt.Sprintf("unknown value that will have at least %d elements", refn.LowerBound()),
70+
)
71+
}
5072
return
5173
}
5274

setvalidator/size_at_most_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ func TestSizeAtMostValidator(t *testing.T) {
6969
max: 2,
7070
expectError: true,
7171
},
72+
// Unknown value will have >= 2 elements
73+
"unknown length lower bound - invalid less than bound": {
74+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
75+
max: 1,
76+
expectError: true,
77+
},
78+
"unknown length lower bound - valid matches bound": {
79+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
80+
max: 2,
81+
},
82+
"unknown length lower bound - valid greater than bound": {
83+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
84+
max: 3,
85+
},
7286
}
7387

7488
for name, test := range tests {

setvalidator/size_between.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,26 @@ func (v sizeBetweenValidator) MarkdownDescription(ctx context.Context) string {
3131
}
3232

3333
func (v sizeBetweenValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
34-
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
34+
if req.ConfigValue.IsNull() {
35+
return
36+
}
37+
38+
if req.ConfigValue.IsUnknown() {
39+
if refn, ok := req.ConfigValue.LengthLowerBoundRefinement(); ok && refn.LowerBound() > int64(v.max) {
40+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
41+
req.Path,
42+
v.Description(ctx),
43+
fmt.Sprintf("unknown value that will have at least %d elements", refn.LowerBound()),
44+
))
45+
}
46+
47+
if refn, ok := req.ConfigValue.LengthUpperBoundRefinement(); ok && refn.UpperBound() < int64(v.min) {
48+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
49+
req.Path,
50+
v.Description(ctx),
51+
fmt.Sprintf("unknown value that will have at most %d elements", refn.UpperBound()),
52+
))
53+
}
3554
return
3655
}
3756

@@ -47,7 +66,26 @@ func (v sizeBetweenValidator) ValidateSet(ctx context.Context, req validator.Set
4766
}
4867

4968
func (v sizeBetweenValidator) ValidateParameterSet(ctx context.Context, req function.SetParameterValidatorRequest, resp *function.SetParameterValidatorResponse) {
50-
if req.Value.IsNull() || req.Value.IsUnknown() {
69+
if req.Value.IsNull() {
70+
return
71+
}
72+
73+
if req.Value.IsUnknown() {
74+
if refn, ok := req.Value.LengthLowerBoundRefinement(); ok && refn.LowerBound() > int64(v.max) {
75+
resp.Error = validatorfuncerr.InvalidParameterValueFuncError(
76+
req.ArgumentPosition,
77+
v.Description(ctx),
78+
fmt.Sprintf("unknown value that will have at least %d elements", refn.LowerBound()),
79+
)
80+
}
81+
82+
if refn, ok := req.Value.LengthUpperBoundRefinement(); ok && refn.UpperBound() < int64(v.min) {
83+
resp.Error = validatorfuncerr.InvalidParameterValueFuncError(
84+
req.ArgumentPosition,
85+
v.Description(ctx),
86+
fmt.Sprintf("unknown value that will have at most %d elements", refn.UpperBound()),
87+
)
88+
}
5189
return
5290
}
5391

setvalidator/size_between_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ func TestSizeBetweenValidator(t *testing.T) {
108108
max: 3,
109109
expectError: true,
110110
},
111+
// Unknown value will have >= 2 elements
112+
"unknown length lower bound - invalid less than bound": {
113+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
114+
min: 1,
115+
max: 1,
116+
expectError: true,
117+
},
118+
"unknown length lower bound - valid matches bound": {
119+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
120+
min: 1,
121+
max: 2,
122+
},
123+
"unknown length lower bound - valid greater than bound": {
124+
val: types.SetUnknown(types.StringType).RefineWithLengthLowerBound(2),
125+
min: 1,
126+
max: 3,
127+
},
128+
// Unknown value will have <= 2 elements
129+
"unknown length upper bound - valid less than bound": {
130+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
131+
min: 1,
132+
max: 5,
133+
},
134+
"unknown length upper bound - valid matches bound": {
135+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
136+
min: 2,
137+
max: 5,
138+
},
139+
"unknown length upper bound - invalid greater than bound": {
140+
val: types.SetUnknown(types.StringType).RefineWithLengthUpperBound(2),
141+
min: 3,
142+
max: 5,
143+
expectError: true,
144+
},
111145
}
112146

113147
for name, test := range tests {

0 commit comments

Comments
 (0)