Skip to content

all: Add support for unknown value refinement data to all validators #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
42 changes: 40 additions & 2 deletions float32validator/at_least.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,26 @@ func (validator atLeastValidator) MarkdownDescription(ctx context.Context) strin
}

func (validator atLeastValidator) ValidateFloat32(ctx context.Context, request validator.Float32Request, response *validator.Float32Response) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
if request.ConfigValue.IsNull() {
return
}

if request.ConfigValue.IsUnknown() {
if refn, ok := request.ConfigValue.UpperBoundRefinement(); ok {
if refn.IsInclusive() && refn.UpperBound() < validator.min {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
validator.Description(ctx),
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
))
} else if !refn.IsInclusive() && refn.UpperBound() <= validator.min {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
validator.Description(ctx),
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
))
}
}
return
}

Expand All @@ -46,7 +65,26 @@ func (validator atLeastValidator) ValidateFloat32(ctx context.Context, request v
}

func (validator atLeastValidator) ValidateParameterFloat32(ctx context.Context, request function.Float32ParameterValidatorRequest, response *function.Float32ParameterValidatorResponse) {
if request.Value.IsNull() || request.Value.IsUnknown() {
if request.Value.IsNull() {
return
}

if request.Value.IsUnknown() {
if refn, ok := request.Value.UpperBoundRefinement(); ok {
if refn.IsInclusive() && refn.UpperBound() < validator.min {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
validator.Description(ctx),
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
)
} else if !refn.IsInclusive() && refn.UpperBound() <= validator.min {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
validator.Description(ctx),
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
)
}
}
return
}

Expand Down
29 changes: 29 additions & 0 deletions float32validator/at_least_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ func TestAtLeastValidator(t *testing.T) {
min: 0.90,
expectError: true,
},
// Unknown value will be < 2.1
"unknown upper bound exclusive - valid less than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 2,
},
"unknown upper bound exclusive - invalid matches bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 2.1,
expectError: true,
},
"unknown upper bound exclusive - invalid greater than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 3,
expectError: true,
},
// Unknown value will be <= 2.1
"unknown upper bound inclusive - valid less than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 2,
},
"unknown upper bound inclusive - valid matches bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 2.1,
},
"unknown upper bound inclusive - invalid greater than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 3,
expectError: true,
},
}

for name, test := range tests {
Expand Down
42 changes: 40 additions & 2 deletions float32validator/at_most.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,26 @@ func (validator atMostValidator) MarkdownDescription(ctx context.Context) string
}

func (v atMostValidator) ValidateFloat32(ctx context.Context, request validator.Float32Request, response *validator.Float32Response) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
if request.ConfigValue.IsNull() {
return
}

if request.ConfigValue.IsUnknown() {
if refn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
if refn.IsInclusive() && refn.LowerBound() > v.max {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at least %f", refn.LowerBound()),
))
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be greater than %f", refn.LowerBound()),
))
}
}
return
}

Expand All @@ -46,7 +65,26 @@ func (v atMostValidator) ValidateFloat32(ctx context.Context, request validator.
}

func (v atMostValidator) ValidateParameterFloat32(ctx context.Context, request function.Float32ParameterValidatorRequest, response *function.Float32ParameterValidatorResponse) {
if request.Value.IsNull() || request.Value.IsUnknown() {
if request.Value.IsNull() {
return
}

if request.Value.IsUnknown() {
if refn, ok := request.Value.LowerBoundRefinement(); ok {
if refn.IsInclusive() && refn.LowerBound() > v.max {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at least %f", refn.LowerBound()),
)
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be greater than %f", refn.LowerBound()),
)
}
}
return
}

