laravel-postal-code-validation: InvalidArgumentException: Unsupported country code {countryCode}

If one are using this package to validate postal codes, then there is a good chance, that one is using a country code validation package like for example the laravel-validation-rules/country-codes package

Problem 1: Invalid country code

If one is sending two fields like this:

use LVR\CountryCode\Two;

Validator::make([
    'country' => 'XX',//invliad country
    'postal_code' => '12345789',
], [
    'country' => ['required', new Two],
    'postal_code' => 'required|postal_code:country'
]);

Then the validation rule is throwing an InvalidArgumentException exception since the country XX is unknown. The problem is that this is already handled by the new Two rule, so it’s unfortunate that the user gets a HTTP 500 error.

Problem 2: Unsupported country code

Let’s assume that a country YY exists, but is not supported by this package.

If one is sending two fields like this:

use LVR\CountryCode\Two;

Validator::make([
    'country' => 'YY',//assume a valid country that is just not supported by this package
    'postal_code' => '12345789',
], [
    'country' => ['required', new Two],
    'postal_code' => 'required|postal_code:country'
]);

Then the validation rule is throwing an InvalidArgumentException exception since the country YY is unknown to the package. There might be use cases where would like a logic that is more like only fail for countries supported by the package and assume valid for unsupported countries.

Suggestion 1: Don’t throw exceptions

I suggest that instead of throwing an InvalidArgumentException the we simply return false. That way validation will fail, but the customer will see that the

Suggestion 2: Add a new rule only for supported countries

If we add a new rule postal_code_only_supported:country which returns false for invalid postal codes in supported countries and true for valid postal codes in supported countries + true for any postalcode in unsupported countries.

With this new rule and the existing rule, then we can support all use cases.

What do you think @axlon?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

Making the main rule work the way you’re suggesting would not be backwards compatible with previous versions and I want to avoid unnecessarily creating a new major.

The lastest version works as such:

  • Providing an invalid country code like postal_code:XX will fail validation, end-users will be presented with the validation.postal_code error message
  • Providing an invalid country code from request such as postal_code:XX where XX does not contain a valid country will also result in validation failure

I plan on releasing a new minor which also allows validation against incorrect/unsupported countries via a modifier or secondary rule. This includes some extra work because I have to account for a lot of different scenario’s

Regarding the exception, I’ve given it a lot of thought and I’ve decided to remove it:

  • it’s currently a undocumented feature
  • it breaks end user experience
  • it isn’t the job of the postal code validation to validate the country code

The idea behind problem 1 is that, were a dev to add an incorrect country code on accident, it’d be noticed immediately. In hindsight however it seems that “feature” doesn’t really play well with the “country code from request” feature which was added later. This should definitely be addressed!

As for problem 2, I very much like your suggestion, but I’m not completely sold on the syntax yet.