weloveahmed/laravel-auto-validator

Automatic validation rules generation for Laravel Eloquent models based on database schema.

Maintainers

Package info

github.com/Weloveahmed/laravel-auto-validator

pkg:composer/weloveahmed/laravel-auto-validator

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-01-16 09:36 UTC

This package is auto-updated.

Last update: 2026-04-16 10:09:33 UTC


README

Latest Version on Packagist Total Downloads License

Automatic validation rules generation for any Eloquent model based on database schema introspection.

โœจ Why this package?

Laravel validation is powerful, but writing FormRequests for every model/table can become repetitive.

This package automatically generates validation rules based on your database schema:

  • Column types
  • Length constraints
  • Required vs nullable
  • Unique indexes
  • Foreign keys (exists rules)

Then allows you to customize and extend rules using:

  • Model overrides
  • Reusable profiles
  • Multi-tenant constraints
  • SoftDeletes-aware uniqueness
  • JSON deep validation (nested rules)

โœ… Features

  • Schema Introspection: Generate rules from column types, lengths, nullability, unique indexes, and foreign keys.
  • Context-Aware: Supports store and update contexts (handles unique ignore automatically).
  • Overrides: Model-specific overrides via simple PHP files.
  • Profiles: Reusable validation templates (e.g. person, address, api-mobile).
  • SoftDeletes Aware: Unique rules can ignore soft-deleted rows.
  • Multi-Tenant Ready: Add tenant constraints to unique and exists.
  • JSON Support: Map JSON columns to arrays and validate nested fields.
  • Performance: Caching of schema and generated rules.
  • Developer Experience: Fluent API + optional Artisan generators.

๐Ÿ“ฆ Installation

composer require weloveahmed/laravel-auto-validator

Laravel auto-discovers the Service Provider.

โš™๏ธ Configuration

Publish the config file:

php artisan vendor:publish --tag=auto-validator-config

Config file location: config/auto-validator.php

return [
    'ignore_columns' => [
        'id',
        'created_at',
        'updated_at',
        'deleted_at',
    ],
    'cache' => [
        'enabled' => true,
        'store' => env('AUTO_VALIDATOR_CACHE_STORE', null),
        'ttl' => 3600,
        'prefix' => 'auto_validator:',
    ],
    'overrides_path' => app_path('AutoValidation'),
    'profiles' => [
        // 'person' => [
        //     'name' => ['required', 'string', 'max:255'],
        // ]
    ],
];

โšก How it works (Internals)

The package follows a simple pipeline:

SchemaReader โ†’ RuleMapper โ†’ RuleFactory โ†’ Overrides โ†’ Profiles โ†’ Context

๐Ÿš€ Basic Usage

This package is designed to work with any Eloquent model.

โœ… Generate Store Rules

use Weloveahmed\AutoValidator\Facades\AutoValidator;

$rules = AutoValidator::for(Employee::class)
    ->store()
    ->rules();

โœ… Validate Store Data

$data = AutoValidator::for(Employee::class)
    ->store()
    ->validate(request()->all());

โœ… Generate Update Rules

$rules = AutoValidator::for(Employee::class)
    ->update($employee->id)
    ->rules();

โœ… Validate Update Data

$data = AutoValidator::for(Employee::class)
    ->update($employee->id)
    ->validate(request()->all());

๐Ÿงฉ Model Overrides

Create a file: app/AutoValidation/Employee.php

return [
    'email' => ['required', 'email'],
    'mobile' => ['nullable', 'regex:/^(01)[0-9]{9}$/'],
];

๐Ÿง  Profiles (Reusable Rule Sets)

Define profiles in config:

'profiles' => [
    'api-mobile' => [
        'mobile' => ['required', 'regex:/^(01)[0-9]{9}$/'],
    ],
],

Apply profile:

$rules = AutoValidator::for(Employee::class)
    ->profile('api-mobile')
    ->store()
    ->rules();

๐Ÿข Multi-Tenancy Support

Implement TenantResolver:

use Weloveahmed\AutoValidator\Contracts\TenantResolver;

class MyTenantResolver implements TenantResolver
{
    public function enabled(): bool { return true; }
    public function tenantId(): mixed { return tenant('id'); }
    public function tenantKeyName(): string { return 'tenant_id'; }
}

Bind it in your AppServiceProvider:

$this->app->bind(TenantResolver::class, MyTenantResolver::class);

๐Ÿ—‘ SoftDeletes-Aware Unique Rules

Only needed if your model uses SoftDeletes.

AutoValidator::for(Employee::class)
    ->store()
    ->softDeletes()
    ->rules();

๐Ÿงพ JSON Deep Validation

To validate nested JSON data, add overrides:

return [
    'grades' => ['nullable', 'array'],
    'grades.*.year' => ['required', 'integer'],
    'grades.*.percentage' => ['required', 'numeric', 'min:0', 'max:100'],
];

๐Ÿ“ฆ Bulk Import (Enterprise Feature)

$results = AutoValidator::for(Employee::class)
    ->store()
    ->bulk()
    ->validateRows($rows);

$validatedRows = $results->validated();
$errors = $results->errors();

๐Ÿ›  Artisan Commands

php artisan autovalidator:request Employee
php artisan autovalidator:override Employee
php artisan autovalidator:profile person

โœ… Testing

composer test

โœ… Compatibility

PHP: 8.2+ Laravel: 10+

๐Ÿ” Security

If you discover any security issues, please email: zinorsro2013@gmail.com

๐Ÿค Contributing

Contributions are welcome! Submit PRs with tests.

๐Ÿ“„ License

MIT License. See LICENSE.md