Skip to content

fix: support escaped "|" in regex validation rules - #312

Merged
inhere merged 2 commits into
gookit:masterfrom
almas-x:fix-301
Sep 10, 2025
Merged

fix: support escaped "|" in regex validation rules#312
inhere merged 2 commits into
gookit:masterfrom
almas-x:fix-301

Conversation

@almas-x

@almas-x almas-x commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

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:

  • Added a new splitRules function in util.go to correctly split rule strings on unescaped pipe (|) characters, ensuring that pipes within regex patterns are not treated as rule separators.
  • Updated the StringRule method in rule.go to use the new splitRules function instead of a basic string split, improving support for complex rule definitions.

Testing:

  • Added a new test case TestIssue_301 in issues_test.go to verify that regex rules with escaped pipes are parsed and validated correctly.

Code Cleanup:

  • Minor reordering of imports in issues_test.go for clarity and organization.

@coveralls

coveralls commented Sep 8, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 17577331416

Details

  • 15 of 17 (88.24%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.05%) to 96.784%

Changes Missing Coverage Covered Lines Changed/Added Lines %
util.go 14 16 87.5%
Totals Coverage Status
Change from base Build 17552101827: -0.05%
Covered Lines: 2919
Relevant Lines: 3016

💛 - Coveralls

@inhere
inhere requested a review from Copilot September 9, 2025 07:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 splitRules function 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

Comment thread util.go
Comment thread util.go
@@ -70,6 +70,29 @@ func parseArgString(argStr string) (ss []string) {
return stringSplit(argStr, ",")
}

Copilot AI Sep 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@inhere inhere added the bug_fix label Sep 9, 2025
@inhere
inhere merged commit b9e7806 into gookit:master Sep 10, 2025
10 of 11 checks passed
@almas-x
almas-x deleted the fix-301 branch September 10, 2025 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regex pipe escape

4 participants