Expand Down
29 changes: 29 additions & 0 deletions float32validator/at_most_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ func TestAtMostValidator(t *testing.T) {
max: 2.00,
expectError: true,
},
// Unknown value will be > 2.1
"unknown lower bound exclusive - invalid less than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
max: 2,
expectError: true,
},
"unknown lower bound exclusive - invalid matches bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
max: 2.1,
expectError: true,
},
"unknown lower bound exclusive - valid greater than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
max: 3,
},
// Unknown value will be >= 2.1
"unknown lower bound inclusive - invalid less than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
max: 2,
expectError: true,
},
"unknown lower bound inclusive - valid matches bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
max: 2.1,
},
"unknown lower bound inclusive - valid greater than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
max: 3,
},
}

for name, test := range tests {
Expand Down
74 changes: 72 additions & 2 deletions float32validator/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,42 @@ func (v betweenValidator) ValidateFloat32(ctx context.Context, request validator
return
}

if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
if request.ConfigValue.IsNull() {
return
}

if request.ConfigValue.IsUnknown() {
if refn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
if refn.IsInclusive() && refn.LowerBound() > v.max {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at least %f", refn.LowerBound()),
))
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be greater than %f", refn.LowerBound()),
))
}
}

if refn, ok := request.ConfigValue.UpperBoundRefinement(); ok {
if refn.IsInclusive() && refn.UpperBound() < v.min {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
))
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
))
}
}
return
}

Expand All @@ -74,7 +109,42 @@ func (v betweenValidator) ValidateParameterFloat32(ctx context.Context, request
return
}

if request.Value.IsNull() || request.Value.IsUnknown() {
if request.Value.IsNull() {
return
}

if request.Value.IsUnknown() {
if refn, ok := request.Value.LowerBoundRefinement(); ok {
if refn.IsInclusive() && refn.LowerBound() > v.max {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at least %f", refn.LowerBound()),
)
} else if !refn.IsInclusive() && refn.LowerBound() >= v.max {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be greater than %f", refn.LowerBound()),
)
}
}

if refn, ok := request.Value.UpperBoundRefinement(); ok {
if refn.IsInclusive() && refn.UpperBound() < v.min {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be at most %f", refn.UpperBound()),
)
} else if !refn.IsInclusive() && refn.UpperBound() <= v.min {
response.Error = validatorfuncerr.InvalidParameterValueFuncError(
request.ArgumentPosition,
v.Description(ctx),
fmt.Sprintf("unknown value that will be less than %f", refn.UpperBound()),
)
}
}
return
}

Expand Down
70 changes: 70 additions & 0 deletions float32validator/between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,76 @@ func TestBetweenValidator(t *testing.T) {
max: 3.10,
expectError: true,
},
// Unknown value will be > 2.1
"unknown lower bound exclusive - invalid less than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
min: 1,
max: 1,
expectError: true,
},
"unknown lower bound exclusive - invalid matches bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
min: 1,
max: 2.1,
expectError: true,
},
"unknown lower bound exclusive - valid greater than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, false),
min: 1,
max: 3,
},
// Unknown value will be >= 2.1
"unknown lower bound inclusive - invalid less than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
min: 1,
max: 1,
expectError: true,
},
"unknown lower bound inclusive - valid matches bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
min: 1,
max: 2.1,
},
"unknown lower bound inclusive - valid greater than bound": {
val: types.Float32Unknown().RefineWithLowerBound(2.1, true),
min: 1,
max: 3,
},
// Unknown value will be < 2.1
"unknown upper bound exclusive - valid less than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 1,
max: 5,
},
"unknown upper bound exclusive - invalid matches bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 2.1,
max: 5,
expectError: true,
},
"unknown upper bound exclusive - invalid greater than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, false),
min: 3,
max: 5,
expectError: true,
},
// Unknown value will be <= 2.1
"unknown upper bound inclusive - valid less than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 1,
max: 5,
},
"unknown upper bound inclusive - valid matches bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 2.1,
max: 5,
},
"unknown upper bound inclusive - invalid greater than bound": {
val: types.Float32Unknown().RefineWithUpperBound(2.1, true),
min: 3,
max: 5,
expectError: true,
},
}

for name, test := range tests {
Expand Down
Loading
Loading