laracraft-tech / laravel-schema-rules
根据您的数据库表结构自动生成 Laravel 验证规则!
Requires
- php: ^7.4 || ^8.0 || ^8.1
- brick/varexporter: ^0.3.8 || ^0.5.0
- doctrine/dbal: ^3.6 || ^4.0.2
- illuminate/contracts: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/database: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/testing: ^8.0 || ^9.0 || ^10.0 || ^11.0
- spatie/laravel-package-tools: ^1.12 || ^1.14
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- nunomaduro/larastan: ^1.0 || ^2.5
- orchestra/testbench: ^6.27 || ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.22 || ^2.0
- spatie/laravel-ray: ^1.32
README
根据您的数据库表结构自动生成基本的 Laravel 验证规则!将这些作为起点,根据需要调整和优化您的验证规则。
如果您喜欢,这里可以使用网络版:https://validationforlaravel.com
安装
您可以通过 composer 安装此包
composer require laracraft-tech/laravel-schema-rules --dev
然后使用以下命令发布配置文件
php artisan vendor:publish --tag="schema-rules-config"
目录
用法
假设您已迁移此虚构的表
Schema::create('persons', function (Blueprint $table) { $table->id(); $table->string('first_name', 100); $table->string('last_name', 100); $table->string('email'); $table->foreignId('address_id')->constrained(); $table->text('bio')->nullable(); $table->enum('gender', ['m', 'f', 'd']); $table->date('birth'); $table->year('graduated'); $table->float('body_size'); $table->unsignedTinyInteger('children_count')->nullable(); $table->integer('account_balance'); $table->unsignedInteger('net_income'); $table->boolean('send_newsletter')->nullable(); });
为整个表生成规则
现在如果您运行
php artisan schema:generate-rules persons
您将得到
Schema-based validation rules for table "persons" have been generated!
Copy & paste these to your controller validation or form request or where ever your validation takes place:
[
'first_name' => ['required', 'string', 'min:1', 'max:100'],
'last_name' => ['required', 'string', 'min:1', 'max:100'],
'email' => ['required', 'string', 'min:1', 'max:255'],
'address_id' => ['required', 'exists:addresses,id'],
'bio' => ['nullable', 'string', 'min:1'],
'gender' => ['required', 'string', 'in:m,f,d'],
'birth' => ['required', 'date'],
'graduated' => ['required', 'integer', 'min:1901', 'max:2155'],
'body_size' => ['required', 'numeric'],
'children_count' => ['nullable', 'integer', 'min:0', 'max:255'],
'account_balance' => ['required', 'integer', 'min:-2147483648', 'max:2147483647'],
'net_income' => ['required', 'integer', 'min:0', 'max:4294967295'],
'send_newsletter' => ['nullable', 'boolean']
]
如您所注意到的,float-column body_size
只被生成到 ['required', 'numeric']
。对于 float
、decimal
和 double
的正确规则尚未实现!
为特定列生成规则
您也可以显式指定列
php artisan schema:generate-rules persons --columns first_name,last_name,email
这将给出
Schema-based validation rules for table "persons" have been generated!
Copy & paste these to your controller validation or form request or where ever your validation takes place:
[
'first_name' => ['required', 'string', 'min:1', 'max:100'],
'last_name' => ['required', 'string', 'min:1', 'max:100'],
'email' => ['required', 'string', 'min:1', 'max:255']
]
生成表单请求类
可选地,您还可以添加一个 --create-request
或 -c
标志,这将为您创建一个具有生成规则的表单请求类!
# creates app/Http/Requests/StorePersonRequest.php (store request is the default) php artisan schema:generate-rules persons --create-request # creates/overwrites app/Http/Requests/StorePersonRequest.php php artisan schema:generate-rules persons --create-request --force # creates app/Http/Requests/UpdatePersonRequest.php php artisan schema:generate-rules persons --create-request --file UpdatePersonRequest # creates app/Http/Requests/Api/V1/StorePersonRequest.php php artisan schema:generate-rules persons --create-request --file Api\\V1\\StorePersonRequest # creates/overwrites app/Http/Requests/Api/V1/StorePersonRequest.php (using shortcuts) php artisan schema:generate-rules persons -cf --file Api\\V1\\StorePersonRequest
始终跳过列
要始终跳过列,请将其添加到配置文件中,在 skip_columns
参数下。
'skip_columns' => ['whatever', 'some_other_column'],
支持的数据库驱动程序
目前,支持的数据库驱动程序有 MySQL
、PostgreSQL
和 SQLite
。
请注意,由于每个驱动程序支持不同的数据类型和范围指定,因此此包生成的验证规则可能因您使用的数据库驱动程序而异。
测试
在运行测试之前,您需要设置一个名为 laravel_schema_rules
的本地 MySQL 数据库,并在 phpunit.xml.dist
文件中更新其 username 和 password。
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。