mwitkow / go-proto-validators

Generate message validators from .proto annotations.
Apache License 2.0
1.08k stars 164 forks source link

Bad Code Generated #97

Open lordnynex opened 3 years ago

lordnynex commented 3 years ago

When a regex contains backticks, they are not properly escaped.

The following regex for validating email addresses will re-produce this issue

"^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
ak89224 commented 3 years ago

I also faced the same issue and looked the code to find that, As, for code generation for regex compilation raw string is used here and It’s not possible to include a backtick in a raw string literal. So, just remove the backtick from the regex expression. Use this one as a workaround : "^[a-zA-Z0-9.!#$%&'*+/=?^_{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"

@mwitkow