perryvandermeer/laravel-console-validator
Composer 安装命令:
composer require perryvandermeer/laravel-console-validator
包简介
Validate arguments for Laravel commands
README 文档
README
This package allows you to easily validate all entered arguments in Laravel commands.
Here's a brief example where we will automatically validate the foo argument against the required and min:3 rules:
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use PerryvanderMeer\LaravelConsoleValidator\ValidatesArguments; class ExampleCommand extends Command { use ValidatesArguments; /** * Set the validation rules that apply to the command. * * @var array<string, array|string> */ protected array $rules = [ 'foo' => ['required', 'min:3'], ]; }
Requirements
This package requires PHP 8.1+ and Laravel 10+.
Installation
You can install the package via composer:
composer require perryvandermeer/laravel-console-validator
Usage
To get started, you will need to include the ValidatesArguments trait in your command.
When the command is executed, it will automatically perform validation before reaching the handle() method.
When the validation fails, the handle() method will never be executed.
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use PerryvanderMeer\LaravelConsoleValidator\ValidatesArguments; class ExampleCommand extends Command { use ValidatesArguments; }
Configuring The Validator
Just like when you use Laravel's validator, it is possible to configure the rules, messages and attributes.
Rules
You may configure the validation rules used by the command by defining the $rules property and/or the rules() method.
The rules() method can be helpful if you are trying to use run-time syntaxes that aren't supported in array properties, like Laravel rule objects such as Rule::password().
When you use both the $rules property and the rules() method, we merge them together.
The property and/or method should return an array of argument / rule pairs and their corresponding validation rules.
/** * Set the validation rules that apply to the command. * * @var array<string, array|string> */ protected array $rules = [ 'title' => ['required', 'min:3'], 'content' => 'required', ];
/** * Get the validation rules that apply to the command. * * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string> */ protected function rules() : array { return [ 'title' => ['required', 'min:3', Rule::unique('posts')], 'content' => 'required', ]; }
Messages
You may customize the error messages used by the command by defining the $messages property and/or the messages() method.
The messages() method can be helpful if you are trying to use run-time syntaxes that aren't supported in array properties.
When you use both the $messages property and the messages() method, we merge them together.
The property and/or method should return an array of argument / rule pairs and their corresponding error messages.
/** * Set the error messages for the defined validation rules. * * @var array<string, string> */ protected array $messages = [ 'title' => 'Whoo general message for title argument..!', 'content.min' => 'Hmm the content is very short..!', ];
/** * Get the error messages for the defined validation rules. * * @return array<string, string> */ protected function messages() : array { return [ 'title' => 'Whoo general message for title argument..!', 'content.min' => 'Hmm the content is very short..!', ]; }
Attributes
Many of Laravel's built-in validation rule error messages contain an :attribute placeholder.
As default, the name of the argument is used as :attribute.
If you would like the :attribute placeholder of your validation message to be replaced with a custom attribute name, you may specify the custom names by defining the $attributes property and/or the attributes() method..
The attributes() method can be helpful if you are trying to use run-time syntaxes that aren't supported in array properties.
When you use both the $attributes property and the attributes() method, we merge them together.
The property and/or method should return an array of argument / name pairs.
/** * Set custom attributes for validator errors. * * @return array<string, string> */ protected array $attributes = [ 'title' => 'custom title', 'content' => 'custom content', ];
/** * Get custom attributes for validator errors. * * @return array<string, string> */ protected function attributes() : array { return [ 'title' => 'custom title', 'content' => 'custom content', ]; }
Preparing Arguments For Validation
If you need to prepare or sanitize any data before you apply your validation rules, you may use the prepareForValidation() method.
/** * Prepare the data for validation. */ protected function prepareForValidation() : void { $this->input->setArgument( name: 'foo', value: "{$this->argument('foo')}-bar", ); }
Working With Validated Arguments
Just like when you use Laravel's validator, it is possible to use the validated arguments in the rest of your implementation.
Retrieving All Validated Arguments
After validating the arguments, you may wish to retrieve the arguments that actually underwent validation.
This can be accomplished in several ways. First, you may call the validated() method within your command.
This method returns an array of the data that was validated.
$this->validated(); // (array) ['foo' => 'bar', 'bar' => 'baz']
When you want a Collection of all arguments that underwent validation, you may call the collect() method within your command.
This method returns a Collection of the data that was validated.
$this->collect(); // collect(['foo' => 'bar', 'bar' => 'baz'])
Retrieving A Single Validated Argument
When you pass an argument to the validated() method, it will only return the validated value of that argument.
When the requested argument has not been validated, it will throw the UnvalidatedArgumentException exception.
$this->validated('foo'); // (string) 'bar'
When you want to make sure the validated value of the argument is cast to a string, you may use the string() method.
$this->string('foo'); // (string) 'bar'
Your command may receive "truthy" values that are actually strings, like (string) 'true' or (string) 'on'.
You may use the bool() or boolean() method to retrieve these values as booleans.
Those methods use PHP's FILTER_VALIDATE_BOOLEAN flag to determine if the passed argument is (bool) true or (bool) false.
$this->boolean('foo'); // (bool) false
Testing
When using laravel/framework above version 11.9.0, you may use the custom assertFailedWithValidationError() method to assert that the command returned any validation error:
use Symfony\Component\Console\Command\Command; $this->artisan('foo') ->assertFailedWithValidationError();
When using a lower version of laravel/framework, you may use the assertExitCode() method to assert that the command returned any validation error:
use Symfony\Component\Console\Command\Command; $this->artisan('foo') ->assertExitCode(Command::INVALID);
In addition, it may be useful to test for a specific validation error.
You may use the expectsOutput() method to assert that the command returned a specific validation error:
$this->artisan('foo') ->expectsOutput('The foo field must be at least 6 characters.')
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
perryvandermeer/laravel-console-validator 适用场景与选型建议
perryvandermeer/laravel-console-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.73k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「console」 「laravel」 「artisan」 「laravel-console-validator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 perryvandermeer/laravel-console-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 perryvandermeer/laravel-console-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 perryvandermeer/laravel-console-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This class provides you with an easy-to-use interface to progress bars.
A Laravel package to retrieve data from Google Search Console
Runn Me! Validation and Sanitization Library
Additional artisan commands for Laravel
Extension for Opis JSON Schema
A table generator class for the PHP CLI.
统计信息
- 总下载量: 9.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-25