Skip to content

Commit

Permalink
Fix pattern rule in repeated and map field (#58)
Browse files Browse the repository at this point in the history
* Fix pattern rule in repeated and map field

Add pattern variable declaration when repeated.items, map.keys
or map.values contains string rule with pattern.

* Add test cases to executor
  • Loading branch information
pragkent authored and rodaine committed Feb 21, 2018
1 parent 39f5c47 commit 930a67c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions templates/go/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,23 @@ var _ error = {{ errname . }}{}
var {{ lookup .Field "Pattern" }} = regexp.MustCompile({{ lit .Rules.GetPattern }})
{{ end }}{{ end }}
{{ if has .Rules "Items"}}{{ if .Rules.Items }}
{{ if has .Rules.Items.GetString_ "Pattern" }} {{ if .Rules.Items.GetString_.Pattern }}
var {{ lookup .Field "Pattern" }} = regexp.MustCompile({{ lit .Rules.Items.GetString_.GetPattern }})
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
{{ if has .Rules.Keys.GetString_ "Pattern" }} {{ if .Rules.Keys.GetString_.Pattern }}
var {{ lookup .Field "Pattern" }} = regexp.MustCompile({{ lit .Rules.Keys.GetString_.GetPattern }})
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Values"}}{{ if .Rules.Values }}
{{ if has .Rules.Values.GetString_ "Pattern" }} {{ if .Rules.Values.GetString_.Pattern }}
var {{ lookup .Field "Pattern" }} = regexp.MustCompile({{ lit .Rules.Values.GetString_.GetPattern }})
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ end }}{{ end }}
`
3 changes: 3 additions & 0 deletions tests/harness/cases/maps.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ message MapNoSparse {

message MapKeys { map<sint32, string> val = 1 [(validate.rules).map.keys.sint32.lt = 0]; }
message MapValues { map<string, string> val = 1 [(validate.rules).map.values.string.min_len = 3]; }

message MapKeysPattern { map<string, string> val = 1 [(validate.rules).map.keys.string.pattern = "(?i)^[a-z0-9]+$"]; }
message MapValuesPattern { map<string, string> val = 1 [(validate.rules).map.values.string.pattern = "(?i)^[a-z0-9]+$"]; }
1 change: 1 addition & 0 deletions tests/harness/cases/repeated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ message RepeatedMinMax { repeated sfixed32 val = 1 [(validate.rules).repeated
message RepeatedExact { repeated uint32 val = 1 [(validate.rules).repeated = {min_items: 3, max_items: 3}]; }
message RepeatedUnique { repeated string val = 1 [(validate.rules).repeated.unique = true]; }
message RepeatedItemRule { repeated float val = 1 [(validate.rules).repeated.items.float.gt = 0]; }
message RepeatedItemPattern { repeated string val = 1 [(validate.rules).repeated.items.string.pattern = "(?i)^[a-z0-9]+$"]; }
message RepeatedEmbedSkip { repeated Embed val = 1 [(validate.rules).repeated.items.message.skip = true]; }
6 changes: 6 additions & 0 deletions tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@ var repeatedCases = []TestCase{

{"repeated - items - valid", &cases.RepeatedItemRule{Val: []float32{1, 2, 3}}, true},
{"repeated - items - valid (empty)", &cases.RepeatedItemRule{Val: []float32{}}, true},
{"repeated - items - valid (pattern)", &cases.RepeatedItemPattern{Val: []string{"Alpha", "Beta123"}}, true},
{"repeated - items - invalid", &cases.RepeatedItemRule{Val: []float32{1, -2, 3}}, false},
{"repeated - items - invalid (pattern)", &cases.RepeatedItemPattern{Val: []string{"Alpha", "!@#$%^&*()"}}, false},

{"repeated - embed skip - valid", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: 1}}}, true},
{"repeated - embed skip - valid (invalid element)", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: -1}}}, true},
Expand Down Expand Up @@ -983,11 +985,15 @@ var mapCases = []TestCase{

{"map - keys - valid", &cases.MapKeys{Val: map[int32]string{-1: "a", -2: "b"}}, true},
{"map - keys - valid (empty)", &cases.MapKeys{Val: map[int32]string{}}, true},
{"map - keys - valid (pattern)", &cases.MapKeysPattern{Val: map[string]string{"A": "a"}}, true},
{"map - keys - invalid", &cases.MapKeys{Val: map[int32]string{1: "a"}}, false},
{"map - keys - invalid (pattern)", &cases.MapKeysPattern{Val: map[string]string{"A": "a", "!@#$%^&*()": "b"}}, false},

{"map - values - valid", &cases.MapValues{Val: map[string]string{"a": "Alpha", "b": "Beta"}}, true},
{"map - values - valid (empty)", &cases.MapValues{Val: map[string]string{}}, true},
{"map - values - valid (pattern)", &cases.MapValuesPattern{Val: map[string]string{"a": "A"}}, true},
{"map - values - invalid", &cases.MapValues{Val: map[string]string{"a": "A", "b": "B"}}, false},
{"map - values - invalid (pattern)", &cases.MapValuesPattern{Val: map[string]string{"a": "A", "b": "!@#$%^&*()"}}, false},
}

var oneofCases = []TestCase{
Expand Down

0 comments on commit 930a67c

Please sign in to comment.