This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from ripienaar/5
(#5) add regex validation
- Loading branch information
Showing
7 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package regex | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
) | ||
|
||
// ValidateString validates that a string matches a regex | ||
func ValidateString(input string, regex string) (bool, error) { | ||
re, err := regexp.Compile(regex) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid regex '%s'", regex) | ||
} | ||
|
||
if !re.MatchString(input) { | ||
return false, fmt.Errorf("input does not match '%s'", regex) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ValidateStructField validates that field holds a string matching the regex | ||
// tag must be in the form `validate:"regex=\d+"` | ||
func ValidateStructField(value reflect.Value, tag string) (bool, error) { | ||
re := regexp.MustCompile(`^regex=(.+)$`) | ||
parts := re.FindStringSubmatch(tag) | ||
|
||
if len(parts) != 2 { | ||
return false, fmt.Errorf("invalid tag '%s', must be in the form regex=^hello.+world$", tag) | ||
} | ||
|
||
if value.Kind() != reflect.String { | ||
return false, fmt.Errorf("only strings can be regex validated") | ||
} | ||
|
||
return ValidateString(value.String(), parts[1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package regex | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFileContent(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validator/Regex") | ||
} | ||
|
||
var _ = Describe("ValidateString", func() { | ||
It("Should match strings correctly", func() { | ||
ok, err := ValidateString("hello world", "world$") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("hello", "world$") | ||
Expect(err).To(MatchError("input does not match 'world$'")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
ok, err = ValidateString("hello", "invalid(") | ||
Expect(err).To(MatchError("invalid regex 'invalid('")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ValidateStructField", func() { | ||
type t struct { | ||
String string `validate:"regex=world$"` | ||
Invalid string `validate:"regex"` | ||
} | ||
|
||
It("Should fail for invalid tags", func() { | ||
st := t{"1", "foo"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("Invalid") | ||
typeField, _ := val.Type().FieldByName("Invalid") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("invalid tag 'regex', must be in the form regex=^hello.+world$")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
|
||
It("Should match the regex correctly", func() { | ||
st := t{"fail", "foo"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("String") | ||
typeField, _ := val.Type().FieldByName("String") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("input does not match 'world$'")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
st.String = "hello world" | ||
val = reflect.ValueOf(st) | ||
valueField = val.FieldByName("String") | ||
|
||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters