Skip to content

Commit f0aff3e

Browse files
authored
Merge pull request #12 from suth/master
Fixes 500 status when an address field is an unexpected array
2 parents e27b327 + 68a1cd8 commit f0aff3e

8 files changed

+103
-3
lines changed

src/Support/Validation/Rules/AdministrativeAreaCodeRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public function __construct(Country $country)
2727
*/
2828
public function passes($attribute, $value) : bool
2929
{
30-
return null !== $this->country->administrativeArea($value);
30+
if (!is_string($value)) {
31+
return false;
32+
}
33+
34+
return null !== $this->country->administrativeArea($value);
3135
}
3236

3337
/**

src/Support/Validation/Rules/AdministrativeAreaNameRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public function __construct(Country $country)
2727
*/
2828
public function passes($attribute, $value) : bool
2929
{
30-
return null !== $this->country->administrativeAreaByName($value);
30+
if (!is_string($value)) {
31+
return false;
32+
}
33+
34+
return null !== $this->country->administrativeAreaByName($value);
3135
}
3236

3337
/**

src/Support/Validation/Rules/CountryCodeRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function __construct(LaravelAddressing $addressing)
2727
*/
2828
public function passes($attribute, $value) : bool
2929
{
30+
if (!is_string($value)) {
31+
return false;
32+
}
33+
3034
return null !== $this->addressing->country($value);
3135
}
3236

src/Support/Validation/Rules/CountryNameRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function __construct(LaravelAddressing $addressing)
2727
*/
2828
public function passes($attribute, $value) : bool
2929
{
30+
if (!is_string($value)) {
31+
return false;
32+
}
33+
3034
return null !== $this->addressing->countryByName($value);
3135
}
3236

src/Support/Validation/Rules/PostalCodeRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public function __construct(Country $country, Subdivision $administrative_area =
3636
*/
3737
public function passes($attribute, $value) : bool
3838
{
39-
// If we don't have a pattern for this country/area, automatically pass
39+
if (!is_string($value)) {
40+
return false;
41+
}
42+
43+
// If we don't have a pattern for this country/area, automatically pass
4044
if (!$pattern = $this->pattern()) {
4145
return true;
4246
}

tests/Validator/AdministrativeAreaValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ public function testPassesIfCountryHasNoAdminAreas()
4949
]));
5050
}
5151

52+
public function testAdminAreaCodeArrayIsInvalid()
53+
{
54+
$this->assertFalse($this->performValidation([
55+
'data' => ['country' => 'US', 'state' => ['CO']],
56+
'rules' => ['state' => 'administrative_area_code:country'],
57+
]));
58+
}
59+
60+
public function testAdminAreaNameArrayIsInvalid()
61+
{
62+
$this->assertFalse($this->performValidation([
63+
'data' => ['country' => 'US', 'state' => ['Colorado']],
64+
'rules' => ['state' => 'administrative_area_name:country'],
65+
]));
66+
}
67+
68+
public function testGeneralAdminAreaArrayIsInvalid()
69+
{
70+
$this->assertFalse($this->performValidation([
71+
'data' => ['country' => 'US', 'state' => ['CO']],
72+
'rules' => ['state' => 'administrative_area:country'],
73+
]));
74+
}
75+
5276
public function testUSStateByName()
5377
{
5478
// Valid state in US

tests/Validator/CountryValidatorTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function testCountryCodeWithCorrectSizeButInvalid()
2525
]));
2626
}
2727

28+
public function testCountryCodeArrayIsInvalid()
29+
{
30+
$this->assertFalse($this->performValidation([
31+
'data' => ['country' => ['US']],
32+
'rules' => ['country' => 'country_code'],
33+
]));
34+
}
35+
2836
public function testCorrectCountryCode()
2937
{
3038
$this->assertTrue($this->performValidation([
@@ -45,11 +53,51 @@ public function testWrongCountryName()
4553
]));
4654
}
4755

56+
public function testCountryNameArrayIsInvalid()
57+
{
58+
$this->assertFalse($this->performValidation([
59+
'data' => ['country' => ['United States']],
60+
'rules' => ['country' => 'country_name'],
61+
]));
62+
}
63+
4864
public function testCorrectCountryName()
4965
{
5066
$this->assertTrue($this->performValidation([
5167
'data' => ['country' => 'United States'],
5268
'rules' => ['country' => 'country_name'],
5369
]));
5470
}
71+
72+
public function testGeneralCountryValidation()
73+
{
74+
// Valid country code
75+
$this->assertTrue($this->performValidation([
76+
'data' => ['country' => 'US'],
77+
'rules' => ['country' => 'country'],
78+
]));
79+
// Invalid country code
80+
$this->assertFalse($this->performValidation([
81+
'data' => ['country' => 'ZZ'],
82+
'rules' => ['country' => 'country'],
83+
]));
84+
// Valid country using its name
85+
$this->assertTrue($this->performValidation([
86+
'data' => ['country' => 'United States'],
87+
'rules' => ['country' => 'country'],
88+
]));
89+
// Invalid country using its name
90+
$this->assertFalse($this->performValidation([
91+
'data' => ['country' => 'United Stattes'],
92+
'rules' => ['country' => 'country'],
93+
]));
94+
}
95+
96+
public function testGeneralCountryArrayIsInvalid()
97+
{
98+
$this->assertFalse($this->performValidation([
99+
'data' => ['country' => ['United States']],
100+
'rules' => ['country' => 'country'],
101+
]));
102+
}
55103
}

tests/Validator/PostalCodeValidatorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function testColoradoPostalCode()
2525
]));
2626
}
2727

28+
public function testArrayPostalCodeInvalid()
29+
{
30+
$this->assertFalse($this->performValidation([
31+
'data' => ['country' => 'US', 'state' => 'CO', 'code' => ['80301']],
32+
'rules' => ['code' => 'postal_code:country,state'],
33+
]));
34+
}
35+
2836
public function testBrazilianPostalCodes()
2937
{
3038
$this->assertTrue($this->performValidation([

0 commit comments

Comments
 (0)