soyhuce/laravel-validation-rules

一套适用于 Laravel 的实用验证规则

1.6.0 2024-09-04 11:50 UTC

This package is auto-updated.

Last update: 2024-09-09 00:36:17 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

本包的主要目标是提供一组 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)。有关更多信息,请参阅 许可文件