Skip to content

Allow static validator to run before prompt's validator. #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that $validateUsing property already allows to be set to NULL.

{
static::$validateUsing = $callback;
}
Expand Down Expand Up @@ -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.'),
};

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ConfirmPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@

Prompt::assertOutputContains('Need to be sure!');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/MultiSearchPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function ($value) {

Prompt::assertOutputContains('And green?');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});

it('supports selecting all options', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/MultiSelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@

Prompt::assertOutputContains('And green?');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/PasswordPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@

Prompt::assertOutputContains('Minimum 8 chars!');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/SearchPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,5 @@

Prompt::assertOutputContains('Please choose green.');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/SelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,5 @@

Prompt::assertOutputContains('Please choose green.');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/SuggestPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,5 @@

Prompt::assertOutputContains('Minimum 2 chars!');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});
2 changes: 1 addition & 1 deletion tests/Feature/TextPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@

Prompt::assertOutputContains('Minimum 2 chars!');

Prompt::validateUsing(fn () => null);
Prompt::validateUsing(null);
});

it('allows customizing the cancellation', function () {
Expand Down