Skip to content

[13.x] Add opt-in Artisan command input validation#59088

Open
pushpak1300 wants to merge 2 commits intolaravel:13.xfrom
pushpak1300:codex/13-command-input-validation
Open

[13.x] Add opt-in Artisan command input validation#59088
pushpak1300 wants to merge 2 commits intolaravel:13.xfrom
pushpak1300:codex/13-command-input-validation

Conversation

@pushpak1300
Copy link
Member

This change introduces opt-in input validation for Artisan commands on Laravel 13, so command authors can validate arguments and options using Laravel validation rules before command logic runs.

Usage

class SendReport extends Command
{
    protected $signature = 'report:send {email?} {--queue=}';

    protected function rules(): array
    {
        return [
            'email' => 'required|email',
            'arguments.email' => 'required|email',
            'options.queue' => 'in:default,high',
            '--queue' => 'in:default,high',
        ];
    }
}

If validation fails, Artisan prints validation errors and exits with code 1, without executing the command handler.

Approach

  • Added an opt-in validation lifecycle for commands that define rules.
  • Added command hooks for rules, messages, attributes, and validationData.
  • Validation data supports plain keys, arguments.*, options.*, and --option aliases.
  • Kept behavior unchanged for commands that do not define rules.
  • Added tests for invalid and valid paths, namespaced keys, dashed option aliases, key collisions, custom messages/attributes, and missing validator binding behavior.

How this accounts for prior attempt feedback

  • No new console validation contract/interface was added; this is trait-based and minimal.
  • Behavior is consistent for CLI and Artisan::call() (validation errors are surfaced and command fails).
  • Option key semantics from previous discussion are handled by supporting both plain option keys and --option aliases.
  • Argument/option name collisions are addressed via namespaced keys (arguments.*, options.*) plus deterministic merged behavior.
  • No silent fallback when validation services are unavailable; missing validator binding is surfaced explicitly.
  • Commands without rules are unaffected (fully backward-compatible default path).

No breaking changes expected.

@pushpak1300 pushpak1300 marked this pull request as ready for review March 5, 2026 06:51
@laravel laravel deleted a comment from github-actions bot Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant