Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Schema Rules Laravel Package

laracraft-tech/laravel-schema-rules

Generate starter Laravel validation rules from your database schema. Create rules for entire tables or selected columns, optionally generate Form Request classes, and configure columns to always skip. Great for fast scaffolding before manual refinement.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:

    • Schema-Driven Validation: Aligns perfectly with Laravel’s validation layer, reducing manual rule duplication and ensuring consistency between database schema and application logic.
    • Form Request Generation: Directly supports Laravel’s best practice of using Form Requests for validation, improving code organization and reusability.
    • Database-Agnostic: Supports MySQL, PostgreSQL, and SQLite, making it versatile for multi-database projects.
    • Extensible: Configurable via skip_columns and customizable rule generation, allowing teams to override defaults (e.g., for business-specific constraints).
    • Non-Invasive: Installed as a dev dependency, avoiding runtime overhead.
  • Cons:

    • Limited Precision: Generates baseline rules (e.g., numeric for float instead of decimal:8,2). Teams must manually refine for edge cases (e.g., precision, custom formats).
    • No Dynamic Rule Injection: Rules are static at generation time; cannot adapt to runtime conditions (e.g., conditional validation).
    • Foreign Key Assumptions: Relies on Laravel’s exists: rule for foreign keys, which may not cover all use cases (e.g., polymorphic relationships).

Integration Feasibility

  • Laravel Ecosystem Fit:
    • Seamlessly integrates with Laravel’s validation system (Form Requests, controllers, API resources).
    • Compatible with Laravel 11–13 (as of v1.6.0), with backward compatibility for older versions.
    • Works alongside existing validation libraries (e.g., Laravel’s built-in rules, spatie/laravel-validation-attributes).
  • Database Schema Dependency:
    • Requires an existing database schema. Not suitable for schema-first or migration-heavy projects where tables are frequently altered.
    • Schema changes (e.g., adding nullable()) must be reflected in validation rules manually or regenerated.
  • Tooling Compatibility:
    • CLI-driven (artisan commands), fitting into Laravel’s CLI workflow.
    • No API or SDK; integration is purely via Artisan commands.

Technical Risk

  • Rule Generation Accuracy:
    • Risk of incorrect rules for unsupported data types (e.g., float/decimal precision, custom enum values).
    • Mitigation: Validate generated rules against test data; override defaults in Form Requests.
  • Performance Impact:
    • Minimal at runtime (rules are static). Generation may slow down CI/CD pipelines if run frequently.
    • Mitigation: Cache generated rules or run during development only.
  • Database Driver Quirks:
    • Rules vary by driver (e.g., PostgreSQL’s jsonb vs. MySQL’s json). Teams using mixed drivers may need custom configurations.
    • Mitigation: Test across target environments early.
  • Breaking Changes:
    • Low risk for Laravel version upgrades (package maintains compatibility). Monitor changelog for schema parsing changes.

Key Questions for TPM

  1. Validation Strategy:
    • How does this fit with existing validation logic (e.g., custom rules, API contracts)?
    • Will teams override generated rules frequently, or use them as a baseline?
  2. CI/CD Integration:
    • Should rule generation be automated in CI (e.g., on schema changes) or manual?
    • How to handle conflicts when schema and validation rules diverge?
  3. Edge Cases:
    • Are there columns requiring custom validation (e.g., regex, conditional rules) that should be excluded from auto-generation?
    • How to handle polymorphic relationships or complex foreign keys?
  4. Performance:
    • For large schemas, will rule generation impact developer productivity?
  5. Maintenance:
    • Who owns updating generated rules when the schema evolves (e.g., adding min: to a string)?

Integration Approach

Stack Fit

  • Primary Use Cases:
    • Form Requests: Ideal for CRUD operations (e.g., StoreUserRequest, UpdatePostRequest).
    • Controller Validation: Quick baseline for ad-hoc validation (e.g., API endpoints).
    • API Contracts: Generate OpenAPI/Swagger validation rules (if extended).
  • Complementary Tools:
    • Laravel Scout: For searchable fields, add custom rules post-generation.
    • Laravel Nova/Vue: Auto-generate validation for admin panels.
    • Testing: Pair with Pest/Feature tests to validate generated rules.
  • Anti-Patterns:
    • Avoid using for dynamic validation (e.g., rules that change based on user roles).
    • Not suitable for schema-less databases (e.g., MongoDB via Laravel’s jenssegers/mongodb).

