Commit 28e61e8e727 for woocommerce
commit 28e61e8e72790d7ae34eff4783cf11e262293b48
Author: Raluca Stan <ralucastn@gmail.com>
Date: Tue Jun 9 18:32:57 2026 +0200
Use public postcode-validator API in checkout postcode check (#65382)
* Use public postcode-validator API in checkout postcode check
Replace the import of the internal `POSTCODE_REGEXES` map from
`postcode-validator/lib/cjs/postcode-regexes.js` with the package's
public `postcodeValidator` and `postcodeValidatorExistsForCountry`
functions. WooCommerce's custom country regex overrides are checked
first, preserving prior behavior.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Add tests for unknown-country fallback in isPostcode
Pin the contract that isPostcode returns true when the country code is
in neither the custom regex map nor the upstream postcode-validator
library. Guards against a regression where the existence check is
dropped and postcodeValidator is called directly, which would throw on
unrecognised country codes and break address validation at checkout.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/use-public-postcode-validator-api b/plugins/woocommerce/changelog/use-public-postcode-validator-api
new file mode 100644
index 00000000000..492602c26dd
--- /dev/null
+++ b/plugins/woocommerce/changelog/use-public-postcode-validator-api
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Use the public postcode-validator API instead of reaching into the package's internal CJS regex map.
diff --git a/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/is-postcode.ts b/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/is-postcode.ts
index 2ab21d238b9..b50e97be32b 100644
--- a/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/is-postcode.ts
+++ b/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/is-postcode.ts
@@ -1,7 +1,10 @@
/**
* External dependencies
*/
-import { POSTCODE_REGEXES } from 'postcode-validator/lib/cjs/postcode-regexes.js';
+import {
+ postcodeValidator,
+ postcodeValidatorExistsForCountry,
+} from 'postcode-validator';
const CUSTOM_REGEXES = new Map< string, RegExp >( [
[ 'BA', /^([7-8]{1})([0-9]{4})$/ ],
@@ -19,21 +22,22 @@ const CUSTOM_REGEXES = new Map< string, RegExp >( [
[ 'SI', /^([1-9][0-9]{3})$/ ],
] );
-const DEFAULT_REGEXES = new Map< string, RegExp >( [
- ...POSTCODE_REGEXES,
- ...CUSTOM_REGEXES,
-] );
-
export interface IsPostcodeProps {
postcode: string;
country: string;
}
const isPostcode = ( { postcode, country }: IsPostcodeProps ): boolean => {
- // If the country is not in the list of regexes, trying to test it would result in an error, so we skip and assume
+ const customRegex = CUSTOM_REGEXES.get( country );
+ if ( customRegex ) {
+ return customRegex.test( postcode );
+ }
+ // If the country is not in the upstream list, trying to validate it would throw, so we skip and assume
// that it is valid.
- const postcodeTest = DEFAULT_REGEXES.get( country )?.test( postcode );
- return typeof postcodeTest !== 'undefined' ? postcodeTest : true;
+ if ( postcodeValidatorExistsForCountry( country ) ) {
+ return postcodeValidator( postcode, country );
+ }
+ return true;
};
export default isPostcode;
diff --git a/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/test/is-postcode.ts b/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/test/is-postcode.ts
index 53372b9a689..a8049816505 100644
--- a/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/test/is-postcode.ts
+++ b/plugins/woocommerce/client/blocks/packages/checkout/utils/validation/test/is-postcode.ts
@@ -189,6 +189,11 @@ describe( 'isPostcode', () => {
[ true, '123456', 'TW' ],
[ true, '12345', 'TW' ],
[ true, '123', 'TW' ],
+
+ // Unknown country codes — assumed valid since no regex applies.
+ [ true, '12345', 'XX' ],
+ [ true, 'anything', 'ZZ' ],
+ [ true, '', 'XX' ],
];
test.each( cases )( '%s: %s for %s', ( result, postcode, country ) =>