Skip to content

Commit fae337d

Browse files
author
Jordan Hall
committed
Add checks against invalid postcodes mentioned in fzaninotto/Faker#1443
1 parent bf3e41c commit fae337d

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/Utils/Validator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
abstract class Validator
66
{
7+
public static $invalidPostcodes = [
8+
'KT185DN',
9+
'AB154YR',
10+
'B628RS',
11+
];
12+
713
public static function validatePostcode($postcode)
814
{
915
$postcode = strtoupper($postcode);
1016

17+
if (in_array(str_replace(' ', '', $postcode), self::$invalidPostcodes)) {
18+
return false;
19+
}
20+
1121
$regex = '#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$#';
1222

1323
$result = preg_match($regex, $postcode);

tests/Unit/BasicUsageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testValidation()
1919

2020
public function testValidationFailure()
2121
{
22-
$postcodes = ['ST163DPA', 'XF2P90', 'Ollie', 'cake', 'ST16 3DPA'];
22+
$postcodes = ['ST163DPA', 'XF2P90', 'Ollie', 'cake', 'ST16 3DPA', 'KT18 5DN', 'AB15 4YR', 'B62 8RS'];
2323

2424
foreach ($postcodes as $postcode) {
2525
$this->assertFalse(Validator::validatePostcode($postcode));

0 commit comments

Comments
 (0)