Skip to content
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

Closed
wants to merge 10 commits into from

Conversation

johnnyxlemonade
Copy link

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 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.
@johnnyxlemonade johnnyxlemonade changed the title Add stricter regex for validating French postal codes Add stricter regex for validating French and Germany postal codes Sep 28, 2024
@johnnyxlemonade
Copy link
Author

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.
@johnnyxlemonade
Copy link
Author

Update Romanian postal code validation with regional checks

In this PR, the validation for Romanian postal codes has been improved. Previously, the validation used a simple regex (/^[0-9]{6}$/) that only ensured the postal code was exactly six digits long. However, this approach did not take into account the actual regional structure of postal codes in Romania.

Changes made:

  • New Regex: The updated regex now checks that the first two digits of the postal code correspond to valid regional codes (01-92). This ensures that only postal codes from recognized Romanian regions are accepted.
  • Why: This change enhances the accuracy of postal code validation, preventing the acceptance of invalid codes that do not match real Romanian locations.

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.

@johnnyxlemonade johnnyxlemonade changed the title Add stricter regex for validating French and Germany postal codes Add stricter regex for validating FR, DE, RO postal codes Sep 29, 2024
@rubentebogt
Copy link
Contributor

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 👍

@BenMorel
Copy link
Member

BenMorel commented Oct 8, 2024

Hi @johnnyxlemonade, thanks for your PR!

I agree with @rubentebogt, we need to update tests (XXFormatterTest, where XX is the country code) to ensure that, at the very least, formats that were erroneously accepted before, are now rejected.

Can you please add those?

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) {
Copy link
Member

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.

Copy link
Author

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
@johnnyxlemonade
Copy link
Author

Hi @johnnyxlemonade, thanks for your PR!

I agree with @rubentebogt, we need to update tests (XXFormatterTest, where XX is the country code) to ensure that, at the very least, formats that were erroneously accepted before, are now rejected.

Can you please add those?

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],
Copy link
Member

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) {
Copy link
Member

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?

Copy link
Member

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!

@johnnyxlemonade johnnyxlemonade closed this by deleting the head repository Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants