Skip to content

Commit 08938e0

Browse files
committed
add int64 validator updates
1 parent 623f39b commit 08938e0

File tree

5 files changed

+229
-24
lines changed

5 files changed

+229
-24
lines changed

int64validator/at_least.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,26 @@ func (validator atLeastValidator) MarkdownDescription(ctx context.Context) strin
3030
}
3131

3232
func (v atLeastValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
33-
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
33+
if request.ConfigValue.IsNull() {
34+
return
35+
}
36+
37+
if request.ConfigValue.IsUnknown() {
38+
if refn, ok := request.ConfigValue.UpperBoundRefinement(); ok {
39+
if refn.IsInclusive() && refn.UpperBound() < v.min {
40+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
41+
request.Path,
42+
v.Description(ctx),
43+
fmt.Sprintf("unknown value that will be at most %d", refn.UpperBound()),
44+
))
45+
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
46+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
47+
request.Path,
48+
v.Description(ctx),
49+
fmt.Sprintf("unknown value that will be less than %d", refn.UpperBound()),
50+
))
51+
}
52+
}
3453
return
3554
}
3655

@@ -44,7 +63,26 @@ func (v atLeastValidator) ValidateInt64(ctx context.Context, request validator.I
4463
}
4564

4665
func (v atLeastValidator) ValidateParameterInt64(ctx context.Context, request function.Int64ParameterValidatorRequest, response *function.Int64ParameterValidatorResponse) {
47-
if request.Value.IsNull() || request.Value.IsUnknown() {
66+
if request.Value.IsNull() {
67+
return
68+
}
69+
70+
if request.Value.IsUnknown() {
71+
if refn, ok := request.Value.UpperBoundRefinement(); ok {
72+
if refn.IsInclusive() && refn.UpperBound() < v.min {
73+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
74+
request.ArgumentPosition,
75+
v.Description(ctx),
76+
fmt.Sprintf("unknown value that will be at most %d", refn.UpperBound()),
77+
)
78+
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
79+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
80+
request.ArgumentPosition,
81+
v.Description(ctx),
82+
fmt.Sprintf("unknown value that will be less than %d", refn.UpperBound()),
83+
)
84+
}
85+
}
4886
return
4987
}
5088

int64validator/at_least_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ func TestAtLeastValidator(t *testing.T) {
4646
min: 1,
4747
expectError: true,
4848
},
49+
// Unknown value will be < 2
50+
"unknown upper bound exclusive - valid less than bound": {
51+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
52+
min: 1,
53+
},
54+
"unknown upper bound exclusive - invalid matches bound": {
55+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
56+
min: 2,
57+
expectError: true,
58+
},
59+
"unknown upper bound exclusive - invalid greater than bound": {
60+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
61+
min: 3,
62+
expectError: true,
63+
},
64+
// Unknown value will be <= 2
65+
"unknown upper bound inclusive - valid less than bound": {
66+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
67+
min: 1,
68+
},
69+
"unknown upper bound inclusive - valid matches bound": {
70+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
71+
min: 2,
72+
},
73+
"unknown upper bound inclusive - invalid greater than bound": {
74+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
75+
min: 3,
76+
expectError: true,
77+
},
4978
}
5079

5180
for name, test := range tests {

int64validator/at_most.go

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

10-
"github.com/hashicorp/terraform-plugin-framework/diag"
1110
"github.com/hashicorp/terraform-plugin-framework/function"
1211
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1312

@@ -36,20 +35,18 @@ func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.In
3635
}
3736

3837
if request.ConfigValue.IsUnknown() {
39-
// Check if there is a lower bound refinement, and if that lower bound indicates the eventual value will be invalid
40-
if lowerRefn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
41-
if lowerRefn.IsInclusive() && lowerRefn.LowerBound() > v.max {
42-
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
38+
if refn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
39+
if refn.IsInclusive() && refn.LowerBound() > v.max {
40+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
4341
request.Path,
44-
"Invalid Attribute Value",
45-
// TODO: improve error messaging?
46-
fmt.Sprintf("Attribute %s %s, got an unknown value that will be greater than: %d", request.Path, v.Description(ctx), lowerRefn.LowerBound()),
42+
v.Description(ctx),
43+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
4744
))
48-
} else if !lowerRefn.IsInclusive() && lowerRefn.LowerBound() >= v.max {
49-
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
45+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
46+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
5047
request.Path,
51-
"Invalid Attribute Value",
52-
fmt.Sprintf("Attribute %s %s, got an unknown value that will be at least: %d", request.Path, v.Description(ctx), lowerRefn.LowerBound()),
48+
v.Description(ctx),
49+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
5350
))
5451
}
5552
}
@@ -71,17 +68,18 @@ func (v atMostValidator) ValidateParameterInt64(ctx context.Context, request fun
7168
}
7269

