Skip to content
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

Add 'StringNotInSlice' validation function #341

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions helper/validation/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
}
}

// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and does not match the value of any element in the invalid slice
// will test with in lower case if ignoreCase is true
func StringNotInSlice(invalid []string, ignoreCase bool) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return warnings, errors
}

for _, str := range invalid {
if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) {
errors = append(errors, fmt.Errorf("expected %s to not be any of %v, got %s", k, invalid, v))
return warnings, errors
}
}

return warnings, errors
}
}

// StringDoesNotContainAny returns a SchemaValidateFunc which validates that the
// provided value does not contain any of the specified Unicode code points in chars.
func StringDoesNotContainAny(chars string) schema.SchemaValidateFunc {
Expand Down
30 changes: 30 additions & 0 deletions helper/validation/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,36 @@ func TestValidationStringInSlice(t *testing.T) {
})
}

func TestValidationStringNotInSlice(t *testing.T) {
runTestCases(t, []testCase{
{
val: "ValidValue",
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
},
// ignore case
{
val: "VALIDVALUE",
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true),
},
{
val: "AnotherInvalidValue",
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got AnotherInvalidValue"),
},
// ignore case
{
val: "INVALIDVALUE",
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true),
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got INVALIDVALUE"),
},
{
val: 1,
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
},
})
}

func TestValidationStringMatch(t *testing.T) {
runTestCases(t, []testCase{
{
Expand Down