How to Generate Validation Rules from a Database Schema in Laravel

How to Generate Validation Rules from a Database Schema in Laravel

The Laravel Schema Rules package automates the generation of fundamental validation rules in Laravel, drawing from your database table schema. It serves as a handy tool for kickstarting your validation process, providing initial rules that you can refine and optimize to suit your specific requirements.

Given the following schema for your table:

Schema::create('users', 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();
});

You can run the schema:generate-rules artisan command to generate validation rules:

$ php artisan schema:generate-rules users

[
    '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']
]

ou can also target specific columns, skip columns, and even generate a form request class.

php artisan schema:generate-rules users --columns first_name,last_name,email

Which gives you:

Schema-based validation rules for table "users" 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/StoreUserRequest.php (store request is the default)
php artisan schema:generate-rules users --create-request 

# creates/overwrites app/Http/Requests/StoreUserRequest.php
php artisan schema:generate-rules users --create-request --force

# creates app/Http/Requests/UpdateUserRequest.php
php artisan schema:generate-rules users --create-request --file UpdateUserRequest

# creates app/Http/Requests/Api/V1/StoreUserRequest.php
php artisan schema:generate-rules users--create-request --file Api\\V1\\StoreUserRequest

# creates/overwrites app/Http/Requests/Api/V1/StoreUserRequest.php (using shortcuts)
php artisan schema:generate-rules users -cf --file Api\\V1\\StoreUserRequest

Always skip columns

To always skip columns add it in the config file, under skip_columns parameter.

'skip_columns' => ['whatever', 'some_other_column'],

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Thank you for your time. You can subscribe to my newsletter below to get free premium contents like this first hand.