moh4mmad/request-validation-generator

A package to generate request validation files based on migration column data types, column lengths, uniqueness, and foreign keys.

v1.0.2 2023-05-22 17:33 UTC

This package is auto-updated.

Last update: 2024-04-23 10:11:42 UTC


README

Request Validation Generator is a Laravel package that automatically generates request validation files based on your migration column data types, column lengths, uniqueness, and foreign keys.

Installation

You can install the package via Composer. Run the following command:

composer require moh4mmad/request-validation-generator

After installing the package, you need to publish the configuration file and migration files. Run the following command to publish the package assets:

php artisan vendor:publish --tag=request-validations

This command will publish the package's configuration file and migration files to your application, allowing you to customize the behavior and modify the generated request validation files if needed.

Usage

To generate request validation files for your migrations, run the following Artisan command:

php artisan generate:request-validations

The command will scan your migration files and generate request validation files based on the column data types, lengths, uniqueness, and foreign keys. The generated files will be placed in the app/Http/Requests directory.

Example

Let's say you have a migration file 20220101000000_create_users_table.php with the following columns:

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->timestamps();
});

Running the generate:request-validations command will generate a request validation file app/Http/Requests/User/Request.php with the following content:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => ['required', 'string'],
            'email' => ['required', 'string', 'email', 'unique:users,email'],
            'password' => ['required', 'string'],
        ];
    }
}

The generated request validation file provides the validation rules for each column in the migration. You can use this file in your controllers or form requests for validating incoming requests.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This package is open-source software licensed under the MIT license.