Skip to content

Commit

Permalink
fix: use numeric comparison to check monotonicity (coder#8436)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek authored Jul 11, 2023
1 parent d8d8eb2 commit bc835db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 16 additions & 4 deletions codersdk/richparameters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codersdk

import (
"strconv"

"golang.org/x/xerrors"

"github.com/coder/terraform-provider-coder/provider"
Expand Down Expand Up @@ -47,14 +49,24 @@ func ValidateWorkspaceBuildParameter(richParameter TemplateVersionParameter, bui
}

if lastBuildParameter != nil && richParameter.Type == "number" && len(richParameter.ValidationMonotonic) > 0 {
prev, err := strconv.Atoi(lastBuildParameter.Value)
if err != nil {
return xerrors.Errorf("Previous parameter value is not a number: %s", lastBuildParameter.Value)
}

current, err := strconv.Atoi(buildParameter.Value)
if err != nil {
return xerrors.Errorf("Current parameter value is not a number: %s", buildParameter.Value)
}

switch richParameter.ValidationMonotonic {
case MonotonicOrderIncreasing:
if lastBuildParameter.Value > buildParameter.Value {
return xerrors.Errorf("Parameter value must be equal or greater than previous value: %s", lastBuildParameter.Value)
if prev > current {
return xerrors.Errorf("Parameter value must be equal or greater than previous value: %d", prev)
}
case MonotonicOrderDecreasing:
if lastBuildParameter.Value < buildParameter.Value {
return xerrors.Errorf("Parameter value must be equal or lower than previous value: %s", lastBuildParameter.Value)
if prev < current {
return xerrors.Errorf("Parameter value must be equal or lower than previous value: %d", prev)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions codersdk/richparameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ func TestRichParameterValidation(t *testing.T) {

monotonicIncreasingNumberRichParameters := []codersdk.TemplateVersionParameter{
{Name: stringParameterName, Type: "string", Mutable: true},
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(10)), ValidationMonotonic: "increasing"},
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "increasing"},
{Name: boolParameterName, Type: "bool", Mutable: true},
}

monotonicDecreasingNumberRichParameters := []codersdk.TemplateVersionParameter{
{Name: stringParameterName, Type: "string", Mutable: true},
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(10)), ValidationMonotonic: "decreasing"},
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "decreasing"},
{Name: boolParameterName, Type: "bool", Mutable: true},
}

Expand Down Expand Up @@ -255,10 +255,14 @@ func TestRichParameterValidation(t *testing.T) {
{numberParameterName, "6", false, monotonicIncreasingNumberRichParameters},
{numberParameterName, "7", true, monotonicIncreasingNumberRichParameters},
{numberParameterName, "8", true, monotonicIncreasingNumberRichParameters},
{numberParameterName, "11", true, monotonicIncreasingNumberRichParameters},
{numberParameterName, "53", true, monotonicIncreasingNumberRichParameters},

{numberParameterName, "6", true, monotonicDecreasingNumberRichParameters},
{numberParameterName, "7", true, monotonicDecreasingNumberRichParameters},
{numberParameterName, "8", false, monotonicDecreasingNumberRichParameters},
{numberParameterName, "11", false, monotonicDecreasingNumberRichParameters},
{numberParameterName, "53", false, monotonicDecreasingNumberRichParameters},

{stringParameterName, "", true, stringRichParameters},
{stringParameterName, "foobar", true, stringRichParameters},
Expand Down

0 comments on commit bc835db

Please sign in to comment.