Skip to content

Commit

Permalink
helper/validation: StringInSlice: Single quote slice elements
Browse files Browse the repository at this point in the history
The validation function's error is ambiguous when dealing with slices
containing spaces. For example:
```
expected role_name to be one of [Apache Spark Administrator Synapse Credential User Synapse Administrator]
```

By changing the format verb, the error becomes more user friendly:
```
expected role_name to be one of ["Apache Spark Administrator" "Synapse Credential User" "Synapse Administrator"]
```

This fixes #464

Signed-off-by: Frank Villaro-Dixon <frank.villarodixon@merkle.com>
  • Loading branch information
Frankkkkk committed Aug 29, 2023
1 parent 3a5e2de commit 3f94215
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion helper/validation/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
}
}

errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v))
errors = append(errors, fmt.Errorf("expected %s to be one of %q, got %s", k, valid, v))
return warnings, errors
}
}
Expand Down
4 changes: 2 additions & 2 deletions helper/validation/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ func TestValidationStringInSlice(t *testing.T) {
{
val: "VALIDVALUE",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got VALIDVALUE`),
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \["ValidValue" "AnotherValidValue"\], got VALIDVALUE`),
},
{
val: "InvalidValue",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got InvalidValue`),
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \["ValidValue" "AnotherValidValue"\], got InvalidValue`),
},
{
val: 1,
Expand Down

0 comments on commit 3f94215

Please sign in to comment.