laracraft-tech/laravel-schema-rules
Composer 安装命令:
composer require laracraft-tech/laravel-schema-rules
包简介
Automatically generate Laravel validation rules based on your database table schema!
README 文档
README
Automatically generate basic Laravel validation rules based on your database table schema! Use these as a starting point to fine-tune and optimize your validation rules as needed.
Here you can use the web version, if you like: https://validationforlaravel.com
Installation
You can install the package via composer:
composer require laracraft-tech/laravel-schema-rules --dev
Then publish the config file with:
php artisan vendor:publish --tag="schema-rules-config"
ToC
Usage
Let's say you've migrated this fictional table:
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(); });
Generate rules for a whole table
Now if you run:
php artisan schema:generate-rules persons
You'll get:
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']
]
As you may have noticed the float-column body_size, just gets generated to ['required', 'numeric'].
Proper rules for float, decimal and double, are not yet implemented!
Generate rules for specific columns
You can also explicitly specify the columns:
php artisan schema:generate-rules persons --columns first_name,last_name,email
Which gives you:
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']
]
Generate Form Request Class
Optionally, you can add a --create-request or -c flag,
which will create a form request class with the generated rules for you!
# 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
Always skip columns
To always skip columns add it in the config file, under skip_columns parameter.
'skip_columns' => ['whatever', 'some_other_column'],
Supported Drivers
Currently, the supported database drivers are MySQL, PostgreSQL, and SQLite.
Please note, since each driver supports different data types and range specifications, the validation rules generated by this package may vary depending on the database driver you are using.
Testing
Before running tests, you need to set up a local MySQL database named laravel_schema_rules and update its username and password in the phpunit.xml.dist file.
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
laracraft-tech/laravel-schema-rules 适用场景与选型建议
laracraft-tech/laravel-schema-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.33k 次下载、GitHub Stars 达 363, 最近一次更新时间为 2023 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laracraft-tech」 「laravel-schema-rules」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laracraft-tech/laravel-schema-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laracraft-tech/laravel-schema-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laracraft-tech/laravel-schema-rules 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Some useful date scopes for your Laravel Eloquent models!
A collection of useful Laravel additions!
Dynamic Model for Laravel!
A collection of useful Laravel additions!
Some useful extension for the carbon library!
Alfabank REST API integration
统计信息
- 总下载量: 63.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 363
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-19