Skip to content

Commit 4a00657

Browse files
authored
fix(validation): prevent type errors in rules using preg_match (#1043)
Co-authored-by: Wilfried JOnker <[email protected]>
1 parent 3eb8b35 commit 4a00657

File tree

13 files changed

+54
-0
lines changed

13 files changed

+54
-0
lines changed

src/Tempest/Validation/src/Rules/Alpha.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
public function isValid(mixed $value): bool
1414
{
15+
if (! is_string($value)) {
16+
return false;
17+
}
18+
1519
return boolval(preg_match('/^[A-Za-z]+$/', $value));
1620
}
1721

src/Tempest/Validation/src/Rules/AlphaNumeric.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
public function isValid(mixed $value): bool
1414
{
15+
if (! is_string($value)) {
16+
return false;
17+
}
18+
1519
return boolval(preg_match('/^[A-Za-z0-9]+$/', $value));
1620
}
1721

src/Tempest/Validation/src/Rules/Numeric.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
public function isValid(mixed $value): bool
1414
{
15+
if (! is_string($value)) {
16+
return false;
17+
}
18+
1519
return boolval(preg_match('/^[0-9]+$/', $value));
1620
}
1721

src/Tempest/Validation/src/Rules/RegEx.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function __construct(
1717

1818
public function isValid(mixed $value): bool
1919
{
20+
if (! is_string($value)) {
21+
return false;
22+
}
23+
2024
return preg_match($this->pattern, $value) === 1;
2125
}
2226

src/Tempest/Validation/src/Rules/Time.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function __construct(
1717

1818
public function isValid(mixed $value): bool
1919
{
20+
if (! is_string($value)) {
21+
return false;
22+
}
23+
2024
if ($this->twentyFourHour) {
2125
return preg_match('/^([0-1][0-9]|2[0-3]):?[0-5][0-9]$|^(([0-1]?[0-9]|2[0-3]):[0-5][0-9])$/', $value) === 1;
2226
}

src/Tempest/Validation/src/Rules/Ulid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
public function isValid(mixed $value): bool
1414
{
15+
if (! is_string($value)) {
16+
return false;
17+
}
18+
1519
return preg_match('/^[0-9A-HJKMNP-TV-Z]{26}$/i', $value) === 1;
1620
}
1721

src/Tempest/Validation/src/Rules/Uuid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
{
1313
public function isValid(mixed $value): bool
1414
{
15+
if (! is_string($value)) {
16+
return false;
17+
}
18+
1519
return boolval(preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $value));
1620
}
1721

src/Tempest/Validation/tests/Rules/AlphaNumericTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public function test_alphanumeric(): void
2020
$this->assertFalse($rule->isValid('string_123'));
2121
$this->assertTrue($rule->isValid('string123'));
2222
$this->assertTrue($rule->isValid('STRING123'));
23+
$this->assertFalse($rule->isValid([])); // Should return false, not a TypeError from preg_match
2324
}
2425
}

src/Tempest/Validation/tests/Rules/AlphaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public function test_alpha(): void
2020
$this->assertFalse($rule->isValid('string123'));
2121
$this->assertTrue($rule->isValid('string'));
2222
$this->assertTrue($rule->isValid('STRING'));
23+
$this->assertFalse($rule->isValid([])); // Should return false, not a TypeError from preg_match
2324
}
2425
}

src/Tempest/Validation/tests/Rules/RegexTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ public function test_regex_rule(): void
2828
$this->assertTrue($rule->isValid('AB'));
2929
$this->assertTrue($rule->isValid('Ab'));
3030
}
31+
32+
public function test_non_imvalid_types(): void
33+
{
34+
$rule = new RegEx('/^[0-9]+$/');
35+
36+
// Invalid types should return false, not a TypeError.
37+
$this->assertFalse($rule->isValid(false));
38+
$this->assertFalse($rule->isValid([]));
39+
$this->assertFalse($rule->isValid(new \stdClass()));
40+
$this->assertFalse($rule->isValid(null));
41+
}
3142
}

0 commit comments

Comments
 (0)