diff --git a/templates/go/msg.go b/templates/go/msg.go index dc40c2ebc..bc01ace08 100644 --- a/templates/go/msg.go +++ b/templates/go/msg.go @@ -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 }} ` diff --git a/tests/harness/cases/maps.proto b/tests/harness/cases/maps.proto index 2bc7b2d56..8b76894fe 100644 --- a/tests/harness/cases/maps.proto +++ b/tests/harness/cases/maps.proto @@ -19,3 +19,6 @@ message MapNoSparse { message MapKeys { map val = 1 [(validate.rules).map.keys.sint32.lt = 0]; } message MapValues { map val = 1 [(validate.rules).map.values.string.min_len = 3]; } + +message MapKeysPattern { map val = 1 [(validate.rules).map.keys.string.pattern = "(?i)^[a-z0-9]+$"]; } +message MapValuesPattern { map val = 1 [(validate.rules).map.values.string.pattern = "(?i)^[a-z0-9]+$"]; } diff --git a/tests/harness/cases/repeated.proto b/tests/harness/cases/repeated.proto index e44178963..c80f6ff61 100644 --- a/tests/harness/cases/repeated.proto +++ b/tests/harness/cases/repeated.proto @@ -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]; } diff --git a/tests/harness/executor/cases.go b/tests/harness/executor/cases.go index fe945107f..612265a48 100644 --- a/tests/harness/executor/cases.go +++ b/tests/harness/executor/cases.go @@ -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}, @@ -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{