Validate Console Command Input With the Command Validator Package

The Command Validator package by Andrea Marco Sartori makes validating the input of console commands a cinch using Laravel's beloved Validator. All the Laravel Validator rules you know and love work with this package, along with any custom validation rules. This package integrates with your application's console commands using the provided ValidatesInput trait, which includes an abstract rules() method. The command signature looks like the following: namespace App\Console\Commands; use Illuminate\Console\Command; use Cerbero\CommandValidator\ValidatesInput; class SampleCommand extends Command { use ValidatesInput; protected $signature = 'app:sample {--start-date=}'; // ... public function rules(): array { return ['start-date' => 'date_format:Y-m-d']; } } I find it really neat that you can use a closure-based custom validation rule directly in your console command with command-specif

Validate Console Command Input With the Command Validator Package


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM

Validate Console Command Input With the Command Validator Package

The Command Validator package by Andrea Marco Sartori makes validating the input of console commands a cinch using Laravel's beloved Validator. All the Laravel Validator rules you know and love work with this package, along with any custom validation rules.

This package integrates with your application's console commands using the provided ValidatesInput trait, which includes an abstract rules() method. The command signature looks like the following:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Cerbero\CommandValidator\ValidatesInput;

class SampleCommand extends Command
{
    use ValidatesInput;
    
    protected $signature = 'app:sample {--start-date=}';

    // ...

    public function rules(): array
    {
        return ['start-date' => 'date_format:Y-m-d'];
    }
}

I find it really neat that you can use a closure-based custom validation rule directly in your console command with command-specific business logic:


public function rules(): array
{
    return [
        'start-date' => [
            'date_format:Y-m-d',
            function (string $attribute, mixed $value, Closure $fail) {
                $date = Carbon::parse($value);
                $startOfYear = Carbon::now()->startOfYear();

                if ($date->lessThan($startOfYear)) {
                    $fail("The {$attribute} must be a date from {$startOfYear->format('Y-m-d')} or later.");
                }
            }
        ],
    ];
}

When validation passes, you know you're working with valid input, and your handle() method can stay clean from manual validation checks.

Another neat use-case using the built-in validation rules is validating that an input exists in the database automatically with the exists rule:

public function rules(): array
{
    return ['user-id' => 'exists:users,id'];
}

Sure, you could easily query a user and conditionally return an error, but I think it's neat that you can validate it using exists automatically and give back a default error message when a record doesn't exist.

You can use this package in your project by installing it via Composer:

composer require cerbero/command-validator

Learn more about this package, get full installation instructions, and view the source code on GitHub.


The post Validate Console Command Input With the Command Validator Package appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow

Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com