7370
if request.Value.IsUnknown() {
74-
// Check if there is a lower bound refinement, and if that lower bound indicates the eventual value will be invalid
75-
if lowerRefn, ok := request.Value.LowerBoundRefinement(); ok {
76-
if lowerRefn.IsInclusive() && lowerRefn.LowerBound() > v.max {
77-
response.Error = function.NewArgumentFuncError(
71+
if refn, ok := request.Value.LowerBoundRefinement(); ok {
72+
if refn.IsInclusive() && refn.LowerBound() > v.max {
73+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
7874
request.ArgumentPosition,
79-
fmt.Sprintf("Invalid Parameter Value: %s, got an unknown value that will be greater than: %d", v.Description(ctx), lowerRefn.LowerBound()),
75+
v.Description(ctx),
76+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
8077
)
81-
} else if !lowerRefn.IsInclusive() && lowerRefn.LowerBound() >= v.max {
82-
response.Error = function.NewArgumentFuncError(
78+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
79+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
8380
request.ArgumentPosition,
84-
fmt.Sprintf("Invalid Parameter Value: %s, got an unknown value that will be at least: %d", v.Description(ctx), lowerRefn.LowerBound()),
81+
v.Description(ctx),
82+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
8583
)
8684
}
8785
}

int64validator/between.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,42 @@ func (v betweenValidator) ValidateInt64(ctx context.Context, request validator.I
4747
return
4848
}
4949

50-
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
50+
if request.ConfigValue.IsNull() {
51+
return
52+
}
53+
54+
if request.ConfigValue.IsUnknown() {
55+
if refn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
56+
if refn.IsInclusive() && refn.LowerBound() > v.max {
57+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
58+
request.Path,
59+
v.Description(ctx),
60+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
61+
))
62+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
63+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
64+
request.Path,
65+
v.Description(ctx),
66+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
67+
))
68+
}
69+
}
70+
71+
if refn, ok := request.ConfigValue.UpperBoundRefinement(); ok {
72+
if refn.IsInclusive() && refn.UpperBound() < v.min {
73+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
74+
request.Path,
75+
v.Description(ctx),
76+
fmt.Sprintf("unknown value that will be at most %d", refn.UpperBound()),
77+
))
78+
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
79+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
80+
request.Path,
81+
v.Description(ctx),
82+
fmt.Sprintf("unknown value that will be less than %d", refn.UpperBound()),
83+
))
84+
}
85+
}
5186
return
5287
}
5388

@@ -72,7 +107,42 @@ func (v betweenValidator) ValidateParameterInt64(ctx context.Context, request fu
72107
return
73108
}
74109

75-
if request.Value.IsNull() || request.Value.IsUnknown() {
110+
if request.Value.IsNull() {
111+
return
112+
}
113+
114+
if request.Value.IsUnknown() {
115+
if refn, ok := request.Value.LowerBoundRefinement(); ok {
116+
if refn.IsInclusive() && refn.LowerBound() > v.max {
117+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
118+
request.ArgumentPosition,
119+
v.Description(ctx),
120+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
121+
)
122+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
123+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
124+
request.ArgumentPosition,
125+
v.Description(ctx),
126+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
127+
)
128+
}
129+
}
130+
131+
if refn, ok := request.Value.UpperBoundRefinement(); ok {
132+
if refn.IsInclusive() && refn.UpperBound() < v.min {
133+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
134+
request.ArgumentPosition,
135+
v.Description(ctx),
136+
fmt.Sprintf("unknown value that will be at most %d", refn.UpperBound()),
137+
)
138+
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
139+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
140+
request.ArgumentPosition,
141+
v.Description(ctx),
142+
fmt.Sprintf("unknown value that will be less than %d", refn.UpperBound()),
143+
)
144+
}
145+
}
76146
return
77147
}
78148

int64validator/between_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,76 @@ func TestBetweenValidator(t *testing.T) {
6969
max: 1,
7070
expectError: true,
7171
},
72+
// Unknown value will be > 2
73+
"unknown lower bound exclusive - invalid less than bound": {
74+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
75+
min: 1,
76+
max: 1,
77+
expectError: true,
78+
},
79+
"unknown lower bound exclusive - invalid matches bound": {
80+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
81+
min: 1,
82+
max: 2,
83+
expectError: true,
84+
},
85+
"unknown lower bound exclusive - valid greater than bound": {
86+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
87+
min: 1,
88+
max: 3,
89+
},
90+
// Unknown value will be >= 2
91+
"unknown lower bound inclusive - invalid less than bound": {
92+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
93+
min: 1,
94+
max: 1,
95+
expectError: true,
96+
},
97+
"unknown lower bound inclusive - valid matches bound": {
98+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
99+
min: 1,
100+
max: 2,
101+
},
102+
"unknown lower bound inclusive - valid greater than bound": {
103+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
104+
min: 1,
105+
max: 3,
106+
},
107+
// Unknown value will be < 2
108+
"unknown upper bound exclusive - valid less than bound": {
109+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
110+
min: 1,
111+
max: 5,
112+
},
113+
"unknown upper bound exclusive - invalid matches bound": {
114+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
115+
min: 2,
116+
max: 5,
117+
expectError: true,
118+
},
119+
"unknown upper bound exclusive - invalid greater than bound": {
120+
val: types.Int64Unknown().RefineWithUpperBound(2, false),
121+
min: 3,
122+
max: 5,
123+
expectError: true,
124+
},
125+
// Unknown value will be <= 2
126+
"unknown upper bound inclusive - valid less than bound": {
127+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
128+
min: 1,
129+
max: 5,
130+
},
131+
"unknown upper bound inclusive - valid matches bound": {
132+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
133+
min: 2,
134+
max: 5,
135+
},
136+
"unknown upper bound inclusive - invalid greater than bound": {
137+
val: types.Int64Unknown().RefineWithUpperBound(2, true),
138+
min: 3,
139+
max: 5,
140+
expectError: true,
141+
},
72142
}
73143

74144
for name, test := range tests {

0 commit comments

Comments
 (0)