Skip to content
This repository was archived by the owner on May 27, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/protoc_gen_validate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def string_template(option_value, name):
{%- endif -%}
{%- if s['pattern'] %}
if re.search(r\'{{ s['pattern'] }}\', {{ name }}) is None:
raise ValidationFailed(\"{{ name }} pattern does not match {{ s['pattern'] }}\")
raise ValidationFailed(\"{{ name }} pattern does not match \" + r\'{{ s['pattern'] }}\')
{%- endif -%}
{%- if s['prefix'] %}
if not {{ name }}.startswith(\"{{ s['prefix'] }}\"):
Expand Down
2 changes: 1 addition & 1 deletion templates/java/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (fns javaFuncs) javaStringEscape(s string) string {
s = s[1 : len(s)-1]
s = strings.ReplaceAll(s, `\u00`, `\x`)
s = strings.ReplaceAll(s, `\x`, `\\x`)
// s = strings.ReplaceAll(s, `\`, `\\`)
s = strings.ReplaceAll(s, `\`, `\\`)
s = strings.ReplaceAll(s, `"`, `\"`)
return `"` + s + `"`
}
Expand Down
2 changes: 2 additions & 0 deletions tests/harness/cases/strings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ message StringMinMaxBytes { string val = 1 [(validate.rules).string = {min_by
message StringEqualMinMaxBytes { string val = 1 [(validate.rules).string = {min_bytes: 4, max_bytes: 8}]; }
message StringPattern { string val = 1 [(validate.rules).string.pattern = "(?i)^[a-z0-9]+$"]; }
message StringPatternEscapes { string val = 1 [(validate.rules).string.pattern = "\\* \\\\ \\w"]; }
message StringPatternQuote { string val = 1 [(validate.rules).string.pattern = "^[^\"]*$"]; }
message StringPatternHex { string val = 1 [(validate.rules).string.pattern = "^\\x41+$"]; }
message StringPrefix { string val = 1 [(validate.rules).string.prefix = "foo"]; }
message StringContains { string val = 1 [(validate.rules).string.contains = "bar"]; }
message StringNotContains { string val = 1 [(validate.rules).string.not_contains = "bar"]; }
Expand Down
6 changes: 6 additions & 0 deletions tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,12 @@ var stringCases = []TestCase{
{"string - pattern (escapes) - invalid", &cases.StringPatternEscapes{Val: "invalid"}, 1},
{"string - pattern (escapes) - invalid (empty)", &cases.StringPatternEscapes{Val: ""}, 1},

{"string - pattern (quote) - valid", &cases.StringPatternQuote{Val: "no quotes"}, 0},
{"string - pattern (quote) - invalid", &cases.StringPatternQuote{Val: "has\"quote"}, 1},

{"string - pattern (hex) - valid", &cases.StringPatternHex{Val: "AAA"}, 0},
{"string - pattern (hex) - invalid", &cases.StringPatternHex{Val: "BBB"}, 1},

{"string - prefix - valid", &cases.StringPrefix{Val: "foobar"}, 0},
{"string - prefix - valid (only)", &cases.StringPrefix{Val: "foo"}, 0},
{"string - prefix - invalid", &cases.StringPrefix{Val: "bar"}, 1},
Expand Down