Skip to content

Commit a6939af

Browse files
committed
int32 validator changes
1 parent 08938e0 commit a6939af

File tree

6 files changed

+280
-6
lines changed

6 files changed

+280
-6
lines changed

int32validator/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) ValidateInt32(ctx context.Context, request validator.Int32Request, response *validator.Int32Response) {
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) ValidateInt32(ctx context.Context, request validator.I
4463
}
4564

4665
func (v atLeastValidator) ValidateParameterInt32(ctx context.Context, request function.Int32ParameterValidatorRequest, response *function.Int32ParameterValidatorResponse) {
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

int32validator/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.Int32Unknown().RefineWithUpperBound(2, false),
52+
min: 1,
53+
},
54+
"unknown upper bound exclusive - invalid matches bound": {
55+
val: types.Int32Unknown().RefineWithUpperBound(2, false),
56+
min: 2,
57+
expectError: true,
58+
},
59+
"unknown upper bound exclusive - invalid greater than bound": {
60+
val: types.Int32Unknown().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.Int32Unknown().RefineWithUpperBound(2, true),
67+
min: 1,
68+
},
69+
"unknown upper bound inclusive - valid matches bound": {
70+
val: types.Int32Unknown().RefineWithUpperBound(2, true),
71+
min: 2,
72+
},
73+
"unknown upper bound inclusive - invalid greater than bound": {
74+
val: types.Int32Unknown().RefineWithUpperBound(2, true),
75+
min: 3,
76+
expectError: true,
77+
},
4978
}
5079

5180
for name, test := range tests {

int32validator/at_most.go

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

3232
func (v atMostValidator) ValidateInt32(ctx context.Context, request validator.Int32Request, response *validator.Int32Response) {
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.LowerBoundRefinement(); ok {
39+
if refn.IsInclusive() && refn.LowerBound() > v.max {
40+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
41+
request.Path,
42+
v.Description(ctx),
43+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
44+
))
45+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
46+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
47+
request.Path,
48+
v.Description(ctx),
49+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
50+
))
51+
}
52+
}
3453
return
3554
}
3655

@@ -44,7 +63,26 @@ func (v atMostValidator) ValidateInt32(ctx context.Context, request validator.In
4463
}
4564

4665
func (v atMostValidator) ValidateParameterInt32(ctx context.Context, request function.Int32ParameterValidatorRequest, response *function.Int32ParameterValidatorResponse) {
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.LowerBoundRefinement(); ok {
72+
if refn.IsInclusive() && refn.LowerBound() > v.max {
73+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
74+
request.ArgumentPosition,
75+
v.Description(ctx),
76+
fmt.Sprintf("unknown value that will be at least %d", refn.LowerBound()),
77+
)
78+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
79+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
80+
request.ArgumentPosition,
81+
v.Description(ctx),
82+
fmt.Sprintf("unknown value that will be greater than %d", refn.LowerBound()),
83+
)
84+
}
85+
}
4886
return
4987
}
5088

int32validator/at_most_test.go

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

5180
for name, test := range tests {

int32validator/between.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,42 @@ func (v betweenValidator) ValidateInt32(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) ValidateParameterInt32(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

int32validator/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.Int32Unknown().RefineWithLowerBound(2, false),
75+
min: 1,
76+
max: 1,
77+
expectError: true,
78+
},
79+
"unknown lower bound exclusive - invalid matches bound": {
80+
val: types.Int32Unknown().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.Int32Unknown().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.Int32Unknown().RefineWithLowerBound(2, true),
93+
min: 1,
94+
max: 1,
95+
expectError: true,
96+
},
97+
"unknown lower bound inclusive - valid matches bound": {
98+
val: types.Int32Unknown().RefineWithLowerBound(2, true),
99+
min: 1,
100+
max: 2,
101+
},
102+
"unknown lower bound inclusive - valid greater than bound": {
103+
val: types.Int32Unknown().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.Int32Unknown().RefineWithUpperBound(2, false),
110+
min: 1,
111+
max: 5,
112+
},
113+
"unknown upper bound exclusive - invalid matches bound": {
114+
val: types.Int32Unknown().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.Int32Unknown().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.Int32Unknown().RefineWithUpperBound(2, true),
128+
min: 1,
129+
max: 5,
130+
},
131+
"unknown upper bound inclusive - valid matches bound": {
132+
val: types.Int32Unknown().RefineWithUpperBound(2, true),
133+
min: 2,
134+
max: 5,
135+
},
136+
"unknown upper bound inclusive - invalid greater than bound": {
137+
val: types.Int32Unknown().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)