A simple laravel package to validate console commands arguments and options.

Overview

Command Validator

A simple laravel package to validate console commands arguments and options.

Installation

Require/Install the package using composer:

composer require touhidurabir/command-validator

Usage

Drop the trait HasCommandValidator in any command and then put the validation rules . thats all .



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Touhidurabir\CommandValidator\HasCommandValidator;

class Test extends Command {
    
    use HasCommandValidator;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run:test
                            {arg     : The command argument}
                            {--opt1= : The command option 1}
                            {--opt2= : The command option 2}';


    /**
     * The command arguments and options validation rules
     *
     * @return array
     */
    protected function rules(): array {

        return [
            'arg'   => ['integer', 'required', 'min:100'],
            'opt1'  => ['integer', 'required', 'max:10'],
            'opt2'  => ['sometimes', 'nullable', 'string'],
        ];
    }
}    

This package also support the message or attribute override ability provided by laravel validation itself .

To override the validation message put method in the command class and fill up as needed

/**
 * Any custom error message
 *
 * @return array
 */
protected function messages(): array {

    return [];
}

To override the validation attributes put method in the command class and fill up as needed

/**
 * Any custom arrtibute names to associated with error messages
 *
 * @return array
 */
protected function attributes(): array {

    return [];
}

By default this validator do not throw an exception when the validation of arguments or options failed but print those as error in the console in a formatted way to check what is missing/failed . But if want to throw exception rather than console printing , override the method allowValidationFailureOnConsole to return false as

/**
 * Should the validation error print on the console
 *
 * @return bool
 */
protected function allowValidationFailureOnConsole() {

    return false;
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

You might also like...
A quick and incomplete example of how to validate a form using a FormValidator class on the simple-mvc
A quick and incomplete example of how to validate a form using a FormValidator class on the simple-mvc

Simple MVC Description This repository is a simple PHP MVC structure from scratch. It uses some cool vendors/libraries such as Twig and Grumphp. For t

A package to validate email domains in a user registration form
A package to validate email domains in a user registration form

This package allows to define a subset of allowed email domains and validate any user registration form with a custom rule.

A package to validate email domains in a user registration form
A package to validate email domains in a user registration form

Laravel Email Domain Rule This package allows to define a subset of allowed email domains and validate any user registration form with a custom rule.

Package for Laravel that gives artisan commands to setup and edit environment files.
Package for Laravel that gives artisan commands to setup and edit environment files.

Setup and work with .env files in Laravel from the command line NOTE: This doesn't work with Laravel 5 since .env files were changed. This is for Lara

This package provides a console command to convert dynamic JS/CSS to static JS/CSS assets.
This package provides a console command to convert dynamic JS/CSS to static JS/CSS assets.

Laravel Nova Search This package provides a console command to convert dynamic JS/CSS to static JS/CSS assets. Requirements laravel-mix v6.0+ php 7.3+

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

A package for Laravel to perform basic git commands on locally integrated packages.

A package for Laravel to perform basic git commands on locally integrated development packages. If working within multiple local development packages or repositories at once this package is meant to ease the burden of navigating to each individual repository to perform basic git commands.

A package for more Laravel commands.

Lara-Commands A package for more Laravel commands. Installation This package can be installed via Composer: composer require nimaw/lara-commands --dev

Releases(1.0.1)
Owner
Touhidur Rahman
Husband, father, big time documentary tv series lover and software engineer . Passionate about PHP, Laravel, Ruby on Rails, Vue.js and C/C++ . Learning Rust .
Touhidur Rahman
Laminas\Console is a component to design and implement console applications in PHP.

laminas-console This package is abandoned and will receive no further development! We recommend using laminas/laminas-cli. Laminas\Console is a compon

Laminas Project 10 Nov 27, 2021
Mutex for Laravel Console Commands.

Laravel Console Mutex Mutex for Laravel Console Commands. Laravel Console Mutex 8.x 8.x 7.x 7.x 6.x 6.x 5.8.* 5.8.* 5.7.* 5.7.* 5.6.* 5.6.* 5.5.* 5.5.

Dmitry Ivanov 125 Dec 17, 2022
A simple Laravel package for generating download links with options such as expire time, IP restrictions, etc.

Generate download links in your Laravel applications This package allows you to generate download links for files. Once installed you can do stuff lik

Arman Ahmadi 12 Nov 24, 2022
Otpify is a Laravel package that provides a simple and elegant way to generate and validate one time passwords.

Laravel Otpify ?? Introduction Otpify is a Laravel package that provides a simple and elegant way to generate and validate one time passwords. Install

Prasanth Jayakumar 2 Sep 2, 2022
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 2022
Gallium is a TALL stack starter kit offering a robust set of options enabling you to get up and running in a snap.

Very short description of the package This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention o

null 1 Nov 20, 2021
Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Laravel Uuid Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for versio

Christoph Kempen 1.7k Dec 28, 2022
Let users pick a color from a fixed list of options.

Let users pick a color from a fixed list of options. This packages implements a new ColorPalette field as described in the mini-series on my blog. It

Ryan Chandler 2 Oct 7, 2022
A Laravel package that allows you to validate your config values and environment.

Table of Contents Overview Installation Requirements Install the Package Publishing the Default Rulesets Usage Creating a Validation Ruleset Using the

Ash Allen 152 Dec 15, 2022
🛂 Use this package to validate the identity card from your country using laravel validation rules.

Identity Card Checker Laravel Validation Rules Use this package to validate the identity card number from your country Installation You can install th

David Torralbo Pérez 13 Feb 8, 2022