Skip to content

Commit

Permalink
feat: add validation for list(string) (#114)
Browse files Browse the repository at this point in the history
* feat: add validation for list(string)

* fix
  • Loading branch information
mtojek committed Mar 15, 2023
1 parent 23a23a2 commit 6fe7fc2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ func (v *Validation) Valid(typ, value string) error {
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
}
case "list(string)":
var listOfStrings []string
err := json.Unmarshal([]byte(value), &listOfStrings)
if err != nil {
return fmt.Errorf("value %q is not valid list of strings", value)
}
}
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,19 @@ func TestValueValidatesType(t *testing.T) {
Min: 0,
Max: 2,
Monotonic: "decreasing",
}, {
Name: "ValidListOfStrings",
Type: "list(string)",
Value: `["first","second","third"]`,
}, {
Name: "InvalidListOfStrings",
Type: "list(string)",
Value: `["first","second","third"`,
Error: regexp.MustCompile("is not valid list of strings"),
}, {
Name: "EmptyListOfStrings",
Type: "list(string)",
Value: `[]`,
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand All @@ -529,6 +542,8 @@ func TestValueValidatesType(t *testing.T) {
if tc.Error != nil {
require.Error(t, err)
require.True(t, tc.Error.MatchString(err.Error()), "got: %s", err.Error())
} else {
require.NoError(t, err)
}
})
}
Expand Down

0 comments on commit 6fe7fc2

Please sign in to comment.