Skip to content

Commit a6f673c

Browse files
authored
Merge pull request #341 from ewbankkit/validate-string-not-in-slice
Add 'StringNotInSlice' validation function
2 parents 4807673 + b5f3b42 commit a6f673c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

helper/validation/strings.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
148148
}
149149
}
150150

151+
// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
152+
// is of type string and does not match the value of any element in the invalid slice
153+
// will test with in lower case if ignoreCase is true
154+
func StringNotInSlice(invalid []string, ignoreCase bool) schema.SchemaValidateFunc {
155+
return func(i interface{}, k string) (warnings []string, errors []error) {
156+
v, ok := i.(string)
157+
if !ok {
158+
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
159+
return warnings, errors
160+
}
161+
162+
for _, str := range invalid {
163+
if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) {
164+
errors = append(errors, fmt.Errorf("expected %s to not be any of %v, got %s", k, invalid, v))
165+
return warnings, errors
166+
}
167+
}
168+
169+
return warnings, errors
170+
}
171+
}
172+
151173
// StringDoesNotContainAny returns a SchemaValidateFunc which validates that the
152174
// provided value does not contain any of the specified Unicode code points in chars.
153175
func StringDoesNotContainAny(chars string) schema.SchemaValidateFunc {

helper/validation/strings_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,36 @@ func TestValidationStringInSlice(t *testing.T) {
305305
})
306306
}
307307

308+
func TestValidationStringNotInSlice(t *testing.T) {
309+
runTestCases(t, []testCase{
310+
{
311+
val: "ValidValue",
312+
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
313+
},
314+
// ignore case
315+
{
316+
val: "VALIDVALUE",
317+
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true),
318+
},
319+
{
320+
val: "AnotherInvalidValue",
321+
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
322+
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got AnotherInvalidValue"),
323+
},
324+
// ignore case
325+
{
326+
val: "INVALIDVALUE",
327+
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true),
328+
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got INVALIDVALUE"),
329+
},
330+
{
331+
val: 1,
332+
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
333+
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
334+
},
335+
})
336+
}
337+
308338
func TestValidationStringMatch(t *testing.T) {
309339
runTestCases(t, []testCase{
310340
{

0 commit comments

Comments
 (0)