rewake / lumen-validation-v6
为 Lumen v6 增强的验证
dev-master / 0.1.2.x-dev
2021-08-25 19:54 UTC
Requires
- illuminate/translation: ^6.0|^7.0|^8.0
- illuminate/validation: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-09-26 02:24:06 UTC
README
注意:此包适用于 Lumen v6。对于 Lumen v5,请使用 https://github.com/rewake/lumen-validation
此库为 illuminate/validation
包提供增强功能,可以对对象和类进行验证,而不是仅对数组进行验证。默认的 Lumen 验证器已被包装,以便所有现有的验证功能 应该 都可用,但是这尚未完全测试。
还提供了一个 ValidationRuleInterface
,以便对验证规则进行分类,以便于使用和代码分离。
注册验证器
包含一个 Service Provider,可以从 app.php
配置文件中轻松注册验证服务。
$app->register(Rewake\Lumen\Providers\ValidationServiceProvider::class);
注意:此 Service Provider 将覆盖 Lumen 中的默认 app('validator')
别名,目前尚未完全测试。如果您希望保持它们分离(或者 需要 保持分离),您可以创建一个新的 Provider 来实现这一点。
示例
public function register()
{
// Register Validation Service
$this->app->singleton(
'validation_service',
\Rewake\Lumen\Services\ValidationService::class
);
}
示例验证类及使用方法
类
<?php
namespace App\Validation;
use Rewake\Lumen\Validation\ValidationRuleInterface;
class ExampleValidation implements ValidationRuleInterface
{
public static function descriptor()
{
return [];
}
public static function rules()
{
return [
"first" => [
'required',
'string'
],
"last" => [
'required',
'string'
],
"id" => [
'required',
'integer'
]
];
}
public static function messages()
{
return [];
}
}
使用方法
app('validator')->validate($data, ExampleValidation::class);