laracraft-tech / laravel-schema-rules
Automatically generate Laravel validation rules based on your database table schema!
Installs: 17 488
Dependents: 1
Suggesters: 0
Security: 0
Stars: 353
Watchers: 8
Forks: 22
Open Issues: 0
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
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.