玉米科技 / laravel-remote-rule
Laravel 远程规则
Requires
- php: ^8.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- illuminate/database: ^9.0|^10.0|^11.0
- illuminate/http: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- guzzlehttp/guzzle: ^7.4
- nunomaduro/collision: ^6.0|^7.10.0|^8.1.1
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
- vimeo/psalm: ^4.20|^5.22
README
Laravel 远程规则
通过远程请求轻松验证数据属性。
此包允许您定义一组自定义规则,通过调用远程服务的 API 来验证传入的请求数据。
一个示例用法可能是一个具有公开注册表单的应用程序,该表单只能允许由远程服务给出的电子邮件列表。您可以在用法部分找到示例。
安装
您可以通过 composer 安装此包
composer require maize-tech/laravel-remote-rule
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --provider="Maize\RemoteRule\RemoteRuleServiceProvider" --tag="remote-rule-migrations" php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Maize\RemoteRule\RemoteRuleServiceProvider" --tag="remote-rule-config"
这是已发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Config model |-------------------------------------------------------------------------- | | Here you may specify the fully qualified class name of the config model. | */ 'config_model' => Maize\RemoteRule\Models\RemoteRuleConfig::class, /* |-------------------------------------------------------------------------- | Attribute cast |-------------------------------------------------------------------------- | | Here you may specify the cast type for all model attributes who contain | sensitive data. | All attributes listed below will be encrypted by default when creating or | updating a model instance. You can disable this behaviour by removing | the attribute cast from the array. | */ 'attribute_cast' => [ 'url' => 'encrypted', 'method' => 'encrypted', 'headers' => 'encrypted:array', 'json' => 'encrypted:array', ], /* |-------------------------------------------------------------------------- | Debug mode |-------------------------------------------------------------------------- | | Here you may enable or disable the debug mode. If enabled, the rule will | throw an exception instead of validating the attribute. | */ 'debug' => false, /* |-------------------------------------------------------------------------- | Validation message |-------------------------------------------------------------------------- | | Here you may specify the message thrown if the validation rule fails. | */ 'validation_message' => 'The :attribute must be valid.', ];
用法
基本
要使用此包,您可以创建一个扩展 RemoteRule
抽象类的类。
use Maize\RemoteRule\RemoteRule; class EmailRule extends RemoteRule { // }
然后,您可以使用您刚刚创建的类的蛇形名称创建一个新的 RemoteRuleConfig
实例。该模型将包含用于发送请求的远程服务的 URL 以及请求方法(通常是 POST
或 GET
),如果需要,还包括自定义头部、JSON 主体和超时。
我们将此数据添加到数据库表中,因为我们认为它是敏感信息,并且我们希望避免将其硬编码到代码库中。实际上,remote_rule_configs
数据库表的所有条目默认都是加密的(可以在配置中禁用)。
您可以使用控制台中的 tinker 创建远程规则配置
php artisan tinker
\Maize\RemoteRule\Models\RemoteRuleConfig::query()->create([ 'name' => 'email_rule', 'url' => 'test.example.com', 'method' => 'POST', 'headers' => [], // can be null if no custom headers are required 'json' => [], // can be null if no custom json body is required 'timeout' => 10, // can be null if you want to use the default timeout ]);
这就完成了!现在,您只需将您的新自定义规则添加到验证数组中即可
use Illuminate\Support\Facades\Validator; $email = 'my-email@example.com'; Validator::make([ 'email' => $email, ], [ 'email' => [ 'string', 'email', new EmailRule, ], ])->validated();
底层,验证规则将向给定的 URL 发送请求,并带有自定义头部和主体(我们在其中附加属性名称和其值),并检查响应是否成功。
自定义响应状态码
您可以通过在您的自定义规则中覆盖 isSuccessful
方法来更改预期的响应状态码。
use Maize\RemoteRule\RemoteRule; use Illuminate\Http\Client\Response; class EmailRule extends RemoteRule { protected function isSuccessful(Response $response): bool { return $response->status() === 204; } }
自定义主体属性
默认情况下,所有自定义规则都会将属性键值对附加到请求主体中,其中键是验证属性的名称,值是表单的输入值。
您可以通过在您的自定义规则中覆盖 getBodyAttribute
方法来更改发送到远程服务的主体属性。
use Maize\RemoteRule\RemoteRule; class EmailRule extends RemoteRule { protected function getBodyAttribute(): array { return ['custom_attribute' => $this->value]; } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
请参阅 我们的安全策略,了解如何报告安全漏洞。
鸣谢
许可协议
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。