How to generate Validation Rules based on your database table schema

Conveniently generate Laravel Validation Rules based on your Database Table Schema!

Zacharias Creutznacher Zacharias Creutznacher

In the fast-paced world of web development, efficiency and automation are key. One repetitive task in this process is defining Validation Rules for data. This is where the "Laravel Schema Rules" package comes into play, especially useful when tasked with defining Validation Rules post-facto for certain endpoints, or for just simplifying the process for defining Validation Rules. The package automatically generates basic Laravel Validation Rules based on your database table schema. This utility is a boon for developers, especially when dealing with complex tables and a multitude of fields, saving time and reducing the potential for error.

#Installation

You can install the package via composer:

 1composer require laracraft-tech/laravel-schema-rules --dev

Then publish the config file with:

 1php artisan vendor:publish --tag="schema-rules-config"

#Usage

Let's say you've migrated this fictional table:

 1<?php
 2
 3Schema::create('persons', function (Blueprint $table) {
 4    $table->id();
 5    $table->string('first_name', 100);
 6    $table->string('last_name', 100);
 7    $table->string('email');
 8    $table->foreignId('address_id')->constrained();
 9    $table->text('bio')->nullable();
10    $table->enum('gender', ['m', 'f', 'd']);
11    $table->date('birth');
12    $table->year('graduated');
13    $table->float('body_size');
14    $table->unsignedTinyInteger('children_count')->nullable();
15    $table->integer('account_balance');
16    $table->unsignedInteger('net_income');
17    $table->boolean('send_newsletter')->nullable();
18});

#Generate rules

Now if you run:

 1php artisan schema:generate-rules persons

You'll get:

 1Schema-based validation rules for table "persons" have been generated!
 2Copy & paste these to your controller validation or form request or where ever your validation takes place:
 3[
 4    'first_name' => ['required', 'string', 'min:1', 'max:100'],
 5    'last_name' => ['required', 'string', 'min:1', 'max:100'],
 6    'email' => ['required', 'string', 'min:1', 'max:255'],
 7    'address_id' => ['required', 'exists:addresses,id'],
 8    'bio' => ['nullable', 'string', 'min:1'],
 9    'gender' => ['required', 'string', 'in:m,f,d'],
10    'birth' => ['required', 'date'],
11    'graduated' => ['required', 'integer', 'min:1901', 'max:2155'],
12    'body_size' => ['required', 'numeric'],
13    'children_count' => ['nullable', 'integer', 'min:0', 'max:255'],
14    'account_balance' => ['required', 'integer', 'min:-2147483648', 'max:2147483647'],
15    'net_income' => ['required', 'integer', 'min:0', 'max:4294967295'],
16    'send_newsletter' => ['nullable', 'boolean']
17]

#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!

 1#!/bin/sh
 2
 3# creates app/Http/Requests/StorePersonRequest.php (store request is the default)
 4php artisan schema:generate-rules persons --create-request
 5
 6# creates/overwrites app/Http/Requests/StorePersonRequest.php
 7php artisan schema:generate-rules persons --create-request --force
 8 
 9# creates app/Http/Requests/UpdatePersonRequest.php
10php artisan schema:generate-rules persons --create-request --file UpdatePersonRequest
11
12# creates app/Http/Requests/Api/V1/StorePersonRequest.php
13php artisan schema:generate-rules persons --create-request --file Api\\V1\\StorePersonRequest
14
15# creates/overwrites app/Http/Requests/Api/V1/StorePersonRequest.php (using shortcuts)
16php artisan schema:generate-rules persons -cf --file Api\\V1\\StorePersonRequest

For the full documentation checkout the README