-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stricter regex for validating FR, DE, RO postal codes #16
Conversation
Updated the postal code validation regex to enforce the specific structure of French postal codes. The new regex ensures that the first two digits fall within the valid ranges (01–95 for metropolitan areas and 96–98 for overseas territories), while still allowing any valid 5-digit code. This improves accuracy over the previous general 5-digit validation, which allowed invalid codes.
Updated the postal code validation logic to enforce the specific structure of German postal codes using a more precise regular expression. This regex ensures that only valid postal codes are accepted, adhering to the defined formats for various regions in Germany. This change improves data integrity and prevents invalid postal code entries.
Updated the postal code validation logic to enforce the specific structure of German postal codes using a more precise regular expression. This regex ensures that only valid postal codes are accepted, adhering to the defined formats for various regions in Germany. This change improves data integrity and prevents invalid postal code entries. |
…heck - Replaced the basic regex `/^[0-9]{6}$/` which only validated a 6-digit format. - Introduced a new regex that ensures the first two digits match valid Romanian regional codes (01-92). - The updated regex now checks both the length (6 digits) and that the postal code belongs to a valid Romanian region. - This improves accuracy by ensuring only valid postal codes for Romanian regions are accepted.
Update Romanian postal code validation with regional checksIn this PR, the validation for Romanian postal codes has been improved. Previously, the validation used a simple regex ( Changes made:
The new validation not only checks the format but also enforces a higher level of data integrity by matching against actual regional codes. This update is important for any application that handles Romanian addresses or postal information. |
Looks cool, i'm not the maintainer of this repo so i have no say in this but i would maybe also add the new format's and regional codes to the corresponding tests in the test files. Also those that should not pass anymore now. Also maybe check some added newlines that are not in the other format classes. Looking forward to seeing this released 👍 |
Hi @johnnyxlemonade, thanks for your PR! I agree with @rubentebogt, we need to update tests ( Can you please add those? |
src/Formatter/FRFormatter.php
Outdated
public function format(string $postcode) : ?string | ||
{ | ||
if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) { | ||
|
||
if (preg_match('/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/', $postcode) !== 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid mixing \d
with ranges, I think it hurts readability. Let's keep [0-9]
everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this perspective, as even if the given numbers are not in a valid range, using [0-9] enhances clarity.
- Updated the invalid postcode test cases to correctly reflect the expected output after formatting. - Ensured that postcodes with spaces and dashes are processed to remove those characters before validation. - Added test cases for both valid and invalid postcodes according to the specified regex pattern.
I overlooked one condition
Hi BenMorel, Thank you for your message! I’ve added and updated the tests in XXFormatterTest to ensure that formats that were erroneously accepted before are now rejected. I apologize for the complications during the testing process; there were quite a few unnecessary commits because I overlooked a condition in the regular expression. Thanks for your understanding! |
['010101', null], | ||
['000000', null], | ||
['(123456)', null], | ||
['!23456', null], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The data passed to CountryPostcodeFormatter::format()
can only contain uppercase alphanumeric characters, so testing with other characters is redundant here.
(we may revisit this in #17 though!)
public function format(string $postcode) : ?string | ||
{ | ||
|
||
if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you removed the updated regex for FR
by mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, nitpick: please try to keep formatting intact (not adding newlines), we don't have a CS fixer on this project yet, so let's try to keep it tidy manually!
Updated the postal code validation regex to enforce the specific structure of French postal codes. The new regex ensures that the first two digits fall within the valid ranges (01–95 for metropolitan areas and 96–98 for overseas territories), while still allowing any valid 5-digit code. This improves accuracy over the previous general 5-digit validation, which allowed invalid codes.