touhidurabir/command-validator
Composer 安装命令:
composer require touhidurabir/command-validator
包简介
A simple laravel package to validate console commands arguments and options.
关键字:
README 文档
README
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 .
<?php 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
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-17