Skip to content
Notifications
Clear all

Anyone else having the 'helpful' regex that breaks existing input validation?

10 Posts
10 Users
0 Reactions
37 Views
(@gracew23)
Reputable Member
Joined: 4 months ago
Posts: 281
Topic starter   [#27884]

Just had a 'helpful' assistant 'improve' a regex for a Canadian postal code field. It proudly suggested `/^[A-Z]d[A-Z] d[A-Z]d$/i`. Looks fine, right?

It breaks validation for existing lowercase user entries and now incorrectly allows lowercase letters in the middle character positions because of the case-insensitive flag. The space is also now optional? The correct pattern is `/^[A-Z]d[A-Z] d[A-Z]d$/`. This isn't an improvement; it's a regression that weakens input validation.

Seen similar? Where a 'fix' for one edge case silently breaks the core validation logic. GW


Trust, but audit.


   
Quote
(@consulting_contractor_mike)
Honorable Member
Joined: 8 months ago
Posts: 393
 

This exact scenario happens more often than you'd think. The case-insensitive flag introduction is particularly insidious because it appears to solve the "uppercase requirement" problem at first glance, but it actually destroys the positional validation you need. The middle alphabetic character must be uppercase specifically to avoid confusion with numbers.

I've seen similar breaks with phone number regexes where someone adds flexibility for international prefixes, but in doing so, makes the area code optional for a system that only serves a domestic audience. The 'fix' introduces a new class of invalid numbers that pass validation.

Your point about the space is key - that's likely a typo in their suggestion where a backslash was dropped, changing `s` to just `s`. It shows the suggestion wasn't context-aware of your original pattern's strictness.


Mike


   
ReplyQuote
(@clarak2)
Estimable Member
Joined: 4 months ago
Posts: 143
 

Oh yeah, the lowercase-in-the-middle issue is a perfect trap. The spec is clear that position needs to be a letter *specifically* to avoid being a digit, but a case-insensitive flag quietly defeats that.

I've had this happen with VAT number validation too. Someone "helpfully" added word boundaries to be more precise, but it broke the batch validation script that checked lines from a CSV, because the boundaries didn't account for newlines. Fixed one edge case, broke the main workflow 😅

Always test the "bad" inputs after a regex change, not just the good ones.


Docs save time


   
ReplyQuote
(@harperj)
Honorable Member
Joined: 4 months ago
Posts: 610
 

That's a great catch on both issues. The space character problem is almost certainly a dropped backslash, turning s into just s. I see this happen when regex gets copied through rich text editors or chat interfaces that strip special characters.

But your main point about the case-insensitive flag is crucial. It's a textbook example of fixing the symptom (uppercase requirement) while breaking the actual rule. The spec needs that middle character to be definitively *not a digit*, which the uppercase class enforces. The flag makes the regex accept things the spec explicitly forbids.

Always makes me run the invalid cases through the unit tests twice after any "helpful" regex edit.


Keep it constructive.


   
ReplyQuote
(@ci_cd_junkie)
Honorable Member
Joined: 8 months ago
Posts: 476
 

Ugh, that `/i` flag "improvement" is so classic. It's treating the letter class as purely about case, ignoring that `[A-Z]` is often deliberately chosen to *exclude* digits.

I ran into something like this with Jenkins pipeline input validation. Someone "fixed" a version regex to be more permissive with `d+` for patch numbers, but it started accepting leading zeros on major versions, which our downstream artifact promotion logic treated as octal. Broke the entire release train for a day.

Your case is worse because the flag change *looks* benign. Makes you wonder if the assistant even tried the regex against invalid test cases like "A1a 1A1" or just patched the obvious "uppercase required" complaint.


pipeline all the things


   
ReplyQuote
(@billyp)
Reputable Member
Joined: 4 months ago
Posts: 284
 

Oh man, the Canadian postal code one gets me every time. That exact regex "fix" is like a rite of passage.

Your point about breaking existing lowercase entries is spot on - it's not just weakening validation, it's actively invalidating previously accepted, user-entered data. I've seen forms silently start rejecting stored addresses after a "helpful" update like this.

The space thing is weird. Maybe they were going for s and the slash got eaten? Either way, making the space optional is definitely wrong for that format.


Always A/B test.


   
ReplyQuote
(@alexgarcia)
Honorable Member
Joined: 4 months ago
Posts: 496
 

You're absolutely right about the silent rejection of stored data, that's often the worst part. It doesn't just create future validation holes, it can corrupt a historical dataset if someone runs a "cleanup" script using the new broken pattern.

I've seen a billing system update do exactly that, flagging previously valid customer postal codes as "format errors" and holding up invoices. The fallout took weeks to untangle because nobody connected the validation "improvement" to the sudden data quality alerts.



   
ReplyQuote
(@fionah)
Reputable Member
Joined: 4 months ago
Posts: 302
 

The case-insensitive flag is the most common regex "fix" made by people who don't understand what they're actually validating. They see it as a convenience feature for users, ignoring that `[A-Z]` is often a deliberate choice to exclude digits zero through nine.

Your space observation is the giveaway. They probably typed `s` and the backslash got stripped somewhere. It shows they didn't actually test the pattern, just eyeballed it. A single test with "A1A 1A1" and "A1A1A1" would've caught it.

This is why I never let an automated "improvement" pass without seeing the failing test cases it's supposed to fix.


trust but verify


   
ReplyQuote
(@auditor_abby)
Reputable Member
Joined: 7 months ago
Posts: 363
 

You're dead on about testing. The backslash stripping is a classic data integrity failure that should flag the entire suggestion for review. I've seen this exact pattern get copied from a compliance document into a production validation rule, turning a required space into a literal 's'.

Your point about eyeballing versus testing is the core of the audit failure. Any regex change, especially from an untrusted source, needs invalid test cases in the commit. If the diff doesn't show the specific broken cases it's meant to reject, I reject the change.


Where is your SOC 2?


   
ReplyQuote
(@gracem)
Reputable Member
Joined: 4 months ago
Posts: 294
 

Yeah, the phone number example hits home. We once had a validation regex for US numbers that someone "improved" to accept a leading plus, but they made the whole country code optional. Suddenly it was passing numbers like +15551234, which our SMS gateway would reject because the leading plus without a proper country code was ambiguous. Took us a while to trace the failed notifications back to that "helpful" edit.

It's that same pattern of solving for one perceived rigidity while ignoring the underlying constraint.


Automate everything.


   
ReplyQuote