Skip to content

Commit 6beacf2

Browse files
committed
float64 validators
1 parent a6939af commit 6beacf2

File tree

6 files changed

+280
-6
lines changed

6 files changed

+280
-6
lines changed

float64validator/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 (validator atLeastValidator) ValidateFloat64(ctx context.Context, request validator.Float64Request, response *validator.Float64Response) {
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() < validator.min {
40+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
41+
request.Path,
42+
validator.Description(ctx),
43+
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
44+
))
45+
} else if !refn.IsInclusive() && refn.UpperBound() <= validator.min {
46+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
47+
request.Path,
48+
validator.Description(ctx),
49+
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
50+
))
51+
}
52+
}
3453
return
3554
}
3655

@@ -46,7 +65,26 @@ func (validator atLeastValidator) ValidateFloat64(ctx context.Context, request v
4665
}
4766

4867
func (validator atLeastValidator) ValidateParameterFloat64(ctx context.Context, request function.Float64ParameterValidatorRequest, response *function.Float64ParameterValidatorResponse) {
49-
if request.Value.IsNull() || request.Value.IsUnknown() {
68+
if request.Value.IsNull() {
69+
return
70+
}
71+
72+
if request.Value.IsUnknown() {
73+
if refn, ok := request.Value.UpperBoundRefinement(); ok {
74+
if refn.IsInclusive() && refn.UpperBound() < validator.min {
75+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
76+
request.ArgumentPosition,
77+
validator.Description(ctx),
78+
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
79+
)
80+
} else if !refn.IsInclusive() && refn.UpperBound() <= validator.min {
81+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
82+
request.ArgumentPosition,
83+
validator.Description(ctx),
84+
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
85+
)
86+
}
87+
}
5088
return
5189
}
5290

float64validator/at_least_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@ func TestAtLeastValidator(t *testing.T) {
5050
min: 0.90,
5151
expectError: true,
5252
},
53+
// Unknown value will be < 2.1
54+
"unknown upper bound exclusive - valid less than bound": {
55+
val: types.Float64Unknown().RefineWithUpperBound(2.1, false),
56+
min: 2,
57+
},
58+
"unknown upper bound exclusive - invalid matches bound": {
59+
val: types.Float64Unknown().RefineWithUpperBound(2.1, false),
60+
min: 2.1,
61+
expectError: true,
62+
},
63+
"unknown upper bound exclusive - invalid greater than bound": {
64+
val: types.Float64Unknown().RefineWithUpperBound(2.1, false),
65+
min: 3,
66+
expectError: true,
67+
},
68+
// Unknown value will be <= 2.1
69+
"unknown upper bound inclusive - valid less than bound": {
70+
val: types.Float64Unknown().RefineWithUpperBound(2.1, true),
71+
min: 2,
72+
},
73+
"unknown upper bound inclusive - valid matches bound": {
74+
val: types.Float64Unknown().RefineWithUpperBound(2.1, true),
75+
min: 2.1,
76+
},
77+
"unknown upper bound inclusive - invalid greater than bound": {
78+
val: types.Float64Unknown().RefineWithUpperBound(2.1, true),
79+
min: 3,
80+
expectError: true,
81+
},
5382
}
5483

5584
for name, test := range tests {

float64validator/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) ValidateFloat64(ctx context.Context, request validator.Float64Request, response *validator.Float64Response) {
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 %f", 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 %f", refn.LowerBound()),
50+
))
51+
}
52+
}
3453
return
3554
}
3655

@@ -46,7 +65,26 @@ func (v atMostValidator) ValidateFloat64(ctx context.Context, request validator.
4665
}
4766

4867
func (v atMostValidator) ValidateParameterFloat64(ctx context.Context, request function.Float64ParameterValidatorRequest, response *function.Float64ParameterValidatorResponse) {
49-
if request.Value.IsNull() || request.Value.IsUnknown() {
68+
if request.Value.IsNull() {
69+
return
70+
}
71+
72+
if request.Value.IsUnknown() {
73+
if refn, ok := request.Value.LowerBoundRefinement(); ok {
74+
if refn.IsInclusive() && refn.LowerBound() > v.max {
75+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
76+
request.ArgumentPosition,
77+
v.Description(ctx),
78+
fmt.Sprintf("unknown value that will be at least %f", refn.LowerBound()),
79+
)
80+
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
81+
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
82+
request.ArgumentPosition,
83+
v.Description(ctx),
84+
fmt.Sprintf("unknown value that will be greater than %f", refn.LowerBound()),
85+
)
86+
}
87+
}
5088
return
5189
}
5290

float64validator/at_most_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@ func TestAtMostValidator(t *testing.T) {
5050
max: 2.00,
5151
expectError: true,
5252
},
53+
// Unknown value will be > 2.1
54+
"unknown lower bound exclusive - invalid less than bound": {
55+
val: types.Float64Unknown().RefineWithLowerBound(2.1, false),
56+
max: 2,
57+
expectError: true,
58+
},
59+
"unknown lower bound exclusive - invalid matches bound": {
60+
val: types.Float64Unknown().RefineWithLowerBound(2.1, false),
61+
max: 2.1,
62+
expectError: true,
63+
},
64+
"unknown lower bound exclusive - valid greater than bound": {
65+
val: types.Float64Unknown().RefineWithLowerBound(2.1, false),
66+
max: 3,
67+
},
68+
// Unknown value will be >= 2.1
69+
"unknown lower bound inclusive - invalid less than bound": {
70+
val: types.Float64Unknown().RefineWithLowerBound(2.1, true),
71+
max: 2,
72+
expectError: true,
73+
},
74+
"unknown lower bound inclusive - valid matches bound": {
75+
val: types.Float64Unknown().RefineWithLowerBound(2.1, true),
76+
max: 2.1,
77+
},
78+
"unknown lower bound inclusive - valid greater than bound": {
79+
val: types.Float64Unknown().RefineWithLowerBound(2.1, true),
80+
max: 3,
81+
},
5382
}
5483

5584
for name, test := range tests {

float64validator/between.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,42 @@ func (v betweenValidator) ValidateFloat64(ctx context.Context, request validator
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 %f", 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 %f", 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 %f", 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 %f", refn.UpperBound()),
83+
))
84+
}
85+
}
5186
return
5287
}
5388

@@ -74,7 +109,42 @@ func (v betweenValidator) ValidateParameterFloat64(ctx context.Context, request
74109
return
75110
}
76111

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

float64validator/between_test.go

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

78148
for name, test := range tests {

0 commit comments

Comments
 (0)