Skip to content

Commit

Permalink
feat: check validator is applied to a defined column
Browse files Browse the repository at this point in the history
Eg. typo in a column name would make it look like a validator is used
when in reality it was just silenty ignored. Check the validated
column exists in the defined columns.
  • Loading branch information
vesse committed May 14, 2024
1 parent 4198020 commit 100c5d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/recipe/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"regexp"
"slices"
"strings"

"github.com/expr-lang/expr"
Expand Down Expand Up @@ -122,6 +123,10 @@ func (v *Variable) Validate() error {
return fmt.Errorf("%s: validator is defined for column while the variable has not defined any", validatorIndex)
}

if slices.Index(v.Columns, validator.Column) == -1 {
return fmt.Errorf("%s: validator defined for undefined column %q", validatorIndex, validator.Column)
}

found := false
for _, c := range v.Columns {
if c == validator.Column {
Expand Down
11 changes: 11 additions & 0 deletions pkg/recipe/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func TestVariableValidation(t *testing.T) {
},
"`options` and `columns` properties can not be defined",
},
{
"validator defined for undefined column",
Variable{
Name: "foo",
Columns: []string{"foo", "bar"},
Validators: []VariableValidator{
{Column: "cat", Pattern: ".*"},
},
},
"validator defined for undefined column \"cat\"",
},
}

for _, scenario := range scenarios {
Expand Down

0 comments on commit 100c5d6

Please sign in to comment.