From 4fa40f6cec5e84f5a41d2b1ef4abb79c982cb14b Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Sat, 8 Mar 2025 12:20:53 +1100 Subject: [PATCH] Allow static validator to run before prompt's validator. --- src/Prompt.php | 4 ++-- tests/Feature/ConfirmPromptTest.php | 2 +- tests/Feature/MultiSearchPromptTest.php | 2 +- tests/Feature/MultiSelectPromptTest.php | 2 +- tests/Feature/PasswordPromptTest.php | 2 +- tests/Feature/SearchPromptTest.php | 2 +- tests/Feature/SelectPromptTest.php | 2 +- tests/Feature/SuggestPromptTest.php | 2 +- tests/Feature/TextPromptTest.php | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Prompt.php b/src/Prompt.php index 324d789..924ed02 100644 --- a/src/Prompt.php +++ b/src/Prompt.php @@ -249,7 +249,7 @@ public static function terminal(): Terminal /** * Set the custom validation callback. */ - public static function validateUsing(Closure $callback): void + public static function validateUsing(?Closure $callback): void { static::$validateUsing = $callback; } @@ -403,8 +403,8 @@ private function validate(mixed $value): void } $error = match (true) { - is_callable($this->validate) => ($this->validate)($value), isset(static::$validateUsing) => (static::$validateUsing)($this), + is_callable($this->validate) => ($this->validate)($value), default => throw new RuntimeException('The validation logic is missing.'), }; diff --git a/tests/Feature/ConfirmPromptTest.php b/tests/Feature/ConfirmPromptTest.php index 4d61fff..641cbcc 100644 --- a/tests/Feature/ConfirmPromptTest.php +++ b/tests/Feature/ConfirmPromptTest.php @@ -145,5 +145,5 @@ Prompt::assertOutputContains('Need to be sure!'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/MultiSearchPromptTest.php b/tests/Feature/MultiSearchPromptTest.php index 0ca4ffc..6e6f0fa 100644 --- a/tests/Feature/MultiSearchPromptTest.php +++ b/tests/Feature/MultiSearchPromptTest.php @@ -354,7 +354,7 @@ function ($value) { Prompt::assertOutputContains('And green?'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); it('supports selecting all options', function () { diff --git a/tests/Feature/MultiSelectPromptTest.php b/tests/Feature/MultiSelectPromptTest.php index 36423a6..c0ee816 100644 --- a/tests/Feature/MultiSelectPromptTest.php +++ b/tests/Feature/MultiSelectPromptTest.php @@ -273,5 +273,5 @@ Prompt::assertOutputContains('And green?'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/PasswordPromptTest.php b/tests/Feature/PasswordPromptTest.php index 277149c..02b76ac 100644 --- a/tests/Feature/PasswordPromptTest.php +++ b/tests/Feature/PasswordPromptTest.php @@ -113,5 +113,5 @@ Prompt::assertOutputContains('Minimum 8 chars!'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/SearchPromptTest.php b/tests/Feature/SearchPromptTest.php index 0a060ca..9eea3b7 100644 --- a/tests/Feature/SearchPromptTest.php +++ b/tests/Feature/SearchPromptTest.php @@ -210,5 +210,5 @@ Prompt::assertOutputContains('Please choose green.'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/SelectPromptTest.php b/tests/Feature/SelectPromptTest.php index 0f52c47..81a30f6 100644 --- a/tests/Feature/SelectPromptTest.php +++ b/tests/Feature/SelectPromptTest.php @@ -364,5 +364,5 @@ Prompt::assertOutputContains('Please choose green.'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/SuggestPromptTest.php b/tests/Feature/SuggestPromptTest.php index da813a7..815eacc 100644 --- a/tests/Feature/SuggestPromptTest.php +++ b/tests/Feature/SuggestPromptTest.php @@ -230,5 +230,5 @@ Prompt::assertOutputContains('Minimum 2 chars!'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); diff --git a/tests/Feature/TextPromptTest.php b/tests/Feature/TextPromptTest.php index dcd7ef1..1eb2bbc 100644 --- a/tests/Feature/TextPromptTest.php +++ b/tests/Feature/TextPromptTest.php @@ -150,7 +150,7 @@ Prompt::assertOutputContains('Minimum 2 chars!'); - Prompt::validateUsing(fn () => null); + Prompt::validateUsing(null); }); it('allows customizing the cancellation', function () {