commerceguys / addressing

A PHP addressing library, powered by CLDR and Google's address data.
MIT License
922 stars 135 forks source link

postal_code_pattern missing from US states since v2.0.0-beta1 #212

Closed mglaman closed 10 months ago

mglaman commented 10 months ago

I was helping an old Laravel project and went to update this library. But our tests started to fail:

        yield 'state with bad zipcode' => ['WI', '13177', [
            'error' => true,
            'message' => '13177 is not a valid postal code for WI.',
            'errorTitle' => 'Validation Error'
        ]];

It appears to be due to https://github.com/commerceguys/addressing/commit/516da8b8b949d69380cca20949332a1ba4b7dfcb which removed postal_code_pattern for US subdivisions.

We relied on "postal_code_pattern": "5[34]".

\CommerceGuys\Addressing\Validator\Constraints\AddressFormatConstraintValidator::validatePostalCode still supports subdivision postal code patterns.

bojanz commented 10 months ago

State-level prefix-based postal code patterns were deprecated in 1.x, that's why 2.x removed them. See #147 for background. There's also https://www.drupal.org/project/address/issues/2828128 for the Drupal-side of the discussion.

We still retain state-level patterns that override the entire country pattern, instead of further refining it.

You would need to copy-over the old patterns from 1.x and implement your own logic on top of the library's, if you want to retain the same behavior.

mglaman commented 10 months ago

Ah, bummer. Yeah, I'll just have to write a custom constraint then