Skip to main content
The Rest Generic Class package provides two types of validation that can be configured independently:
  1. Validation Cache - Caches database existence validation queries
  2. Column Validation - Validates column names in filter conditions

Validation Cache

The ValidatesExistenceInDatabase trait performs database existence checks during validation. These queries can be cached to reduce database load.

Enable Validation Cache

boolean
default:"true"
Master switch for validation query caching. When enabled, database existence checks are cached for the configured TTL.

Configure TTL

integer
default:"3600"
Time-to-live in seconds for cached validation results.Recommendation: Use a longer TTL (1+ hours) since table structures and validation rules rarely change during runtime.

Set Cache Prefix

string
default:"validation"
Prefix for validation cache keys. Helps organize and identify validation-related cache entries.Example keys:
  • validation:products:exists:123
  • validation:users:columns

Choose Database Connection

string
default:"db"
Database connection name used for validation queries.Use case: Route validation queries to a read replica to reduce load on the primary database.

Configuration Structure

Column Validation

Column validation ensures that filter conditions only reference valid database columns, preventing SQL injection and information disclosure.

Enable Column Validation

boolean
default:"true"
Validates that column names in oper filter conditions exist in the database table.When enabled:
  • Queries column list from database
  • Checks filter columns against valid columns
  • Caches column lists per table
When disabled:
  • No column validation occurs (security risk)

Strict Column Validation

boolean
default:"true"
Enforces strict column validation behavior.When true:
  • Invalid column names cause validation errors
  • Request is rejected with 422 status
When false:
  • Invalid columns are silently ignored
  • Query proceeds with valid columns only

Column Cache TTL

Column lists are cached automatically to avoid repeated database schema queries:
Column cache TTL is set in the config file, not via environment variable. To customize, publish and edit config/rest-generic-class.php.

Security Considerations

Always Enable in Production

Security critical: Both REST_VALIDATE_COLUMNS and REST_STRICT_COLUMNS should be true in production.Disabling these allows:
  • Potential SQL injection via crafted column names
  • Information disclosure through error messages
  • Bypassing intended column access restrictions

Production Configuration

When to Disable

Only disable column validation in these scenarios:
  • Local development - For faster iteration without validation overhead
  • Trusted internal tools - When all API consumers are internal and trusted
  • Performance testing - To measure raw query performance
Never disable in production or staging environments accessible to untrusted users.

Performance Impact

Validation Cache Benefits

Column Validation Overhead

Column lists are cached per table, so the overhead only occurs on the first request to each resource type.

Custom Validation Rules

You can extend validation behavior in your service classes:

Custom Column Validation

Override Validation Connection

Custom Existence Validation

Filtering Configuration

Additional filtering safety limits are configured alongside column validation:

Max Depth

Prevents excessively nested filter conditions:
With max_depth: 5, requests with more than 5 nesting levels are rejected.

Max Conditions

Limits total number of filter conditions:
With max_conditions: 100, requests with more than 100 total conditions are rejected.

Strict Relations

Requires explicit relation declarations in models:
With strict_relations: true, only relations listed in RELATIONS can be loaded via relations parameter.
Disabling strict_relations allows loading any relationship defined on the model, which may expose unintended data.

Example Configurations

High-Security Production

Development Environment

Performance-Optimized

Troubleshooting

Error: Invalid column 'xyz' in filter conditionsCause: Column doesn’t exist in the table or isn’t accessibleSolution:
  1. Verify column name spelling
  2. Check if column exists in database: DESCRIBE table_name
  3. Clear column cache if schema changed: php artisan cache:clear
  4. If column exists but still fails, check column_cache_ttl and wait for cache expiry
Symptom: High load on primary database from validation queriesSolution: Route validation to read replica
Ensure mysql_read is configured in config/database.php:
Symptom: New columns not recognized or deleted columns still cachedSolution: Clear validation cache
Or clear specific validation cache keys:

Environment Variables

Complete reference of validation-related environment variables

Cache Strategy

Learn about response caching configuration

Filtering

Learn how filtering works with column validation