From aacfc0194559a404aac3eefb819a25e1b7ea8a46 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Sun, 27 Aug 2017 09:50:21 -0700 Subject: [PATCH 1/2] helper/validation: Add ValidateListUniqueStrings Add the ValidateListUniqueStrings function, which is a ValidateFunc that ensures a list has no duplicate items in it. It's useful for when a list is needed over a set because order matters, yet the items still need to be unique. --- helper/validation/validation.go | 14 ++++++++++++++ helper/validation/validation_test.go | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/helper/validation/validation.go b/helper/validation/validation.go index 221433b7c..def3ca24f 100644 --- a/helper/validation/validation.go +++ b/helper/validation/validation.go @@ -144,3 +144,17 @@ func ValidateJsonString(v interface{}, k string) (ws []string, errors []error) { } return } + +// ValidateListUniqueStrings is a ValidateFunc that ensures a list has no +// duplicate items in it. It's useful for when a list is needed over a set +// because order matters, yet the items still need to be unique. +func ValidateListUniqueStrings(v interface{}, k string) (ws []string, errors []error) { + for n1, v1 := range v.([]interface{}) { + for n2, v2 := range v.([]interface{}) { + if v1.(string) == v2.(string) && n1 != n2 { + errors = append(errors, fmt.Errorf("%q: duplicate entry - %s", k, v1.(string))) + } + } + } + return +} diff --git a/helper/validation/validation_test.go b/helper/validation/validation_test.go index 0ca847fe6..fccffe805 100644 --- a/helper/validation/validation_test.go +++ b/helper/validation/validation_test.go @@ -166,6 +166,25 @@ func TestValidateJsonString(t *testing.T) { } } +func TestValidateListUniqueStrings(t *testing.T) { + runTestCases(t, []testCase{ + { + val: []interface{}{"foo", "bar"}, + f: ValidateListUniqueStrings, + }, + { + val: []interface{}{"foo", "bar", "foo"}, + f: ValidateListUniqueStrings, + expectedErr: regexp.MustCompile("duplicate entry - foo"), + }, + { + val: []interface{}{"foo", "bar", "foo", "baz", "bar"}, + f: ValidateListUniqueStrings, + expectedErr: regexp.MustCompile("duplicate entry - (?:foo|bar)"), + }, + }) +} + func runTestCases(t *testing.T, cases []testCase) { matchErr := func(errs []error, r *regexp.Regexp) bool { // err must match one provided @@ -185,6 +204,10 @@ func runTestCases(t *testing.T, cases []testCase) { continue } + if len(errs) != 0 && tc.expectedErr == nil { + t.Fatalf("expected test case %d to produce no errors, got %v", i, errs) + } + if !matchErr(errs, tc.expectedErr) { t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs) } From b0152f6be36757c27941375c882840393a4cec93 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Mon, 28 Aug 2017 13:49:11 -0700 Subject: [PATCH 2/2] Add newline to end of validation.go --- helper/validation/validation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/validation/validation.go b/helper/validation/validation.go index 421c91442..8fc30478a 100644 --- a/helper/validation/validation.go +++ b/helper/validation/validation.go @@ -168,4 +168,4 @@ func ValidateRegexp(v interface{}, k string) (ws []string, errors []error) { errors = append(errors, fmt.Errorf("%q: %s", k, err)) } return -} \ No newline at end of file +}