laracraft-tech / laravel-schema-rules by Sairahcaz

Automatically generate Laravel validation rules based on your database table schema!
17,570
353
8
Package Data
Maintainer Username: Sairahcaz
Maintainer Contact: zacharias@laracraft.tech (Zacharias Creutznacher)
Package Create Date: 2023-06-02
Package Last Update: 2024-08-13
Home Page: https://laracraft.tech/blog/how-to-generate-validation-rules-based-on-your-database-table-schema
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:01:59
Package Statistics
Total Downloads: 17,570
Monthly Downloads: 1,032
Daily Downloads: 31
Total Stars: 353
Total Watchers: 8
Total Forks: 22
Total Open Issues: 0

Laravel Schema Rules

Latest Version on Packagist Tests Check & fix styling License Total Downloads

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

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.