Skip to content

Commit

Permalink
Merge pull request #172 from denouche/fix/matches
Browse files Browse the repository at this point in the history
Fix/matches
  • Loading branch information
asaskevich committed Dec 27, 2016
2 parents 7b3beb6 + e87863f commit 86842c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var ParamTagMap = map[string]ParamValidator{
var ParamTagRegexMap = map[string]*regexp.Regexp{
"length": regexp.MustCompile("^length\\((\\d+)\\|(\\d+)\\)$"),
"stringlength": regexp.MustCompile("^stringlength\\((\\d+)\\|(\\d+)\\)$"),
"matches": regexp.MustCompile(`matches\(([^)]+)\)`),
"matches": regexp.MustCompile(`^matches\((.+)\)$`),
}

type customTypeTagMap struct {
Expand Down
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func isValidTag(s string) bool {
}
for _, c := range s {
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
case strings.ContainsRune("\\'\"!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
Expand Down
28 changes: 28 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,10 @@ type StringMatchesStruct struct {
StringMatches string `valid:"matches(^[0-9]{3}$)"`
}

type StringMatchesComplexStruct struct {
StringMatches string `valid:"matches(^\\$\\([\"']\\w+[\"']\\)$)"`
}

type Post struct {
Title string `valid:"alpha,required"`
Message string `valid:"ascii"`
Expand Down Expand Up @@ -2087,6 +2091,30 @@ func TestStringMatchesStruct(t *testing.T) {
}
}

func TestStringMatchesComplexStruct(t *testing.T) {
var tests = []struct {
param interface{}
expected bool
}{
{StringMatchesComplexStruct{"$()"}, false},
{StringMatchesComplexStruct{"$('AZERTY')"}, true},
{StringMatchesComplexStruct{`$("AZERTY")`}, true},
{StringMatchesComplexStruct{`$("")`}, false},
{StringMatchesComplexStruct{"AZERTY"}, false},
{StringMatchesComplexStruct{"$AZERTY"}, false},
}

for _, test := range tests {
actual, err := ValidateStruct(test.param)
if actual != test.expected {
t.Errorf("Expected ValidateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err != nil {
t.Errorf("Got Error on ValidateStruct(%q): %s", test.param, err)
}
}
}
}

func TestValidateStruct(t *testing.T) {

var tests = []struct {
Expand Down

0 comments on commit 86842c5

Please sign in to comment.