From e33aa9dc2e8015d1dcba0bdabf95813ddec95828 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Mon, 27 Nov 2017 17:13:25 -0800 Subject: [PATCH 1/2] helper/validation: Add StringMatch StringMatch returns a validation function that can be used to match a string against a regular expression. This can be used for simple substring validations or more complex validation scenarios. Optionally, an error message can be returned so that the user is returned a better error message other than that their field did not match a regular expression that they might not be able to understand. --- helper/validation/validation.go | 22 ++++++++++++++++++++++ helper/validation/validation_test.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/helper/validation/validation.go b/helper/validation/validation.go index 2ac1028cc1..65a453026f 100644 --- a/helper/validation/validation.go +++ b/helper/validation/validation.go @@ -106,6 +106,28 @@ func StringLenBetween(min, max int) schema.SchemaValidateFunc { } } +// StringMatch returns a SchemaValidateFunc which tests if the provided value +// matches a given regexp, which must compile or else the function will panic. +// Optionally an error message can be provided to return something friendlier +// than "must match some globby regexp". +func StringMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc { + return func(i interface{}, k string) ([]string, []error) { + v, ok := i.(string) + if !ok { + return nil, []error{fmt.Errorf("expected type of %s to be string", k)} + } + + if ok := r.MatchString(v); !ok { + if message != "" { + return nil, []error{fmt.Errorf("invalid value for %s (%s)", k, message)} + + } + return nil, []error{fmt.Errorf("expected value of %s to match regular expression %q", k, r)} + } + return nil, nil + } +} + // NoZeroValues is a SchemaValidateFunc which tests if the provided value is // not a zero value. It's useful in situations where you want to catch // explicit zero values on things like required fields during validation. diff --git a/helper/validation/validation_test.go b/helper/validation/validation_test.go index 4ed22a2e90..cd68fa18d1 100644 --- a/helper/validation/validation_test.go +++ b/helper/validation/validation_test.go @@ -111,6 +111,25 @@ func TestValidationStringInSlice(t *testing.T) { }) } +func TestValidationStringMatch(t *testing.T) { + runTestCases(t, []testCase{ + { + val: "foobar", + f: StringMatch(regexp.MustCompile(".*foo.*"), ""), + }, + { + val: "bar", + f: StringMatch(regexp.MustCompile(".*foo.*"), ""), + expectedErr: regexp.MustCompile("expected value of [\\w]+ to match regular expression " + regexp.QuoteMeta(`".*foo.*"`)), + }, + { + val: "bar", + f: StringMatch(regexp.MustCompile(".*foo.*"), "value must contain foo"), + expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must contain foo\\)"), + }, + }) +} + func TestValidationRegexp(t *testing.T) { runTestCases(t, []testCase{ { From 5da62a44f403614814fb415166e1a2f8c0053649 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Fri, 1 Dec 2017 10:48:25 -0800 Subject: [PATCH 2/2] helper/validation: Fix comment on StringMatch To match the current implementation. --- helper/validation/validation.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/helper/validation/validation.go b/helper/validation/validation.go index 65a453026f..b929163157 100644 --- a/helper/validation/validation.go +++ b/helper/validation/validation.go @@ -107,9 +107,8 @@ func StringLenBetween(min, max int) schema.SchemaValidateFunc { } // StringMatch returns a SchemaValidateFunc which tests if the provided value -// matches a given regexp, which must compile or else the function will panic. -// Optionally an error message can be provided to return something friendlier -// than "must match some globby regexp". +// matches a given regexp. Optionally an error message can be provided to +// return something friendlier than "must match some globby regexp". func StringMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc { return func(i interface{}, k string) ([]string, []error) { v, ok := i.(string)