Migration Path

  1. Pilot Phase:
    • Start with 1–2 high-traffic tables (e.g., users, orders) to validate rule quality.
    • Compare generated rules against existing validation logic for accuracy.
  2. Incremental Adoption:
    • Generate rules for new tables during development.
    • For existing tables, manually migrate validation to Form Requests, then regenerate.
  3. Tooling Integration:
    • Add a post-migrate hook to regenerate rules after schema changes.
    • Example:
      # In a custom Artisan command or Git hook
      php artisan schema:generate-rules users --create-request
      
  4. Customization Layer:
    • Extend Form Requests to override generated rules:
      public function rules()
      {
          $schemaRules = (new SchemaRules)->forTable('users');
          return array_merge($schemaRules, [
              'email' => ['unique:users,email,' . $this->user->id],
          ]);
      }
      

Compatibility

  • Laravel Versions:
    • Tested on 11–13; verify compatibility with your version (e.g., Laravel 10 may need v1.3.x).
  • Database Drivers:
    • Confirm support for your primary driver (e.g., PostgreSQL’s jsonb requires v1.3.2+).
    • For custom data types (e.g., uuid-ossp), extend the package or skip columns.
  • Existing Validation:
    • Use skip_columns to exclude fields with custom rules (e.g., password, token).
    • Merge generated rules with manual rules in Form Requests.

Sequencing

  1. Pre-requisites:
    • Database schema must be finalized (or changes managed via migrations + rule regeneration).
    • Laravel project bootstrapped (composer, Artisan).
  2. Implementation Steps:
    Step Action Owner
    1 Install package (composer require --dev) DevOps/Backend
    2 Publish config (php artisan vendor:publish) Backend
    3 Test rule generation on a sample table QA/Backend
    4 Integrate into CI (optional) DevOps
    5 Train team on overriding rules Tech Lead
    6 Roll out to pilot tables Backend
  3. Post-Launch:
    • Monitor for false positives/negatives in validation.
    • Update documentation for teams on customizing rules.

Operational Impact

Maintenance

  • Proactive:
    • Schema Changes: Regenerate rules after migrations:
      php artisan schema:generate-rules users --force
      
    • Config Updates: Modify skip_columns in config/schema-rules.php for team-wide exclusions.
  • Reactive:
    • Rule Overrides: Document why manual rules exist (e.g., in Form Request comments).
    • Deprecation: Monitor Laravel version support; plan upgrades proactively.
  • Tooling:
    • Add a schema:rules:update command to your custom Artisan commands for team-wide updates.

Support

  • Common Issues:
    • Incorrect Rules: Debug by comparing schema (e.g., php artisan schema:dump) with generated output.
    • Missing Columns: Ensure skip_columns is configured correctly.
    • Performance: Rule generation is O(1) per table; no runtime impact.
  • Troubleshooting:
    • Check .env database config matches the target schema.
    • Use --columns flag to isolate problematic columns.
  • Documentation:
    • Create internal docs on:
      • How to override rules.
      • When to skip columns (e.g., computed fields, soft deletes).
      • Example workflow for schema changes.

Scaling

  • Large Schemas:
    • Performance: Rule generation scales linearly with table size. For 100+ tables, consider:
      • Parallelizing generation (e.g., via Laravel queues).
      • Caching generated rules in a config file.
    • Team Coordination: Use a shared config for skip_columns to avoid duplication.
  • Multi-Environment:
    • Generate rules per environment if schemas differ (e.g., dev vs. prod).
    • Example:
      # Generate for staging schema
      php artisan schema:generate-rules --env=staging users
      
  • Microservices:
    • Generate rules per service boundary (e.g., auth-service:users).

**Failure Modes

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport