fix: support escaped "|" in regex validation rules - #312
Merged
Conversation
Pull Request Test Coverage Report for Build 17577331416Details
💛 - Coveralls |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue with parsing validation rules that contain escaped pipe characters (\|) in regex patterns. The primary purpose is to ensure that escaped pipes within regex validation rules are not treated as rule separators.
Key changes:
- Added a new
splitRulesfunction to correctly parse validation rule strings with escaped pipe characters - Updated the rule parsing logic to use the new function instead of basic string splitting
- Added test coverage to verify the fix works correctly
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| util.go | Adds the new splitRules function that handles escaped pipe characters in rule strings |
| rule.go | Updates StringRule method to use the new splitRules function for proper rule parsing |
| issues_test.go | Adds test case and reorganizes imports to verify the escaped pipe fix |
| @@ -70,6 +70,29 @@ func parseArgString(argStr string) (ss []string) { | |||
| return stringSplit(argStr, ",") | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
The function lacks documentation explaining its purpose, parameters, and behavior with escaped characters. Add a comment documenting that this function splits validation rules on unescaped pipe characters and how it handles escaped pipes.
Suggested change
| // splitRules splits a string of validation rules on unescaped pipe ('|') characters. | |
| // If a pipe character is escaped with a backslash ('\|'), it is treated as part of the rule, | |
| // not as a separator. For example, "min:1|max:10\|with\|pipe|required" will be split into | |
| // ["min:1", "max:10|with|pipe", "required"]. Leading and trailing whitespace is trimmed from each rule. | |
| // Parameter: | |
| // rules: string containing validation rules separated by pipe characters, possibly escaped. | |
| // Returns: | |
| // ss: slice of strings, each representing a rule. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close #301
This PR addresses an issue with parsing validation rules that contain escaped pipe (
|) characters, particularly relevant for regex rules in the validation library. The key changes involve introducing a new function to correctly split rule strings, updating the rule parsing logic to use this function, and adding a test to verify the fix.Rule Parsing Improvements:
splitRulesfunction inutil.goto correctly split rule strings on unescaped pipe (|) characters, ensuring that pipes within regex patterns are not treated as rule separators.StringRulemethod inrule.goto use the newsplitRulesfunction instead of a basic string split, improving support for complex rule definitions.Testing:
TestIssue_301inissues_test.goto verify that regex rules with escaped pipes are parsed and validated correctly.Code Cleanup:
issues_test.gofor clarity and organization.