soyhuce / laravel-validation-rules
一套适用于 Laravel 的实用验证规则
1.6.0
2024-09-04 11:50 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0 || ^11.0
- illuminate/translation: ^10.0 || ^11.0
- illuminate/validation: ^10.0 || ^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.7
- larastan/larastan: ^2.0.1
- nunomaduro/collision: ^7.10 || ^8.1
- orchestra/testbench: ^8.0 || ^9.0
- pestphp/pest: ^2.24
- pestphp/pest-plugin-laravel: ^2.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.4
README
本包的主要目标是提供一组 Laravel 验证规则,以便更容易地编写验证。
Validator::make($data, rules([ 'email' => ['required', DbRules::string()], // ['required', 'string', 'max:255'] 'smaller' => ['nullable', DbRules::string(20)], // ['nullable', 'string', 'max:20'] 'birthday' => [DbRules::date()], // ['date_format:Y-m-d'] 'tiny' => [DbRules::tinyInteger()], // ['integer', 'min:-128', 'max:127'] // ... ]));
安装
您可以通过 composer 安装此包
composer require soyhuce/laravel-validation-rules
使用方法
可用规则
数据库相关规则
DbRules::string
DbRules::boolean
DbRules::enum
DbRules::date
DbRules::dateTime
DbRules::tinyInteger
DbRules::unsignedTinyInteger
DbRules::smallInteger
DbRules::unsignedSmallInteger
DbRules::mediumInteger
DbRules::unsignedMediumInteger
DbRules::integer
DbRules::unsignedInteger
DbRules::bigInteger
DbRules::unsignedBigInteger
DbRules::smallIncrements
DbRules::mediumIncrements
DbRules::increments
DbRules::bigIncrements
DbRules::float
DbRules::double
其他规则
MisRules::safePassword
: GDPR/CNIL 兼容密码(至少 12 个字符)MisRules::mediumPassword
: GDPR/CNIL 兼容密码(至少 8 个字符)
在条件 Rule::when
中使用
扩展 CompoundRule
的规则不能直接在 Rule::when
中使用。
在这种情况下,您需要使用 ...
'commission_account' => Rule::when( fn(Fluent $data) => $data->get('commission') !== null, - ['required', DbRules::string()], + ['required', ...DbRules::string()], 'exclude' ),
测试
composer test
更新日志
有关最近更改的更多信息,请参阅 更新日志。
贡献
有关详细信息,请参阅 贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。