From 0b335b588220820f6f1eafed335f67ecfce4792a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 27 Apr 2026 15:56:15 +0000 Subject: [PATCH 1/3] Add $allowEmpty parameter to URL validator and update tests Agent-Logs-Url: https://github.com/utopia-php/validators/sessions/cce228f0-a09a-4f4c-83c5-06eebec02fe8 Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com> --- src/Validator/URL.php | 10 +++++++++- tests/Validator/URLTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Validator/URL.php b/src/Validator/URL.php index 1c12008..ceb80f4 100644 --- a/src/Validator/URL.php +++ b/src/Validator/URL.php @@ -15,12 +15,16 @@ class URL extends Validator { protected array $allowedSchemes; + protected bool $allowEmpty; + /** * @param array $allowedSchemes + * @param bool $allowEmpty */ - public function __construct(array $allowedSchemes = []) + public function __construct(array $allowedSchemes = [], bool $allowEmpty = false) { $this->allowedSchemes = $allowedSchemes; + $this->allowEmpty = $allowEmpty; } /** @@ -49,6 +53,10 @@ public function getDescription(): string */ public function isValid($value): bool { + if ($this->allowEmpty && empty($value)) { + return true; + } + if (\filter_var($value, FILTER_VALIDATE_URL) === false) { return false; } diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php index ddf52df..da2f646 100644 --- a/tests/Validator/URLTest.php +++ b/tests/Validator/URLTest.php @@ -52,4 +52,16 @@ public function testIsValidAllowedSchemes(): void $this->assertSame(true, $this->url->isValid('https://example.com')); $this->assertSame(false, $this->url->isValid('gopher://www.example.com')); } + + public function testAllowEmpty(): void + { + $urlAllowEmpty = new URL([], true); + $this->assertSame(true, $urlAllowEmpty->isValid('')); + $this->assertSame(true, $urlAllowEmpty->isValid(null)); + $this->assertSame(true, $urlAllowEmpty->isValid('https://example.com')); + $this->assertSame(false, $urlAllowEmpty->isValid('not-a-url')); + + $this->assertSame(false, $this->url->isValid('')); + $this->assertSame(false, $this->url->isValid(null)); + } } From 14357f73d39e087027be8464acaf2896828e2511 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 27 Apr 2026 15:56:57 +0000 Subject: [PATCH 2/3] Use precise empty check (=== '' or null) instead of empty() in URL validator Agent-Logs-Url: https://github.com/utopia-php/validators/sessions/cce228f0-a09a-4f4c-83c5-06eebec02fe8 Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com> --- src/Validator/URL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/URL.php b/src/Validator/URL.php index ceb80f4..6f6a666 100644 --- a/src/Validator/URL.php +++ b/src/Validator/URL.php @@ -53,7 +53,7 @@ public function getDescription(): string */ public function isValid($value): bool { - if ($this->allowEmpty && empty($value)) { + if ($this->allowEmpty && ($value === '' || $value === null)) { return true; } From c2aa8887fa4be3cc8dc109446836b92f8334bab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 27 Apr 2026 18:03:26 +0200 Subject: [PATCH 3/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matej Bačo --- src/Validator/URL.php | 2 +- tests/Validator/URLTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/URL.php b/src/Validator/URL.php index 6f6a666..bb26568 100644 --- a/src/Validator/URL.php +++ b/src/Validator/URL.php @@ -53,7 +53,7 @@ public function getDescription(): string */ public function isValid($value): bool { - if ($this->allowEmpty && ($value === '' || $value === null)) { + if ($this->allowEmpty && $value === '') { return true; } diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php index da2f646..d7d010e 100644 --- a/tests/Validator/URLTest.php +++ b/tests/Validator/URLTest.php @@ -57,7 +57,7 @@ public function testAllowEmpty(): void { $urlAllowEmpty = new URL([], true); $this->assertSame(true, $urlAllowEmpty->isValid('')); - $this->assertSame(true, $urlAllowEmpty->isValid(null)); + $this->assertSame(false, $urlAllowEmpty->isValid(null)); $this->assertSame(true, $urlAllowEmpty->isValid('https://example.com')); $this->assertSame(false, $urlAllowEmpty->isValid('not-a-url'));